Pohlig-Hellman and Safe Primes
The difficulty of a discrete logarithm depends not on the size of the group but on the size of its largest prime-order subgroup. Pohlig and Hellman showed how to split the problem along the factors of the group order and glue the pieces back with the Chinese remainder theorem. Their method is the reason for safe primes and prime-order subgroups.
The reduction
Let have order and . For each prime power :
- Raise to the cofactor: has order , and . So is a discrete log in a group of size .
- Solve that small log (by brute force or baby-step giant-step).
- Combine all with the CRT.
The cost is about instead of : tiny when every is small. (For prime powers one solves digit by digit in base ; the brute-force version below is fine for small powers.)
from sympy import factorint, isprime, primitive_root
from math import prod
def small_log(g, h, p, order):
e = 1
for k in range(order):
if e == h:
return k
e = e * g % p
raise ValueError("no log")
def pohlig_hellman(g, h, p):
n = p - 1
residues, moduli = [], []
for q, e in factorint(n).items():
qe = q**e
gq, hq = pow(g, n // qe, p), pow(h, n // qe, p)
residues.append(small_log(gq, hq, p, qe))
moduli.append(qe)
N = prod(moduli)
x = sum(r * (N // m) * pow(N // m, -1, m) for r, m in zip(residues, moduli)) % N
return x, list(zip(residues, moduli))
# p - 1 is smooth: every prime factor is small
p = 2 * 3**5 * 5**3 * 7**2 * 11 * 13 + 1
print("p =", p, "prime:", isprime(p), " p - 1 =", factorint(p - 1))
g = primitive_root(p)
secret = 123456789 % (p - 1)
x, parts = pohlig_hellman(g, pow(g, secret, p), p)
print("pieces (x mod q^e):", parts)
print("recovered:", x, " correct:", x == secret)p = 425675251 prime: True p - 1 = {2: 1, 3: 5, 5: 3, 7: 2, 11: 1, 13: 1}
pieces (x mod q^e): [(1, 2), (153, 243), (39, 125), (15, 49), (5, 11), (1, 13)]
recovered: 123456789 correct: True
Every piece lives in a group of at most 243 elements, so the whole log costs a few hundred multiplications although has 9 digits.
The defence: a large prime factor
If has a prime factor of 256 bits, the piece modulo alone costs work. Two standard ways to get this:
- Safe primes : then and the only small subgroup has order 2 (it leaks one bit, the parity of ).
- Prime-order subgroups: choose with for a large prime , and use a generator of order exactly . DSA and many Diffie-Hellman groups do this.
from sympy import isprime, factorint
p = 2027 # safe: 2026 = 2 * 1013
print(factorint(p - 1), isprime((p - 1) // 2))
q = 1013
g = pow(2, 2, p) # squares generate the subgroup of order q
print("order of g:", next(k for k in range(1, p) if pow(g, k, p) == 1)){2: 1, 1013: 1} True
order of g: 1013
Small-subgroup attacks
A server that computes for a value sent by an attacker can leak its secret piece by piece: the attacker sends of small order and observes (for example through a MAC on the response) which of the possible keys was used, learning . Pohlig-Hellman with the CRT then reconstructs . The countermeasure is to check that received values lie in the right subgroup: and .
p = 12 * (2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 29) + 1 # p - 1 is very smooth
from sympy import isprime
print(p, isprime(p))
secret = 1234567 # server's private exponent (below p - 1)
leaks = {}
for q in (3, 5, 7, 11, 13, 17, 19, 23, 29):
y = pow(3, (p - 1) // q, p) # an element of order q (or 1)
if y == 1:
continue
response = pow(y, secret, p) # what the server computes
leaks[q] = next(k for k in range(q) if pow(y, k, p) == response)
print("secret mod q:", leaks)
from math import prod
N = prod(leaks)
rec = sum(r * (N // q) * pow(N // q, -1, q) for q, r in leaks.items()) % N
print("recovered secret mod", N, ":", rec, " true:", secret % N)77636318761 True
secret mod q: {3: 1, 5: 2, 7: 5, 11: 4, 13: 9, 17: 10, 19: 4, 23: 19, 29: 8}
recovered secret mod 3234846615 : 1234567 true: 1234567
In software
- TLS 1.3's
ffdhegroups (RFC 7919) are safe primes; implementations must still reject . - Elliptic-curve libraries check that incoming points are on the curve and not of small order ("invalid curve" and "twist" attacks are the same idea); X25519 avoids the issue by clamping the scalar to a multiple of the cofactor 8.
Exercises
Exercise 1: Cost estimate
A group has order . Roughly how expensive is a discrete log with Pohlig-Hellman, and which factor dominates?
Answer
About operations, dominated by the prime 1000003.
Solution
The part costs about , the part about , both negligible. The prime 1000003 costs about 1000 steps with baby-step giant-step.
Exercise 2: Safe prime check
Is 1019 a safe prime? Is 1021?
Answer
1019 is (, 509 prime); 1021 is not ().
Solution
A safe prime needs prime. is prime. is even.
Exercise 3: Subgroup check (code)
With and , which of the values pass the check and ?
Answer
4 and 1000 pass; 5 fails the subgroup check; 1 and 2026 fail the range check.
Solution
In a safe-prime group the order- subgroup consists of the squares, so the check accepts quadratic residues.
p, q = 2027, 1013
for y in (1, 2026, 4, 5, 1000):
print(y, 1 < y < p - 1 and pow(y, q, p) == 1)1 False
2026 False
4 True
5 False
1000 True
Test yourself
Pohlig-Hellman makes discrete logs easy when the group order…
- is prime
- has only small prime factors
- is a power of 10
- is odd
For a safe prime , the subgroups of have orders…
- 1, 2, ,
- every number up to
- only
- powers of 2
The defence against small-subgroup attacks is to…
- use a smaller prime
- check that received values lie in the intended prime-order subgroup
- reuse exponents
- send twice