Path: blob/main/foundations/05-discrete-log-diffie-hellman/sage/05b-primitive-roots-generators.ipynb
483 views
Notebook 05b: Primitive Roots and Generators
Module 05. The Discrete Logarithm and Diffie-Hellman
Motivating Question. In , the element generates every nonzero residue: . But only generates , half the group. Why the difference? Which elements are generators, how many are there, and how do we find them efficiently? This matters because the DLP is only meaningful when generates a large (sub)group.
Prerequisites. You should be comfortable with:
Cyclic groups and element order (Module 01)
Euler's totient function (Module 04)
The discrete logarithm problem (notebook 05a)
Learning objectives. By the end of this notebook you will be able to:
Define the order of an element and compute it.
Define primitive root (generator) and explain why primitive roots exist for primes.
Test whether a given element is a primitive root using the factorisation of .
Count and find all primitive roots modulo .
Explain why the choice of generator matters for DLP-based cryptography.
1. Order of an Element
Definition. Let be a group and . The order of , written , is the smallest positive integer such that (the identity).
In , the identity is , and the group has order . By Lagrange's theorem (Module 01), always divides .
Key fact: is a generator (primitive root) of if and only if .
Checkpoint 1. Look at the table above. Which elements have order 6 (= )? Which have order 3? Order 2? Can you see that the orders are always divisors of 6?
2. Primitive Roots
Definition. An element is a primitive root modulo if , i.e., the powers give every element of .
Theorem (Existence). For every prime , primitive roots modulo exist. In fact, there are exactly of them.
This is a nontrivial result! Not every group has generators, but always does (because it is cyclic).
Misconception alert. "Small elements like 2 or 3 are always primitive roots." Not true! For example, is not a primitive root mod 7 (it has order 3), but is (order 6). Whether a given element is a primitive root depends on the prime, not on the element's size.
The count always matches .
3. The Efficient Primitive Root Test
Testing whether is a primitive root by computing all powers is , far too slow for cryptographic primes.
The trick: has order if and only if does NOT have order dividing any maximal proper divisor of . By Lagrange's theorem, if , then divides for some prime factor of .
Theorem (Efficient Test). is a primitive root modulo if and only if
This requires only as many exponentiations as there are distinct prime factors of , which is typically very few.
Checkpoint 2. For (so ), how many exponentiations do we need to test one candidate? Test whether is a primitive root mod 31 by checking modulo 31.
4. Finding All Primitive Roots
Once you have one primitive root , you can find all of them:
Theorem. If is a primitive root mod , then is also a primitive root if and only if .
In other words, the set of all primitive roots is .
Bridge from Module 01. This is a direct application of the theory of cyclic groups from Module 01: in a cyclic group of order , the element has order . So is a generator iff .
5. Subgroups and Non-Generators
When is not a primitive root, its powers form a proper subgroup of . The subgroups of a cyclic group of order correspond to divisors of : for each divisor , there is exactly one subgroup of order .
6. Why Generators Matter for Cryptography
If you use a non-generator for Diffie-Hellman, the "shared secret" lives in a subgroup. If that subgroup is small, an attacker can:
Enumerate all elements of the subgroup.
Check which one equals the public value.
Recover the secret exponent.
This is called a small-subgroup attack.
Crypto foreshadowing. In practice, DH uses one of two strategies to avoid this:
Safe primes: choose with prime. Then has only subgroups of order . Use a generator of the order- subgroup (the quadratic residues).
Schnorr groups: work in a prime-order subgroup of order where and is large.
Both ensure that the DLP has no exploitable substructure.
7. SageMath Tools
SageMath provides primitive_root(p) (smallest primitive root) and multiplicative_order() (order of any element).
Checkpoint 3. For a safe prime with prime, we have . What fraction of are generators? (Answer: .) Safe primes have the highest possible generator density.
8. Exercises
Exercise 1 (Worked): Primitive Root Test by Hand
Problem. Is a primitive root modulo ?
Solution. We have . The distinct prime factors are .
Check:
. Since : pass.
. Since : pass.
Both checks passed, so is a primitive root mod 13.
Exercise 2 (Guided): Finding All Primitive Roots mod 19
Problem.
Factor .
Use the efficient test to find the smallest primitive root modulo 19.
List all primitive roots modulo 19 using the formula .
Verify that the count equals .
Hint: , so the prime factors are .
Exercise 3 (Independent): Safe Primes and Generator Density
Problem.
A prime is safe if is also prime. Find all safe primes less than 200.
For each safe prime, compute the fraction of elements that are primitive roots.
Compare with non-safe primes of similar size. Is the generator density higher or lower for safe primes? Explain why.
Summary
| Concept | Key Fact |
|---|---|
| Order of | Smallest with ; always divides |
| Primitive root | with ; generates the full group |
| Existence | Primitive roots exist for every prime |
| Count | Exactly primitive roots modulo |
| Efficient test | Check for each prime factor of |
| Crypto relevance | Using a non-generator enables small-subgroup attacks |
Now that we know how to pick a good generator, we are ready to build the Diffie-Hellman key exchange protocol.