Skip to content
Trapdoor·Lab

Shor's algorithm

1Toy

A period instead of factors

RSA rests on the fact that nobody can factor the product n = p · q of two large primes (RSA on small numbers). Peter Shor showed that a quantum computer could — in a number of steps polynomial in the number of digits. His algorithm reduces factoring to finding the order: the smallest r with aʳ ≡ 1 (mod N), the period of the powers of a, which go round in a cycle as in the Arithmetic modulo p station. The rest is classical. When r is even, y = a^(r/2) satisfies y² ≡ 1, so N divides (y − 1)(y + 1); if moreover y ≠ N − 1 (that is, y is not −1 modulo N), then gcd(y − 1, N) and gcd(y + 1, N) are factors of N. The “Break it” mode of the RSA station already shows this classical half, except that there a plain search through the powers finds the order — hopeless for a real n.

The quantum step looks for the period differently. The input register has m qubits, where Q = 2^m is the smallest power of two not below N². Hadamard gates give a superposition of all x from 0 to Q − 1, and modular exponentiation writes aˣ mod N into a second register — for all x at once. The state is periodic with period r, but it cannot be read out: a measurement gives one value. The quantum Fourier transform (QFT) turns the period into peaks near multiples of Q/r, so a measurement gives k ≈ j · Q/r, and expanding k/Q as a continued fraction gives back j/r. Every candidate for r is checked classically: is aʳ ≡ 1 (mod N)? The QFT on m qubits is about m²/2 gates, whereas a classical computer would have to store and transform the whole vector of 2^m numbers.

That is exactly what the toy does: it stores Q complex numbers for every value of the second register and applies the QFT as its circuit builds it — a Hadamard on each qubit, the controlled rotations (diagonal, so applied in one pass) and the swaps. It is a simulation on your device, not a quantum computer — and that is why it works only for small N. For N = 57 the two registers have 18 qubits, and each further qubit doubles the memory. The same rule (Q ≥ N²) would give a 2048-bit n registers of about 4096 and 2048 qubits, and a state vector of about 2⁶¹⁴⁴ amplitudes. The simulator also has to compute aˣ mod N for every x — that table alone gives the period away — so the toy reads r only from the sampled measurement k.

Shor’s algorithm on a small N

Simulation: the quantum step computes a state vector on your device

The simulator has to compute aˣ mod N for every x — that table alone gives the period away. A quantum computer shows no such table: it gives one measurement per run, so the toy reads r only from the sampled k. Every qubit doubles the simulation’s memory, which is why it works only for N < 64.

15
7

Only a coprime to 15 — any other a would give a factor straight away through the gcd.

Input-register qubits
8 (Q = 256)
Work-register qubits
4
State-vector amplitudes of both registers
2^12 = 4,096
QFT gates
H: 8 · rotations: 28 · swaps: 4

Step 4 of 4 · The quantum Fourier transform — 40 gates on 8 qubits — turns the period into peaks: the probability gathers near multiples of Q/r. This chart exists only in the simulation; a quantum computer gives one sample per run.

First values of 7ˣ mod 15, from x = 0: 1, 7, 4, 13, 1, 7, 4, 13, …

0.2500255k

After the QFT: k from 0 to Q − 1. The peaks sit near multiples of Q/r; the circle is the measurement.

The same algorithm breaks the discrete logarithm, and with it Diffie–Hellman, ECDH, ECDSA and Ed25519 (Elliptic curves over a small field). Shor described it in the same paper. When h = gˢ, the function f(x, y) = gˣ · h⁻ʸ = g^(x − s·y) does not change when x grows by s and y by 1 — it has a hidden period (s, 1), which a two-dimensional QFT finds. On an elliptic curve the same works with point addition: f(x, y) = x·G − y·P, where P = s·G is the public key. The algorithm does not care what the group is — it needs a function with a hidden period. That is why a large enough quantum computer would break RSA, DH and elliptic curves all at once. It would not break ML-KEM or ML-DSA: they rest on lattices, and NIST considers them secure even against an adversary with a quantum computer (FIPS 203, FIPS 204).

2Break it

When Shor fails

The algorithm is probabilistic and fails in three ways, each with a classical cure. The first is the measurement. k = 0 says nothing; a k from the peak j · Q/r where j and r share a divisor gives a divisor of r instead of r; when r does not divide Q, the measurement may land beside a peak. So every candidate is checked, a failed measurement is repeated, and two denominators are joined by their lcm — in the toy above one run gives r with a probability of more than 30% for every N and a. The second is the base a: the order may be odd, or a^(r/2) ≡ −1, and then a new a is drawn. The third is N itself. For a prime power no a works, because modulo pᵏ the only square roots of 1 are ±1. A classical check before the run catches such an N, as it does an even one. An RSA modulus — the product of two different primes — is neither, so none of these exceptions protects RSA. Check it in the grid: every a from 2 to N − 2 with what comes out of its period.

When Shor fails

You are on the attacker’s side, with a quantum computer. Look for N and a for which the algorithm gives no factors — and see what rescues it.
21

N = 21 is not a power of an integer

The orders in the grid are computed classically, by searching the powers — they are the same r the quantum step of the toy above delivers.

  • factors
  • odd r
  • a^(r/2) ≡ −1
  • shares a factor with N

The order of 4 modulo 21 is r = 3 — odd, so a^(r/2) does not exist. Draw another a.

6 of the 10 values of a coprime to 21 give factors (60%). When an a fails, drawing the next one is enough.

The rescue: draw another a. In the grid, at least half of the a work for every N with at least two different prime factors.

An experimental run of the algorithm in 2001 factored 15 = 3 · 5 (below) — the same number the toy starts with. Records call for care: Smolin, Smith and Vargo showed that a “compiled” version of the algorithm “factors” products of large primes in constant time on two qubits. The measure of difficulty is the length of the period found, not the size of the number, so such a demonstration says nothing about RSA. Nobody has yet built a computer that would break a real key — NIST IR 8547 says plainly that the migration starts before one exists. The estimates of its size are falling fast, though: for RSA-2048 from 20 million noisy qubits in 2019 to fewer than a million in 2025, under the same hardware assumptions (details below).

3In the real world

From 15 to RSA-2048

  • RSA-2048 — a 2025 estimateCraig Gidney estimates that a 2048-bit RSA integer could be factored in less than a week by a quantum computer with fewer than a million noisy qubits — assuming a gate error rate of 0.1%, a surface code cycle time of 1 µs and a control system reaction time of 10 µs. The 2019 estimate under the same assumptions said 20 million qubits and eight hours.Source (RSA-2048 — a 2025 estimate)
  • The secp256k1 curve — 2026Babbush, Gidney, Boneh et al. (Google Quantum AI, Ethereum Foundation, Stanford): the discrete logarithm on the 256-bit secp256k1 curve that cryptocurrencies rest on needs fewer than 1200 logical qubits and 90 million Toffoli gates (or fewer than 1450 and 70 million). On superconducting hardware with a 10⁻³ physical error rate and planar connectivity the circuit would run in minutes on fewer than half a million physical qubits. The authors replaced the attack details with a zero-knowledge proof.Source (The secp256k1 curve — 2026)
  • Neutral atoms — P-256Cain, Preskill et al. (March 2026) put Shor’s algorithm on as few as 10,000 neutral-atom qubits in their title. In their estimate the discrete logarithm on the P-256 curve would take a few days on 26,000 physical qubits, and factoring RSA-2048 one to two orders of magnitude longer. The authors note that experiments already trap more than 6,000 atomic qubits.Source (Neutral atoms — P-256)
  • NIST IR 8547The October 2026 report: RSA, ECDSA, EdDSA, DH and ECDH — all vulnerable to Shor’s algorithm — will be deprecated by NIST after 2030 and disallowed in its standards after 2035. Among the main reasons for urgency it names “harvest now, decrypt later”: adversaries collect encrypted data now to decrypt it once quantum technology matures.Source (NIST IR 8547)
  • ML-DSA — FIPS 204Since 13 August 2024 NIST has a standard for lattice signatures, ML-DSA (derived from CRYSTALS-Dilithium), published together with ML-KEM (FIPS 203) for key establishment. NIST believes ML-DSA to be secure even against an adversary with a large-scale quantum computer. The ML-DSA station covers it.Source (ML-DSA — FIPS 204)
  • A 2001 run: 15 = 3 · 5Vandersypen et al. (Nature, December 2001) ran Shor’s algorithm in an experiment and factored 15 on seven nuclear spins in a molecule, with room-temperature liquid-state NMR. They stressed themselves that the result does not imply scalability.Source (A 2001 run: 15 = 3 · 5)
4Take it to your project

Migrate, don’t panic

Good practice

Take an inventory: where your system uses RSA, DH and elliptic curves — in TLS, in tokens, in code signing, in keys stored on disk. Start with key establishment, because only it is exposed to “harvest now, decrypt later”: turn on the X25519MLKEM768 hybrid (Hybrid schemes), which current TLS libraries already use. A recording does not threaten signatures, but replacing them takes longer (NIST IR 8547) — plan for ML-DSA, first where a device verifies code signatures and cannot be updated. Leave symmetric cryptography alone: NIST does not expect to replace AES, SHA-2 or HKDF as part of this migration.

JavaScript
// Inventory of WebCrypto keys: which ones Shor's algorithm breaks
const SHOR = new Set(['RSA-OAEP', 'RSA-PSS', 'RSASSA-PKCS1-v1_5', 'ECDSA', 'ECDH', 'Ed25519', 'X25519']);
const quantumVulnerable = (key) => SHOR.has(key.algorithm.name);

quantumVulnerable(rsaKey); // true — move to ML-KEM / ML-DSA
quantumVulnerable(aesKey); // false — AES-GCM stays
Anti-pattern

Do not defend with a longer key: Shor’s cost is polynomial, so RSA-8192 or a bigger curve raises the cost of the attack only polynomially. Do not build a new protocol on ECDH alone for data that must stay secret in ten years — recorded traffic will wait. Do not throw away the classical half in a hurry either: ML-KEM alone, without a hybrid, has no margin in case a weakness turns up in the new algorithm. And do not buy home-grown “quantum-proof” schemes — the answer is the FIPS 203 and FIPS 204 standards from audited libraries.

JavaScript
// ✕ a longer key is no defence: Shor's cost grows only polynomially
await crypto.subtle.generateKey(
  { name: 'RSA-OAEP', modulusLength: 8192, publicExponent: new Uint8Array([1, 0, 1]), hash: 'SHA-256' },
  false,
  ['encrypt', 'decrypt'],
);
// ✕ ECDH alone for data secret for years: a recording of the exchange is enough
const key = await crypto.subtle.deriveKey({ name: 'ECDH', public: peer }, mine, { name: 'AES-GCM', length: 256 }, false, ['encrypt']);