Path: blob/main/foundations/05-discrete-log-diffie-hellman/sage/05a-the-discrete-log-problem.ipynb
483 views
Notebook 05a: The Discrete Logarithm Problem
Module 05. The Discrete Logarithm and Diffie-Hellman
Motivating Question. In the real numbers, if someone tells you , you solve instantly. But what if someone tells you ? You can't just take a logarithm, the group structure of scrambles the usual patterns. This "discrete logarithm" problem is believed to be fundamentally hard for large primes, and that hardness is the foundation of Diffie-Hellman, ElGamal, DSA, and elliptic curve cryptography.
Prerequisites. You should be comfortable with:
Modular arithmetic and cyclic groups (Module 01)
The multiplicative group and Euler's totient (Module 04)
Learning objectives. By the end of this notebook you will be able to:
State the discrete logarithm problem precisely.
Distinguish the DLP from ordinary (real-valued) logarithms.
Compute discrete logs by brute force and understand why brute force fails for large groups.
Use SageMath's
discrete_log()to solve DLP instances.Appreciate the asymmetry that makes DLP useful for cryptography: exponentiation is fast, but inversion (the discrete log) is slow.
1. Ordinary vs Discrete Logarithms
Over the real numbers, the exponential function is a bijection from to , so it has a well-defined inverse: the logarithm .
| Setting | Given | Find | Method |
|---|---|---|---|
| , closed-form formula | |||
| No known efficient general method! |
The key difference: in , the exponential function "wraps around" modulo . The smooth, monotone structure that makes real logarithms easy is destroyed by the modular reduction.
Look at the right plot: the values jump around unpredictably. Given a -value, you cannot "read off" the from the plot, you would have to check each point. This visual scrambling is the heart of the DLP's difficulty.
2. The Formal Definition
Definition (Discrete Logarithm Problem). Let be a cyclic group of order , and let be a generator. Given , find an integer with such that
We write and call the discrete logarithm of with respect to base .
The most common concrete setting is (the multiplicative group modulo a prime ), which has order .
Misconception alert. "The discrete log might not exist." If is a generator of the cyclic group , then every element is a power of , so the discrete log always exists and is unique modulo . The problem is not existence, it is finding it efficiently.
Checkpoint 1. Using the table above, find (i.e., find such that ). Scan the right column for 8 and read off . Could you do this if had 300 digits?
3. Brute-Force Search
The simplest approach: try until . This requires at most multiplications.
For , this is instant. For , the number of operations exceeds the number of atoms in the observable universe ().
4. The One-Way Function: Fast Forward, Slow Backward
The cryptographic magic is the asymmetry between exponentiation and discrete log:
| Operation | Complexity | 2048-bit prime |
|---|---|---|
| Compute (square-and-multiply) | multiplications | ~2048 multiplications, milliseconds |
| Find from (best known) | Sub-exponential but still infeasible |
This makes exponentiation a one-way function (under the DLP assumption): easy to compute, hard to invert.
Let us see this asymmetry in action.
Checkpoint 2. If brute force on a 7-digit prime takes about 1 second, roughly how long would brute force take on a 20-digit prime? (Hint: the group order grows with , and brute force is .)
5. Visualising the Scramble
Let us build a more detailed picture of why discrete logs are hard. We will map out which exponent maps to which group element and look for patterns.
Observations:
The scatter plot looks essentially random, no visible pattern linking to .
The histogram is perfectly uniform: every value appears exactly once (since is a generator).
The consecutive differences are also erratic, knowing gives no useful information about without computing it.
This "pseudorandom" behaviour is what makes the DLP hard: there is no shortcut that exploits structure in the output.
6. SageMath's discrete_log()
SageMath has a built-in discrete_log() function that uses a combination of algorithms (baby-step giant-step, Pohlig-Hellman, index calculus) to solve DLPs much faster than brute force. We will study these algorithms individually in notebooks 05e and 05f.
Bridge from Module 04. In Module 04 we used
power_mod(g, x, p)to compute efficiently. Nowdiscrete_log()does the inverse: given the result , it recovers . Notice the asymmetry,power_modis always fast, whilediscrete_logbecomes slow as grows.
Notice how the time grows rapidly with the number of bits. At cryptographic sizes (2048 bits), even SageMath's sophisticated algorithms cannot solve the DLP in reasonable time.
Crypto foreshadowing. This one-way property is the bedrock of:
Diffie-Hellman key exchange (notebook 05c): Alice publishes , Bob publishes ; neither can recover the other's secret exponent, yet both can compute .
ElGamal encryption: the public key is ; encrypting is easy, but decrypting without requires solving the DLP.
DSA / Schnorr signatures (Module 09): signing uses the secret exponent, verification uses only the public power.
Elliptic curve cryptography (Module 06): the same DLP idea, but in elliptic curve groups where the problem is even harder per bit.
7. Exercises
Exercise 1 (Worked): Manual DLP in a Small Group
Problem. In with generator , find (i.e., find such that ).
Solution. Compute successive powers of 2 modulo 11:
| 0 | 1 |
| 1 | 2 |
| 2 | 4 |
| 3 | 8 |
| 4 | 5 |
| 5 | 10 |
| 6 | 9 |
| 7 | 7 |
So in .
Exercise 2 (Guided): Timing the One-Way Function
Problem. For a 50-bit prime :
Compute for a random , and time it.
Recover using
discrete_log(), and time it.Compute the ratio: how many times slower is the "backward" direction?
Hint: Use next_prime(2^50) for and walltime() for timing.
Exercise 3 (Independent): DLP Existence and Uniqueness
Problem.
In , let . Build the complete discrete log table (i.e., for each , find ). Verify that every element has a unique discrete log in .
Now try in . Is a generator? Build its power table and explain what you observe.
What happens if you try to compute using brute force with only the powers of 3? Does it exist?
Summary
| Concept | Key Fact |
|---|---|
| Discrete log problem | Given in a cyclic group, find such that |
| Existence | If generates the group, always exists and is unique mod $ |
| Brute force | Try all : $O( |
| One-way asymmetry | Exponentiation is ; discrete log is believed hard (sub-exponential best known for ) |
| Cryptographic use | DLP hardness underlies DH, ElGamal, DSA, and ECC |
The DLP is only hard when the group is chosen carefully. In the next notebook, we will study primitive roots and generators, the elements that make cyclic and give the DLP its full strength.