Shor’s algorithm

On this page 6

Shor’s algorithm is a quantum algorithm for integer factorization and discrete logarithms that runs in polynomial time, exponentially faster than the best known classical methods. Published by Peter Shor in 1994, it is the most famous of all quantum algorithms and the main reason quantum computing is treated as a long-term threat to public-key cryptography.

The algorithm reduces factoring to period finding, a problem a quantum computer solves efficiently with the quantum Fourier transform. Only the period-finding step is quantum; the reduction before it and the number theory after it run on an ordinary classical computer.

How Shor’s algorithm turns factoring into period finding

To factor an odd composite number N, the algorithm first picks a random integer a smaller than N that shares no common factor with it. The function f(x) = a^x mod N is periodic, and its period r is called the order of a modulo N. If r is even and a^(r/2) is not congruent to −1 modulo N, then computing gcd(a^(r/2) ± 1, N) with the Euclidean algorithm yields a nontrivial factor of N.

Classically, finding r is as hard as factoring itself. The quantum part solves it by preparing a superposition over many exponents x, computing a^x mod N into a second register, and applying the quantum Fourier transform to the first register. Interference concentrates probability on values close to multiples of N²/r, and a classical continued fraction expansion recovers r from a measured value.

The steps below show the division of labor between classical and quantum processing in a single run.

StepProcessorPurpose
Random base selectionClassicalChoose a coprime to N, check gcd
Superposition preparationQuantumHadamard gates on the exponent register
Modular exponentiationQuantumCompute a^x mod N in superposition
Quantum Fourier transformQuantumConvert periodicity into measurable peaks
MeasurementQuantumObtain a sample related to k/r
Continued fractionsClassicalExtract the period r
Factor extractionClassicalCompute gcd(a^(r/2) ± 1, N)
Main steps of Shor’s algorithm

A single run can fail, for example when r is odd or the measurement gives an unhelpful value, but the success probability per run is constant, so a few repetitions with new random bases are usually enough.

Complexity and quantum resource requirements

For an n-bit number, Shor’s algorithm needs roughly O(n³) elementary gates with schoolbook arithmetic, and asymptotically faster multiplication lowers this further. The best classical method, the general number field sieve, runs in sub-exponential time of roughly exp(c · n^(1/3) · (log n)^(2/3)), which becomes infeasible for numbers of 2048 bits.

MethodRunning time for n-bit inputPractical reach
Trial divisionExponentialSmall numbers only
General number field sieveSub-exponentialRecord near 829 bits
Shor’s algorithmPolynomial, about O(n³)Needs fault-tolerant hardware
Classical and quantum factoring compared

The dominant cost is modular exponentiation, not the Fourier transform. Logical qubit requirements are on the order of 2n to 3n logical qubits in compact constructions, but each logical qubit must be protected by quantum error correction. Published resource estimates for breaking RSA-2048 have fallen over time, from about a billion physical qubits in early studies to estimates in the range of about one million noisy physical qubits running for several days, under assumed error rates near 0.1%. These figures depend heavily on architecture assumptions and remain estimates rather than demonstrations.

Impact of Shor’s algorithm on public-key cryptography

Shor’s algorithm breaks every widely deployed public-key scheme whose security rests on factoring or discrete logarithms. A generalization solves the discrete logarithm problem over finite fields and over elliptic curve groups, and both cases are instances of the abelian hidden subgroup problem. Symmetric ciphers and hash functions are affected only by the much weaker quadratic speedup of Grover’s algorithm.

SchemeHard problemEffect of Shor’s algorithm
RSAInteger factorizationBroken in polynomial time
Diffie–HellmanFinite-field discrete logarithmBroken in polynomial time
ECDSA and ECDHElliptic-curve discrete logarithmBroken, with fewer qubits than RSA
AES-256No algebraic structure exploitedNot affected by Shor
ML-KEM (Kyber)Module lattice problemsNo known quantum break
Cryptographic schemes and their exposure

The practical concern is harvest now, decrypt later: an adversary can record encrypted traffic today and decrypt it once a large quantum computer exists. This is why standards bodies such as NIST published post-quantum cryptography standards in 2024 and why organizations are planning migrations long before such a machine is available.

Experimental demonstrations and common misconceptions

Laboratory demonstrations so far have factored only very small numbers such as 15 and 21, and most of them relied on compiled circuits that used prior knowledge of the answer to simplify the computation. These experiments validate components such as order finding, but they do not show scalable factoring.

MisconceptionCorrect view
It tries all factors simultaneouslyIt finds a period through interference
Current quantum computers can break RSAFault-tolerant machines are required
It breaks all encryptionSymmetric ciphers and lattice schemes survive
Large factoring records existHonest demonstrations remain tiny numbers
Frequent misunderstandings about Shor’s algorithm
  • Claims of factoring large numbers on annealers or hybrid devices usually rely on different, non-scalable methods, not Shor’s algorithm.
  • Variants such as Regev’s algorithm reduce gate counts but require more qubits or repetitions, so the trade-off is still being studied.

A plain-language summary

Much online security relies on one assumption: multiplying two huge prime numbers is easy, but splitting the result back into those primes is practically impossible. Shor’s algorithm is a method that a large enough quantum computer could use to do that splitting quickly.

It works by spotting a hidden repeating pattern in a sequence of numbers, a bit like hearing the rhythm in a long drumbeat. Once the rhythm is known, ordinary arithmetic reveals the primes. No existing machine can do this for real security keys yet, but the risk is serious enough that new quantum-resistant encryption is already being rolled out.

An example from practice

A national health insurer stores patient records that must remain confidential for at least 25 years. Its security architects review the TLS connections and archive encryption, which use RSA-2048 and elliptic-curve key exchange, and recognize that both would fall to Shor’s algorithm on a future fault-tolerant quantum computer.

Because recorded traffic could be decrypted later, the team classifies long-lived data as high exposure to harvest-now, decrypt-later attacks. It deploys hybrid key exchange that combines classical X25519 with ML-KEM on external connections, keeps AES-256 for stored data, and inventories every system that still depends on RSA signatures.

The outcome is a staged five-year migration plan that prioritizes the most sensitive data first, so the insurer does not need to predict the exact year a cryptographically relevant quantum computer appears.