How Shor's Algorithm Breaks RSA
RSA encryption has protected private communications, financial transactions, and software distribution for nearly five decades. Its security rests on a single assumption: that factoring a large composite number into its prime factors is computationally infeasible. This assumption has held against every classical attack we have devised.
Peter Shor's 1994 algorithm changed the picture entirely. A quantum computer running Shor's algorithm can factor an -bit integer in time — polynomial, not exponential. For RSA, this is catastrophic (Shor, 1997).
This post works through the mathematics step by step: why factoring is hard classically, what makes quantum computers different, the key mathematical insight connecting factoring to period-finding, and how the Quantum Fourier Transform makes period-finding tractable.
Why RSA Works (Classically)
RSA key generation proceeds as follows (Rivest et al., 1978):
- Choose two large primes and , each typically 2048–4096 bits.
- Compute .
- Compute Euler's totient: .
- Choose coprime to (commonly ).
- Compute such that .
The public key is ; the private key is .
The security reduces to the following: given only and , an adversary cannot compute without knowing , and computing requires knowing and , which requires factoring .
Multiplying takes microseconds. Factoring back into and — with current classical algorithms — takes longer than the age of the universe for 2048-bit keys.
How Hard Is Factoring, Classically?
Trial division — checking every integer up to — runs in time , which is in terms of the bit-length. For a 2048-bit , this is operations: entirely hopeless.
The best known classical algorithm is the General Number Field Sieve (GNFS) (Lenstra et al., 1993), which runs in heuristic time:
For a 2048-bit , this is approximately operations — still astronomically large. The 768-bit RSA-768 challenge was factored in 2009 using the GNFS across thousands of machines over two years (Kleinjung et al., 2010); RSA-2048 remains unfactored.
The GNFS is sub-exponential but still super-polynomial. It does not break RSA in practice — but it does set a floor on how large keys must be.
What Makes a Quantum Computer Different?
A classical bit is either 0 or 1. A qubit can be in a superposition (Nielsen & Chuang, 2010):
where and . Before measurement, the qubit exists in both states simultaneously. Upon measurement, it collapses to with probability and to with probability .
A register of qubits can represent a superposition of all states simultaneously:
A quantum gate applied to this register acts on all states at once — this is quantum parallelism. But measurement collapses the superposition to a single outcome.
The art of quantum algorithm design is using interference — the wave-like cancellation and reinforcement of probability amplitudes — to make desired outcomes more probable and undesired ones less probable, so that measurement yields a useful result with high probability (Nielsen & Chuang, 2010).
The Key Insight: Factoring Reduces to Period-Finding
Shor's algorithm does not attempt to factor directly. Instead, it exploits a classical number-theoretic connection (Shor, 1997):
Claim. If we can find the order (period) of a random element — that is, the smallest positive integer such that — then we can likely factor .
Here is why. If is even, write:
Since , either or (or both) divides one of the two factors. If , then:
This gives a non-trivial factor of using only one GCD computation — an classical operation.
One can show that for a randomly chosen , the probability that is odd or that is at most . By repeating with different values of , the failure probability drops exponentially fast.
The problem has shifted. We no longer need to factor ; we need to find the period of . This function is periodic: for all . Finding the period classically requires evaluating at points — still exponential. Quantum computing finds the period in polynomial time.
The Quantum Period-Finding Algorithm
Here is the structure of the quantum subroutine (Shor, 1997; Nielsen & Chuang, 2010):
Step 1: Initialize a superposition.
Create a quantum register of qubits, where , initialized in uniform superposition:
Step 2: Compute in superposition.
Using quantum circuits for modular exponentiation, compute and store the result in a second register:
All evaluations happen simultaneously through quantum parallelism.
Step 3: Measure the second register.
Suppose the measurement yields the value for some . The first register collapses to a superposition over all with — exactly those congruent to :
where is the number of such terms.
Step 4: Apply the Quantum Fourier Transform.
The first register now encodes a periodic function with period . The Quantum Fourier Transform (QFT) is the quantum analogue of the discrete Fourier transform (Nielsen & Chuang, 2010):
Applied to , the QFT concentrates probability amplitude at multiples of :
The complex exponentials destructively interfere at non-multiples of and constructively reinforce at multiples.
Step 5: Measure and extract .
Measurement yields some value for a random integer . Dividing by :
By the continued fraction algorithm, we recover exactly from this rational approximation in polynomial time — the approximation is guaranteed to be within with high probability.
The Quantum Fourier Transform
The QFT deserves a closer look because it is the computational heart of the algorithm.
The classical Discrete Fourier Transform (DFT) on points takes time via the Fast Fourier Transform. The QFT on qubits (where ) uses only quantum gates — exponentially fewer operations in terms of (Nielsen & Chuang, 2010). It is implemented as a sequence of Hadamard gates and controlled phase rotations:
Figure 1. Three-qubit QFT circuit. R_k = diag(1, e^(2πi/2^k)). Filled dot = control; × = SWAP. Dashed lines separate stages.
The total circuit depth is gates for qubits, compared to the operations of the classical FFT. The speedup is exponential.
Overall Complexity
Putting it all together (Shor, 1997):
| Step | Classical Cost | Quantum Cost |
|---|---|---|
| Choose random | ||
| Check | ||
| Modular exponentiation | quantum gates | |
| Quantum Fourier Transform | — | gates |
| Continued fraction recovery |
Total: quantum gate operations, where is the number of bits in the modulus. This is polynomial. Classically, RSA-2048 requires approximately operations; quantum mechanically, around gate operations — well within reach of a fault-tolerant quantum computer.
What "Sufficiently Large" Means
The practical caveat is significant. Shor's algorithm requires a fault-tolerant quantum computer with enough logical qubits and sufficiently low error rates. Current quantum computers are noisy intermediate-scale quantum (NISQ) devices — they lack the error correction infrastructure to run Shor's algorithm against real RSA key sizes.
A 2021 resource analysis estimated that factoring a 2048-bit RSA modulus would require approximately 20 million physical qubits running for about eight hours under optimistic hardware assumptions (Gidney & Ekerå, 2021). Current leading hardware operates in the hundreds to low thousands of physical qubits, with error rates still well above the fault-tolerance threshold.
This does not mean RSA is safe indefinitely. The harvest now, decrypt later threat is real: adversaries can record encrypted traffic today and decrypt it once quantum hardware matures. Communications requiring long-term secrecy must transition to post-quantum cryptography immediately.
Post-Quantum Cryptography
The response is post-quantum cryptography (PQC) — systems whose hardness assumptions are not known to be vulnerable to quantum algorithms.
In 2024, NIST finalized its first post-quantum standards (National Institute of Standards and Technology, 2024):
- ML-KEM (formerly CRYSTALS-Kyber): a key encapsulation mechanism based on the hardness of the Module Learning With Errors (MLWE) problem — a lattice problem.
- ML-DSA (formerly CRYSTALS-Dilithium): a digital signature scheme, also lattice-based.
- SLH-DSA (formerly SPHINCS+): a stateless hash-based signature scheme relying only on the security of cryptographic hash functions.
Lattice problems — specifically, finding a short vector in a high-dimensional lattice — are not known to yield efficiently to quantum algorithms. The best known quantum attacks offer only modest speedups, leaving large parameter margins of safety.
The transition is underway. TLS, SSH, code signing, certificate authorities, and hardware security modules all require updates. The cryptographic infrastructure of the internet is being renegotiated in real time.
References
Gidney, C., & Ekerå, M. (2021). How to factor 2048 bit RSA integers in 8 hours using 20 million noisy qubits. Quantum, 5, 433. https://doi.org/10.22331/q-2021-04-15-433
Kleinjung, T., Aoki, K., Franke, J., Lenstra, A. K., Thomé, E., Bos, J. W., Gaudry, P., Kruppa, A., Montgomery, P. L., Osvik, D. A., te Riele, H., Timofeev, A., & Zimmermann, P. (2010). Factorization of a 768-bit RSA modulus. In T. Rabin (Ed.), Advances in cryptology — CRYPTO 2010 (pp. 333–350). Springer. https://doi.org/10.1007/978-3-642-14623-7_18
Lenstra, A. K., Lenstra, H. W., Jr., Manasse, M. S., & Pollard, J. M. (1993). The number field sieve. In A. K. Lenstra & H. W. Lenstra, Jr. (Eds.), The development of the number field sieve (pp. 11–42). Springer. https://doi.org/10.1007/BFb0091537
National Institute of Standards and Technology. (2024). Post-quantum cryptography standardization. U.S. Department of Commerce. https://csrc.nist.gov/projects/post-quantum-cryptography
Nielsen, M. A., & Chuang, I. L. (2010). Quantum computation and quantum information (10th anniversary ed.). Cambridge University Press.
Rivest, R. L., Shamir, A., & Adleman, L. (1978). A method for obtaining digital signatures and public-key cryptosystems. Communications of the ACM, 21(2), 120–126. https://doi.org/10.1145/359340.359342
Shor, P. W. (1997). Polynomial-time algorithms for prime factorization and discrete logarithms on a quantum computer. SIAM Journal on Computing, 26(5), 1484–1509. https://doi.org/10.1137/S0097539795293172