1. Bit
  2. Qubit
  3. Superposition
  4. Measurement
  5. Entanglement
  6. Circuits
  7. Fourier
  8. Shor
  9. Grover
  10. Correction

Chapter 07 · Shor's algorithm

Factoring by finding a period

The security of most of the internet rests on one assumption: that factoring large numbers is hard. In 1994 Peter Shor showed that a quantum computer could do it in polynomial time, by turning factoring into finding the period of a function and the period into a Fourier peak.

Multiplying two primes of three hundred digits takes a computer microseconds. Undoing the product, recovering the primes from the result, would take the best known classical algorithms longer than the age of the universe. On that asymmetry rests RSA (Rivest, Shamir and Adleman, 1977), the cryptosystem that protects a large share of the world's communications. In 1994 Peter Shor, at Bell Labs, showed that a large enough quantum computer would break it.

A little number theory

We work modulo N: two integers are congruent, a≡b(modN), if N divides a−b. The numbers coprime to N form a group under multiplication, ℤN∗, with φ(N) elements (φ is Euler's totient function).

Theorem (Euler, 1763)

If gcd⁡(a,N)=1, then aφ(N)≡1(modN). In particular the powers axmodN repeat periodically: the order of a, the smallest r>0 with ar≡1(modN), exists and divides φ(N).

RSA uses N=pq and, with φ(N)=(p−1)(q−1), chooses an encryption exponent e and a decryption exponent d with ed≡1(modφ(N)). Knowing p and q makes d easy to compute; without them, nobody knows how to do it efficiently. The best classical factoring algorithm, the general number field sieve, takes time about

exp⁡((649)1/3(ln⁡N)1/3(ln⁡ln⁡N)2/3),

sub-exponential but enormous: factoring the 829-bit number RSA-250 in 2020 took about 2,700 core-years of computation.

From factoring to periods

The first idea of Shor's algorithm is classical, and goes back to Gary Miller (1976): if we know how to compute orders, we know how to factor.

Theorem (reduction to order finding)

Let N be odd, composite and not a prime power, with k≥2 distinct prime factors, and let a be chosen uniformly from ℤN∗ with order r. With probability at least 1−21−k≥12, r is even and ar/2≢−1(modN). In that case

gcd⁡(ar/2−1,N)andgcd⁡(ar/2+1,N)

are non-trivial factors of N.

Why the gcd works

Since ar≡1, N divides ar−1=(ar/2−1)(ar/2+1). It does not divide the first factor, because r is the smallest exponent with ar≡1; nor the second, by assumption. So N shares a non-trivial factor with each of them, which Euclid's algorithm finds in polynomial time. The probability bound comes from the Chinese remainder theorem: the orders of a modulo each prime power behave independently.

Everything comes down to finding the period r of f(x)=axmodN. Classically this is as hard as factoring. Quantumly, it is a job for the Fourier transform.

Quantum order finding

  1. Prepare two registers and put the first, of t qubits with N2≤Q=2t<2N2, in a uniform superposition: 1Q∑x=0Q−1|x⟩|0⟩.
  2. Compute f reversibly in the second register: 1Q∑x|x⟩|axmodN⟩. Modular exponentiation is done by repeated squaring, with O(n3) gates for n-bit numbers; it is the most expensive part of the algorithm.
  3. Measure the second register (or just forget it). The first one collapses to a state that is uniform over an arithmetic progression x0,x0+r,x0+2r,…: a periodic state of period r.
  4. Apply the QFT to the first register and measure. The result y is, with high probability, close to a multiple of Q/r: |yQ−ℓr|≤12Q for some ℓ.

The last step is to recover r from the fraction y/Q, a problem of number theory more than two thousand years old: approximating a number by fractions with small denominators.

Theorem (Legendre, 1798)

If |x−pq|<12q2, then pq is one of the convergents of the continued fraction expansion of x.

Since 12Q≤12N2<12r2, the fraction ℓ/r (in lowest terms) appears among the convergents of y/Q, which can be computed with Euclid's algorithm. If ℓ and r happen to be coprime, which happens with probability Ω(1/log⁡log⁡r), the denominator is the order r. A few repetitions are enough.

Shor's algorithm on small numbers. Choose N and a base a. 1. The function axmodN is periodic. 2. The quantum part (simulated exactly): the distribution of the measurement after the QFT, with peaks near the multiples of Q/r. 3. A sample, its continued-fraction expansion and the candidate period. 4. The greatest common divisors that give the factors. Some bases fail (r odd or ar/2≡−1): try another one.
Theorem (Shor, 1994)

A quantum computer can factor an n-bit integer with O(n3) elementary gates (or O(n2log⁡nlog⁡log⁡n) with fast multiplication) and success probability bounded away from zero. The same method computes discrete logarithms, which also breaks Diffie–Hellman and elliptic-curve cryptography.

How far away is it?

In 2001 an IBM team factored 15=3×5 with seven nuclear spins in a molecule. Since then the record of honest implementations has barely moved: Shor's algorithm needs many high-quality qubits and very long circuits, which means error correction. The estimates of resources needed for RSA-2048 have been falling: about 20 million physical qubits for 8 hours (Gidney and Ekerå, 2019), and less than a million noisy qubits in under a week (Gidney, 2025). Today's processors have on the order of a hundred to a thousand.

The threat is taken seriously, because encrypted data can be stored today and decrypted in the future. In 2024 NIST published the first post-quantum cryptography standards (FIPS 203, 204 and 205), based on problems over lattices and hash functions for which no efficient quantum algorithm is known. The mathematical lesson of Shor's algorithm goes beyond cryptography: quantum computers are extraordinarily good at finding hidden periodic structure in abelian groups (the hidden subgroup problem), and nobody knows whether that power extends much further.

References

  1. R. L. Rivest, A. Shamir and L. Adleman (1978). “A Method for Obtaining Digital Signatures and Public-Key Cryptosystems”. Communications of the ACM, 21(2).
  2. G. L. Miller (1976). “Riemann's Hypothesis and Tests for Primality”. Journal of Computer and System Sciences, 13(3).
  3. P. W. Shor (1994). “Algorithms for quantum computation: discrete logarithms and factoring”. FOCS (journal version: SIAM J. Computing, 1997).
  4. L. M. K. Vandersypen et al. (2001). “Experimental realization of Shor's quantum factoring algorithm using nuclear magnetic resonance”. Nature, 414.
  5. C. Gidney and M. Ekerå (2021). “How to factor 2048 bit RSA integers in 8 hours using 20 million noisy qubits”. Quantum, 5 (arXiv 2019).
  6. C. Gidney (2025). “How to factor 2048 bit RSA integers with less than a million noisy qubits”. arXiv preprint.
  7. NIST (2024). FIPS 203, 204 and 205: post-quantum cryptography standards.