Learn › Blockchain & Access Control › Module 2 · Public-key cryptography
Diffie–Hellman, the discrete logarithm and baby-step giant-step
Diffie–Hellman key exchange with primitive roots and two worked examples, why discrete logarithms resist binary search, and Shanks' baby-step giant-step algorithm traced in full.
Diffie–Hellman key exchange (1976)
Two strangers agree a shared secret over a public channel. DH does only key exchange; the result keys a symmetric cipher such as AES.
Public: prime q and a primitive root α — its powers α1…αq−1 mod q produce every value 1…q − 1.
Alice: secret XA, sends YA = αXA mod q. Bob: secret XB, sends YB = αXB mod q.
K = YBXA = YAXB = αXAXB mod q.
Worked example 1
q = 353, α = 3. XA = 97 ⇒ YA = 40. XB = 233 ⇒ YB = 248. K = 24897 = 40233 mod 353 = 160. Eve sees only 353, 3, 40 and 248.
Worked example 2 (corrected whiteboard)
n = 11, g = 7; powers of 7: 7, 5, 2, 3, 10, 4, 6, 9, 8, 1. x = 9 ⇒ A = 8; y = 6 ⇒ B = 4. K = 49 = 86 ≡ 3 (mod 11). (The rough notes first wrote A = 2, which mismatches.)
TLS 1.3 and Signal use ephemeral (EC)DHE for every session, which gives forward secrecy: a later key theft does not expose past sessions. Unauthenticated DH is open to man-in-the-middle; sign the exchanged values.
The discrete logarithm problem
To get K, Eve must find x with gx ≡ y (mod p). Over the reals 2x is monotonic, so binary search finds x = 33 for 8,589,934,592 in a handful of guesses. Modulo a prime the values wrap unpredictably (2x mod 11: 2, 4, 8, 5, 10, …), so there is no “too high/too low” signal. With p around 2,048 bits, exhaustive search is infeasible.
Shanks’ baby-step giant-step (1971)
A time–memory trade-off solving the DLP in O(√p) time and space instead of O(p).
m = ⌈√p⌉; write x = mj + i (0 ≤ i, j < m). Then gmj ≡ y·g−i (mod p).
- Giant steps: tabulate gmj mod p for j = 0…m − 1, sorted by value.
- Baby steps: for i = 0, 1, … compute y·(g−1)i mod p and look it up.
- On a match, x = mj + i.
Worked example: 5x ≡ 20 (mod 47)
m = 7; 57 ≡ 11. Giant steps 11j: 1, 11, 27, 15, 24, 29, 37 (j = 0…6).
5−1 ≡ 19. Baby steps 20·19i: 20, 4, 29 → match at i = 2, j = 5.
x = 7·5 + 2 = 37.
Exercise: 5x ≡ 41 (mod 193)
m = 14, 5−1 ≡ 116. Answer: x = 151 (j = 10, i = 11). Try it before checking.
Because generic attacks (BSGS, Pollard rho) cost √(group order), a 256-bit group order is needed for 128-bit security.