The Discrete Logarithm Problem
Computing takes a few thousand multiplications even for 2048-bit numbers. Going back, finding from , is believed to be infeasible. This one-way function is the foundation of Diffie-Hellman, ElGamal, DSA and, on elliptic curves, of most of today's key exchanges and signatures.
Definition
Let be a cyclic group with generator of order (for example a primitive root modulo a prime , with ). For , the discrete logarithm is the unique with
Like ordinary logarithms, . Unlike them, there is no smooth curve to follow: the powers of jump around without visible pattern.
p, g = 1019, 2 # 2 is a primitive root mod 1019
xs = range(1, 21)
print([pow(g, x, p) for x in xs])
h = 777
x = next(x for x in range(p - 1) if pow(g, x, p) == h)
print(f"log_2({h}) mod {p} = {x}; check: {pow(g, x, p)}")[2, 4, 8, 16, 32, 64, 128, 256, 512, 5, 10, 20, 40, 80, 160, 320, 640, 261, 522, 25]
log_2(777) mod 1019 = 1004; check: 777
The brute-force search took up to steps. For a 2048-bit that is steps.
Easy direction, hard direction
p = 2**127 - 1 # a large prime
g = 3
x = 0x1D2C3B4A5968778695A4B3C2D1E0F
h = pow(g, x, p) # instant
print(h)
print("bits of x:", x.bit_length(), " brute force would need up to", f"2^{(p - 1).bit_length()}", "steps")130174081287324249630274075872079940790
bits of x: 113 brute force would need up to 2^127 steps
How hard is it really?
The best algorithms depend on the group:
| Group | Best known attack | Running time |
|---|---|---|
| any group of order | baby-step giant-step, Pollard rho | about |
| order with small prime factors | Pohlig-Hellman | about for the largest prime |
| index calculus, number field sieve | sub-exponential, like factoring | |
| elliptic curves (well chosen) | Pollard rho only | about |
The last two rows explain key sizes: a 256-bit elliptic curve gives about 128-bit security, while needs a 3072-bit prime for the same level, because index calculus exploits the fact that integers factor into small primes, and elliptic-curve points do not.
Related problems
Protocols usually rely on slightly weaker assumptions:
- Computational Diffie-Hellman (CDH): given and , computing is hard.
- Decisional Diffie-Hellman (DDH): cannot even be distinguished from a random group element.
Solving discrete logs breaks CDH, and CDH breaks DDH. DDH fails in the full group because the Legendre symbol leaks whether an exponent is even, which is one reason protocols work in a prime-order subgroup.
p, g = 1019, 2 # g is a primitive root: odd powers are non-residues
a, b = 345, 678
A, B, K = pow(g, a, p), pow(g, b, p), pow(g, a * b, p)
legendre = lambda y: pow(y, (p - 1) // 2, p) # 1 for squares, p-1 for non-squares
print("parity of a, b, ab:", a % 2, b % 2, a * b % 2)
print("Legendre of A, B, K:", legendre(A), legendre(B), legendre(K))parity of a, b, ab: 1 0 0
Legendre of A, B, K: 1018 1 1
From and alone an eavesdropper learns the parity of and , and therefore of : a bit of information about the shared secret.
In software
- Finite-field Diffie-Hellman (
ffdhe2048and up in TLS 1.3), DSA (no longer approved for signing in FIPS 186-5) and ElGamal rely on discrete logs in . - The 2015 Logjam attack precomputed the number field sieve for one widely shared 512-bit prime, after which individual discrete logs took about a minute; reuse of a few 1024-bit primes was estimated to be within reach of nation-state budgets.
Exercises
Exercise 1: Small log by hand
Find modulo 17 (3 is a primitive root).
Answer
Solution
, , , . So .
Exercise 2: Log rules
Modulo 17 with base 3, and . Find and without a search.
Answer
; as well, since .
Solution
gives 2 directly. , so , consistent with .
Exercise 3: Time the search (code)
Count how many multiplications a brute-force search needs for modulo 1019 for every , and report the average.
Answer
About on average.
Solution
p, g = 1019, 2
steps = {}
x, y = 0, 1
while y not in steps:
steps[y] = x
y = y * g % p
x += 1
print(len(steps), sum(steps.values()) / len(steps))1018 508.5
The logs each occur once, so the average is . The work grows linearly with , that is exponentially in its number of bits.
Test yourself
The discrete logarithm of to base modulo is…
- the with
Why do elliptic-curve groups allow much shorter keys than ?
- they are faster
- index-calculus attacks do not apply to well-chosen curves
- they use smaller primes for all purposes
- they have no discrete logs
What does an eavesdropper learn in Diffie-Hellman over the full group via the Legendre symbol?
- the whole secret
- the parity of the exponents and of the shared exponent
- the prime
- nothing