Every bank has RSA somewhere. It signs the certificate on the public website, protects code-signing keys, wraps keys inside hardware security modules and underpins older payment and mainframe interfaces. All of that rests on a single mathematical belief: splitting a very large number into its prime factors is practically impossible.
In 1994 Peter Shor showed that a large enough quantum computer would make it easy.[1] This article walks through exactly how, with a complete worked example you can follow with a pencil, then looks at what the algorithm costs and what has really been built so far.
Why RSA rests on factoring
An RSA public key contains a number N, the modulus, which is the product of two secret primes p and q. For RSA-2048, N has 2,048 bits, about 617 decimal digits. Anyone can multiply two primes together in a fraction of a second. Going the other way, from N back to p and q, is the hard direction. Whoever learns p and q can work out the private key. Nobody else can, so the signature or the encrypted key stays safe.
The best classical method, the number field sieve, is faster than trying every divisor, but it still grows so quickly that large keys are out of reach. The public factoring records show the scale. RSA-129 (426 bits) fell in 1994 to a volunteer internet effort. RSA-768 (232 digits) was factored in December 2009, with a reported effort of almost 2,000 years of computing on a single 2.2 GHz processor core.[6] RSA-250 (829 bits) fell in February 2020 after about 2,700 core-years.[7] As a very rough extrapolation of the sieve, RSA-2048 needs on the order of 1011 times the work of RSA-250. That is an estimate, but it explains why nobody expects a classical break.
Shor's algorithm changes the picture because its running time grows only polynomially with the number of bits. Doubling the key length makes the quantum attack several times more expensive, not astronomically so. That is why longer RSA keys do not solve the problem.
From factoring to period finding
Factoring can be rewritten as a different problem, finding a period, and the period problem is the one a quantum computer is good at. The reduction is classical number theory. Here it is in words.
Pick a number a smaller than N that shares no factor with N. List the powers of a, always keeping only the remainder after dividing by N: a0, a1, a2, and so on. Because there are only N possible remainders, the list must eventually return to 1, and from then on it repeats. The number of steps before it returns to 1 is the period r, also called the order of a modulo N.
Suppose r is even. Then ar ≡ 1 (mod N), so (ar/2)2 − 1 is a multiple of N. Write that as a difference of squares:
N divides the product of the two brackets. Neither bracket can be a multiple of N on its own in the first case, because ar/2 − 1 would then be 0 modulo N, which contradicts r being the smallest exponent that gives 1. The second bracket is a multiple of N only in the unlucky case ar/2 ≡ −1 (mod N). Otherwise the primes of N are shared out between the two brackets, and the greatest common divisor of each bracket with N is a proper factor. Euclid's algorithm, about 2,300 years old, finds a greatest common divisor in a handful of steps and needs no quantum help.
Everything hinges on finding r. A classical computer has no shortcut, because the powers jump around unpredictably. A quantum computer can find r efficiently.
Modular exponentiation
The function whose period we want is f(x) = ax mod N. This is modular exponentiation, and a classical computer evaluates it cheaply even for enormous x, by repeated squaring. To find ax with a 2,048-bit x, you square about 2,048 times and multiply in a few extra factors, reducing modulo N after each step. RSA itself performs the same operation when it signs or decrypts.
The quantum computer must run this arithmetic on a register that holds many values of x at once, so the circuit has to be reversible, with no information thrown away. Building a reversible modular multiplier for 2,048-bit numbers is the bulk of the cost of Shor's algorithm. The quantum Fourier transform that follows is comparatively cheap.
A full worked example: N = 15, a = 7
Take N = 15 = 3 × 5, so we know the answer in advance. Choose a = 7. It shares no factor with 15, so we may proceed. Compute the powers of 7, keeping the remainder modulo 15:
| x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| 7^x mod 15 | 1 | 7 | 4 | 13 | 1 | 7 | 4 | 13 | 1 |
The sequence 1, 7, 4, 13 returns to 1 and repeats. The period is r = 4. Now apply the recipe.
- r = 4 is even, so continue.
- Compute ar/2 = 72 = 49. Since 49 = 3 × 15 + 4, this is 4 modulo 15.
- Check that 4 is not −1 modulo 15. Minus one modulo 15 is 14, so it is not. The unlucky case has been avoided.
- Compute gcd(4 − 1, 15) = gcd(3, 15) = 3.
- Compute gcd(4 + 1, 15) = gcd(5, 15) = 5.
That is the whole classical part. For a 2,048-bit N exactly the same steps apply. Only the period-finding step is out of reach for classical machines.
1 · classical check
gcd(2, 55) = 1, so a shares no factor with N. Now the hard part: find the period of aˣ mod N.
2 · the repeating pattern
The values of ax mod N repeat every r = 20 steps; each colour band is one period. A classical computer has to walk the sequence to see it repeat. For a 2,048-bit N the period can have hundreds of digits, so that walk never ends.
3 · quantum interference
The quantum computer puts all Q = 4,096 values of x into superposition, computes ax mod N for all of them at once, then applies the quantum Fourier transform. Everything cancels except sharp peaks at multiples of Q / r. One measurement lands on a peak.
4 · from period to factors
Measure first. The outcome y is close to a multiple of Q / r; a continued-fraction expansion of y / Q recovers r.
N = ? × ?
What the quantum computer does
The quantum computer finds r without listing the powers one by one. It does this in four steps. The numbers that appear along the way are amplitudes, which only turn into probabilities when you measure.
1. Superposition over x
Take a register of enough qubits to hold numbers up to Q, where Q is a power of two at least as large as N2. For N = 15, N2 = 225, so Q = 256 and the register needs eight qubits. Apply a Hadamard gate to each qubit. The register is now in an equal superposition of every value of x from 0 to 255.
2. Compute ax mod N into a second register
A reversible circuit computes 7x mod 15 for all those x in a single pass and stores the answers in a second register. The two registers are now entangled. This does not hand you 256 answers. You hold one combined state and can read it out only once.
3. Measure the second register
Suppose you measure the second register and see 4. The first register collapses to exactly those x with 7x ≡ 4, which are x = 2, 6, 10, 14 and so on, every fourth value. The first register is now a regular comb of equally spaced values, spaced r apart, with an unknown starting offset.
4. Quantum Fourier transform and interference
The quantum Fourier transform converts a pattern in x into a pattern in frequency. A comb with spacing r becomes a comb in frequency space with peaks at multiples of Q/r. The unknown offset only changes the phases of the peaks, not their positions. The mechanism is interference: amplitudes for frequencies that are multiples of Q/r add up, and the others cancel.
For our example Q/r = 256/4 = 64, so the peaks sit at 0, 64, 128 and 192. Measuring the register gives one of those four, each with probability one quarter.
| Result | As a fraction of Q | Reduced fraction | What you learn |
|---|---|---|---|
| 0 | 0/256 | 0/1 | Nothing. Retry. |
| 64 | 64/256 | 1/4 | Denominator 4. The period is 4. |
| 128 | 128/256 | 1/2 | Denominator 2. Test: 7² mod 15 is 4, not 1, so reject and retry. |
| 192 | 192/256 | 3/4 | Denominator 4. The period is 4. |
Half of the runs give a usable answer straight away and the other half are simply repeated. For a realistic N the period does not divide Q exactly, and the peaks are not on whole numbers, which leads to the next step.
Continued fractions
For most N the period r does not divide Q exactly, so the peaks fall between whole numbers and the measured value y is only the nearest integer to a multiple of Q/r. The ratio y/Q is then a close approximation to a fraction s/r, where s is some whole number. We want the denominator r.
The continued fraction expansion is a classical method that finds the best simple fractions close to a given number. Because Q was chosen to be at least N2, only one fraction with a denominator below N lies close enough to y/Q, so the method pins down r, or sometimes a factor of it. The classical computer then checks the candidate by confirming that ar ≡ 1 (mod N) and runs the gcd step above.
Why unlucky choices of a just mean retrying
The algorithm is probabilistic and several things can go wrong. None is fatal, because each failure costs one more run with a fresh random choice.
- Shared factor. If a happens to share a factor with N, gcd(a, N) already gives a factor and no quantum computer is needed.
- Odd period. If r is odd, the difference-of-squares step does not apply. For N = 21 and a = 4 the powers are 4, 16, 1, so r = 3. Pick another a.
- The −1 case. If ar/2 ≡ −1 (mod N), both gcds return 1 or N. With N = 15 and a = 14, which is −1 itself, the period is 2 and 141 ≡ −1, so the run fails.
- Unhelpful measurement. The measured value may reveal only a divisor of r, as the result 128 did above. Repeat the run, or combine several runs.
For a modulus built from two large primes, a randomly chosen a gives an even period and avoids the −1 case with probability at least one half, so the expected number of attempts is small. This is a standard result in the analysis of the algorithm.[1]
What it costs in qubits and gates
Two quantities matter. The first is the number of logical qubits, the error-corrected qubits that the algorithm treats as perfect. A simple textbook design needs a little over twice the key length, which is a few thousand logical qubits for RSA-2048. Cleverer designs trade qubits for time.
The second is the number of operations. The modular multiplier dominates, and published estimates count billions of the expensive non-Clifford gates, which must run in a long and strictly ordered sequence. One uncorrected mistake can spoil the whole run, so the machine must correct errors continuously.
Error correction is where the physical cost appears. A surface code of distance d uses about 2d² − 1 physical qubits per logical qubit, and at today's error rates estimates need hundreds to about a thousand physical qubits for each one. Multiply a few thousand logical qubits by that overhead, add space for the factories that produce the special states the expensive gates consume, and the totals reach the millions in the 2019 estimate and the high hundreds of thousands in 2025.
The history of resource estimates
The headline numbers have fallen steeply, and the assumptions behind them matter as much as the totals.
| Estimate | Noisy qubits | Runtime | Assumptions |
|---|---|---|---|
| Gidney and Ekerå, 2019[2] | about 20 million | about 8 hours | Surface-code error correction on a two-dimensional grid of qubits; error rate and cycle time as stated in the paper |
| Gidney, May 2025[3] | under one million | under a week | 0.1% gate error rate; 1 microsecond surface-code cycle; 10 microsecond control reaction time |
The 2025 estimate cut the qubit count by a factor of about twenty in exchange for a longer runtime. The improvement came from better algorithms and error-correction techniques, with no change in hardware. Both estimates assume a machine that does not yet exist, with far more qubits than any device built so far at an error rate and speed that today's best devices approach but have not sustained at that scale.
What has really been demonstrated
In 2001 a team ran Shor's algorithm on a seven-qubit nuclear magnetic resonance machine and factored 15.[4] In 2012 a photonic experiment reported factoring 21, using a compiled and simplified circuit.[5] Hardware demonstrations remain at toy numbers.
These experiments prove the principle and say little about scale. The more relevant progress is in error correction. In December 2024 Google announced Willow, a 105-qubit chip whose logical error rate fell as the surface code grew from distance 3 to 5 to 7, the behaviour known as operating below threshold.[8] Any large machine needs that property. The distance to thousands of logical qubits running billions of gates remains very large.
What to watch for
Expert surveys such as the Global Risk Institute's annual Quantum Threat Timeline place a cryptographically relevant quantum computer most likely in the 2030s, with wide uncertainty.[10] Nobody can date it. These signals tell you more than any prediction.
- Logical qubits rather than physical ones: how many, at what error rate, and for how many operations.
- Logical error rates continuing to fall as codes get larger.
- Demonstrations of logical gates, not only memory, at scale.
- New resource estimates that lower the cost of the multiplier or the error-correction overhead.
- Changes to official deadlines. NIST's draft transition plan proposes deprecating quantum-vulnerable public-key algorithms at the 112-bit security level after 2030 and disallowing all of them after 2035.[9]
What to do now
No practical RSA key length is safe against Shor's algorithm, so the action is replacement, and the first step is knowing where RSA is.
- Run
qsc scanover repositories, dependencies, configuration, certificates and infrastructure to find RSA keys, certificates and library calls, and export the result as a CBOM (CycloneDX 1.6). - Use
--target host:443to probe live endpoints, and--cloudto inventory cloud key services. - Rank the findings by the lifetime of what they protect, as described in Harvest now, decrypt later.
- Review
qsc fixplans, apply the safe configuration changes, and use--baselineon later scans to show progress. - Check the readiness tracker before assuming a platform or provider supports a post-quantum replacement.
Related reading: Elliptic curves and discrete logarithms under Shor covers the other half of the threat, Inside a quantum computer explains why the hardware is hard to build, and The NIST post-quantum standards describes the replacements.