Send your own name
(59,2,13,23,"YOUR NAME")Swap in your name (letters and spaces only). Watch the last row (m, the message) fill in one letter at a time.
Diffie-Hellman (named after Whitfield Diffie and Martin Hellman, who published it in 1976) lets two people who have never met agree on a secret number, even though everything they say to each other can be overheard. This guide shows how it works with numbers small enough to check by hand, how an eavesdropper breaks it when the numbers are small, and why quantum computers are a big deal for it. Every example matches what you will see on the Diffie-Hellman page in Redux.
How Redux's Diffie-Hellman problem models a key exchange on toy parameters: the instance formats, the discrete logarithm behind it, two classical attacks (brute force and baby-step giant-step), why the certificate carries a private key, how to read the step table, and where Shor's algorithm enters. Every number on this page comes from the default instance (59,2,13,23,"Hello World").
Top: the message. Middle: each letter as a number (A = 1, B = 2, ..., space = 27). Bottom: what actually travels, and all an eavesdropper sees.
Each symbol is encoded as m (A = 1 ... Z = 26, space = 27) and encrypted as c = m · s mod p with the shared secret s = 40.
Alice and Bob want to send each other secret messages, but they have never met, and Eve hears everything they say. To lock messages they need a secret number that only the two of them know. How do you agree on a secret when someone is always listening?
Think of paint. Mixing two paints is easy, but nobody can pull a mixed paint apart again. Diffie-Hellman does the same thing with numbers:
Diffie-Hellman is a key exchange, not an encryption scheme: two parties agree on a shared secret over a public channel. Redux models it from the eavesdropper's side. Eve is passive: she knows the public parameters (p, g) and both public values A = ga mod p and B = gb mod p, and wants the shared secret s = gab mod p, or the message encrypted with it.
In message mode, (p,g,a,b,"TEXT"), the instance string contains both private keys and the plaintext so that Redux can run the exchange for you; the problem keeps only p, g, A, B and the ciphertext. Note that positions 3 and 4 are private keys here, and public values in Eve's-view instances (p,g,A,B).
In Diffie-Hellman, "mixing" means this: multiply together as many 2s as your secret number, divide by 59, and keep only the remainder, the amount left over. Alice's secret is 13, so she works out 2 × 2 × ... × 2 with 13 twos, which is 8,192. That is 138 × 59 with 50 left over, so Alice sends 50.
59 is a prime number: it divides evenly only by 1 and itself. That matters for the math, but you don't need the details here.
Change the secret numbers below and watch both of them land on the same shared secret.
Public: a prime p and a generator g of the multiplicative group mod p (here 2 has order 58 = p − 1, so its powers run through every value 1 ... 58). Alice picks a, sends A = ga mod p; Bob picks b, sends B = gb mod p. Both compute s = gab: Alice as Ba, Bob as Ab.
Recovering a from (p, g, A) is the discrete logarithm problem (DLP). Computing gab from ga and gb is the computational Diffie-Hellman problem (CDH); solving the DLP solves CDH, and no better route is known for well-chosen groups.
Alice gets the shared secret the same way she made her mix: she multiplies 13 copies of Bob's 47 together and keeps the remainder after dividing by 59. Bob does the same with 23 copies of Alice's 50. Secret numbers go from 1 to 57: 58 twos leave a remainder of 1 again, and after that the remainders repeat, so bigger secrets add nothing. Tip for checking by hand: take the remainder after every multiplication, and the numbers stay small.
Private keys 1 ≤ a, b ≤ p − 2, as Redux's instance check requires.
Why do they both get the same number? Alice's mix is made of 13 twos, and Bob multiplies 23 copies of it together. Bob's mix is made of 23 twos, and Alice multiplies 13 copies of it together. Either way the result is 13 × 23 = 299 twos multiplied together, so both remainders are the same: 40. And taking the remainder along the way, as Alice and Bob do, never changes the final remainder, so working with 47 instead of the huge number gives the same answer.
Now Alice and Bob share the secret 40, and Eve doesn't. Alice turns each letter into a number (A = 1, B = 2, ..., Z = 26, space = 27), multiplies it by 40, divides by 59 and sends the remainder. H is 8; 8 × 40 = 320, which is 5 × 59 with 25 left over, so H is sent as 25.
To unlock, Bob multiplies by 31 instead and keeps the remainder after dividing by 59. Why 31? Bob finds it by trying 40 × 1, 40 × 2, 40 × 3, ... until the remainder is 1: 40 × 31 = 1,240, which is 21 × 59 with 1 left over. That means multiplying by 31 undoes multiplying by 40. So 25 × 31 = 775, which is 13 × 59 with 8 left over: H again.
Type your own message below to see each letter locked.
Message mode encrypts each symbol as c = m · s mod p and decrypts with m = c · s−1 mod p, where s−1 = 31 (40 · 31 = 1240 = 21 · 59 + 1). Codes must stay below p, so message mode requires p > 27.
Instance checks: p ≥ 3, 1 < g < p; in Eve's view 1 ≤ A, B ≤ p − 1; in message mode 1 ≤ a, b ≤ p − 2 and only letters and spaces (lower case is accepted; the answer comes back in upper case). Whether p is prime and g a generator is not checked up front: the attacks find out, as an attacker would (section 9).
This is a teaching cipher, not real ElGamal. One fixed s for every symbol makes this a substitution cipher on 28 symbols: equal letters give equal ciphertexts (the three L's in HELLO WORLD are all 8). Real ElGamal uses a fresh random exponent for every message, and real protocols (TLS, SSH, Signal) feed s into a key-derivation function and encrypt with a symmetric cipher such as AES. Redux keeps the per-symbol version because every step is visible and checkable by hand.
Eve knows the starting number 2, the prime 59, and Alice's mix, 50. If she can find out how many twos went into 50, she has Alice's secret, and then she can do exactly what Alice did to get the shared secret 40.
The simplest way: list the remainders of 2, 2 × 2, 2 × 2 × 2, and so on, one more two each time, until she reaches 50. Each new remainder is just the previous one doubled (taking off 59 if it goes past). Below, the list is laid out 8 to a row, so the square in row 1, column 5 is the one with 1 × 8 + 5 = 13 twos. The big number in each square is the remainder; the small one in the corner (on wider screens) counts the twos. Eve starts from 1, before any twos, so 13 twos is her 14th try. Press the button to watch her search.
The default solver in Redux's problem definition. It walks x = 0, 1, 2, ... computing gx mod p by one multiplication per step until gx = A, then sets s = Bx mod p and decrypts each symbol with s−1. Up to p − 1 multiplications: linear in p, exponential in its bit length.
With Alice's secret, Eve rebuilds the shared secret and unlocks every letter. Press play to watch her notebook fill in. It is the same table Redux shows, with Redux's own explanations. A few row names are friendlier here: Redux says "public value" for a mix and "private key" for a secret number.
The step table below replays the solver's recorded steps for the default instance, with Redux's own text. Switch to baby-step giant-step to compare (section 6).
Reading Redux's shorthand. Redux writes its notebook in math shorthand. Here is what it means:
These are Redux's names for Eve's two attacks. "Brute force" means trying every possibility in turn, which is what this section does, and "discrete log" is the math name for "how many twos went in". The second one is the shortcut in section 6.
| Value | Number | Can Eve see it?Visible to the attacker? | How Eve knows itHow the attacker knows it |
|---|
Blue: the row Eve just worked out. Green appears when the message is found. Bob's secret stays "?": Eve never needs it.Blue is Redux's ElementHighlight (the value found in this step); green is Solution. b stays "?": one private key is enough.
Here is the Diffie-Hellman page in Redux, and what each part does.
The Diffie-Hellman page (/Diffie-Hellman in Redux_Frontend). The parts below map onto Redux's problem interface: the instance, the chosen solver, the chosen visualization, and the verifier.
1
2
3
4
5
6
7
(p,g,a,b,"TEXT") or (p,g,A,B). Edit to change it; it is checked as you type.
After Run solve, use the player under the table: ▶ plays the steps, the arrows move one step, and the slider jumps. Its counter includes the starting picture, so it always reads one more than the table's own step number.The player's count includes the initial frame (visualize), so it runs to 14 while the frame titles from StepsVisualization run to 13.
The Result panel shows the answer and lets you check any answer you type. Redux calls a proposed answer a certificate. Here it has two parts, Alice's secret number and the message: (13,"HELLO WORLD"). Why the secret number too? Redux can't check a message on its own, because only someone who knows a secret number can make the shared secret. With Alice's secret it can: it checks that 13 twos really give 50, makes the shared secret from it, and locks your message to compare with what Eve intercepted.
The Default Diffie-Hellman Verifier checks ga mod p = A for 0 ≤ a ≤ p − 2, then computes s = Ba mod p, re-encrypts the proposed message and compares it with c. A message or a shared secret alone could not be verified from public values: deciding whether a candidate equals gab is the decisional Diffie-Hellman problem (DDH), assumed hard. The private key is an efficiently checkable witness, which is why the problem is declared in NP: Diffie-Hellman = {<p, g, A, B> | some a with 0 <= a <= p - 2 has g^a = A (mod p)}.




The box turns red and Run solve is grayed out. A message may only use letters and spaces, so "Hi!" fails because of the "!". The prime must also be bigger than 27, so every letter's number fits under it, and the secret numbers must be between 1 and 57 when the prime is 59.
A ProblemParseException (HTTP 400) disables Run. The backend's reason for (59,2,13,23,"Hi!") is that '!' cannot be encrypted; for (23,5,4,10,"HI"), that a message needs p > 27; for (59,2,0,23,"HI"), that a must satisfy 1 ≤ a ≤ p − 2.
Look at the grid again: Alice's secret is just row × 8 + column. Eve doesn't need to walk through every square to find which row and column 50 sits in. In 1971 Daniel Shanks found a shortcut, now called baby-step giant-step:
Shanks (1971). Let m = ⌈√(p − 1)⌉ = 8 and write a = i·m + j with 0 ≤ i, j < m. Store the baby steps gj for j < m in a hash table, then compute the giant steps γi = A · (g−m)i for i = 0, 1, ... until γi = gj; then a = i·m + j. Here g−8 mod 59 = 3.
Cost: about 2√p multiplications and √p stored values instead of p, a square-root speed-up that is still exponential in the bit length. Pollard's rho matches the time with constant memory; Pohlig-Hellman reduces the problem to the largest prime factor of p − 1 (here 59 = 2 · 29 + 1 is a safe prime, so that factor is 29). For generic algorithms, which use only the group operation, about √q operations are necessary in a group of prime order q (Shoup, 1997), which is why elliptic-curve groups, where nothing better than generic attacks is known, need only about 256-bit keys.
For Alice's 50 that is 8 baby steps and 2 giant steps, 10 steps instead of 14. That doesn't sound like much, but as the prime gets bigger, the shortcut's number of steps grows much more slowly: for a prime of about a million, Redux's brute force needs 777,778 tries on the bigger exercise in section 9, and the shortcut needs 1,779.
59 has 2 digits, so Eve finds Alice's secret in a moment. Real primes have hundreds of digits. With the slow method, each extra digit makes the list 10 times longer. For a 40-digit prime, the slow method needs a number of tries with about 40 digits; the shortcut needs a number of steps with about 20 digits. That is a huge saving, but still hopeless for big primes. Slide to see what that means for a computer doing a billion steps every second.
Rough operation counts against the size of p in bits, constants ignored: brute force ~2b; baby-step giant-step and Pollard's rho ~2b/2; the number field sieve for discrete logs, the best known classical method for prime fields, ~exp(1.923 (ln p)1/3 (ln ln p)2/3), the same shape as for factoring; Shor's algorithm ~b3 quantum gates. The vertical axis is logarithmic: each gridline is 1020 times the one below.
Names like 512-bit and 2048-bit count binary digits, the 0s and 1s computers write numbers with: 512 binary digits is about 155 ordinary digits, and 2048 is about 617. In 2015, researchers showed that some websites still accepted 512-bit primes, which could be broken with smarter methods than the ones on this page. Today 2048-bit primes are common, and many systems use a related kind of mixing built on "elliptic curves" instead.
The 795-bit marker is the prime-field discrete log record set in 2019 with the number field sieve (Boudot et al., alongside the RSA-240 factorization), which showed a discrete log costs only somewhat more than factoring a number of the same size. 2048-bit groups are a common choice today (for example the RFC 7919 groups for TLS). Logjam (2015) showed that 512-bit "export" groups could be broken in practice after a one-time precomputation. Shor's curve assumes a large, error-corrected quantum computer, which does not exist yet.
A quantum computer works by the rules of very tiny particles. For most jobs it isn't faster than a normal computer, but for a few special puzzles it is enormously faster. Unmixing Diffie-Hellman's numbers is one of them.
In 1994 Peter Shor found the method, now called Shor's algorithm (an algorithm is a step-by-step method). A big enough quantum computer running it could find Alice's secret from the numbers Eve sees, even for the sizes used today. Today's quantum computers are much too small and make too many errors to break real Diffie-Hellman, but people are already switching to new kinds of locks that quantum computers can't pick easily. The elliptic-curve versions used widely today fall to Shor's algorithm too.
Shor's algorithm (1994) solves discrete logarithms in time polynomial in log p. It evaluates f(x, y) = gx Ay mod p = gx + a y on a superposition of (x, y) and applies the quantum Fourier transform to both inputs; a measurement gives (u, v) with v ≡ a·u (mod r), where r is the order of g, so a = v · u−1 mod r whenever u is invertible mod r. The same algorithm breaks elliptic-curve Diffie-Hellman, and resource estimates (Roetteler, Naehrig, Svore and Lauter, 2017) find that elliptic-curve keys need fewer qubits to break than RSA moduli of comparable classical strength. Exercise 5 in section 9 does the classical post-processing for one measurement.
Quantum computer guides are in progress. They will cover Shor's and Grover's algorithms step by step.
Instances are written (prime, starting number, Alice's secret, Bob's secret, "message"). Copy an instance, press Edit on the Diffie-Hellman page, paste it, then Run solve. Redux allows about 10 runs a minute; if it says "Too many runs in a row", wait a few seconds.
(59,2,13,23,"YOUR NAME")Swap in your name (letters and spaces only). Watch the last row (m, the message) fill in one letter at a time.
(59,2,7,30,"HI")Alice picks 7 and Bob picks 30. What do they send each other, and what is their shared secret? Check with the mixing panel in section 2 first.
Alice sends 10 and Bob sends 57. The shared secret is 49, and HI is sent as 38, 28. Eve finds Alice's 7 after 8 tries.
(23,5,4,10)With four numbers, the instance is Eve's view: the prime 23, the starting number 5, and the two mixes 4 and 10. What is Alice's secret, and the shared secret?
Alice's secret is 4 (found on try 5: 5 × 5 × 5 × 5 = 625, which is 27 × 23 with 4 left over). The shared secret is 10 × 10 × 10 × 10 = 10,000, which leaves 18.
After running the starting instance (59,2,13,23,"Hello World"), open Result and type (12,"HELLO WORLD") in the certificate box, then press Check.
"Not a valid certificate." The message is right, but 12 twos give 25, not 50, so 12 isn't Alice's secret.
(1000003,2,777777,123456,"HELLO WORLD")Run it with each attack. How many steps does each one report?
Brute force: 777,778 tries. Baby-step giant-step: 1,779 multiplications. Redux picks a row length that, multiplied by itself, just passes the prime, so rows and columns come out about equal: 1,001 × 1,001 is just over a million. Alice's mix sits in row 777, column 0. Both find Alice's secret 777777 and the message HELLO WORLD.
(23,5,4,10,"HI")Why won't Redux run this one? (With a message in it, the 4 and 10 are Alice's and Bob's secret numbers, not their mixes.)
23 is smaller than 27. The letter numbers go up to 27, and the prime has to be bigger than every number it locks.
(59,4,2,3)What do both solvers report, and why?
"No solution: A = 2 is not a power of g = 4 mod p = 59." 4 = 22 has order 29 and generates only the 29 quadratic residues; 2 is a non-residue mod 59 (59 ≡ 3 mod 8), so no a exists.
(55,5,2,3,"HI")The instance parses. Run both solvers: why do they fail at different points?
Brute force finds a = 2 (52 = 25 = A), then s = 152 mod 55 = 5 has no inverse mod 55. Baby-step giant-step needs g−1 first and stops because gcd(5, 55) = 5. Both report p is not prime: g and s must be units, and ElGamal decryption needs s invertible.
(1000003,2,777777,123456,"HELLO WORLD")Predict m, i and j for baby-step giant-step, and both solvers' step counts, then run them.
m = ⌈√1000002⌉ = 1001; 777777 = 777 · 1001 + 0, so i = 777, j = 0, and m + i + 1 = 1779 multiplications. Brute force: 777,778 tries. Both are exponential in the bit length; BSGS halves the exponent.
Compute 40−1 mod 59 by the extended Euclidean algorithm, and check it with Fermat's little theorem.
59 = 1·40 + 19, 40 = 2·19 + 2, 19 = 9·2 + 1, so 1 = 19·59 − 28·40 and 40−1 ≡ −28 ≡ 31. Fermat: 4057 mod 59 = 31 as well.
For g = 2, A = 50, p = 59 (r = 58), one run of Shor's algorithm measures (u, v) = (5, 7). Recover a. Which values of u would force another run?
5−1 mod 58 = 35, so a = 7 · 35 mod 58 = 245 mod 58 = 13. u must be invertible mod 58: even u and u = 29 share a factor with 58 and need another run (or extra work).
Try the certificate "HELLO WORLD" alone, then explain why the verifier insists on the key.
It is rejected as a certificate format error: the format is (a,"TEXT"). Checking a message needs s, and getting s from (p, g, A, B) is CDH; even recognizing s is DDH. The private key is a witness that can be checked with one modular exponentiation.
| Problem | DIFFIEHELLMAN, display name Diffie-Hellman (Problems/NPComplete/NPC_DIFFIEHELLMAN/), page /Diffie-Hellman. Declared complexity class NP; problem type AlgebraAndNumberTheory. |
|---|---|
| Instance | (p,g,A,B) (Eve's view) or (p,g,a,b,"TEXT") (message mode); p ≥ 3, 1 < g < p, 1 ≤ A, B ≤ p − 1 or 1 ≤ a, b ≤ p − 2; text of letters and spaces needs p > 27. Default (59,2,13,23,"Hello World"). |
| Encryption | c = m · s mod p per symbol, s = Ba = Ab mod p; decryption m = c · s−1 mod p. |
| Certificate | Alice's private key a with 0 ≤ a ≤ p − 2 and ga mod p = A; in message mode (a,"TEXT") (any case). |
| Solvers | DH Brute Force Discrete Log (default): x = 0, 1, 2, ... until gx = A; O(p). DH Baby-Step Giant-Step: m = ⌈√(p − 1)⌉, a = i·m + j; O(√p) time and memory. Both finish through the same shared-secret and decryption steps. |
| Visualization | Diffie-Hellman Attacker Knowledge Table (DynamicTable): a, then s, then m letter by letter; b stays "?". |
| Verifier | Default Diffie-Hellman Verifier: ga = A, then re-encrypt the message with s = Ba. |
| Sources | Diffie, W., and Hellman, M. E. (1976). New Directions in Cryptography. IEEE Transactions on Information Theory, 22(6), 644–654. ElGamal, T. (1985). A Public Key Cryptosystem and a Signature Scheme Based on Discrete Logarithms. IEEE Transactions on Information Theory, 31(4), 469–472. Shanks, D. (1971). Class number, a theory of factorization, and genera. Proc. Symp. Pure Math., 20, 415–440. |