Search

Search papers and lessons

Discrete Logarithm Problem

Level 3 · 9 min read

Given a generator g of a cyclic group and an element h, find the exponent x with g^x = h; this is easy to compute forward and believed hard to reverse.

On this page

  • Colour = topic mix
  • Discrete math
  • Abstract algebra
  • Security & cryptography
  • Sign in to see your progress

TL;DR

Take a cyclic group with generator gg and an element hh in it. The discrete logarithm problem asks for the exponent xx with gx=hg^x = h. Computing hh from gg and xx takes a few multiplications, even for huge xx. Going the other way, from hh back to xx, has no known fast general method, and that gap is what modern key exchange relies on.

Where you'll see it: in the Diffie–Hellman key exchange, in elliptic-curve variants of it, and in digital signature schemes such as DSA.

Intuition

Exponents, but in a clock

On the real numbers, the logarithm answers the question "what power of 2 gives 8?" The answer is 3, and logarithms are easy to compute there. Now replace the real numbers with the clock arithmetic of integers mod a prime. Powers wrap around, so the sequence g,g2,g3,…g, g^2, g^3, \dots jumps unpredictably between residues instead of growing smoothly.

Why does this matter? In the real numbers, you can read off an exponent by looking at the size of the answer. Mod a prime, the size tells you nothing. The residues look scrambled, and the only generic way to find xx is to test candidates. That scrambling is what makes the problem useful.

The picture below shows the powers of 3 mod 17. The sequence cycles through every nonzero residue, so starting from 3, each target is reached at exactly one exponent.

flowchart LR
  E1["3^1 = 3"] --> E2["3^2 = 9"]
  E2 --> E3["3^3 = 10"]
  E3 --> E4["3^4 = 13"]
  E4 --> E5["3^5 = 5"]
  E5 --> E6["..."]
  E6 --> E16["3^16 = 1"]

Going forward is a single chain of multiplications. Going backward means searching this chain for a target you have been given.

The math

Definition: Discrete logarithm problem

Let GG be a cyclic group of order nn with generator gg. Given h∈Gh \in G, find the integer xx with 0≤x<n0 \le x \lt n and gx=hg^x = h. The integer xx is the discrete logarithm of hh to the base gg, written log⁡gh\log_g h.

In the most common setting, G=Zp×G = \mathbb{Z}_p^{\times} for a prime pp, the nonzero residues mod pp under multiplication. This group is cyclic and has order n=p−1n = p - 1. The group operation is written multiplicatively, so gxg^x means gg multiplied by itself xx times, reduced mod pp.

The forward direction is fast

Computing gx mod pg^x \bmod p uses repeated squaring. Write xx in binary, square gg repeatedly, and multiply together the squares that match the 1-bits of xx. For an exponent with bb bits this takes about 2b2b modular multiplications, so the cost grows with the number of digits of xx, not with its size.

The same fact is stated in algorithm terms: with bb the bit length of xx and pp of similar size, the forward cost is polynomial in bb.

The backward direction is the problem

The simplest method is to compute g1,g2,g3,…g^1, g^2, g^3, \dots and stop when hh appears. This needs up to nn steps. A cleverer method is baby-step giant-step. Let m=⌈n⌉m = \lceil \sqrt{n} \rceil, store gjg^j for j=0,…,m−1j = 0, \dots, m-1 in a table, then test h⋅g−mih \cdot g^{-mi} for i=0,1,…i = 0, 1, \dots until it appears in the table. This needs about n\sqrt{n} steps and memory, so it is much better than brute force but still exponential in the number of digits of nn.

For Zp×\mathbb{Z}_p^{\times} with pp prime, the best known attacks are sub-exponential, through index-calculus methods, and no polynomial-time classical algorithm is known. Those are the facts the cryptographic argument relies on. The problem is not proven hard; only the absence of a known fast method is established.

Key result: the baby-step giant-step count

For a group of order nn, the search needs about n\sqrt{n} table entries and about n\sqrt{n} tests. Doubling the number of digits of nn roughly squares the work. This is why key sizes grow with the group order, and why the cost is measured with big-O notation as O(n)O(\sqrt{n}).

Key result: the role of the factorisation of nn

The group order n=p−1n = p - 1 matters. The Pohlig–Hellman method splits the problem into one subproblem per prime factor of nn, each in a subgroup of prime order. The total cost is dominated by the largest prime factor qq of nn (about q\sqrt{q} steps with baby-step giant-step). So a safe choice of prime pp has p−1=2qp - 1 = 2q for a large prime qq, and the problem lives in a subgroup of order qq where the easy shortcuts vanish.

Worked examples

Example
Solving $3^x \equiv 13 \pmod{17}$ by hand

Here g=3g = 3, h=13h = 13, p=17p = 17. The exponent is small, so a direct search works.

  1. 31=33^1 = 3.
  2. 32=93^2 = 9.
  3. 33=27≡10(mod17)3^3 = 27 \equiv 10 \pmod{17}.
  4. 34=3⋅10=30≡13(mod17)3^4 = 3 \cdot 10 = 30 \equiv 13 \pmod{17}.

The fourth power matches hh, so x=4x = 4.

Interpretation. Four multiplications were enough because xx is small. The same search on a group with n≈1077n \approx 10^{77} elements would be infeasible by brute force, which is the point of the problem.

Example
Baby-step giant-step on the same problem

Use g=3g = 3, h=13h = 13, p=17p = 17, so n=16n = 16 and m=⌈16⌉=4m = \lceil \sqrt{16} \rceil = 4.

  1. Baby steps: compute gjg^j for j=0,1,2,3j = 0, 1, 2, 3. The values are 1,3,9,101, 3, 9, 10. Store them in a table with their exponents.
  2. Compute g−4g^{-4}, the inverse of g4=13g^4 = 13 mod 17. Since 13⋅4=52=3⋅17+113 \cdot 4 = 52 = 3 \cdot 17 + 1, the inverse is 4.
  3. Giant steps: set y0=h=13y_0 = h = 13. It is not in the table.
  4. Set y1=y0⋅4=52≡1y_1 = y_0 \cdot 4 = 52 \equiv 1. The value 1 is in the table at j=0j = 0.
  5. Write x=im+jx = i m + j with i=1i = 1 and j=0j = 0. Then x=4+0=4x = 4 + 0 = 4.

Interpretation. Six table operations (four stores and two lookups) found the answer. The method scales as n\sqrt{n}, so for a group with nn near 103010^{30} it would still need about 101510^{15} steps, which is why the parameters are chosen with a large nn.

Example
The public value in a key exchange

This example is illustrative only and uses numbers far too small to be secure. Take p=23p = 23 and g=5g = 5. Alice picks a secret exponent a=6a = 6 and publishes A=56 mod 23A = 5^6 \bmod 23.

  1. 52=25≡25^2 = 25 \equiv 2.
  2. 53=5⋅2=105^3 = 5 \cdot 2 = 10.
  3. 56=102=100≡8(mod23)5^6 = 10^2 = 100 \equiv 8 \pmod{23}, so A=8A = 8.

An eavesdropper sees A=8A = 8 and wants aa. With p=23p = 23 the eavesdropper can simply test 51,52,…5^1, 5^2, \dots until one equals 8, and reach 56=85^6 = 8 after six tries. The example shows the mechanism, not its security.

Interpretation. The public value AA hides aa only as long as the search is expensive. Real systems use groups where the search is far beyond reach, and the attacker's problem is exactly the discrete logarithm.

Tradeoffs and failure modes

The problem is a good building block only when its parameters are chosen carefully. The main ways to get it wrong are listed below.

SituationProblemBetter choice
Small prime ppBrute force or baby-step giant-step finishes quickly.Choose pp large, so p\sqrt{p} is out of reach.
p−1p - 1 has only small prime factorsPohlig–Hellman reduces the problem to small subgroups and solves each one.Choose a safe prime, where (p−1)/2(p-1)/2 is also prime.
Reusing exponents or noncesRepeated values can leak the secret.Draw fresh random exponents for each session.
Trusting a generator without checkingA small-order element makes the group much smaller than assumed.Validate that the element has the expected large order.
Assuming the problem is proven hardOnly the lack of a known fast algorithm is established.Treat parameter choices as conservative and follow current guidance.

A common mistake is to think that "the discrete log is hard" means it is impossible. It means that no fast general method is known, which can change if a new algorithm appears or if a quantum computer runs Shor's algorithm. Shor's algorithm solves the problem in polynomial time on a quantum computer, so the hardness assumption is only about classical machines.

History

Discrete logarithms were studied as a number-theoretic question long before they were used in cryptography. Carl Friedrich Gauss worked with primitive roots modulo primes in the Disquisitiones Arithmeticae (1801), which is the algebra underlying the problem.

The cryptographic use began in 1976, when Whitfield Diffie and Martin Hellman published New Directions in Cryptography. They proposed a key exchange in which two parties agree on a secret using only public values, and the security of the exchange rests on the difficulty of computing discrete logarithms. The idea spread quickly because it solved a problem that had been hard before: sharing a secret over an insecure channel without meeting in advance.

Algorithms for the problem improved over the following decades. Daniel Shanks's baby-step giant-step (early 1970s) and the Pohlig–Hellman method (1978) came first, and index-calculus methods followed for groups like Zp×\mathbb{Z}_p^{\times}. Those advances are why key sizes grew over time.

Check yourself

QuestionIn Z17×\mathbb{Z}_{17}^{\times} with generator 33, what is the discrete logarithm of 1616?Show answer

Compute powers of 3 mod 17 until you reach 16. The sequence is 3,9,10,13,5,15,11,163, 9, 10, 13, 5, 15, 11, 16 for exponents 11 to 88. Check the last step: 37=3⋅15=45≡113^7 = 3 \cdot 15 = 45 \equiv 11, and 38=3⋅11=33≡163^8 = 3 \cdot 11 = 33 \equiv 16. So log⁡316=8\log_3 16 = 8.

QuestionWhy does baby-step giant-step need about n\sqrt{n} steps when the group has nn elements?Show answer

The method stores about n\sqrt{n} baby steps and then tests about n\sqrt{n} giant steps. Each giant step covers a block of n\sqrt{n} exponents. Together the two parts cover all nn possible exponents, and the work is roughly n+n=O(n)\sqrt{n} + \sqrt{n} = O(\sqrt{n}).

QuestionWhy is a safe prime pp, where (p−1)/2(p-1)/2 is also prime, a better choice than an arbitrary prime?Show answer

Pohlig–Hellman solves the problem separately for each prime-power part of the group order, so its cost is set by the largest prime factor of p−1p - 1. For a safe prime, p−1=2qp - 1 = 2q with qq a large prime, so the subproblem of size qq is still hard and the shortcut gains almost nothing. If p−1p - 1 is built only from small primes, the problem breaks into small pieces that are easy to solve.

QuestionA system uses the discrete logarithm problem with a group of about 22562^{256} elements. Roughly how many steps does baby-step giant-step need, and is that feasible?Show answer

Baby-step giant-step needs about 2256=2128\sqrt{2^{256}} = 2^{128} steps. A count of 21282^{128} operations is far beyond any current computer, so the method is infeasible at this size. This illustrates why the group order is chosen to be very large.

Sources