Path: blob/main/foundations/05-discrete-log-diffie-hellman/sage/05f-pohlig-hellman.ipynb
483 views
Notebook 05f: The Pohlig-Hellman Algorithm
Module 05. The Discrete Logarithm and Diffie-Hellman
Motivating Question. BSGS solves the DLP in time for any group of order . But what if factors into small primes, say ? Could we somehow solve the DLP in each small prime-power subgroup separately and then combine the results? The Pohlig-Hellman algorithm does exactly this, reducing the DLP in a group of order to DLPs in groups of order equal to each prime factor of . The cost is where , dramatically faster when all are small.
Prerequisites. You should be comfortable with:
The Chinese Remainder Theorem (Module 04d)
Baby-step giant-step (notebook 05e)
Subgroups and element order (notebook 05b)
Learning objectives. By the end of this notebook you will be able to:
Explain why smooth group orders make the DLP easy.
Project a DLP instance into prime-order subgroups.
Solve sub-DLPs and combine with CRT.
Implement Pohlig-Hellman from scratch.
Explain why safe primes resist this attack.
1. Smooth Numbers and Vulnerable Groups
Definition. An integer is -smooth if all its prime factors are .
If the group order is smooth, the DLP can be broken efficiently. Let us compare two primes of similar size.
Checkpoint 1. If , what is the largest prime factor? What would the BSGS cost be for the full group vs the largest subgroup?
2. The Key Idea: Projection into Subgroups
Suppose . We want to find such that .
Step 1: Project. For each prime power , define:
Then has order , and where .
Step 2: Solve. Solve each small DLP: in a group of order .
Step 3: Combine. Use the Chinese Remainder Theorem to recover from .
Bridge from Module 04. This is a direct application of the CRT from notebook 04d! There we used CRT to solve systems of congruences. Here we use it to combine DLP solutions from different subgroups.
3. Solving Sub-DLPs
Each projected DLP is in a group of order . For prime (when ), we just run BSGS in a group of order . For prime powers (), we can further decompose using a "lifting" technique, but for simplicity we'll use BSGS on the full prime-power subgroup.
Misconception alert. "Pohlig-Hellman always makes the DLP easy." No! It only helps when the group order has small prime factors. If is prime (or has a large prime factor), Pohlig-Hellman reduces to a single BSGS in a group of that large order, no speedup.
4. CRT Combination
We now have for each prime power. The CRT gives us the unique .
5. Complete Implementation
Checkpoint 2. What happens if you run
pohlig_hellmanon a safe prime like (where )? What are the subgroup orders? Is there a speedup over plain BSGS?
6. Performance Comparison
Let us compare Pohlig-Hellman, BSGS, and brute force on smooth vs safe primes.
7. Why Safe Primes Resist Pohlig-Hellman
For a safe prime with prime:
has only two prime factors: and .
The DLP in the order- subgroup is trivial (only check 2 elements).
The DLP in the order- subgroup requires work.
Since , the overall cost is , essentially no better than generic BSGS.
This is why safe primes are used for Diffie-Hellman parameters in practice (e.g., RFC 3526).
Crypto foreshadowing. The same principle applies to elliptic curves (Module 06): we choose curves where the group order is prime (or nearly prime), so Pohlig-Hellman provides no advantage. A curve with a smooth group order would be cryptographically useless.
Checkpoint 3. Among primes , which one has the smoothest (smallest largest prime factor)? Which is a safe prime?
8. Exercises
Exercise 1 (Worked): Pohlig-Hellman by Hand
Problem. Solve using Pohlig-Hellman.
Solution. .
Subgroup of order (): project , . Solve .
Subgroup of order : project , . Solve .
CRT: , .
Exercise 2 (Guided): Attack on a Smooth-Order Group
Problem. A careless implementer uses for Diffie-Hellman (this is prime!).
Factor and identify why this is vulnerable.
Generate a DH exchange (pick random , compute ).
As Eve, use Pohlig-Hellman to recover Alice's secret from .
Compute the shared secret.
Hint: p - 1 = 15728640 = 2^20 * 3 * 5. The largest prime factor is just 5!
Exercise 3 (Independent): Measuring Smoothness Vulnerability
Problem.
Write a function
pohlig_hellman_cost(n)that estimates the total BSGS cost: .For primes in the range , compute this cost and the "full BSGS" cost .
Plot the ratio. Which primes are most vulnerable? Can you identify the safe primes?
Summary
| Concept | Key Fact |
|---|---|
| Smooth order | If has only small prime factors, DLP is easy |
| Projection | Map DLP into subgroups: , |
| Sub-DLP | Solve in group of order via BSGS |
| CRT | Combine to recover |
| Cost | , fast when all are small |
| Defence | Use safe primes so has a large prime factor |
This completes Module 05! We have built a full picture of the discrete logarithm: from definition (05a), through generators (05b), to the Diffie-Hellman protocol (05c), its theoretical security (05d), and the two main attacks (05e, 05f).
Next module: Module 06. Elliptic Curves