TExcelTAD
Home › Number Theory & Cryptography › The Discrete Logarithm Problem

The Discrete Logarithm Problem

Computing gx mod pg^x \bmod p takes a few thousand multiplications even for 2048-bit numbers. Going back, finding xx from gxg^x, 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 GG be a cyclic group with generator gg of order nn (for example a primitive root modulo a prime pp, with n=p−1n = p - 1). For h∈Gh \in G, the discrete logarithm log⁡gh\log_g h is the unique x∈{0,…,n−1}x \in \{0, \ldots, n - 1\} with

gx≡h(modp)g^x \equiv h \pmod{p}

Like ordinary logarithms, log⁡g(ab)≡log⁡ga+log⁡gb(modn)\log_g(ab) \equiv \log_g a + \log_g b \pmod{n}. Unlike them, there is no smooth curve to follow: the powers of gg jump around without visible pattern.

python · try it yourself
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 p−1p - 1 steps. For a 2048-bit pp that is 220482^{2048} steps.

Easy direction, hard direction

python · try it yourself
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 nn baby-step giant-step, Pollard rho about n\sqrt{n}
order nn with small prime factors Pohlig-Hellman about q\sqrt{q} for the largest prime q∣nq \mid n
Zp×\mathbb{Z}_p^\times index calculus, number field sieve sub-exponential, like factoring
elliptic curves (well chosen) Pollard rho only about n\sqrt{n}

The last two rows explain key sizes: a 256-bit elliptic curve gives about 128-bit security, while Zp×\mathbb{Z}_p^\times 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 gag^a and gbg^b, computing gabg^{ab} is hard.
  • Decisional Diffie-Hellman (DDH): gabg^{ab} cannot even be distinguished from a random group element.

Solving discrete logs breaks CDH, and CDH breaks DDH. DDH fails in the full group Zp×\mathbb{Z}_p^\times because the Legendre symbol leaks whether an exponent is even, which is one reason protocols work in a prime-order subgroup.

python · try it yourself
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 AA and BB alone an eavesdropper learns the parity of aa and bb, and therefore of abab: a bit of information about the shared secret.

In software

  • Finite-field Diffie-Hellman (ffdhe2048 and up in TLS 1.3), DSA (no longer approved for signing in FIPS 186-5) and ElGamal rely on discrete logs in Zp×\mathbb{Z}_p^\times.
  • 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 log⁡313\log_3 13 modulo 17 (3 is a primitive root).

Answer
Solution

31=33^1 = 3, 32=93^2 = 9, 33=27≡103^3 = 27 \equiv 10, 34=30≡133^4 = 30 \equiv 13. So log⁡313=4\log_3 13 = 4.

Exercise 2: Log rules

Modulo 17 with base 3, log⁡313=4\log_3 13 = 4 and log⁡32=14\log_3 2 = 14. Find log⁡39\log_3 9 and log⁡326\log_3 26 without a search.

Answer

log⁡39=2\log_3 9 = 2; log⁡326=log⁡39=2\log_3 26 = \log_3 9 = 2 as well, since 26≡926 \equiv 9.

Solution

9=329 = 3^2 gives 2 directly. 26=2⋅1326 = 2 \cdot 13, so log⁡326≡14+4=18≡2(mod16)\log_3 26 \equiv 14 + 4 = 18 \equiv 2 \pmod{16}, consistent with 26≡9(mod17)26 \equiv 9 \pmod{17}.

Exercise 3: Time the search (code)

Count how many multiplications a brute-force search needs for log⁡2h\log_2 h modulo 1019 for every hh, and report the average.

Answer

About (p−1)/2=509(p - 1)/2 = 509 on average.

Solution
python · try it yourself
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 0,…,10170, \ldots, 1017 each occur once, so the average is 1017/2=508.51017/2 = 508.5. The work grows linearly with pp, that is exponentially in its number of bits.

Test yourself

The discrete logarithm of hh to base gg modulo pp is…

  1. ln⁡h/ln⁡g\ln h / \ln g
  2. the xx with gx≡h(modp)g^x \equiv h \pmod{p}
  3. hg mod ph^g \bmod p
  4. h/gh / g

Why do elliptic-curve groups allow much shorter keys than Zp×\mathbb{Z}_p^\times?

  1. they are faster
  2. index-calculus attacks do not apply to well-chosen curves
  3. they use smaller primes for all purposes
  4. they have no discrete logs

What does an eavesdropper learn in Diffie-Hellman over the full group Zp×\mathbb{Z}_p^\times via the Legendre symbol?

  1. the whole secret
  2. the parity of the exponents and of the shared exponent
  3. the prime pp
  4. nothing