The one-sentence version
Two strangers on a public channel each pick a secret exponent , exchange and , and can then compute a shared key only they know. To compute it, an eavesdropper essentially has to solve the discrete logarithm: find from . But if the group's order has only small prime factors, the Pohlig–Hellman algorithm uses the Chinese Remainder Theorem to split the problem into many small ones and solve them one by one. So the work must happen in a subgroup of large prime order.
This article has three layers:
- The intuition: mixing paint
- Real-world use: TLS, Signal, VPNs and the Logjam attack
- The mathematics: cyclic groups, discrete logarithms, Pohlig–Hellman, baby-step giant-step and worked examples
Layer 1: The Intuition
1.1 Mixing paint
Alice and Bob want to agree on a secret color in a room where everyone can listen in:
- They publicly agree on a shared color: yellow;
- Alice secretly picks red, Bob secretly picks blue;
- Each mixes their secret color into yellow: Alice publishes "yellow + red" (orange), Bob publishes "yellow + blue" (green);
- Alice takes the green and mixes in her red: yellow + blue + red; Bob takes the orange and mixes in his blue: yellow + red + blue;
- They end up with the same brown.
An eavesdropper saw yellow, orange and green, but mixed paint can't be separated, so the brown stays out of reach.
| Analogy | Mathematics |
|---|---|
| Shared color | The public group and generator |
| Secret colors | Private keys , |
| Mixing paint | Exponentiation |
| Paint can't be separated | The discrete logarithm is hard |
| The shared brown | The shared key |
1.2 The 1976 revolution
Before this, two people who wanted to communicate securely had to meet in advance to exchange keys (diplomatic couriers, codebooks). In their 1976 paper "New Directions in Cryptography", Diffie and Hellman first showed publicly that strangers can agree on a key over a public channel, out of nothing. This was the start of public-key cryptography, and it earned them the 2015 Turing Award. (Declassified files later showed that Malcolm Williamson at Britain's GCHQ had secretly discovered the same method a few years earlier.)
Layer 2: Real-World Use
- HTTPS (TLS 1.3): every handshake uses ECDHE (DH on an elliptic curve) to generate temporary keys.
- Forward secrecy: each session uses one-time values of , discarded afterwards. Even if the server's long-term private key leaks later, past conversations can't be decrypted. TLS 1.3 requires this.
- The Signal protocol: an X3DH handshake plus the "double ratchet" performs fresh DH on nearly every message, constantly updating keys.
- VPNs (IPsec, WireGuard) and SSH: all use DH or ECDH to negotiate session keys.
The Logjam attack (2015)
Two problems came together:
- Many servers still supported 512-bit "export-grade" DH, left over from 1990s US export controls, and attackers could trick connections into downgrading to it;
- Huge numbers of servers shared the same prime . In the number field sieve for discrete logarithms, the most expensive precomputation depends only on . After a week of precomputation on one 512-bit , breaking each connection takes just a minute or two.
The researchers also estimated that a nation-state could do the same precomputation for a few of the most common 1024-bit primes. After that, 1024-bit DH was retired, and the mainstream moved to 2048 bits or more, or to elliptic curves.
Man-in-the-middle attacks
Plain DH doesn't verify who the other party is. An attacker can run DH separately with Alice and with Bob, relaying and reading everything in between. So real protocols always add signatures or certificates (RSA, ECDSA, Ed25519) to confirm "the one I'm shaking hands with really is them".
Layer 3: The Mathematics
3.1 The protocol
Public parameters: a prime and an element of .
| Alice | Public channel | Bob |
|---|---|---|
| Pick secret | Pick secret | |
| Compute | |
Compute |
| Compute | Compute |
Correctness only needs the exponent law .
3.2 Why the discrete logarithm is hard
is a cyclic group of order (a general theorem about the multiplicative group of a finite field). Taking a generator , the map
is a group isomorphism: addition on the left corresponds to multiplication on the right. The forward direction takes only multiplications with fast exponentiation; the reverse direction is the discrete logarithm, with no known polynomial-time classical algorithm.
| Algorithm | Applies to | Rough cost |
|---|---|---|
| Brute force | Any group | |
| Baby-step giant-step, Pollard rho | Any group ("generic algorithms") | |
| Pohlig–Hellman | Group order has small prime factors | , where is the largest prime factor of |
| Index calculus, number field sieve | (exploiting integer factorization) | Subexponential |
Elliptic curve groups have no useful notion of "factoring", so index-calculus methods don't apply, leaving only the generic algorithms. This is why ECC keys can be so much shorter.
3.3 Pohlig–Hellman: why prime order
Suppose the group order is . To solve :
- For each prime factor , raise both sides to the power : . The base on the left has order only , so this is a small discrete logarithm of order ; solve it for ;
- Use the Chinese Remainder Theorem to combine all the into .
The cost is set by the largest prime factor. So if is a product of many small primes, the discrete logarithm is easy. The countermeasure is to pick a safe prime (with also prime) and work in the subgroup of order .
In the language of abstract algebra, this is a direct application of the isomorphism (when the are pairwise coprime): a large cyclic group splits into a direct product of small cyclic groups, and the hard problem splits with it.
3.4 Worked examples
One DH handshake
, (a generator of ).
- Alice picks :
- Bob picks :
- Alice computes ; Bob computes
The shared key is . (Checked with Python.)
Pohlig–Hellman recovers Alice's private key
The eavesdropper knows and , and wants to solve . The group order is .
Find : raise both sides to the power . and . So , is even, and .
Find : raise both sides to the power . and . We need , trying only values: , so .
Combine with the CRT: and give .
Instead of trying values, we tried only . At scale, the difference is exponential. (Checked with Python.)
3.5 Baby-step giant-step
Even with a large prime order , trading space for time gets you to : let and write .
- Baby steps: store the table ;
- Giant steps: compute in turn, checking the table each time. On a hit, , which gives .
This is why a 256-bit group provides only 128-bit security: .
Summary
| Layer | Key points |
|---|---|
| Intuition | Paint is easy to mix and hard to separate |
| Algebra | is a cyclic group isomorphism; its reverse is the discrete logarithm |
| Security | Small prime factors of the order are split off by Pohlig–Hellman + CRT; generic algorithms also achieve |
| Engineering | Use safe primes or elliptic curves; add signatures against man-in-the-middle attacks; ephemeral keys provide forward secrecy |