Number Theory

Primitive Roots and the Discrete Logarithm

Find generators modulo a prime, read exponents backward as discrete logarithms, and see why that reversal matters in cryptography.

Key takeaways

  • A primitive root modulo p has order p − 1, so its powers produce every nonzero remainder.
  • Every prime has primitive roots, exactly φ(p − 1) of them, where φ is Euler's totient function.
  • To test a candidate, you need to check only one power for each prime factor of p − 1.
  • Computing powers is fast, while recovering the exponent is believed, though not proved, to be hard for well-chosen large groups.

A primitive root modulo a prime p is a number whose powers run through every nonzero remainder before repeating. Modulo 11, the powers of 2 are 2, 4, 8, 5, 10, 9, 7, 3, 6, 1: all ten nonzero remainders, each exactly once. So 2 is a primitive root modulo 11.

Reading that list backward gives the discrete logarithm. The question "which power of 2 gives 9?" has the answer 6. For tiny numbers you can look it up. For primes hundreds of digits long, no efficient method on ordinary computers is known, and that gap supports a family of cryptographic systems.

The order of a number

Work modulo n and take a number a that shares no factor with n. The order of a is the smallest positive k with a^k ≡ 1 (mod n). After that point the powers repeat.

Modulo 7, the powers of 2 are 2, 4, 1, so 2 has order 3. The powers of 3 are 3, 2, 6, 4, 5, 1, so 3 has order 6.

Euler's theorem says a^φ(n) ≡ 1 (mod n), where φ(n) counts the integers from 1 to n that are coprime to n. Every order therefore divides φ(n). For a prime p, φ(p) = p − 1, and the same fact follows from Fermat's little theorem. A primitive root is a number that achieves the largest possible order, φ(n).

Powers of 3 modulo 7 visit every nonzero remainder
3⁶ ≡ 1 Back to 1, then repeat 3⁵ ≡ 5 4 × 3 = 12 leaves 5 3⁴ ≡ 4 6 × 3 = 18 leaves 4 3¹ ≡ 3 Start with the generator 3² ≡ 2 9 leaves remainder 2 3³ ≡ 6 2 × 3 = 6 1 2 3 4 5 6 3¹ ≡ 3 Start with the generator 1 3² ≡ 2 9 leaves remainder 2 2 3³ ≡ 6 2 × 3 = 6 3 3⁴ ≡ 4 6 × 3 = 18 leaves 4 4 3⁵ ≡ 5 4 × 3 = 12 leaves 5 5 3⁶ ≡ 1 Back to 1, then repeat 6

Which moduli have primitive roots?

Every prime has at least one. The key step in the proof is that a polynomial of degree d has at most d roots modulo a prime. That limits how many elements can have each small order, and counting forces some element to have order p − 1.

Once you have one primitive root g, you have them all. The power g^k is also a primitive root exactly when gcd(k, p − 1) = 1. Modulo 11, the allowed exponents are 1, 3, 7 and 9, giving the roots 2, 8, 7 and 6. That is φ(10) = 4 of them.

For composite moduli the situation is more restrictive. Primitive roots exist only for 1, 2, 4, powers of odd primes, and twice powers of odd primes. Modulo 8, each of 1, 3, 5 and 7 squares to 1, so nothing reaches order φ(8) = 4.

Testing a candidate efficiently

You do not need to list all the powers. If g fails to be a primitive root modulo p, its order is a proper divisor of p − 1, so it divides (p − 1)/q for some prime q dividing p − 1.

  1. Factor p − 1 into primes.
  2. For each prime factor q, compute g^((p−1)/q) modulo p.
  3. If none of these equals 1, g is a primitive root.

Take p = 41, where p − 1 = 40 = 2³ × 5. For g = 6, compute 6^20 and 6^8.

6² = 36 ≡ −5
6⁴ ≡ 25
6⁸ ≡ 625 ≡ 10          so 6⁸ is not 1
6^16 ≡ 100 ≡ 18
6^20 = 6^16 · 6⁴ ≡ 18 · 25 = 450 ≡ 40 ≡ −1

Neither is 1, so 6 is a primitive root modulo 41. By contrast, 2^20 ≡ 1 (mod 41), so 2 is not. There is no simple formula for the smallest primitive root, but trying small numbers usually succeeds quickly. The catch is step 1, since factoring a very large p − 1 can be difficult.

A second run shows how little work is needed. For p = 19 we have p − 1 = 18 = 2 × 3², so the primes to test are 2 and 3. For g = 2, compute 2^9 ≡ 18 and 2^6 ≡ 7. Neither is 1, so 2 generates all 18 nonzero remainders, and you have checked just two powers.

A pattern you can see in decimals

Primitive roots hide inside long division. For a prime p other than 2 and 5, the length of the repeating block in the decimal expansion of 1/p equals the order of 10 modulo p. Each step of the division multiplies the current remainder by 10 and reduces it modulo p, so the remainders are exactly the powers of 10.

p decimal behavior of 1/p order of 10 is 10 a primitive root?
7 block 142857 6 yes
13 block 076923 6 no, since p − 1 = 12
17 block of 16 digits 16 yes

When 10 is a primitive root, the block is as long as it can possibly be. The famous rotations of 142857, such as 2 × 142857 = 285714, happen because multiplying by 2 simply starts the same cycle of remainders at a different point.

Discrete logarithms as an index table

Fix a primitive root g modulo p. Every nonzero a equals g^k for exactly one k from 0 to p − 2. That k is the discrete logarithm, or index, of a to the base g. For p = 11 and g = 2:

a 1 2 3 4 5 6 7 8 9 10
log 0 1 8 2 4 9 7 3 6 5

Logarithms turn multiplication into addition, now modulo p − 1. To multiply 7 by 8, add their logs: 7 + 3 = 10 ≡ 0, which is the log of 1. Check: 56 = 5 × 11 + 1.

They also turn roots into division. To solve x³ ≡ 5 (mod 11), you need 3 × log x ≡ 4 (mod 10). The inverse of 3 modulo 10 is 7, so log x ≡ 28 ≡ 8, giving x = 3. Indeed 27 ≡ 5. Solving that middle equation is a linear congruence.

Counting solutions with a generator

A primitive root also explains the structure of power equations. Write x = g^t. The equation x^k ≡ 1 (mod p) becomes kt ≡ 0 (mod p − 1), which has exactly gcd(k, p − 1) solutions for t.

So modulo 13 the equation x⁴ ≡ 1 has gcd(4, 12) = 4 solutions, namely 1, 5, 8 and 12. Check one: 5² = 25 ≡ 12, and 12² = 144 ≡ 1. By contrast x⁵ ≡ 1 has only the solution x = 1.

The same view shows that the nonzero squares are exactly the even powers of g. Half of the nonzero remainders are squares, and a number is a square precisely when its discrete logarithm is even. This is the starting point for quadratic residues. It also reveals a small leak: an observer can always learn whether a secret exponent is even, which is one reason careful protocols work in a subgroup of large prime order.

Computing a logarithm without a table

Trying exponents one at a time takes up to p − 1 steps. The baby-step giant-step method needs only about √p steps, at the cost of memory.

Solve 2^x ≡ 18 (mod 29), where 2 is a primitive root. Choose m = 6, since 6² ≥ 28.

  • Baby steps: store 2^0 through 2^5, which are 1, 2, 4, 8, 16 and 3.
  • Giant factor: 2^6 ≡ 6, and the inverse of 6 modulo 29 is 5.
  • Giant steps: begin at 18, which is not in the list. Multiply by 5 to get 90 ≡ 3.
  • Match: 3 is 2^5, after one giant step, so x = 6 × 1 + 5 = 11.

Check: 2^11 = 2048 = 70 × 29 + 18.

Better algorithms exist. Index calculus exploits the way many integers factor into small primes, and modern descendants of it run far faster than √p for prime fields, though still not in polynomial time.

Key exchange in miniature

Diffie–Hellman key exchange lets two parties agree on a secret over an open channel. With p = 23 and g = 5, suppose one picks the private exponent 6 and the other picks 15.

first sends    5^6  mod 23 = 8
second sends   5^15 mod 23 = 19
first computes   19^6  mod 23 = 2
second computes   8^15 mod 23 = 2

Both obtain 2, because each has computed 5^(6 × 15). An eavesdropper sees 23, 5, 8 and 19. The obvious attack is to take a discrete logarithm. The full protocol is walked through in Diffie–Hellman step by step.

These numbers protect nothing. Real systems use enormous parameters, authentication and carefully reviewed software, so never build your own encryption for real data.

Why the choice of group matters

The difficulty of the discrete logarithm depends on where you compute it. If p − 1 has only small prime factors, the Pohlig–Hellman method splits the problem into small pieces and solves it easily. For this reason, designers choose groups containing a subgroup of large prime order.

Addition modulo n offers no protection at all: finding k with kg ≡ a is just division. Groups of points on elliptic curves are at the other end, with no known shortcut comparable to index calculus for well-chosen curves. That is why elliptic curve cryptography can use shorter keys.

Nobody has proved that the problem is hard. A sufficiently large quantum computer running Shor's algorithm would solve it efficiently, which motivates the move to other methods.

Open questions and pitfalls

Artin's conjecture says that any integer other than −1 and the perfect squares is a primitive root for infinitely many primes. Christopher Hooley showed in 1967 that it follows from a generalized form of the Riemann hypothesis. Unconditional results show that it holds for most such integers in a precise sense, yet as of the mid-2020s no specific value, not even 2, had been proved to work.

Watch for these slips when you calculate:

  • Reducing exponents modulo p. Exponents are reduced modulo p − 1, or modulo the order of the base.
  • Assuming 2 always generates. It fails modulo 7 and modulo 41.
  • Asking for a logarithm that does not exist. If the base is not a primitive root, some targets are never reached. Modulo 7, no power of 2 equals 3.
  • Confusing the discrete version with the ordinary logarithm. The remainders jump around with no useful sense of "bigger" or "closer".

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 59 articles in Number Theory →

Latest news on this topic