cs.thefarshad
hard

Shor's Algorithm & Breaking RSA

Factoring large numbers is the rock RSA stands on — Shor's algorithm reduces factoring to period finding and cracks it in polynomial time.

Most of the internet’s encryption rests on one assumption: that factoring a large number N=pqN = p \cdot q back into its prime factors pp and qq is hard. The best known classical algorithms take time that grows nearly exponentially in the number of digits, so a 20482048-bit RSA key is safe against classical machines. Shor’s algorithm breaks that assumption by factoring in polynomial time on a quantum computer.

Pick NN and a base aa below, then step through it: the animation reveals the sequence a0,a1,a2,modNa^0, a^1, a^2, \dots \bmod N until it returns to 11 — that is the period rr — and then runs the classical gcd\gcd post-processing that produces the two factors.

N =
base a =
f(x) = ax mod N = 7x mod 15
x=01
x=1·
x=2·
x=3·
x=4·
period: step until the sequence returns to 1…
step 0/7

Why factoring breaks RSA

In RSA, the public key includes the modulus N=pqN = p \cdot q and an exponent ee. The matching private key depends on φ(N)=(p1)(q1)\varphi(N) = (p-1)(q-1), which you can only compute if you know pp and qq. So:

factor N    recover p,q    recover φ(N)    recover the private key\text{factor } N \;\Longrightarrow\; \text{recover } p, q \;\Longrightarrow\; \text{recover } \varphi(N) \;\Longrightarrow\; \text{recover the private key}

Anyone who can factor NN can decrypt every message and forge every signature. RSA is secure only because nobody has an efficient classical factoring algorithm.

Reducing factoring to period finding

Shor’s key insight is that factoring is equivalent to period finding, and period finding is exactly what a quantum computer does well. The recipe:

  1. Pick a random aa with 1<a<N1 < a < N that is coprime to NN (if gcd(a,N)1\gcd(a, N) \neq 1, then gcd(a,N)\gcd(a, N) is already a factor — you got lucky).
  2. Find the period rr of the function f(x)=axmodNf(x) = a^x \bmod N: the smallest r1r \geq 1 with ar1(modN)a^r \equiv 1 \pmod N. This is the quantum step — the quantum Fourier transform extracts rr efficiently, where a classical computer would have to search.
  3. If rr is odd, or if ar/21(modN)a^{r/2} \equiv -1 \pmod N, this aa is unlucky — start over with a different aa.
  4. Otherwise ar/2a^{r/2} is a nontrivial square root of 11, and

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

are nontrivial factors of NN. Both gcds are computed classically and quickly with Euclid’s algorithm.

The reason it works: ar1a^r \equiv 1 means NN divides ar1=(ar/21)(ar/2+1)a^r - 1 = (a^{r/2}-1)(a^{r/2}+1). If NN doesn’t divide either factor on its own (the conditions in step 3), then NN must share part of itself with each — and the gcd extracts that shared part.

Worked example: factoring N=15N = 15

Take N=15N = 15 and a=7a = 7. Compute the powers of 77 modulo 1515:

71=7,724,7313,741(mod15)7^1 = 7, \quad 7^2 \equiv 4, \quad 7^3 \equiv 13, \quad 7^4 \equiv 1 \pmod{15}

The sequence returns to 11 at exponent 44, so the period is r=4r = 4. It is even, and 72=494≢1(mod15)7^{2} = 49 \equiv 4 \not\equiv -1 \pmod{15}, so this aa works. Now take the two gcds:

gcd(721,  15)=gcd(48,15)=3,gcd(72+1,  15)=gcd(50,15)=5\gcd(7^2 - 1,\; 15) = \gcd(48, 15) = 3, \qquad \gcd(7^2 + 1,\; 15) = \gcd(50, 15) = 5

giving 15=3×515 = 3 \times 5. With p=3p = 3 and q=5q = 5 recovered, the RSA private key built on N=15N = 15 is fully exposed.

What is actually quantum here

Only step 2 — finding the period rr — needs a quantum computer. Everything else (picking aa, computing gcds, checking parity) is ordinary classical arithmetic. The quantum subroutine prepares a superposition over many xx, computes axmodNa^x \bmod N in superposition, and applies the quantum Fourier transform so that measuring the result reveals the period. The visualizer above shows the output of that subroutine — the periodic sequence — together with the classical wrapper that turns the period into factors.

Takeaways

  • RSA’s security rests entirely on the hardness of factoring N=pqN = p \cdot q; factoring NN recovers the private key.
  • Shor reduces factoring to finding the period rr of f(x)=axmodNf(x) = a^x \bmod N, which a quantum computer does in polynomial time via the quantum Fourier transform.
  • If rr is even and ar/2≢1(modN)a^{r/2} \not\equiv -1 \pmod N, then gcd(ar/2±1,N)\gcd(a^{r/2} \pm 1, N) are nontrivial factors; otherwise pick a new aa.
  • For N=15,a=7N = 15, a = 7 the period is r=4r = 4 and gcd(48,15)=3\gcd(48,15)=3, gcd(50,15)=5\gcd(50,15)=5, so 15=3×515 = 3 \times 5.

References