Skip to content

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

"A quantum computer factors by trying all possible divisors at once." That is the parallelism myth again, and it fails for the same reason as always - a measurement would give you one random guess. Shor never searches for factors. It converts factoring into a completely different question, finding the period of a repeating sequence, and only that one question goes to the quantum machine.

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.

Shor's classical wrapper: plot a^x mod N, read off the period r, and let gcd finish the factoring.
N
02468150

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

Feed a superposition over exponents through the function and you get a state whose amplitudes repeat with exactly period - a comb. Run the quantum Fourier transform and, exactly as the periodscope showed, the weight concentrates on multiples of ; measure once and reason back to . That is the only quantum step, and it turns an exponential-time search for the period into a polynomial-time one.

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

Factoring is not known to be NP-complete, and Shor does notshow quantum computers beat the hardest classical problems in general - that boundary is the subject of the P versus NP lesson. Shor works because factoring has a very particular hidden periodic structure that the QFT is tailor-made to expose. Most problems have no such structure, which is exactly why the speedup is special rather than universal.
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)
Shor's structure: classical wrapper, one quantum call

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.

drop here

a periodic function whose period hides the answer

drop here

yields the factors through gcd(a^(r/2) +- 1, N)

drop here

the quantum step that finds the period fast

drop here

broken because it relies on factoring being hard

Primary source

Classiq: Shor's Algorithm Explained

A 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

  1. 1.Classiq, Shor's Algorithm Explained
  2. 2.Nielsen and Chuang, Quantum Computation and Quantum Information, chapter 5 (order-finding and factoring)