Shor's Algorithm
Quantum period-finding ยท QFT ยท continued fractions
Factor an integer N by turning it into quantum order-finding: extract the period of a mod N via the QFT and continued fractions, then recover the factors with gcd(a^(r/2)ยฑ1, N).
What is Shor's Algorithm?
Back in 1994, mathematician Peter Shor had a brilliant insight: a quantum computer could factor large numbers exponentially faster than any classical method. It's one of the most dazzling results in computer science โ and the spark that launched the whole field of post-quantum cryptography.
The problem it solves
Split a big integer N back into its prime building
blocks. For a 2048-bit number, the best classical algorithm (GNFS)
would chew on it for billions of years โ Shor's algorithm wraps it
up in hours. ๐
Why it's a big deal
RSA, ECC, and Diffie-Hellman โ the encryption guarding nearly every website, bank, and message โ all lean on factoring being hard. Shor's algorithm flips that on its head, and that's exactly why it's so exciting to explore.
The clever trick
Here's the magic: Shor reframes factoring as a period-finding puzzle, then lets a quantum Fourier transform spot that period in a single elegant shot. Fire up the demo below and watch it unfold step by step!
Why does the period r let you factor N?
Pick a base a and look at its order r โ the smallest power with ar โก 1 (mod N). That one fact is a factoring machine:
- ar โก 1 means ar โ 1 โก 0 (mod N).
- If r is even, factor the left side as a difference of squares: (ar/2 โ 1)(ar/2 + 1) โก 0 (mod N).
- So N divides that product. Unless ar/2 โก ยฑ1 (the two dead-ends the demo retries past), N's prime factors must split between the two factors โ so each shares a real factor with N.
- Peel them off with the fast, classical gcd(ar/2 ยฑ 1, N).
The quantum computer's only job is finding r. Everything above is ordinary arithmetic โ which is why the whole attack collapses to "find the period fast."
Why does the QFT concentrate at multiples of Q/r?
After order-finding, the register holds every input x that shares one f(x) = ax mod N value โ an evenly spaced comb xโ, xโ+r, xโ+2r, โฆ The QFT gives each state a phase that spins as a clock hand, at a rate set by the frequency m you might measure.
- At a frequency m where mยทr/Q is a whole number, every hand in the comb points the same way โ they add up (constructive interference). That happens exactly at m = kยทQ/r.
- At any other m the hands fan out around the circle and cancel (destructive interference) โ near-zero probability.
So a single measurement almost always lands on a spike at kยทQ/r. The phasor wheels below animate exactly this โ watch the hands align at the period and scatter off it. (classically simulated)
Enter N and press โถ Run Shor's Algorithm to begin.
Visualization will appear here after running the algorithm.
FACTORING COMPLEXITY AT RSA-2048
Bars are on a logโโ axis (length โ number of digits in the op count). The 25-unit gap between them is a factor of ~1025ร โ twenty-five orders of magnitude, not the 2โ3ร a linear bar would suggest.
The classical bar is the conservative anchor: NIST (SP 800-57 Part 1 Rev. 5) rates RSA-2048 at 112-bit security, i.e. 2112 โ 5 ร 1033 operations. Evaluating the GNFS cost function itself โ LN[1/3, 1.923] = exp(81) for a 2048-bit N โ gives โ 1035 (โ 2117). Both land in the 1034โ1035 band, so 1034 is plotted; the real gap is 25 orders of magnitude or more.
RESOURCE REQUIREMENTS
| Target | Logical Qubits | Physical Qubits (est.) | Threat Timeline |
|---|---|---|---|
| RSA-512 | ~1,030 | ~500K | Already broken classically |
| RSA-2048 | ~4,100 | <1M (Gidney, 2025) | 10โ25 years |
| RSA-4096 | ~8,200 | ~2M | 15โ30 years |
| ECC P-256 | ~2,330 | <500K (Google, 2026) | 10โ20 years |
| ECC P-384 | ~3,500 | ~750K | 15โ25 years |
Largest verified error-corrected demonstration to date: 96 logical qubits (QuEra, Nature, Jan 2026) โ and at far lower code distance than Shor needs
Gap to RSA-2048: ~4,100 logical qubits needed
โถ Why Shor breaks ECC too
Shor's algorithm also solves the discrete logarithm problem โ the mathematical foundation of ECC and Diffie-Hellman.
ECDH, ECDSA, and all ECC-based protocols are equally vulnerable. Smaller key sizes make ECC faster to break than RSA on a quantum computer.
QUANTUM-RESISTANT AFTER SHOR
- โ AES-256 โ Grover reduces to 128-bit effective โ still strong
- โ SHA-3 โ Grover halves preimage resistance (SHA3-256 โ ~2128) โ still usable
- โ ML-KEM (Kyber) โ Lattice problems โ no known efficient quantum attack
- โ ML-DSA โ Lattice problems โ no known efficient quantum attack
- โ SLH-DSA โ Hash-based โ quantum-safe by design
- โ BB84 QKD โ Physics-based โ immune to all computational attacks
- โ RSA (any size) โ Broken by Shor
- โ ECC (any curve) โ Broken by Shor
- โ Diffie-Hellman โ Broken by Shor
“No known efficient quantum attack” is the precise claim for the lattice schemes โ not “no quantum speedup at all.” Grover-accelerated lattice sieving does shave a sub-exponential factor off the best known attack, which is why NIST’s ML-KEM/ML-DSA parameter sets already price a quantum sieve in. What no one has is a polynomial-time quantum algorithm for the underlying lattice problems, the way Shor is one for factoring.
Run the algorithm above to see a live demo.
โ crypto-lab-kyber-vault โ post-quantum replacement
โ crypto-lab-bb84 โ physics-based alternative