On this page
What is the discrete logarithm problem?
The discrete logarithm problem asks: given a prime p, a base g and a number h, find the whole number x with gˣ ≡ h (mod p). That exponent x is called the discrete logarithm of h to the base g.
Take g = 2 and p = 11. Keep doubling and reduce mod 11 each time:
2¹ = 2, 2² = 4, 2³ = 8, 2⁴ = 16 ≡ 5, 2⁵ ≡ 10
2⁶ ≡ 9, 2⁷ ≡ 7, 2⁸ ≡ 3, 2⁹ ≡ 6, 2¹⁰ ≡ 1
All ten non-zero remainders appear exactly once, so 2 is a primitive root mod 11 (see our post on what a primitive root modulo a prime is). Now solve 2ˣ ≡ 9 (mod 11). Read it off the list: x = 6, because 2⁶ = 64 = 5 × 11 + 9. After 2¹⁰ ≡ 1 the cycle repeats, as Lagrange's theorem on group order predicts: the order of any element divides p − 1.
For p = 11 a list is fine. For a prime hundreds of digits long, nobody can write the list, and that is the point.
How does baby-step giant-step find x in about √p steps?
Baby-step giant-step, published by Daniel Shanks in 1971, trades memory for time. Set m = ⌈√(p − 1)⌉ and write the unknown as x = im + j with 0 ≤ j < m. Then gˣ ≡ h becomes gʲ ≡ h · (g⁻ᵐ)ⁱ. Store the m baby steps gʲ in a table, then take giant steps by multiplying h by g⁻ᵐ until you land on a table entry. That costs about √p multiplications and √p stored values instead of up to p tries.
Worked example: p = 31, g = 3 (its powers hit all 30 non-zero remainders, so 3 is a primitive root mod 31). Solve 3ˣ ≡ 21 (mod 31).
m = ⌈√30⌉ = 6.
Baby steps 3⁰ to 3⁵: 1, 3, 9, 27, 19, 26.
Giant-step multiplier: 3⁶ = 729 = 23 × 31 + 16, so 3⁶ ≡ 16. Since 16 × 2 = 32 ≡ 1, 3⁻⁶ ≡ 2.
Giant steps from h = 21, doubling each time: 21, 42 ≡ 11, 22, 44 ≡ 13, 26. The value 26 is in the table at j = 5, after i = 4 giant steps.
x = 4 × 6 + 5 = 29. Check: 3³⁰ ≡ 1, so 3²⁹ ≡ 3⁻¹ ≡ 21, because 3 × 21 = 63 = 2 × 31 + 1.
That took 6 table entries and 5 giant-step values, 11 numbers in all, where trying x = 1, 2, 3 and so on would have taken 29.
Why is the discrete logarithm problem hard for large p?
Whitfield Diffie and Martin Hellman set out the gap in their 1976 paper New directions in cryptography. Computing gˣ mod q by repeated squaring takes at most 2 log₂ q multiplications, while for carefully chosen q the best algorithm they knew for logarithms mod q needed on the order of q^(1/2) operations. For a prime of about 200 bits, that is at most 400 multiplications one way and about 2¹⁰⁰, roughly 10³⁰, operations the other. They also wrote that they had no proof that logarithms mod q are hard to compute.
There is still no such proof, and no efficient classical method for computing discrete logarithms in general is known. Attacks faster than √p, such as index calculus and the number field sieve, do exist, so real primes are long. NIST's key-management guide SP 800-57 Part 1 rates finite-field Diffie–Hellman with a 2048-bit prime at 112 bits of security and lists 3072 bits for 128. If √p were the best attack, 2048 bits would give 1024.
Quantum computers are the known exception. Peter Shor's 1997 paper gave polynomial-time quantum algorithms for factoring and discrete logarithms, and the same NIST guide says Diffie–Hellman key agreement may need to be replaced with quantum-resistant counterparts.
How do Diffie–Hellman and ElGamal depend on it?
Diffie–Hellman lets two people agree on a secret over a public line. Here is a toy version with p = 11 and g = 2:
Asha picks the secret a = 4 and sends 2⁴ ≡ 5.
Ben picks the secret b = 7 and sends 2⁷ = 128 ≡ 7.
Asha computes 7⁴ ≡ 3 and Ben computes 5⁷ ≡ 3. Both now hold 3, which is 2²⁸ mod 11.
An eavesdropper sees 2, 11, 5 and 7. Recovering a from 5 is exactly a discrete logarithm.
Taher ElGamal's 1985 paper, A public key cryptosystem and a signature scheme based on discrete logarithms, turned the key exchange into encryption whose security, in its words, relies on the difficulty of computing discrete logarithms over finite fields. To send Ben m = 6, Asha picks a fresh k = 3, computes K = 7³ ≡ 2, and sends the pair c₁ = 2³ = 8 and c₂ = K × m = 12 ≡ 1. Ben recovers K = 8⁷ ≡ 2 with his secret b, then divides: 1 × 2⁻¹ ≡ 1 × 6 = 6. The paper also warns that p − 1 needs a large prime factor, and notes that breaking Diffie–Hellman was not yet proved equivalent to computing discrete logarithms.
How can you practise discrete logarithms by hand?
List the powers of 3 mod 13. You get 3, 9, 1 and then a repeat, so 3 is not a primitive root mod 13. Find one that is.
Pick any h mod 31, solve 3ˣ ≡ h by baby-step giant-step, and count your multiplications.
Run the toy Diffie–Hellman with a friend using p = 31 and g = 3. Exchange only your public numbers and check that you reach the same key.
Read about Carmichael numbers and Fermat pseudoprimes to see why checking that p is really prime takes care.
If modular inverses or the order of an element are the shaky part, the Fermi Concept Library covers every concept in mathematics and computing. When a giant step lands in the wrong place, the Fermi Problem Library has a step-by-step solver that explains the reasoning.



