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.
In this chapter
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 : two integers are congruent, , if divides . The numbers coprime to form a group under multiplication, , with elements ( is Euler's totient function).
If , then . In particular the powers repeat periodically: the order of , the smallest with , exists and divides .
RSA uses and, with , chooses an encryption exponent and a decryption exponent with . Knowing and makes 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
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.
Let be odd, composite and not a prime power, with distinct prime factors, and let be chosen uniformly from with order . With probability at least , is even and . In that case
are non-trivial factors of .
Why the gcd works
Since , divides . It does not divide the first factor, because is the smallest exponent with ; nor the second, by assumption. So 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 modulo each prime power behave independently.
Everything comes down to finding the period of . Classically this is as hard as factoring. Quantumly, it is a job for the Fourier transform.
Quantum order finding
- Prepare two registers and put the first, of qubits with , in a uniform superposition: .
- Compute reversibly in the second register: . Modular exponentiation is done by repeated squaring, with gates for -bit numbers; it is the most expensive part of the algorithm.
- Measure the second register (or just forget it). The first one collapses to a state that is uniform over an arithmetic progression : a periodic state of period .
- Apply the QFT to the first register and measure. The result is, with high probability, close to a multiple of : for some .
The last step is to recover from the fraction , a problem of number theory more than two thousand years old: approximating a number by fractions with small denominators.
If , then is one of the convergents of the continued fraction expansion of .
Since , the fraction (in lowest terms) appears among the convergents of , which can be computed with Euclid's algorithm. If and happen to be coprime, which happens with probability , the denominator is the order . A few repetitions are enough.
A quantum computer can factor an -bit integer with elementary gates (or 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 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
- 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).
- G. L. Miller (1976). “Riemann's Hypothesis and Tests for Primality”. Journal of Computer and System Sciences, 13(3).
- P. W. Shor (1994). “Algorithms for quantum computation: discrete logarithms and factoring”. FOCS (journal version: SIAM J. Computing, 1997).
- L. M. K. Vandersypen et al. (2001). “Experimental realization of Shor's quantum factoring algorithm using nuclear magnetic resonance”. Nature, 414.
- 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).
- C. Gidney (2025). “How to factor 2048 bit RSA integers with less than a million noisy qubits”. arXiv preprint.
- NIST (2024). FIPS 203, 204 and 205: post-quantum cryptography standards.