Post · 4 min read· pp. 86–88

Primitive Root

Definition

In modular arithmetic, a number gg is called a primitive root modulo n if every number coprime to nn is congruent to a power of gg modulo nn. Mathematically, gg is a primitive root modulo n if and only if for any integer aa such that gcd⁡(a,n)=1\gcd(a, n) = 1, there exists an integer kk such that:

gk≡a(modn)g^k \equiv a \pmod n.

kk is then called the index or discrete logarithm of aa to the base gg modulo nn. gg is also called the generator of the multiplicative group of integers modulo nn.

In particular, for the case where nn is a prime, the powers of primitive root runs through all numbers from 11 to n−1n-1.

Existence

Primitive root modulo nn exists if and only if:

  • nn is 1, 2, 4, or
  • nn is power of an odd prime number (n=pk)(n = p^k), or
  • nn is twice power of an odd prime number (n=2⋅pk)(n = 2 \cdot p^k).

This theorem was proved by Gauss in 1801.

Relation with the Euler function

Let gg be a primitive root modulo nn. Then we can show that the smallest number kk for which gk≡1(modn)g^k \equiv 1 \pmod n is equal ϕ(n)\phi (n). Moreover, the reverse is also true, and this fact will be used in this article to find a primitive root.

Furthermore, the number of primitive roots modulo nn, if there are any, is equal to ϕ(ϕ(n))\phi (\phi (n) ).

Algorithm for finding a primitive root

A naive algorithm is to consider all numbers in range [1,n−1][1, n-1]. And then check if each one is a primitive root, by calculating all its power to see if they are all different. This algorithm has complexity O(g⋅n)O(g \cdot n), which would be too slow. In this section, we propose a faster algorithm using several well-known theorems.

From previous section, we know that if the smallest number kk for which gk≡1(modn)g^k \equiv 1 \pmod n is ϕ(n)\phi (n), then gg is a primitive root. Since for any number aa relative prime to nn, we know from Euler's theorem that aϕ(n)≡1(modn)a ^ { \phi (n) } \equiv 1 \pmod n, then to check if gg is primitive root, it is enough to check that for all dd less than ϕ(n)\phi (n), gd≢1(modn)g^d \not \equiv 1 \pmod n. However, this algorithm is still too slow.

From Lagrange's theorem, we know that the index of 1 of any number modulo nn must be a divisor of ϕ(n)\phi (n). Thus, it is sufficient to verify for all proper divisor d∣ϕ(n)d \mid \phi (n) that gd≢1(modn)g^d \not \equiv 1 \pmod n. This is already a much faster algorithm, but we can still do better.

Factorize ϕ(n)=p1a1⋯psas\phi (n) = p_1 ^ {a_1} \cdots p_s ^ {a_s}. We prove that in the previous algorithm, it is sufficient to consider only the values of dd which have the form ϕ(n)pj\frac { \phi (n) } {p_j}. Indeed, let dd be any proper divisor of ϕ(n)\phi (n). Then, obviously, there exists such jj that d∣ϕ(n)pjd \mid \frac { \phi (n) } {p_j}, i.e. d⋅k=ϕ(n)pjd \cdot k = \frac { \phi (n) } {p_j}. However, if gd≡1(modn)g^d \equiv 1 \pmod n, we would get:

gϕ(n)pj≡gd⋅k≡(gd)k≡1k≡1(modn)g ^ { \frac { \phi (n)} {p_j} } \equiv g ^ {d \cdot k} \equiv (g^d) ^k \equiv 1^k \equiv 1 \pmod n.

i.e. among the numbers of the form ϕ(n)pi\frac {\phi (n)} {p_i}, there would be at least one such that the conditions were not met.

Now we have a complete algorithm for finding the primitive root:

  • First, find ϕ(n)\phi (n) and factorize it.

  • Then iterate through all numbers g∈[1,n]g \in [1, n], and for each number, to check if it is primitive root, we do the following:

    • Calculate all gϕ(n)pi(modn)g ^ { \frac {\phi (n)} {p_i}} \pmod n.
    • If all the calculated values are different from 11, then gg is a primitive root.

    Running time of this algorithm is O(Ans⋅log⁡ϕ(n)⋅log⁡n)O(Ans \cdot \log \phi (n) \cdot \log n) (assume that ϕ(n)\phi (n) has log⁡ϕ(n)\log \phi (n) divisors).

Shoup (1990, 1992) proved, assuming the generalized Riemann hypothesis, that gg is O(log⁡6p)O(\log^6 p).

Implementation

The following code assumes that the modulo p is a prime number. To make it works for any value of p, we must add calculation of ϕ(p)\phi (p).

text
int powmod (int a, int b, int p) {
    int res = 1;
    while (b)
        if (b & 1)
            res = int (res * 1ll * a % p),  --b;
        else
            a = int (a * 1ll * a % p),  b >>= 1;
    return res;
}

int generator (int p) {
    vector<int> fact;
    int phi = p-1,  n = phi;
    for (int i=2; i*i<=n; ++i)
        if (n % i == 0) {
            fact.push_back (i);
            while (n % i == 0)
                n /= i;
        }
    if (n > 1)
        fact.push_back (n);

    for (int res=2; res<=p; ++res) {
        bool ok = true;
        for (size_t i=0; i<fact.size() && ok; ++i)
            ok &= powmod (res, phi / fact[i], p) != 1;
        if (ok)  return res;
    }
    return -1;
}