Secrets and One-Way Functions

Every online secret rests on a problem being hard. Multiply primes in a blink and fail to undo it, then tour the five worlds we might be living in

Secrets and One-Way Functions

Every padlock on the internet is a bet that some problem in NP is hard: not just in the worst case, but for nearly every instance, for as long as the secret matters. If P = NP, every one of those locks can be picked. If P ≠ NP, they still might be.

A One-Way Street

Slide the size of the primes up and watch multiplying stay instant while two factoring algorithms fall behind, then switch the chart out to 617 digits. Below, do the same with powers modulo a prime.

Forwards: multiply
159017
× 955901
──────────────
159017
0
1431153
795085
795085
1431153
──────────────
152004509317
36 single-digit multiplications by the school method.
Backwards: factor
N = 152004509317 (12 digits) = ? × ?
Trial division: 0 divisions so far…
Pollard’s rho: 0 operations so far…
36
digit multiplications to compute N
0…
divisions by trial division, not finished
0…
operations by Pollard’s rho, not finished
4.4 × 10¹⁵⁴
operations rho would need at 617 digits (extrapolated)
Add one digit to each prime and multiplying costs a few more digit products, while trial division takes ten times as long and Pollard’s rho about three times as long. Extrapolating these two algorithms to a 617-digit N gives 1.6 × 10³⁰⁸ and 4.4 × 10¹⁵⁴ operations: 5.0 × 10²⁹¹ years and 1.4 × 10¹³⁸ years at a billion operations a second. The best algorithm known does far better, roughly 1.6 × 10³⁵ by its heuristic formula, which is still 5.0 × 10¹⁸ years. None of this proves that factoring is hard. It shows how these particular algorithms behave.

Sky: multiplying. Rose: trial division, which tries 2 and then every odd number, so it needs about √N / 2 divisions. Pink: Pollard’s rho, about 2.5 × N1/4 operations (three modular multiplications and a gcd per step). Dots are measured runs; amber marks the secret primes. The primes are random and pass a Miller–Rabin test. Each search is cut off after 3 seconds of computing, which trial division reaches at around 9 digits per prime and Pollard’s rho at around 14.

The same street, a different function: powers modulo a prime
Forwards: raise g to the power x
p = 30707123, g = 2, secret x = 22121900
gx mod p = 12121952, by repeated squaring
bitstepexponent so farvalue
1start12
0square24
1square, × base532
0square101024
1square, × base212097152
0square4218819429
0square8417879336
0square16816869889
1square, × base33729737455
1square, × base6757847928
0square135021735378
0square270026017724
0square540017451596
1square, × base1080126372826
1square, × base216037893554
0square432062986140
1square, × base8641312010383
1square, × base17282720231714
0square34565417349679
1square, × base69130926475829
0square138261830142163
1square, × base276523719930276
1square, × base553047524130110
0square110609506539192
0square2212190012121952
25 binary digits, 35 modular multiplications.
Backwards: find x from g, p and the answer
Baby steps: writing down g0, g1, g2, … in a table (0 multiplications so far).
35
multiplications to compute gx mod p
0…
multiplications to recover x
5,542
table entries stored, about √p
…
how much dearer backwards is than forwards
Going forwards, each extra digit of p adds three or four squarings. Going backwards, each extra two digits multiply the work, and the memory, by ten. Real systems use a p of 600 digits or more, where a table of √p entries could not be stored in the universe; the best known algorithms are much cleverer than this one and still far from polynomial. As with factoring, nobody has proved that no fast method exists.

Here p is a safe prime (p = 2q + 1 with q prime) and g is its smallest primitive root, so every remainder from 1 to p − 1 is a power of g exactly once. Baby-step giant-step (Daniel Shanks) writes x = i·m + j with m about √p: it tabulates gj for every j below m, then tries i = 0, 1, 2, … Amber marks the secret. The prime is capped at 12 digits, a table of a million entries.

Go deeper:One-Way Functions, and What Nobody Has Proved

A one-way function is a function that can be computed in polynomial time but that no polynomial-time algorithm, even one that flips coins, can invert on more than a negligible fraction of inputs. Inverting means finding any input that gives the right output. Multiplying two primes and raising a number to a power modulo a prime are the two classic candidates.

Nobody has proved that a one-way function exists. If P = NP, none does, because finding an input that produces a given output is a search problem whose answers are easy to check. So a proof that one exists would also prove P ≠ NP. The converse is not known: P ≠ NP says only that some instances of some problem are hard, and a one-way function needs a problem that is hard on almost every instance that can be generated. Hardness in the worst case is not hardness on average.

The two factoring algorithms in the demo are far from the best. The general number field sieve is sub-exponential: its running time grows faster than any polynomial in the number of digits but much more slowly than the square or fourth root of N. The largest RSA challenge number factored in public so far, RSA-250, has 829 bits and fell in 2020 after a very large computation. The dashed lines on the chart extrapolate two specific algorithms; they are not lower bounds on anything.

Factoring is not believed to be NP-complete. Phrased as a yes-or-no question (does N have a factor below k?), it lies in both NP and co-NP: a yes is certified by a factor, and a no by the complete prime factorisation of N, which can be checked because primality has short proofs. A problem in NP ∩ co-NP cannot be NP-complete unless NP = co-NP. See also: Between and Beyond.

Breaking the Lock

Type a message and follow it through encryption and decryption with a small RSA key. Then choose an attacker: factor N by brute force, or ask an oracle a few dozen yes-or-no questions.

1. Make a key
Secret primes p = 3733, q = 5807
Public modulus N = p × q = 21677531 (25 bits)
Public exponent e = 17
(p − 1)(q − 1) = 21667992
Private exponent d = 16569641, the inverse of e modulo (p − 1)(q − 1)
Finding d: Euclid’s algorithm on (p − 1)(q − 1) and e
divisionremainder = s·(p − 1)(q − 1) + t·e
21667992 = 1274587 × 17 + 13s = 1, t = −1274587
17 = 1 × 13 + 4s = −1, t = 1274588
13 = 3 × 4 + 1s = 4, t = −5098351
4 = 4 × 1 + 0s = −17, t = 21667992
The remainder reaches 1 with t = −5098351, so e × t leaves remainder 1, and d is t reduced modulo (p − 1)(q − 1). 4 divisions.
2. Send a message
blocktextas a number msent: me mod Nreceived: cd mod N
“Mee”507222974223155072229
“t a”7610465156679507610465
“t n”761047884034547610478
“oon”7303022101820027303022
Encrypting block 1 with the public key: 5072229e mod N
bitstepexponent so farvalue
1start15072229
0square26266773
0square419634414
0square814996205
1square, × base177422315
5 binary digits, 5 modular multiplications.
Decrypting it with the private key: 7422315d mod N
bitstepexponent so farvalue
1start17422315
1square, × base311365134
1square, × base77666657
1square, × base154410797
1square, × base311459758
1square, × base6317818434
0square12614115692
0square2525917032
1square, × base50510007604
1square, × base10113674795
0square202213645451
1square, × base40452700286
0square80903444512
1square, × base1618115735277
0square323624097333
1square, × base6472512348118
0square12945015300787
0square25890021326550
1square, × base51780116363309
0square103560210016759
1square, × base207120517314897
0square41424106685390
0square82848209521734
1square, × base165696415072229
24 binary digits, 36 modular multiplications.
The receiver reads “Meet at noon”. Tap a row to see that block’s working.
3. Attack it, knowing only N, e and the numbers sent
Trial division: 0 divisions…
Pollard’s rho: 0 operations…
1.6 × 10³⁰⁸
operations by trial division: 5.0 × 10²⁹¹ years
4.4 × 10¹⁵⁴
operations by Pollard’s rho: 1.4 × 10¹³⁸ years
1.6 × 10³⁵
operations by number field sieve (formula): 5.0 × 10¹⁸ years
Public exponent e
25 bits
size of this key’s modulus N
4
divisions to find d, knowing p and q
4
blocks in the message
1.6 × 10³⁵
operations to factor a real 2,048-bit N, by the best known formula
Anyone may know N and e, and anyone can encrypt. Decrypting needs d, and computing d needs p and q. So the whole secret is the factorisation of a public number. At this size an attacker factors N at once. The same attack on a 2,048-bit key is the chart’s story: every known classical method is hopeless, and a polynomial-time answer to one NP question would finish it in a couple of thousand steps.

Amber: secrets. Sky: what is public. Rose: an attacker’s work. This is textbook RSA with tiny primes and no padding. It is insecure in other ways too: equal blocks encrypt to equal numbers, a short message with e = 3 can be read by taking an ordinary cube root, and with blocks this small an attacker could simply encrypt every possible block and compare. Real RSA uses primes of 300 digits or more and randomised padding. The demo is here to show the mathematics. Characters outside plain ASCII are sent as “?”.

Go deeper:Public Keys, RSA, and Why P = NP Would End Them

Public-key cryptography was proposed by Whitfield Diffie and Martin Hellman in 1976: two people who have never met agree on a secret while an eavesdropper hears every word. The RSA system is due to Ron Rivest, Adi Shamir and Leonard Adleman (1977). Clifford Cocks had found an equivalent scheme at GCHQ in 1973; his work was declassified in 1997.

RSA works because (me)d leaves remainder m on division by N whenever e·d leaves remainder 1 on division by (p − 1)(q − 1). Anyone who can factor N can compute d, so breaking RSA is no harder than factoring. Whether it is as hard, that is, whether every way of reading RSA messages yields a way of factoring, is unknown.

Every cryptographic task asks the adversary to solve a problem in NP. Encryption, signatures, pseudorandom generators, commitments, zero-knowledge proofs: in each, the adversary may guess the key and check the guess. So all of them fall together if P = NP. The oracle in the demo makes this concrete. Its question has short certificates for yes, and binary search turns a decision procedure into the factor itself, the same search-from-decision trick that works for every NP-complete problem. See also: The Web of Reductions.

One-way functions turn out to be exactly the right assumption for much of cryptography. They are necessary and sufficient for private-key encryption, for pseudorandom generators (Johan Håstad, Russell Impagliazzo, Leonid Levin and Michael Luby, 1999) and for digital signatures. Public-key encryption seems to need more: nobody knows how to build it from an arbitrary one-way function, and every known scheme leans on a problem with special algebraic or geometric structure, such as factoring, discrete logarithms or lattices.

Five Worlds

Visit each of the five islands and watch the board light up with what exists there. The two panels underneath measure the distinctions the worlds turn on.

Cryptomania
Public-key cryptography exists.
Strangers can agree on a secret in public. This is the world most people assume we live in, and the one the internet is built for.
yesAre some NP problems hard in the worst case?
yesAre some NP problems hard on average, on instances that can be generated?
yesDo one-way functions exist?
yesDoes public-key cryptography exist?
Fast SAT on every instanceABSENT
Public-key cryptography gives one-way functions, so P ≠ NP.
Fast SAT on typical instancesABSENT
Breaking a randomly generated key is a hard typical instance.
Hard puzzles you can generateEXISTS
A public key is a hard puzzle.
Hard puzzles generated with the answerEXISTS
Key generation produces the puzzle and its answer together.
Private-key encryptionEXISTS
Everything available in Minicrypt is available here.
Digital signaturesEXISTS
Everything available in Minicrypt is available here.
Key exchange between strangersEXISTS
This is the definition of the world.
Secure online commerceEXISTS
Exchange a key with the shop, check its signature, encrypt the rest: the padlock in the browser.
What would show we live here
A proof that some key-exchange protocol or public-key encryption scheme is secure.
What would show we do not
An efficient attack on every public-key scheme, for instance an efficient algorithm for SAT.
No such result exists for any of the five. Most researchers would bet on Cryptomania, because decades of effort have failed to break the leading public-key schemes. That is a belief, not a theorem. One recent line of work gives the middle question a new form: Yanyi Liu and Rafael Pass showed in 2020 that one-way functions exist exactly when a time-bounded version of Kolmogorov complexity is hard to compute on average.
Worst case against average case
Random 3-SAT formulas with 80 variables, solved here by DPLL as you watch. Far from the threshold every one is quick. Near it a few take many times longer. In Cryptomania, some way of generating formulas produces hard ones most of the time.
A puzzle made with its answer
Published: N = 579422688387514714379994052667593765777
Kept in your pocket: p = 18201826253028864709
Made with two random primes and 400 digit multiplications. Solving it with Pollard’s rho would take about 1.4 × 10¹⁰ operations: 14 seconds at a billion a second.
In Cryptomania, some puzzle of this kind really is hard for everyone but its maker: that is what a one-way function is. Whether multiplying primes is one of them is a further belief, also unproved.
Digits in each prime of the puzzle
6 of 8
capabilities that exist in Cryptomania
…
median branching steps at 2 clauses per variable
…
median at 4.26, the threshold
…
branching steps for the slowest formula
Read the islands from left to right: each answers yes to one more question than the last. Algorithms are lost first, and cryptography arrives only at the fourth island. P ≠ NP is, roughly, only the border of the first island. It takes three further steps, each unproved, to reach the world the internet assumes.

The board restates Impagliazzo’s definitions and the known implications between them; it is not a measurement. The two panels below it are real computations, but small ones: 80 variables and 8 formulas per column say nothing about how hardness grows, and nobody has proved that random formulas at the threshold are hard on average. A formula that needs more than 3,000 branching steps is cut off and marked. Emerald dots are satisfiable formulas, rose ones are not. See also the Computational Complexity module for the whole phase transition.

Go deeper:Impagliazzo’s Five Worlds

Russell Impagliazzo described the five worlds in “A Personal View of Average-Case Complexity” (1995). Each is a consistent answer to a chain of open questions, and each is compatible with everything proved so far. The point of the map is that “P versus NP” is the coarsest question on it. Settling it in the expected direction would, roughly speaking, rule out Algorithmica and leave four worlds standing.

The second question, worst case against average case, has its own theory. Leonid Levin (1986) defined what it means for a problem with a distribution on its instances to be complete for average-case NP. The theory is harder to use than NP-completeness, and few natural problems are known to be complete in his sense. The third question separates “hard” from “usefully hard”: Pessiland has hard instances everywhere and no way to plant a secret in one.

Lattice problems are the main candidates for cryptography that would survive large quantum computers, and they are notable for another reason. Miklós Ajtai (1996) showed that for certain lattice problems, solving random instances is as hard as solving the worst instances of a related problem. That is a worst-case-to-average-case reduction, the kind of bridge that is missing for NP as a whole. See also: Would a Quantum Computer Help?

None of the five worlds has been ruled out. The islands in the demo are definitions and the implications between them, and the two measurements are small experiments. Neither is evidence about which world is ours.

Key Takeaways

  • A one-way function is easy to compute and hard to invert on almost every input; nobody has proved that one exists, and if P = NP none does.
  • RSA’s whole secret is the factorisation of a public number, and a polynomial-time answer to one NP question would recover it in a few thousand steps.
  • P ≠ NP would not by itself make cryptography secure: that needs problems that are hard on average, and whose hard instances can be generated together with their answers.
  • Impagliazzo’s five worlds, from Algorithmica to Cryptomania, are all consistent with what is known; most researchers bet on Cryptomania, and it is a bet.

Spot a mistake or have an idea?

Search