Is RSA built on an NP-hard problem? Not quite
6 min read
You'll often hear that RSA, long used to protect web traffic, is secure because factoring is NP-hard. It sounds right, and it isn't quite. The real story is more interesting, and it shows what P vs NP does and doesn't tell us about security.
This article builds a tiny RSA you can play with, measures why factoring is the barrier, then returns to the map from the P vs NP page.
How RSA works in four steps
- Pick two primes, p and q. Multiply them: n = p × q. Publish n.
- Compute φ = (p − 1)(q − 1). Choose a public exponent e that shares no factor with φ.
- Work out the private exponent d, the number with e × d ≡ 1 (mod φ). You can only do this if you know φ, and that needs p and q.
- Encrypt a number m as mᵉ mod n. Decrypt with cᵈ mod n.
Anyone can encrypt with the public pair (n, e). Decrypting needs d, and working out d from the public values needs φ, which needs the factors of n. Try it below.
Real RSA uses primes of about 1,024 bits each, padding, and a vetted library. This toy encrypts one letter at a time, which a real system must never do.
- n = p × q (public)
- 3233
- e (public)
- 17
- φ = (p−1)(q−1) (secret)
- 3120
- d (private)
- 2753
Letters as numbers (A=1)
8 5 12 12 15
Encrypted: mᵉ mod 3233
2041 3086 1730 1730 3031
Decrypted with d: cᵈ mod 3233
HELLO
A toy: tiny primes, one letter per block and no padding. Never use anything like this for real data.
Easy one way, hard the other
Multiplying two primes takes about (bits ÷ 2)² steps with the schoolbook method. Even for a 2048-bit n that's around a million steps, a blink. Going backwards, finding p and q from n, is factoring.
The simplest method, trial division, tries every possible divisor up to √n. Every two extra bits doubles the work. Drag the slider and compare.
- Multiply the two primes1,024 steps
At a billion steps a second: instant
- Factor n back by trial division4.29 × 10^9 steps
At a billion steps a second: 4.3 seconds
Bars use a log scale: each tick is ×10. Smarter factoring methods exist, but none known to a classical computer is fast.
Faster methods than trial division exist. The general number field sieve is the best known classical one, but its running time still grows faster than any polynomial in the number of bits. That is the whole security argument: nobody knows a fast classical way to factor, and decades of effort haven't found one. (Strictly, RSA relies on the slightly different "RSA problem" of undoing mᵉ mod n. Factoring n would solve it, but nobody has proved the two are equally hard.) "Nobody knows" isn't a proof, and that matters in a moment.
So is factoring NP-hard?
Factoring fits the NP pattern: given a claimed factor, you check it with one division. But it is not known to be NP-complete, and there is strong reason to doubt it is. (Since factoring is in NP, being NP-hard and being NP-complete mean the same thing here.)
The yes/no version ("does n have a factor d with 1 < d < k?") is in NP and in coNP. A "yes" is checked with the factor. A "no" is checked with the complete prime factorisation. If factoring were NP-complete, that would force NP = coNP, which almost nobody believes.
So factoring sits in the dashed "in-between?" zone of the map. Open it on the last step and flip to P ≠ NP:
Step 5 of 5
Two possible worlds
P ≠ NP, which almost everyone expects (88% in a 2019 poll of researchers). Then NP-complete problems have no fast solution, ever. Ladner's theorem (1975) adds that NP would also hold in-between problems, neither easy nor complete. Factoring and graph isomorphism are suspects, but none is proven.
Quantum computers add a twist. Shor's algorithm (announced 1994) factors in polynomial time on a large enough quantum computer. That doesn't mean quantum computers solve NP-complete problems. Factoring just isn't one of them, as far as anyone knows.
What would P = NP mean for RSA?
- If P = NP, there would be a polynomial-time algorithm for every problem in NP, factoring included. In theory RSA, and essentially all public-key cryptography, would fall. (How practical that is would depend on how large the polynomial is.)
- If P ≠ NP, RSA is still not guaranteed safe. Factoring could be easy even then, because it isn't known to be NP-complete.
There is a second gap. NP-hardness is a worst-case idea: some inputs are hard. Cryptography needs problems that are hard on typical inputs, since a randomly chosen key must be hard to break, not just the worst possible one.
If a one-way function exists (easy to compute, hard to undo on average), then P ≠ NP. But nobody knows how to go the other way: P ≠ NP alone is not known to give secure cryptography.
What comes next
Because of Shor's algorithm, standards bodies are moving to cryptography built on other problems. In August 2024 NIST finalised its first three post-quantum standards: FIPS 203 (ML-KEM, key exchange, derived from CRYSTALS-Kyber), FIPS 204 (ML-DSA, signatures) and FIPS 205 (SLH-DSA, hash-based signatures). ML-KEM is built on lattices, not on factoring.
In short
- RSA's security rests on factoring being hard, not on a proven NP-hard problem.
- Factoring is in NP and coNP, not known to be NP-complete, and unlikely to be.
- P = NP would break RSA in principle. P ≠ NP wouldn't, by itself, make it safe.
- Quantum computers threaten RSA through Shor's algorithm, not through NP.
Sources
- Rivest, Shamir and Adleman, A Method for Obtaining Digital Signatures and Public-Key Cryptosystems (1978)
- Wikipedia, Integer factorization and Co-NP: factoring in NP ∩ coNP, and what NP-completeness would imply
- Wikipedia, One-way function: one-way functions imply P ≠ NP, and the converse is open
- Peter Shor, Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer
- NIST, First 3 finalized post-quantum encryption standards (August 2024)
Keep exploring