TExcelTAD
Home › Number Theory & Cryptography › Pohlig-Hellman and Safe Primes

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 gg have order n=q1e1⋯qrern = q_1^{e_1} \cdots q_r^{e_r} and h=gxh = g^x. For each prime power qe∣nq^e \mid n:

  1. Raise to the cofactor: gq=gn/qeg_q = g^{n/q^e} has order qeq^e, and hq=hn/qe=gqxh_q = h^{n/q^e} = g_q^{x}. So x mod qex \bmod q^e is a discrete log in a group of size qeq^e.
  2. Solve that small log (by brute force or baby-step giant-step).
  3. Combine all x mod qex \bmod q^e with the CRT.

The cost is about ∑eiqi\sum e_i \sqrt{q_i} instead of n\sqrt{n}: tiny when every qiq_i is small. (For prime powers one solves digit by digit in base qq; the brute-force version below is fine for small powers.)

python · try it yourself
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 pp has 9 digits.

The defence: a large prime factor

If nn has a prime factor qq of 256 bits, the piece modulo qq alone costs q=2128\sqrt{q} = 2^{128} work. Two standard ways to get this:

  • Safe primes p=2q+1p = 2q + 1: then p−1=2qp - 1 = 2q and the only small subgroup has order 2 (it leaks one bit, the parity of xx).
  • Prime-order subgroups: choose pp with q∣p−1q \mid p - 1 for a large prime qq, and use a generator gg of order exactly qq. DSA and many Diffie-Hellman groups do this.
python · try it yourself
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 yay^a for a value yy sent by an attacker can leak its secret aa piece by piece: the attacker sends yy of small order qiq_i and observes (for example through a MAC on the response) which of the qiq_i possible keys was used, learning a mod qia \bmod q_i. Pohlig-Hellman with the CRT then reconstructs aa. The countermeasure is to check that received values lie in the right subgroup: 1<y<p−11 < y < p - 1 and yq≡1(modp)y^q \equiv 1 \pmod{p}.

python · try it yourself
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 ffdhe groups (RFC 7919) are safe primes; implementations must still reject y∈{0,1,p−1}y \in \{0, 1, p - 1\}.
  • 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 n=210⋅35⋅1000003n = 2^{10} \cdot 3^5 \cdot 1000003. Roughly how expensive is a discrete log with Pohlig-Hellman, and which factor dominates?

Answer

About 1000003≈1000\sqrt{1000003} \approx 1000 operations, dominated by the prime 1000003.

Solution

The 2102^{10} part costs about 10210\sqrt{2}, the 353^5 part about 535\sqrt{3}, 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 (1018=2⋅5091018 = 2 \cdot 509, 509 prime); 1021 is not (1020=4⋅3⋅5⋅171020 = 4 \cdot 3 \cdot 5 \cdot 17).

Solution

A safe prime needs (p−1)/2(p - 1)/2 prime. (1019−1)/2=509(1019 - 1)/2 = 509 is prime. (1021−1)/2=510(1021 - 1)/2 = 510 is even.

Exercise 3: Subgroup check (code)

With p=2027p = 2027 and q=1013q = 1013, which of the values y=1,2026,4,5,1000y = 1, 2026, 4, 5, 1000 pass the check 1<y<p−11 < y < p - 1 and yq≡1y^q \equiv 1?

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-qq subgroup consists of the squares, so the check accepts quadratic residues.

python · try it yourself
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…

  1. is prime
  2. has only small prime factors
  3. is a power of 10
  4. is odd

For a safe prime p=2q+1p = 2q + 1, the subgroups of Zp×\mathbb{Z}_p^\times have orders…

  1. 1, 2, qq, 2q2q
  2. every number up to pp
  3. only p−1p - 1
  4. powers of 2

The defence against small-subgroup attacks is to…

  1. use a smaller prime
  2. check that received values lie in the intended prime-order subgroup
  3. reuse exponents
  4. send gg twice