|q⟩ Bad Qubits

intermediate · Programming · Shor's Algorithm & Period Finding

Factoring and Cryptography

The most celebrated application of quantum computing is Shor's algorithm: a quantum procedure that factors large integers in polynomial time. RSA — the cryptographic backbone of most encrypted internet traffic — derives its security from the fact that no efficient classical algorithm for factoring is known. Understanding why factoring breaks RSA, and why quantum computers can factor efficiently, is the conceptual foundation for everything that follows in this module.

RSA in one paragraph

RSA key generation works like this. Choose two large primes pp and qq (each hundreds of digits long) and form their product

N=pq.N = p \cdot q.

The public key consists of NN and a small exponent ee. The private key is dd, computed from ee, pp, and qq using Euler's theorem:

ed1(modφ(N)),φ(N)=(p1)(q1).e \cdot d \equiv 1 \pmod{\varphi(N)}, \qquad \varphi(N) = (p-1)(q-1).

To encrypt a message mm one computes c=memodNc = m^e \bmod N; to decrypt, the key holder computes m=cdmodNm = c^d \bmod N. The security claim is simple: knowing NN but not pp and qq makes computing φ(N)\varphi(N), and therefore dd, computationally infeasible.

Why factoring is the hard part

Given pp and qq it takes a single multiplication to produce NN. Given only NN, recovering pp and qq is the integer factorisation problem. No classical algorithm is known to solve it in time polynomial in the number of bits of NN.

The best classical algorithm, the General Number Field Sieve, runs in sub-exponential time roughly

exp ⁣(c(lnN)1/3(lnlnN)2/3)\exp\!\left( c \cdot (\ln N)^{1/3} (\ln \ln N)^{2/3} \right)

for a small constant c1.923c \approx 1.923. For a 2048-bit NN this is astronomically large. RSA therefore rests on the computational hardness assumption: factoring NN is hard when NN is the product of two comparable-sized primes.

Enter Shor's algorithm

In 1994, Peter Shor showed that a quantum computer can factor NN in time polynomial in logN\log N — essentially O((logN)3)O((\log N)^3) elementary gate operations with some polylogarithmic overhead. That is an exponential speedup over the best known classical method.

Shor's algorithm does not directly factor NN. Instead it solves a related problem called order-finding (also called period-finding): given integers aa and NN with gcd(a,N)=1\gcd(a, N) = 1, find the smallest positive integer rr such that

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

The integer rr is called the order of aa modulo NN. Once rr is known, a simple classical calculation reveals the factors of NN with high probability.

The reduction from factoring to order-finding is entirely classical — the quantum part is order-finding itself, accomplished via the Quantum Fourier Transform (QFT), which is the subject of the next several lessons.

From order to factors

Here is the classical post-processing step, included now so the full picture is clear.

Suppose we have found the order rr of some randomly chosen aa. If rr is even and ar/2≢1(modN)a^{r/2} \not\equiv -1 \pmod{N}, then

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

Because N=pqN = pq divides the left side but neither factor on the right is divisible by NN (by our assumptions), the greatest common divisors

gcd(ar/21,  N)andgcd(ar/2+1,  N)\gcd(a^{r/2} - 1,\; N) \quad \text{and} \quad \gcd(a^{r/2} + 1,\; N)

are non-trivial divisors of NN, i.e., either pp or qq. Both GCDs are computed in O((logN)2)O((\log N)^2) time by the Euclidean algorithm.

A random aa has at least a 11/2k11 - 1/2^{k-1} chance of yielding an even rr with the required property, where kk is the number of distinct prime factors of NN. For N=pqN = pq (two primes) this means success probability at least 1/21/2; repeating a handful of times gives overwhelming confidence.

Why classical computers cannot keep up

The exponential gap between O((logN)3)O((\log N)^3) (Shor) and the sub-exponential GNFS is decisive in the long run. Doubling the bit length of NN approximately doubles the cost of Shor's algorithm but increases the GNFS cost by a far larger factor. This means that, as quantum hardware scales, longer RSA keys provide only a temporary fix.

Post-quantum cryptography addresses this by replacing RSA with problems believed to be hard even for quantum computers — lattice problems, hash-based signatures, and others. But those are outside this module. Our goal is to understand how Shor's quantum speedup works from the inside, starting with the Quantum Fourier Transform.

Roadmap for this module

The lessons ahead build Shor's algorithm layer by layer:

  1. The Quantum Fourier Transform (QFT) — the engine that detects periodicity in a quantum superposition.
  2. Quantum Phase Estimation (QPE) — uses the QFT to read out an eigenvalue of a unitary operator.
  3. Modular exponentiation — how to implement axmodNa^x \bmod N as a quantum circuit.
  4. Full Shor's algorithm — putting QPE and modular exponentiation together to find rr.

By the end you will have written and run all the key sub-circuits on small instances such as N=15=3×5N = 15 = 3 \times 5.

Sign in on the full site to ask questions and join the discussion.