In 1994, Peter Shor published an algorithm that solved, on a quantum computer, a problem that no classical computer can solve efficiently: factoring the product of two large prime numbers. The practical consequence, understood immediately by every cryptographer who read the paper, was that RSA — the encryption scheme protecting bank transactions, government communications, and essentially all authenticated traffic on the internet — was conditionally broken. Conditionally, because Shor’s algorithm requires a fault-tolerant quantum computer with millions of low-error qubits, and no such machine exists today. But the algorithm does not care about engineering constraints. It is correct, and the world has spent the last thirty years preparing for the day the hardware catches up.

Understanding Shor’s algorithm at depth reveals something surprising: the quantum speedup does not come from trying all possible factors at once. It comes from a specific mathematical reduction that transforms the factoring problem into a period-finding problem, and then from a quantum subroutine — the Quantum Fourier Transform — that finds periods exponentially faster than any classical method.

Key Concepts in This Article

Order of a mod N

The smallest positive integer r such that aʳ ≡ 1 (mod N). Also called the period of the function f(x) = aˣ mod N. Finding this order is the quantum part of Shor's algorithm.

Quantum Fourier Transform

The quantum analogue of the discrete Fourier transform. Acting on n qubits, it can be implemented in O(n²) gates — exponentially fewer than the O(n 2ⁿ) steps needed to classically Fourier transform 2ⁿ amplitudes.

Modular Exponentiation

Computing aˣ mod N — the expensive quantum subroutine that encodes the periodic function into a quantum register. This accounts for most of the gate count in Shor's circuit.

Continued Fractions

A classical algorithm used in the final step of Shor's procedure to extract the exact period r from the approximate rational k/r returned by measuring the quantum register after the QFT.

The Problem: Why Factoring Secures the Internet

RSA encryption was published in 1977 by Rivest, Shamir, and Adleman. Its security rests on a single mathematical asymmetry: multiplying two large prime numbers p and q together is trivially fast, but recovering p and q from their product N = p × q is — as far as anyone knows — computationally hard for large N.

To encrypt a message, the recipient publishes N (typically 2048 bits long for modern RSA) and a public exponent e. Anyone can encrypt by computing ciphertext = messageᵉ mod N. To decrypt, the recipient needs the private key d, which is computed from p and q. Without knowing p and q, computing d requires factoring N — and factoring a 2048-bit semiprime N takes more operations than the estimated number of atoms in the observable universe using the best classical algorithm (the General Number Field Sieve).

The security of RSA, ECC (elliptic curve cryptography), and Diffie-Hellman key exchange all reduce, in different ways, to the hardness of either integer factoring or the discrete logarithm problem. Shor’s algorithm solves both in polynomial time.

The Reduction: From Factoring to Period-Finding

Shor’s algorithm does not directly attempt to divide N by potential factors. Instead, it exploits a classical theorem from number theory to reduce factoring to a different problem.

Step 1 — Choose a random base. Pick a random integer aa with 1<a<N1 < a < N. Compute gcd(a,N)\gcd(a, N). If gcd(a,N)>1\gcd(a, N) > 1, we got lucky — we found a non-trivial factor immediately (this happens rarely for large NN). Otherwise, continue.

Step 2 — Find the order. Compute rr, the smallest positive integer such that:

ar1(modN)a^r \equiv 1 \pmod{N}

This rr is called the order of aa modulo NN (or the period, since the sequence axmodNa^x \bmod N repeats with period rr).

Step 3 — Extract factors. If rr is even and ar/2≢1(modN)a^{r/2} \not\equiv -1 \pmod{N}, then:

(ar/21)(ar/2+1)0(modN)(a^{r/2} - 1)(a^{r/2} + 1) \equiv 0 \pmod{N}

Since neither factor is 0modN\equiv 0 \bmod N (by assumption), they must share a factor with NN. Computing gcd(ar/21,N)\gcd(a^{r/2} - 1, N) and gcd(ar/2+1,N)\gcd(a^{r/2} + 1, N) yields the prime factors pp and qq.

The probability that a randomly chosen aa satisfies the conditions in Step 3 is at least 1/21/2 for N=pqN = pq with two distinct odd prime factors. In practice, two to three repetitions of the algorithm suffice to find the factors with high probability.

The entire classical number theory is efficient — every step except Step 2. Finding the order rr of amodNa \bmod N classically requires evaluating axmodNa^x \bmod N for up to rr values of xx, and rr can be as large as N/422046N/4 \approx 2^{2046} for RSA-2048. This is exponential. This is the one step that a quantum computer does exponentially faster.

The Quantum Subroutine: Period-Finding via QFT

The quantum order-finding algorithm exploits the fact that the function f(x)=axmodNf(x) = a^x \bmod N is periodic — it repeats with period rr. Finding the period of a periodic function is exactly what a Fourier transform does.

f(x) = 7ˣ mod 15 — PERIODIC WITH r = 4 QFT OUTPUT — PEAKS AT MULTIPLES OF 2ⁿ/r x aˣ mod N 0 4 8 12 15 0 1 2 3 4 5 6 7 8 9 10 11 r = 4 measurement outcome probability 0 2ⁿ/4 2ⁿ/2 3·2ⁿ/4 equal probability at k·2ⁿ/r → measure → continued fractions → r
Figure 1 — Concrete example: N=15, a=7. Left: the sequence 7ˣ mod 15 is periodic with period r=4 (blue: first cycle, purple: subsequent cycles). Right: after applying the QFT to a quantum register encoding the periodic superposition, probability peaks appear at exactly the multiples of 2ⁿ/r. One measurement gives k·2ⁿ/r for some integer k; the continued fraction algorithm then recovers r=4 exactly.

The Algorithm: Step by Step

SHOR'S ALGORITHM — BLOCK DIAGRAM |0⟩ⁿ |0⟩ᵐ H⊗ⁿ uniform superpos. Modular Exp. |x⟩|0⟩ → |x⟩|aˣmodN⟩ (most expensive) Measure ancilla → periodic superpos. in |x⟩ QFT O(n²) gates Measure → k·2ⁿ/r Classical Continued fractions → r → gcd(a^(r/2)±1, N) p, q factors of N ① INIT ② ORACLE ③ COLLAPSE ④ QFT ⑤ MEASURE ⑥ CLASSICAL
Figure 2 — Shor's algorithm block diagram. The quantum register (blue) is placed in uniform superposition by Hadamard gates, then entangled with an ancilla register (purple) via modular exponentiation. Measuring the ancilla collapses the quantum register to a periodic superposition. The QFT converts that periodicity to frequency-domain peaks; one measurement then yields a multiple of 2ⁿ/r. Classical continued-fraction analysis recovers r exactly, and two GCD computations yield the prime factors.

Walking through each step:

① Initialisation. Prepare an nn-qubit quantum register in the equal superposition of all 2n2^n computational basis states by applying a Hadamard gate to each qubit. For an NN-bit number we choose n=2log2Nn = 2\lceil \log_2 N \rceil — roughly twice the bit-length of NN — to ensure sufficient resolution.

② Modular exponentiation. Apply the unitary UU that maps x0xaxmodN|x\rangle|0\rangle \to |x\rangle|a^x \bmod N\rangle. Because the input register is in superposition, this computes axmodNa^x \bmod N for every xx from 00 to 2n12^n - 1 simultaneously — but not in a way that gives access to all values at once. The joint state is a quantum superposition that encodes all of them. This modular exponentiation circuit is the most expensive part of the algorithm, requiring O(n3)O(n^3) elementary quantum gates using efficient reversible arithmetic.

③ Collapse. Measure the ancilla (second) register. The outcome is some specific value c=ax0modNc = a^{x_0} \bmod N. This projects the first register into a superposition of only those xx values where axmodN=ca^x \bmod N = c — that is, {x0,x0+r,x0+2r,}\{x_0, x_0+r, x_0+2r, \dots\}. The register is now a periodic superposition with unknown period rr spaced at exact intervals.

④ Quantum Fourier Transform. Apply the QFT to the first register. The QFT maps a periodic superposition in position space to probability peaks in frequency space — exactly at the multiples of 2n/r2^n/r. This is the quantum analogue of how a classical DFT of a periodic signal produces spikes at the signal’s harmonics.

⑤ Measurement. Measure the first register. The result is some integer mm close to k(2n/r)k \cdot (2^n/r) for a random integer kk. This gives us an approximate rational representation of k/r=m/2nk/r = m/2^n.

⑥ Classical post-processing. Apply the continued fraction algorithm to m/2nm/2^n to find the best rational approximations with denominator N\leq N. One of these approximations equals k/rk/r exactly; its denominator is rr (or a divisor of rr — with small probability we may need to repeat). With rr in hand, compute gcd(ar/21,N)\gcd(a^{r/2}-1, N) and gcd(ar/2+1,N)\gcd(a^{r/2}+1, N). With probability 1/2\geq 1/2 over the random choice of aa, these yield the prime factors pp and qq.

Why the QFT Provides an Exponential Speedup

The Quantum Fourier Transform acts on an n-qubit state as:

QFTj=12nke2πijk/2nk\mathrm{QFT}\,|j\rangle = \frac{1}{\sqrt{2^n}} \sum_k e^{2\pi i j k / 2^n}\,|k\rangle

This is precisely the discrete Fourier transform of the basis vector amplitudes. The classical DFT on 2n2^n points requires O(n2n)O(n \cdot 2^n) operations (with the FFT). The QFT implements it in O(n2)O(n^2) quantum gates — an exponential improvement.

The catch is that you cannot read out all 2ⁿ amplitudes of the QFT output, since measurement collapses the state to a single value. The power of the QFT in Shor’s algorithm lies not in reading all amplitudes, but in the fact that the measurement outcome is concentrated at the useful values — the multiples of 2ⁿ/r — because the QFT produces constructive interference there and destructive interference everywhere else. You don’t need all the information; you need one sample from a distribution that is almost entirely concentrated on the answer.

This is the precise sense in which quantum computers outperform classical computers on this problem: the QFT exploits quantum interference to make the answer overwhelmingly likely to be sampled after a single measurement, whereas a classical computer must scan through O(r)O(N)O(r) \approx O(N) possibilities to find the period.

The Numbers: What Breaking RSA Actually Requires

CLASSICAL vs QUANTUM SCALING FOR INTEGER FACTORING RSA key size (bits) operations (log scale) 512 1024 2048 4096 10³⁰ 10⁵⁰ 10⁷⁰ 10⁹⁰ 10¹¹⁰ GNFS (classical) Shor (quantum) RSA-2048 classical: ~10¹⁰⁰ ops computationally infeasible quantum: ~10¹⁰ gates feasible with fault-tolerant QC
Figure 3 — Scaling comparison for integer factoring. The General Number Field Sieve (best classical algorithm) has sub-exponential but still super-polynomial scaling — the operation count grows faster than any polynomial in the key size. Shor's algorithm has polynomial scaling in the number of qubits required. At RSA-2048, the gap is roughly 10⁹⁰ operations: classical ~10¹⁰⁰, quantum ~10¹⁰. The quantum algorithm does not just win — the problem that took the age of the universe classically takes hours on a sufficiently large quantum computer.
SHOR'S ALGORITHM — HARDWARE REQUIREMENTS RSA KEY SIZE LOGICAL QUBITS QUANTUM GATES PHYS. QUBITS (SC, d=17) RUN TIME† 512-bit ~1,100 ~6 × 10⁹ ~600,000 ~hours 1024-bit ~2,200 ~5 × 10¹⁰ ~1.2 million ~days 2048-bit (RSA) ~4,000 ~4 × 10¹¹ ~2–4 million ~weeks 4096-bit ~8,000 ~3 × 10¹² ~4–8 million ~months Estimates use Beauregard-style circuit (2n+3 logical qubits), surface code distance d=17, physical error rate p=0.1%. †Run time assumes 1 µs gate cycle time (superconducting). Actual run time depends heavily on T-factory throughput and scheduling.
Figure 4 — Hardware requirements for Shor's algorithm at various RSA key sizes. The dominant cost is not qubit count but gate count — particularly the modular exponentiation circuit, which requires O(n³) quantum gates and drives the run time. A cryptographically relevant quantum computer (CRQC) capable of breaking RSA-2048 requires roughly 4,000 logical qubits and 2–4 million physical qubits under optimistic but realistic assumptions.

What Has Actually Been Demonstrated

Every experimental demonstration of Shor’s algorithm to date has used special-purpose circuits that exploit prior knowledge of the answer to simplify the computation — rendering them proofs of principle rather than genuine demonstrations of the factoring task. The first demonstration, by IBM’s NMR group in 2001, factored N=15 into 3 × 5 using 7 qubits. Subsequent experiments factored 15 and 21 on photonic, superconducting, and ion trap platforms, always using highly optimised circuits that would not generalise to unknown factors.

No experiment has run a full, general-purpose instance of Shor’s algorithm on any number larger than 21. This is not a criticism of the demonstrations — they were validating quantum gate implementations — but it means the gap between current hardware and a cryptographically relevant quantum computer (CRQC) is enormous: from factoring 21 with 5 logical qubits to factoring RSA-2048 with ~4,000 logical qubits and ~2–4 million physical qubits, executing ~10¹¹ quantum gates at fault-tolerant fidelity.

Most credible estimates from government and academic sources place the first CRQC 10–20 years away, with significant uncertainty in both directions.

The Cryptographic Consequence: What the World Is Doing About It

The response to Shor’s algorithm has been one of the largest coordinated efforts in the history of cryptography. In 2016, NIST launched a competition to standardise post-quantum cryptographic (PQC) algorithms — schemes whose security does not depend on the hardness of factoring or discrete logarithms and therefore cannot be broken by Shor’s algorithm. In 2024, after eight years of evaluation, NIST finalised three standards:

These algorithms are believed to be secure against both classical and quantum computers, including Shor’s algorithm (which does not apply to lattice or hash-based problems).

The migration is urgent because of “harvest now, decrypt later” (HNDL) attacks: an adversary with sufficient storage can collect encrypted traffic today and decrypt it retroactively once a sufficiently large quantum computer becomes available. Traffic encrypted with RSA in 2025 may be decrypted in 2035 or 2040 — a particular concern for government, intelligence, and financial data with long-term sensitivity. The National Security Agency, CISA, and equivalent agencies in the UK, EU, and Canada have all issued guidance directing a migration to PQC algorithms on specific timelines.

Shor’s algorithm is not a current threat. It is a future threat that must be mitigated now, because the data being encrypted today will still be sensitive when the hardware eventually arrives. That calculation is why a theoretical result from 1994 continues to drive operational security decisions at every major technology company and government agency three decades later.

The Enduring Significance

Shor’s algorithm matters beyond its cryptographic implications. It was the first evidence that quantum computers offer an exponential speedup on a practically important problem — not a contrived mathematical exercise, but the specific computational problem that underpins global financial security. It established that quantum advantage is not merely theoretical. And it forced the classical cryptography community to confront a deadline: the window between “a sufficiently large quantum computer does not exist” and “it does” may be the most consequential engineering transition of the 21st century.

The algorithm itself is a beautiful piece of mathematics: a reduction from number theory to signal processing, from an algebraic group structure to quantum interference, from classical GCDs to quantum measurement. Its key steps — the Fourier transform of a periodic function, the extraction of a period from a single sample — could in principle be explained to anyone who has taken a signal processing course. What is quantum about it is not mysterious; it is precise.

Understanding Shor’s algorithm is not optional for anyone who wants to understand why quantum computing matters, what fault-tolerant hardware must achieve, or why the internet’s cryptographic infrastructure is currently being replaced. It is the reason the field exists in its current form.

Sources & Further Reading

  1. Peter W. Shor. Algorithms for Quantum Computation: Discrete Logarithms and Factoring. Proc. 35th Annual Symposium on Foundations of Computer Science (FOCS) (1994)
  2. Peter W. Shor. Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer. SIAM Journal on Computing, 26(5); arXiv:quant-ph/9508027 (1997)
  3. Stéphane Beauregard. Circuit for Shor’s algorithm using 2n+3 qubits. Quantum Information and Computation; arXiv:quant-ph/0205095 (2003)
  4. Craig Gidney, Martin Ekerå. How to Factor 2048-bit RSA Integers in 8 Hours Using 20 Million Noisy Qubits. Quantum 5, 433; arXiv:1905.09749 (2021)
  5. NIST. Module-Lattice-Based Key-Encapsulation Mechanism Standard (FIPS 203). Federal Information Processing Standards Publication 203 (2024)

Discussion

← All articles