Primitive Roots
Modulo a prime , some numbers generate every nonzero residue as a power: run through 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 is a unit whose order is , the largest possible. Then every unit is a power of , and the group of units is cyclic.
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 has a primitive root; in fact it has exactly of them.
Idea of the proof. For each divisor of , the elements of order are roots of , which has at most roots in the field . If one element of order exists, its powers are all roots, and exactly of them have order . So there are either 0 or elements of order . Since (Gauss's sum) and every unit has some order, none of the counts can be 0. In particular occurs times.
More generally, primitive roots modulo exist exactly for ( an odd prime). Modulo 8, for instance, every unit squares to 1.
Testing a candidate
You do not need all powers. is a primitive root iff
because if the order were a proper divisor of , it would divide some . This needs the factorization of , which is why cryptographic groups use primes with known structure such as safe primes .
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 is a primitive root, then is a primitive root exactly when , since . That explains the count .
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 has an index (discrete logarithm) with , and multiplication becomes addition of indices modulo , the way log tables turned multiplication into addition before calculators.
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 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 or the whole group depending on .
- Number-theoretic transforms need a primitive root of a prime like , 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: , all six residues. Powers of 2: , order 3.
Exercise 2: How many?
How many primitive roots does the prime 41 have?
Answer
Solution
.
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 .
Solution
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 , 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 is an element of order…
- 2
To test whether is a primitive root modulo , check for…
- every
- every prime dividing
- only
- every divisor of
Which modulus has no primitive root?
- 9
- 10
- 12
- 13