TExcelTAD
Home › Number Theory & Cryptography › Primitive Roots

Primitive Roots

Modulo a prime pp, some numbers generate every nonzero residue as a power: g,g2,…,gp−1g, g^2, \ldots, g^{p-1} run through 1,…,p−11, \ldots, p - 1 in some order. These primitive roots turn multiplication into addition of exponents, and they are the generators Diffie-Hellman and ElGamal are built on.

Definition

A primitive root modulo nn is a unit gg whose order is φ(n)\varphi(n), the largest possible. Then every unit is a power of gg, and the group of units is cyclic.

python · try it yourself
p = 23
for g in (2, 5):
    powers = [pow(g, k, p) for k in range(1, p)]
    print(f"g={g}: order {powers.index(1) + 1:2}  all residues: {sorted(powers) == list(range(1, p))}")
print("powers of 5:", [pow(5, k, 23) for k in range(1, 23)])
g=2: order 11  all residues: False
g=5: order 22  all residues: True
powers of 5: [5, 2, 10, 4, 20, 8, 17, 16, 11, 9, 22, 18, 21, 13, 19, 3, 15, 6, 7, 12, 14, 1]

2 has order 11 modulo 23, so it reaches only half the residues; 5 has order 22 and reaches them all.

Existence

Theorem. Every prime pp has a primitive root; in fact it has exactly φ(p−1)\varphi(p - 1) of them.

Idea of the proof. For each divisor dd of p−1p - 1, the elements of order dd are roots of xd−1x^d - 1, which has at most dd roots in the field Fp\mathbb{F}_p. If one element aa of order dd exists, its powers a,a2,…,ada, a^2, \ldots, a^d are all dd roots, and exactly φ(d)\varphi(d) of them have order dd. So there are either 0 or φ(d)\varphi(d) elements of order dd. Since ∑d∣p−1φ(d)=p−1\sum_{d \mid p-1} \varphi(d) = p - 1 (Gauss's sum) and every unit has some order, none of the counts can be 0. In particular d=p−1d = p - 1 occurs φ(p−1)\varphi(p - 1) times.

More generally, primitive roots modulo nn exist exactly for n=1,2,4,pk,2pkn = 1, 2, 4, p^k, 2p^k (pp an odd prime). Modulo 8, for instance, every unit squares to 1.

Testing a candidate

You do not need all p−1p - 1 powers. gg is a primitive root iff

g(p−1)/q≢1(modp)for every prime q∣p−1g^{(p-1)/q} \not\equiv 1 \pmod{p} \quad \text{for every prime } q \mid p - 1

because if the order were a proper divisor of p−1p - 1, it would divide some (p−1)/q(p - 1)/q. This needs the factorization of p−1p - 1, which is why cryptographic groups use primes with known structure such as safe primes p=2q+1p = 2q + 1.

python · try it yourself
from sympy import factorint, primitive_root, totient

def is_primitive_root(g, p):
    return all(pow(g, (p - 1) // q, p) != 1 for q in factorint(p - 1))

p = 23
roots = [g for g in range(1, p) if is_primitive_root(g, p)]
print(roots, len(roots), totient(p - 1))
for p in (2027, 10007, 1_000_003, 2**61 - 1):
    print(p, "smallest primitive root:", primitive_root(p), " p - 1 =", factorint(p - 1))
[5, 7, 10, 11, 14, 15, 17, 19, 20, 21] 10 10
2027 smallest primitive root: 2  p - 1 = {2: 1, 1013: 1}
10007 smallest primitive root: 5  p - 1 = {2: 1, 5003: 1}
1000003 smallest primitive root: 2  p - 1 = {2: 1, 3: 1, 166667: 1}
2305843009213693951 smallest primitive root: 37  p - 1 = {2: 1, 3: 2, 5: 2, 7: 1, 11: 1, 13: 1, 31: 1, 41: 1, 61: 1, 151: 1, 331: 1, 1321: 1}

All primitive roots from one

If gg is a primitive root, then gkg^k is a primitive root exactly when gcd⁡(k,p−1)=1\gcd(k, p - 1) = 1, since ord⁡(gk)=(p−1)/gcd⁡(k,p−1)\operatorname{ord}(g^k) = (p - 1)/\gcd(k, p - 1). That explains the count φ(p−1)\varphi(p - 1).

python · try it yourself
from math import gcd

p, g = 23, 5
print(sorted(pow(g, k, p) for k in range(1, p - 1) if gcd(k, p - 1) == 1))
[5, 7, 10, 11, 14, 15, 17, 19, 20, 21]

Logarithm tables

With a primitive root, every unit aa has an index (discrete logarithm) kk with gk≡ag^k \equiv a, and multiplication becomes addition of indices modulo p−1p - 1, the way log tables turned multiplication into addition before calculators.

python · try it yourself
p, g = 13, 2
log = {pow(g, k, p): k for k in range(p - 1)}
print("index table:", dict(sorted(log.items())))
a, b = 7, 11
k = (log[a] + log[b]) % (p - 1)
print(f"{a}*{b} mod {p}: via indices g^{k} = {pow(g, k, p)}, directly {a * b % p}")
index table: {1: 0, 2: 1, 3: 4, 4: 2, 5: 9, 6: 5, 7: 11, 8: 3, 9: 8, 10: 10, 11: 7, 12: 6}
7*11 mod 13: via indices g^6 = 12, directly 12

Building that table takes pp steps, fine for 13 and impossible for a 2048-bit prime: that asymmetry is the discrete logarithm problem.

In software

  • Diffie-Hellman groups (RFC 3526, RFC 7919) use safe primes with generator 2, which generates the subgroup of order qq or the whole group depending on p mod 8p \bmod 8.
  • Number-theoretic transforms need a primitive root of a prime like 998244353=119⋅223+1998244353 = 119 \cdot 2^{23} + 1, whose powers give the "roots of unity" for an FFT done exactly in integers.

Exercises

Exercise 1: By hand

Is 3 a primitive root modulo 7? Is 2?

Answer

3 is; 2 is not (order 3).

Solution

Powers of 3 modulo 7: 3,2,6,4,5,13, 2, 6, 4, 5, 1, all six residues. Powers of 2: 2,4,12, 4, 1, order 3.

Exercise 2: How many?

How many primitive roots does the prime 41 have?

Answer
Solution

φ(40)=φ(8)φ(5)=4⋅4=16\varphi(40) = \varphi(8)\varphi(5) = 4 \cdot 4 = 16.

Exercise 3: No primitive root (code)

Show that there is no primitive root modulo 15 by computing the maximum order of a unit.

Answer

The maximum order is 4, but φ(15)=8\varphi(15) = 8.

Solution
python · try it yourself
from math import gcd
from sympy import n_order
units = [a for a in range(1, 15) if gcd(a, 15) == 1]
print({a: n_order(a, 15) for a in units})
{1: 1, 2: 4, 4: 2, 7: 4, 8: 4, 11: 2, 13: 4, 14: 2}

By the CRT the unit group is (Z/3)××(Z/5)×(\mathbb{Z}/3)^\times \times (\mathbb{Z}/5)^\times, and the order of a pair is the lcm of orders 2 and 4, at most 4.

Test yourself

A primitive root modulo a prime pp is an element of order…

  1. 2
  2. pp
  3. p−1p - 1
  4. (p−1)/2(p - 1)/2

To test whether gg is a primitive root modulo pp, check g(p−1)/q≠1g^{(p-1)/q} \ne 1 for…

  1. every q<pq < p
  2. every prime qq dividing p−1p - 1
  3. q=2q = 2 only
  4. every divisor of pp

Which modulus has no primitive root?

  1. 9
  2. 10
  3. 12
  4. 13