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 back into its prime factors and is hard. The best known classical algorithms take time that grows nearly exponentially in the number of digits, so a -bit RSA key is safe against classical machines. Shor’s algorithm breaks that assumption by factoring in polynomial time on a quantum computer.
Pick and a base below, then step through it: the animation reveals the sequence until it returns to — that is the period — and then runs the classical post-processing that produces the two factors.
Why factoring breaks RSA
In RSA, the public key includes the modulus and an exponent . The matching private key depends on , which you can only compute if you know and . So:
Anyone who can factor 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:
- Pick a random with that is coprime to (if , then is already a factor — you got lucky).
- Find the period of the function : the smallest with . This is the quantum step — the quantum Fourier transform extracts efficiently, where a classical computer would have to search.
- If is odd, or if , this is unlucky — start over with a different .
- Otherwise is a nontrivial square root of , and
are nontrivial factors of . Both gcds are computed classically and quickly with Euclid’s algorithm.
The reason it works: means divides . If doesn’t divide either factor on its own (the conditions in step 3), then must share part of itself with each — and the gcd extracts that shared part.
Worked example: factoring
Take and . Compute the powers of modulo :
The sequence returns to at exponent , so the period is . It is even, and , so this works. Now take the two gcds:
giving . With and recovered, the RSA private key built on is fully exposed.
What is actually quantum here
Only step 2 — finding the period — needs a quantum computer. Everything else (picking , computing gcds, checking parity) is ordinary classical arithmetic. The quantum subroutine prepares a superposition over many , computes 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 ; factoring recovers the private key.
- Shor reduces factoring to finding the period of , which a quantum computer does in polynomial time via the quantum Fourier transform.
- If is even and , then are nontrivial factors; otherwise pick a new .
- For the period is and , , so .