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.
Dated quantum resource estimates
| Target and estimate | Logical qubits | Physical qubits | Estimated runtime |
|---|---|---|---|
| RSA-2048 β GidneyβEkerΓ₯, 2019 preprint / 2021 publication | Separate circuit account; not taken from the 2025 estimate | 20 million noisy qubits | 8 hours |
| RSA-2048 β Gidney, 21 May 2025, Β§3.2 | 1,409 active; 1,537 including idle hot patches | 897,864 before slack; approximately one million with safety slack | Less than one week |
These are conditional resource estimates, not executed RSA-2048 attacks or dates for a future quantum computer. The 2025 design trades space for time using approximate residue arithmetic, yoked cold storage and magic-state cultivation. Both estimates assume nearest-neighbor connectivity, 0.1% uniform gate error, 1 ΞΌs surface-code cycles and 10 ΞΌs control reaction time. Logical and physical counts from different circuit designs must not be combined into one estimate.
Neutral-atom architecture experiment (online 10 November 2025; January 2026 issue): the up-to-96-logical-qubit result is a postselected distance-4 cluster-state experiment (Fig. 6), distinct from the paperβs non-postselected surface-code QEC experiments. It is not a 96-qubit cryptanalytic computer, and its task, code distance and postselection prevent a simple qubit-count comparison with the factoring estimates.
βΆ 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