The Famous Algorithms
Shor's algorithm and RSA
Factoring becomes period finding, and RSA falls
The security of most of the internet rests on one bet: that multiplying two large primes is easy but recovering them from the product is not. Shor's algorithm collects on the opposite side of that bet. And the surprise is how little of it is quantum - almost the entire algorithm is grade-school number theory, with one exponentially hard step handed off to the quantum Fourier transform you just met. That single handoff is enough to break RSA.
What Shor is not
Factor a number by hand, watching the period
The reduction is easier to trust once you have run it. Pick a modulus and a base below. The widget plots , which always eventually repeats, shades one full period , and then finishes the job with two greatest-common-divisor computations. Try , then hunt for a base where it fails and makes you retry.
2^x mod 15 repeats with period 4. The shaded band is one full period; the transform in the last lesson is what finds it fast.
r = 4 is even, and a^(r/2) = 2^2 mod 15 = 4.
gcd(4 - 1, 15) = 3 and gcd(4 + 1, 15) = 5
15 = 3 x 5. Factored.
Everything on screen except finding the period is cheap classical arithmetic. The plot repeats; the two gcds are instant. The one step that does not scale - locating the period when is hundreds of digits long - is the step the quantum computer takes over.
The reduction, step by step
To factor , pick a random with . If you already stumbled onto a factor and you are done. Otherwise, look at the sequence . Because there are only finitely many residues, it must cycle, and its is the smallest positive exponent with
Now the algebra that turns a period into factors. If is even, write . Since , the product of those two factors is a multiple of , so - as long as - each shares a real factor with :
That is the whole classical wrapper, and it is what the widget computed. Roughly half of random bases give an even that avoids the trap, so a couple of tries almost always works. The catch is the missing piece: for a real RSA modulus, computing the period by classical means is itself as hard as factoring. No speedup there.
Where the quantum computer earns its keep
Why RSA falls
RSA publishes a modulus and trusts that nobody can recover the primes and . The best classical factoring algorithms run in time that grows nearly exponentially in the number of digits, so a 2048-bit modulus is safe against every classical machine for the foreseeable future. Shor factors in time polynomial in the number of digits. A large enough, reliable quantum computer therefore recovers and from the public , and the whole scheme unravels. The same period-finding idea also breaks Diffie-Hellman and elliptic-curve cryptography, which rest on a close cousin, the discrete logarithm.
This is a structure story, not raw power
from math import gcd
from random import randrange
def shor(N):
a = randrange(2, N)
g = gcd(a, N)
if g > 1:
return g, N // g # lucky: a shared a factor with N
r = quantum_find_period(a, N) # the ONLY quantum step (QFT-based)
if r % 2 != 0 or pow(a, r // 2, N) == N - 1:
return shor(N) # bad base, retry with a fresh a
return gcd(pow(a, r // 2) - 1, N), gcd(pow(a, r // 2) + 1, N)Lock it in
- Shor never searches for factors; it reduces factoring to finding the period r of a^x mod N.
- When r is even and a^(r/2) is not -1 mod N, gcd(a^(r/2) +- 1, N) are nontrivial factors - pure classical arithmetic.
- The only quantum step is period-finding via the QFT, which turns an exponential search into a polynomial-time one.
- RSA rests on factoring being hard, so efficient factoring breaks it; the same idea breaks Diffie-Hellman and elliptic-curve crypto too.
Check yourself
How does Shor's algorithm turn factoring N into something a quantum computer can speed up?
Why does breaking factoring break RSA?
Recall: how does Shor convert factoring into period-finding, and why does RSA care?
The reduction and its consequence in one recall. Try to state it, then check.
Match each piece of Shor's algorithm to its role.
a periodic function whose period hides the answer
yields the factors through gcd(a^(r/2) +- 1, N)
the quantum step that finds the period fast
broken because it relies on factoring being hard
Primary source
Classiq: Shor's Algorithm ExplainedA clear end-to-end walk through the reduction, the order-finding circuit, and the classical post-processing, with the number theory kept front and center so the quantum step reads as one component rather than the whole trick.[1]
The consequence for real systems - what to migrate, what survives, and how soon - is the subject of the state-of-the-field and post-quantum cryptography lesson.[2]
Sources