Copyright © 1986-2015 by Sean Erik O'Connor. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License
This technical note explains in great detail the technique used to compute primitive polynomials from the paper by Alanen and Knuth with extensions due to Sugimoto . Prerequisites: Knowledge of elementary number theory, and finite fields as covered in books on abstract algebra. See the references.
I have a C++ program which computes primitive polynomials of arbitrary degree n modulo any prime p.
Here is an ultra-fast review of some basic results we'll need to use.
An integer $a$ is defined to be a primitive root of the prime $p$ if $a^k \ne 1 (mod \: p)$ for $0 < k < p-1$ but $a^{p-1} \equiv 1 (mod \: p)$ i.e. the integer a generates the cyclic group $\{ 1, 2, 3, ..., p-1 \}$ under modulo $p$ multiplication. Primitive roots exist for all prime p. Example: 2 is a primitive root of 3 because $2^1 \equiv 2 \not \equiv 1 (mod \: 3)$ but $2^2 \equiv 1 (mod \: p)$
The Phi or Totient function is defined by $$\phi( 1 ) = 1$$ $$\phi( n ) = \text{ (number of integers between 1 and n-1 relatively prime to n) } \text{ for } n \gt 1$$
Euler's Totient with upper and lower bounds.
Integers a and b are defined to be relatively prime (coprime) if the greatest common divisor of a and b is 1). For prime p, $$ \phi( p ) = p-1 $$ In general, if n has the prime factorization $$ n = p_1^{e_1} \ldots p_k^{e_k} $$ then $$ \phi( n ) = n \left( 1 - {1 \over {p_1}} \right) \ldots \left( 1 - {1 \over {p_k}} \right) $$
Example: $\phi( 18 ) = \phi( 2 \bullet 3^2 ) = 18 \bullet ( 1 - {1 \over 2} )( 1 - {1 \over 3} ) = 6$ = number of integers between 1 and 17 relatively prime to 18 = # {1, 5, 7, 11, 13, 17}
We have the following bounds for $\phi$, $$ {n \over { e^\gamma \log \log n + {5 \over {2 \log \log n}} } } \le \phi(n) \le n $$ where the lower bound holds except for the numbers, $n = \{ 2, 3, 5, 7, 11, 13, 17, 19, 23 \}$. For our example, this is 4.239 ≤ φ ( 18 ) ≤ 18. A looser lower bound is $\phi(n) \ge \sqrt{ n } \text{ for } n \ge 6$.
If p is a prime and p does not divide a, then $a^{p-1} \equiv 1 (mod \: p)$ and for any integer a, $a^p \equiv a (mod \: p)$. For example, $3^7 \equiv 3 \bullet 9^3 \equiv 3 \bullet 2^3 \equiv 3 (mod \: 7)$
Any finite field has $p^n$ elements where n is 1 or greater and p is prime. All fields with the same number of elements are isomorphic. A field with $p^n$ elements is called Galois Field $GF \left( p^n \right)$.
The Galois field $GF \left( p^n \right)$ is a quotient ring $GF ( p ) [ x ] / ( g(x) )$ where g(x) is any monic irreducible polynomial of degree n with coefficients in GF(p), and (g(x)) is the maximal principle ideal generated by g(x). In other words, it is an algebraic extension of GF(p).
The field is exactly the set of all polynomials of degree 0 to n-1 with the two field operations being addition and multiplication of polynomials modulo g(x) and with modulo p integer arithmetic on the polynomial coefficients.
For example, the field $GF( 3^2 ) = GF( 3 ) / (x^2 + x + 2)$ is the set of polynomials $\{ 0, 1, 2, x, x+1, x+2, 2x, 2x+1, 2x+2 \}$ with addition and multiplication of polynomials modulo $x^2 + x + 2$ and modulo 3.
$GF \left( p^n \right)$ forms a cyclic group of order $p^n-1$ under multiplication with the elements, $\{ 1, a, a^2, \ldots, a^{p^n-2} \}$ where the field element $a$ is called a generator of the group (or primitive element of the field). Multiplication is defined by $a^i \bullet a^j = a^{(i+j) mod( p^n - 1 )}$ For consistency, we define the zero element of the field as $a^{-\infty} = 0$. Zero is not a primitive element.
Example: We show that $a = 2x$ is a primitive element of the field $GF( 3^2 )$ by multiplying out powers of a.
$$a = 2x$$
$$a^2 = 4 x^2 (mod \: x^2 + x + 2, mod \: 3) = 2x + 1$$
$$a^3 = x + 1$$
$$a^4 = 2$$
$$a^5 = x$$
$$a^6 = x + 2$$
$$a^7 = 2 x + 2$$
$$a^8 = a^{8 \: mod( 3^2 - 1 )} = a^0 = 1$$
The field element $a^s$ is a primitive element iff $gcd( s, p^n-1 ) = 1$
Thus, it's intuitively obvious there are $\phi( p^n - 1 )$ primitive elements. In the example above, $GF(3^2)$ has $\phi(8) = 4$ primitive elements, and they are $\{ a, a^3, a^5, a^7 \} = \{2x, x+1, x, 2x + 2 \}$
Any field element a satisfies the equation $a^{p^n} \equiv a$ and non-zero field elements satisfy $a^{p^n-1} \equiv 1$. Example: $a^8 = 1$ in $GF( 3^2 )$.
Any polynomial $f(x)$ with coefficients in $GF( p )$ obeys the identity $f( x^p ) ={[ f( x ) ]}^p$. This implies, if $B$ is any root of $f(x)$ in $GF \left( p^n \right)$ then then so are $ \{ B, B^p, B^{p^2}, B^{p^3}, \ldots \}$ Example: if $f(x) = x^2 + x + 2$ then $f( x^3 ) = (x^3)^2 + (x^3) + 2 = (x^2 + x + 2)^3 = {f( x )^3} (mod \: 3)$
$GF( p )$ is the smallest subfield of $GF( p^n )$. It is the field of integers under modulo $p$ arithmetic. An element $a \in GF( p^n )$ is isomorphic to an element of $GF( p )$ if it satisfies the equation, $a^p \equiv a$ For example, for $a^4 = 2$ above, $(a^4)^3 \equiv a^{12 \: mod \: 8} \equiv a^4$
The minimal polynomial $f( x )$ of a field element $B \in GF( p^n )$ is the unique polynomial of lowest degree which has $B$ as a root and whose coefficients lie in $GF( p )$. More precisely, the coefficients are allowed to be in $ GF( p^n )$ as long as they are isomorphic to elements of $GF( p )$ Example: $h( z ) = z^2 + 2z + 2 = (z - a) (z - a^3)$ is the minimal polynomial of $a = 2x$
The conjugates of a field element $B \in GF( p^n )$ are the r elements, $\{ B, B^p, B^{p^2}, B^{p^3}, \ldots , B^{p^{r - 1}} \}$ where $r \le n$ is the smallest integer for which $B^{p^r} \equiv B$ Example: $B$ has $r = 2$ conjugates, $B = a = 2x$ and $B^3 = a^3 = x + 1$ because $B^{3^2} \equiv a^9 \equiv B$
The conjugates of the field element $B$ are exactly the roots of the minimal polynomial for $B$. The minimal polynomial can then be written explicitly as $$f(x)=(x-B)(x-B^p)(x-B^{p^2}) \ldots (x-B^{p^{r-1}})$$ Example: The minimal polynomial $h(z) = z^2 + 2z + 2 = a^0z^2 + a^4z + a^4$ factors as $h(z)=(z-a)(z-a^3)=(z-2x)(z-(x+1))$
In the cyclic group $G = (a) = \{1, a, a^2, \ldots, a^{N-1}\}$ of order $N$, the element $a^s$ is a generator if and only if s and N are relatively prime. That is why $\{ a^1, a^3, a^5, a^7 \}$ were primitive elements of the field $GF( 3^2 )$. There are $\phi( N )$ generators.
Suppose we are given a set S and m of its subsets, $\{S_1, S_2, \ldots, S_m\}$. Let $\lVert S \lVert$ denote the number of elements in a set S. Then we have the formula, $$\lVert S - \bigcup_{i=1}^m S_i = \lVert S \lVert - \sum_{1 \le i \le m} \lVert S_i \lVert + \sum_{1 \le i \lt j \le m} \lVert S_i \cap S_j \lVert - \sum_{1 \le i \lt j \lt k \le m} \lVert S_i \cap S_j \cap S_k \lVert + \ldots {(-1)}^m \lVert S_1 \cap \ldots S_m \lVert. $$ Example: Use inclusion and exclusion to count the number of primes between 1 and 10 by subtracting off all multiples of 2, 3, 4, ..., 10. (1 is counted along with the primes). Let $S = {1,2,3, \ldots, 10}$, m = 9, and let $S_i$ be the set of multiples of i+1 except itself. Then $\lVert S \lVert = 10, \lVert S_1 \lVert = \lVert {4,6,8,10} \lVert = 4, \lVert S_2 \lVert = \lVert {6,9} \lVert = 2 \lVert S_3 \lVert = \lVert {8} \lVert = 1, \lVert S_4 \lVert = \lVert {10} \lVert = 1$ $S_5$ through $S_9$ are empty. $\lVert S_1 \cap S_2 \lVert = \lVert \{ 4, 6, 8, 10 \} \cap \{ 6, 9 \} \lVert = \lVert {6} \lVert = 1$ and $\lVert S_1 \cap S_3 \lVert = \lVert = \lVert S_1 \cap S_4 \lVert = 1$ All other intersections are empty as are all intersections of 3 or more sets. The inclusion and exclusion principle then gives us $\lVert S - \bigcup_{i=1}^m S_i = 10 - (4 + 2 + 1 + 1) - (1 + 1 + 1) - 0 + \ldots + 0 = 5$ which agrees with the direct calculation, $\lVert S - \bigcup_{i=1}^m S_i = \lVert \{ 1, 2, 3, \ldots, 10 \} - \{ 4, 6, 8, 10 \} \lVert = \lVert \{ 1, 2, 3, 5, 7 \} \lVert = 5.$
f(x) is a primitive polynomial iff the field element x generates the cyclic group of non-zero field elements of the finite field $GF( p^n )$ where p is prime and $n \ge 2$. In particular, as a generator of the non-zero field elements, f(x) satisfies the equations
Lower bound factor in parentheses.
$$x^{p^n-1} \equiv 1 (mod f(x), p)$$ but $$x^m \neq 1 (mod f(x), p) \text{ for } 1 \le m \le p^n-2$$
The probability that a random f(x) is a primitive polyomial is $P(n,p)={{\phi(p^n-1)/n} \over {p^n-1}}$ which is bounded above and below by $$ {1 \over n} \left( {1 \over {e^\gamma (\log n + \log \log p) + {5 \over {2 \log n + \log \log p}}}} \right) \le P(n,p) \le {1 \over n} $$
The factor in parentheses falls slowly to approximately 0.0623901 for n = p = 1000, so if we generate polynomials at random and are able to test them quickly for primitivity, we have a fast algorithm.
Instead of checking p^{n}-1 conditions for each candidate polynomial, as in the definition, we can reduce the number of conditions to the number of distinct prime factors of $(p^n - 1)/(p-1)$. For example, for a primitive polynomial of degree 9 modulo 3, we test 2 conditions instead of 19,682: $(3^9 - 1)/(3-1) = 13 * 757$ and for degree 10 modulo 2, we can test 3 conditions instead of 1023.
Step 0) Factor $r = {{p^n-1} \over {p-1}}$ into distinct primes: $r = p_1^{e_1} \ldots p_k^{e_k}$ Note that when $r = {{2^n-1} \over {2-1}}$ is a Mersenne prime we can skip all time-consuming factoring, and search for very high degree primitive polynomials.
Step 1) Generate a new nth degree monic polynomial (randomly or sequentially) of the form, $f(x) = x^n + a_{n-1} x^{n-1} + \ldots + a_1 x + a_0$ where we take care that all $p^n$ possible combinations of coefficients are generated exactly once.
Step 2) Check that $( -1 )^n a_0$ is a primitive root of the prime p. Use the quick test that a is a primitive root of prime p iff $a^{ {{p-1} \over q} } \ne 1 (mod \: p)$ for all prime $q \vert (p-1)$ Of course, $a \ne 0 (mod \: p)$ and 1 is the primitive root of $p=2$. Empirically, about 37% of random integers in $[ 0, p-1 ]$ are primitive roots of p.
Step 3) Check for absence of linear factors by testing that $f(a) \ne 0 (mod \: p)$ for $1 \le a \le p-1$ When $n \ge p$ 63.2% of polynomials will have linear factors and be rejected.
Step 4) Check if f( x ) has two or more irreducible factors using the Berlekamp polynomial factorization method, and reject it as not primitive if it does. The probability that a random polynomial of degree n is irreducible is between $1 \over 2n$ and $1 \over n$.
Step 5) Check if $x^r \equiv a (mod f(x), p)$ for some integer $a$. Most polynomials are rejected here.
Step 6) Check $a \equiv (-1)^n a_0 (mod \: p)$
Step 7) Check $x^m \ne \text{ Integer } (mod f(x), p)$ for $m \in \{ {r \over {p_1}}, {r \over {p_2}}, \ldots {r \over {p_k}} \}$ But skip the test for $m = {r \over p_i} \: \text{if} \: p_i \vert (p-1)$
Step 8) If the polynomial f(x) passes tests in steps 1-7, stop. It is a primtive polynomial of degree n, modulo p. Otherwise, repeat steps 1-7 until a primitive polynomial is found in not more than p^{n} iterations.
Factoring r into primes is the time consuming part, followed by computing $x^n (mod f(x), p)$ on the coefficients, but is still much faster than attemping to use the definition of primitive polynomial directly.
Let's illustrate the steps involved in the algorithm using Mathematica. This Mathematica notebook is distributed under the terms of the GNU General Public License.
Primitive Polynomial
Search Example
Sean E. O'Connor
artificer!AT!seanerikoconnor!DOT!freeservers!DOT!com
Abstract
We describe methods for testing whether a polynomial is primitive or irreducible using a Mathematica script. The straightforward test to determine whether a polynomial f(x) is primitive over GF(p) by checking if x is a generator of the field GF() is exponentially slow. We show a much faster test, due to Alanen, Knuth and Sugimoto based on factoring
1. Preliminaries
First we have to set up a series of helper functions. Let's try them out in GF(), using the primitive polynomial,
In[325]:=
The number of elements in the field, which is the total number of polynomials is
In[326]:=
In[327]:=
Out[327]=
The number of distinct primitive polynomials is
In[328]:=
In[329]:=
Out[329]=
The probability of a random polynomial being primitive is
In[330]:=
Out[330]=
The number of polynomials with linear factors is
In[331]:=
In[332]:=
Out[332]=
The probability of a random polynomial having no linear factors is
In[333]:=
Out[333]=
Is a a primitive root of p, i.e. does a generate the multiplicative group GF(p)?
In[334]:=
In[335]:=
Out[335]=
a = 4 is not a generator of GF(p)
In[336]:=
Out[336]=
In[337]:=
Out[337]=
But a = 3 is a generator of GF(p)
In[338]:=
Out[338]=
Extract a polynomial's constant term,
In[339]:=
In[340]:=
Out[340]=
Test if a polynomial is constant,
In[341]:=
In[342]:=
Out[342]=
Evaluate a polynomial at {0,...,p-1} to check if it has any zeros over GF(p),
In[343]:=
In[344]:=
Out[344]=
In[345]:=
Out[345]=
Evaluate
In[346]:=
In[347]:=
Out[347]=
Form the Q matrix from the rows of coefficients of the powers
In[348]:=
In[349]:=
Out[349]=
In[350]:=
In[351]:=
Out[351]//MatrixForm=
In[352]:=
In[353]:=
Out[353]=
Determine the left nullspace of Q-I and its nullity.
In[354]:=
In[355]:=
Out[355]=
In[356]:=
In[357]:=
Out[357]=
2. Fast Algorithm for Finding a Primitive Polynomial
Here's a reasonably fast method for testing a polynomial modulo p for primitivity.
In[358]:=
Test the method on several polynomials. First, our example polynomial.
In[359]:=
Now test the method on several polynomials.
In[360]:=
In[361]:=
In[362]:=
In[363]:=
In[370]:=
3. Slow Algorithm for Finding a Primitive Polynomial
The exponentially slow (brute force) method is to try (mod f(x), p) and check we don't get 1 until k = -1.
Here are the first few powers,
In[365]:=
Out[365]=
Scan through all the powers, and make sure 1 is the last one!
In[366]:=
In[367]:=
We'll show primitive polynomials are exactly the minimal polynomials of the primitive elements of a field.
Theorem. Any minimal polynomial of a primitive element $a \in GF(p^n)$ with $p \ge 2$ prime and $n \gt 1$ is a primitive polynomial.
Proof. Let $f(x)$ be the minimal polynomial of the primitive field element $a$. Recall a field always contains a primitive element and minimal polynomials exist for each field element. We'll show $x$ is a generator of the field.
Suppose to the contrary, that $x^m \equiv 1 (mod f(x), p)$ for some $m$, $1 \le m \le p^n-2$. Then there is an $h(x)$ such that $x^m - 1 \equiv h(x) f(x) (mod \: p)$ Since $f$ is a minimal polynomial of $a$, it has $a$ as a root: $f(a) \equiv 0 \: (mod f(x), p)$ so $a^m \equiv 1 (mod f(x), p)$ contradicting the primitivity of a because its order is not maximal.
By Fermat's theorem for fields, the non-zero field element x satisfies $x^{p^n-1} \equiv 1 (mod f(x), p)$ so $x$ is a generator of the field, and $f(x)$ is a primitive polynomial. $\blacksquare$
Theorem. The minimal polynomial f(x) of the primitive element a has the explicit formula, $$ f( x ) = (x - a)(x - a^p)(x - a^{p^2}) \dots (x - (a^s)^{p^{n - 1}}) $$ where the roots of f(x) are the n conjugates of a.
Proof. By the minimal polynomial formula, $$ f( x ) = (x - a)(x - a^p)(x - a^{p^2}) \dots (x - (a^{p^{r - 1}}) $$ where r is the least integer such that $$ a^{p^r} = a $$
But a is a primitive element, so all the powers of a are distinct, until by Fermat's theorem for fields, $$ a^{p^n} = a $$ Thus r=n. $\blacksquare$
Theorem. Any primitive polynomial f(x) of degree n modulo p is the minimal polynomial of a primitive element of the field.
Proof. Suppose f(x) is a primitive polynomial of degree n modulo p. Then x is a primitive element of the field $GF( p^ n )$ i.e. it generates the field. x is a root because $ f( x ) \equiv 0 (mod f( x ), p ) $ By the polynomial field identity, the powers of x, $ \{ x, x^p, x^{p^2}, \ldots , x^{p^{n-1}} \}$ are also roots. They are all distinct since x is a generator and has maximal order. Since the degree of f is n, there can be no more roots. Thus f(x) is the minimal polynomial of the primitive element x. $\blacksquare$
Theorem. A primitive polynomial f(x) is irreducible in GF(p). (As an aside, polynomials which are irreducible, but not primitive also generate fields, but they are minimal polynomials of non-primitive elements.)
Proof. If not, we'd have f(x)=g(x) h(x) for some polynomials of degree < n. But then g(x) h(x) = 0 (mod f(x), p), and our field would have zero divisors, a contradiction. $\blacksquare$
Theorem. For any primitive polynomial f(x), $ f( a ) \neq 0 \quad \text{for } a \in \{ 0, 1, \ldots , p-1 \} $ If f(x) has zeros in GF(p), it would contains linear factors, contradicting irreducibility. $\blacksquare$
We develop an efficient test for a polynomial to be primitive. It is like "factoring" the original definition into two simpler ones.
Lemma. Suppose n is the smallest integer for which $x^n \equiv 1 (mod \: f( x ), p )$ if $x^m \equiv 1 (mod \: f( x ), p )$ then $n | m$
The same result holds if we replace the condition $ \equiv 1$ with the condition $\equiv \text{ integer }$.
Also, for any integer a and prime p, if n is the smallest number for which $a^n \equiv 1 (mod \: p)$ and $a^m \equiv 1 (mod \: p)$ then $n | m$
Proof. Since n is the smallest such number, $m > n$ so by the division algorithm, $m = n q + r \text{ where } 0 \le r \lt n$ which gives $$1 \equiv x^m \equiv x^{n q + r} \equiv {\left( x^n \right)}^q x^r (mod \: f( x ), p) \equiv 1^q x^r (mod \: f( x ), p )$$
But since r < n, this would contradict the minimality of n unless r=0. Thus $m = n q$ or $n | m$ The same exact reasoning based on minimal order holds for the other two conditions. $\blacksquare$
Theorem. Let r be the smallest number for which $x^r \equiv a \: (mod \: f( x ), p)$ for some integer a and let k be the smallest number for which $x^k \equiv 1 \: (mod \: f( x ), p)$ (Such numbers always exists by Fermat's theorem for fields and the well-ordering principle.)
Given a, let t be the smallest number for which $a^t \equiv 1 (mod \: p)$ then $k = r t$
Proof. Observe $x^{rt} \equiv { \left( x^r \right) }^t \equiv 1 \: (mod \: f( x ), p )$ By the lemma, $k | r t$ But since 1 is an integer, by the lemma, we must have $r | k$ From these divisibility results, we can write for some integers u and v, $r t = k u$ and $k = v r$ giving $r t = v r u$ and by cancellation, $t = u v$ But then $$ {\large 1 \equiv x^k \equiv x^{ {r t} \over u} \equiv { \left( x^r \right) }^{t \over u} \equiv a^{t \over u} (mod \: f( x ), p ) \equiv a^{ {u v} \over u} \equiv a^v (mod \: p) } $$ By the lemma, $t | v$ But we already had $v | t$ from $t = u v$, so $t = v$, implying $u = 1$ implying $k = r t$ $\blacksquare$
Corollary. Given the definition of the numbers r and t as in the lemma above, if $r = {{p^n - 1} \over {p - 1}}$ and $t = p - 1$ then $k = p^n - 1$ so $f( x )$ is a primitive polynomial by definition.
Theorem. Let's go in the opposite direction. Defining r, t and k as in the lemma, let f(x) be primitive. It follows that $r = {{p^n - 1} \over {p - 1}}$ and $t = p - 1$
Proof. Since f(x) is primitive, Now our first theorem says Next, by Fermat's theorem, so by the lemma, and then for some s, Combining all of this, Upon cancelling t, Now we show which will force the equality and prove the first part of the theorem.
A field element, is an element of iff its p-1 power is 1. by the primitivity of f(x), proving By the lemma, The value of t is Let's summarize all of this in a theorem:
Theorem. f(x) is a primitive polynomial iff these two conditions hold: The smallest value of r such that for some integer a is The smallest value of t such that is In other words a must be a primitive root of p.
Theorem. Let f(x) be a primitive polynomial of degree n, modulo p. The the constant term of f is
Proof. By the lemma, the primitive polynomial can be written as and as the minimal polynomial Multiplying out and comparing constant terms gives which by geometric progression is
Corollary. where a is the integer of the theorem earlier. Also, a is a primitive root of p.
Proof.
Thus But a is a primitive root of p by the theorem, so is a primitive root of p as well.
Let's show we don't need to test r conditions, saving considerable time.
Lemma. Suppose that r has prime factorization, If then
Proof. Assume to the contrary that there is some m < r for which
Let then by Euclid's algorithm there exist integers u and v for which yielding Now consider Since is an integer. But d is the greatest common divisor of m and r, where m < r, so d < r also. Thus contains at least one factor for some i making an integer. But then for some i, which is a contradiction.
Theorem. Factor r into primes, The k+1 conditions hold iff is the smallest number for which
Proof. Let's take the easy direction first. If r is the smallest number for which then it's intuitively obvious that Now take the hard direction. Assume all k+1 conditions hold. By the lemma, and by assumption we had
We'll tie together all the theory here. But first, we need a few more theorems.
Theorem. For if it implies is not a primitive root of p for any integer a.
Proof. By Fermat's theorem, if a isn't a multiple of p, But so for some s<p-1, giving Thus cannot be a primitive root of p. In the case, we'd have so again, is not a primitive root of p.
Theorem. Suppose where If is an integer and a primitive root of p, then
Proof. Suppose for some integer a. but the theorem above says is not a primitive root of p, a contradiction.
Theorem. Let the prime factorization of r be and let where Then f(x) is a primitive polynomial of degree n modulo p iff the following conditions hold:
Proof. For the forward direction, assume f(x) is primitive. By the previous theorems, (1)-(4) hold. For the reverse direction, assume (1)-(4) hold. Conditions (3)-(4) together imply a is a primitive root of p and the minimality of t as a power of a. By a previous theorem, f(x) is primitive. And by the theorem above, we can skip the test if for some
Theorem. The number of primitive polynomials of the field is
Proof. We need a few lemmas, but first, some notation. Let be the primitive elements of the field. They are precisely the generators of the cyclic group of non-zero elements of the field. Let be the primitive elements of the field. They are precisely the generators of the cyclic group of non-zero elements of the field. Let
Lemma.
Proof. Suppose By definition of conjugate, for some k, But since is a generator of the group of non-zero field elements, the order of the generator, and the size of the group, must be relatively prime. But and have no common factors, making and relatively prime as well. Thus B is a generator and so
For the other direction, suppose Then for some i, so by definition.
Lemma. The sets of conjugates are either disjoint or identical.
Proof. Suppose and let and have some common element B. Then because B is a conjugate of both sets, for some u and v, If we keep raising this equation to the pth power repeatedly modulo the left side will cycle through all the elements of and the right side will cycle through all elements of Thus any element in one set is also in the other and they are identical.
Lemma. The number of distinct is the number of primitive polynomials.
Proof. A primitive polynomial is the minimal polynomial of a generator, and its roots are conjugates of the generator.
So (number of primitive polynomials) = (number of distinct minimal polynomials of all the generators) = (number of disjoint sets of conjugate roots) = (number of disjoint conjugate sets )
Lemma. has n distinct elements.
Proof. See the earlier lemma which gives the formula for the roots of a primitive polynomial.
Proof of theorem.
Eliminate duplicate sets of conjugates and write where the are disjoint sets. It follows that and the number of primitive polynomials is
Corollary. The probability that a randomly chosen polynomial f(x) of degree n modulo p is primitive is and we have the bounds,
Proof. Use the fact that there are total polynomials and the bounds on the Euler phi function.
When about 63.2% of all polynomials have linear factors (zeros in GF(p)), and so are not primitive. To check a polynomial for zeros quickly, we use Horner's rule to evaluate f(x) for x=0, 1 ... p-1.
Theorem. The number of nth degree polynomials modulo p with no linear factors is and
Proof. We'll use the principle of inclusion and exclusion.
Let S be the set of all monic nth degree polynomials modulo p. There are p choices for each of the n coefficients, so
Now let be the set of all nth degree polynomials having one or more factors of the form (x-i), i=0,...,p-1. A polynomial in this set has the form There is one choice for the first factor, and choices for the second factor giving There are possible sets one for each choice of root i.
The set is the set of nth degree polynomials containing both factors (x-i) and (x-j) at least once. An element of this set looks like After picking i and j, there remain choices for the coefficients, giving The number of such sets is the number of ways of picking distinct i and j from among p possibilities, Similarly, there are many sets for distinct i, j, and k whose cardinality is
At some point, the intersections become empty. When we run out of roots to use for linear factors, and the last type of polynomial to consider has the form It has only one set containing polynomials.
On the other hand, if n < p, we end up factoring the polynomial completely: There is only one polynomial of this type, but the set contains elements.
Counting up the number of possibilities by the principle of inclusion and exclusion gives the formulas.
Example: Let n=3 and p=3.
By inclusion-exclusion, the number of polynomials with no linear factors is
Corollary. The number of polynomials of degree n modulo p with one or more linear factors is
Theorem. For the usual case of we have
Proof. By the binomial theorem,
so giving
Theorem. Pick an nth degree polynomial at random where The probability that it has one or more linear factors is
Proof. The total number of polynomials is Assuming each choice to be equally likely, the probability is
Lemma. The function is an increasing function.
Proof. We'll show
Taking natural logarithms gives
Differentiating,
so
Since we need only show or, since that By definition of log base e,
Now we argue using the fact that
Area under the natural log curve.
the natural log is the area under the 1/t curve.
We'll show that Area B is too hard to handle directly, so let's estimate it. The curve 1/t is concave upward since its second derivative is Thus C > B. But C = (1/2) x base x height = Putting it all together,
Corollary. Is a decreasing function for
Theorem. For we have the bounds
Proof. From calculus, Giving By the corollary, P(n,p) decreases with increasing p, so it stays within the limits above. However, it decreases very slowly: P(n, 4) = 0.684, P(n, 100) = 0.634 and P(n, 500) = 0.632, close to the limiting value of 0.632120559...
We explain the theory behind testing a polynomial f(x) in modulo p arithmetic for reducibility; in particular, finding out whether it has two or more distinct irreducible factors.
We use a modification of the Berlekamp method for factoring polynomials over a finite field.
Differences are that
So the standard proof will need to be modified.
Any monic polynomial f(x) factors into the product of r distinct irreducible polynomials, If then f(x) is reducible and can be rejected as a candidate for a primitive polynomial.
So how to find r? The trick, discovered by Berlekamp, is to find a congruence equation based on the Chinese Remainder theorem for polynomials whose solutions give us information about the number of factors.
Theorem. The two sets, and are the identical. By the Chinese remainder theorem for polynomials, the set has solutions, but we can't find r because we don't know the irreducible factors. But the set is more tractable as we shall see.
Proof. because by the Chinese remainder theorem for polynomials, there is a unique v(x) for any given r integers, so there are solutions. because 0 and 1 are solutions. To prove equality, if then by Fermat's theorem, so that
On the other hand, if then so using the identity, we get
Since the ring of polynomials is a UFD, we can factor each factor into the product of unique monic irreducible polynomials as
where and some of the could be zero. Comparing factors, we have For we cannot have and simultaneously because we'd get and implying leading to which implies, since the polynomial is > 1, a contradiction. Thus if the sum collapses to one term, for some j and for some j. Thus for all i, for some j implying
But S2 = S1, so let's find how many solutions v(x) solve the equation of S2 to get
Theorem. iff where
and and
Proof. and But in a field of characteristic p, we have the polynomial identity, and so we have Comparing equations, we have iff or
The number of elements in the set is the number of solutions v to matrix equation, or equivalently The number of solutions = number of linear combinations of the basis vectors in the left nullspace of Q-I with weighting factors in GF(p). This number is Now we finally find r which is then = # basis vectors of left nullspace of (Q-I).
A column reduction by Gaussian elimination using mod p arithmetic will reduce Q-I to column-echelon form.
This will not change the rank or nullity, being equivalent to a series of multiplications by non-singular elementary matrices.
One can read off the rank of the n x n column-echelon form matrix by counting the number of pivotal elements in the columns.
By Sylvester's theorem of linear algebra, rank + nullity = n, and upon applying this to the transpose of a matrix, Nullity of left nullspace = n - rank.
Now that we have found one primitive polynomial f(x) by searching, we can find all the others.
${Theorem.}$ If f(x) is any primitive polynomial with root a = x then all ${\phi( p^n - 1)/n}$ primitive polynomials are given by the set $$ \{ f_s(x) = (x - a^s)(x - a^{sp})(x - a^{s p^2}) \dots (x - a^{s p^{n - 1}}) \: | \: gcd( s, p^n - 1 ) = 1 \} $$ ${Proof.}$ We know that all the primitive elements of the field ${GF(p^n)}$ are given by $$ \{ a^s \: | \: gcd( s, p^n - 1 ) = 1 \} $$ and the minimal polynomial of primitive element $a^s$ is the primitive polynomial $$ f_s(x) = (x - (a^s))(x - (a^s)^p)(x - (a^s)^{p^2}) \dots (x - (a^s)^{p^{n - 1}}) $$ So the straightforward way to compute ${f_s(x)}$ would be to use finite field arithmetic. A better idea from Berlekamp is to note that $$ f_s(a^s) = 0 $$ and if we write $$ f_s( x ) = c_n x^n + c_{n-1} x^{n-1} + \dots + c_1 x + c_0 $$ then $$ c_n (a^s)^n + c_{n-1} (a^s)^{n-1} + \dots + c_1 (a^s) + c_0 = 0 $$ This can be written in matrix format as $$ M c + c_n a^{sn}= 0 $$ where the columns of M are coefficients of the polynomials $ a^s = x^s mod (f(x), p) $ and $ a^{sn}$ is also a column vector of polynomial coefficients. $$ M = \left( \begin{array} { ccccc } | & | & | & \quad & | \\ 1 & a^s & a^{2s} & \ldots & a^{ (n-1) s} \\ | & | & | & \quad & | \end{array} \right) $$ The columns of M are linearly independent; a linear dependency among the columns would imply $$ g(a^s) = 0 $$ for some polynomial g(x) of degree n-1, contradicting the minimality of f(x). Thus M is invertible and we have a unique solution $$ c = -M^{-1} {c_n a^{sn}} $$ We can use Gaussian elimination on the matrix M and back substitute to find the coefficients c of the primitive polynomial with $O(n^2)$ operations. However, there is a faster way using $O(n)$ operations. Note that $$ f_s(a^s) a^{ks} = 0 $$ for $k=0,1,2,...,n$ which can be written as $$ \begin{array} { cccc } c_n a^{sn} + c_{n-1} a^{s(n-1)} + \dots + c_1 a^s + c_0 = 0 \\ c_n a^{s(n+1)} + c_{n-1} a^{sn} + \dots + c_1 a^{2s} + c_0 a^s = 0 \\ \ldots \\ c_n a^{s(2n)} + c_{n-1} a^{s(2n-1)} + \dots + c_1 a^{s(n+1)} + c_0 a^{sn} = 0 \\ \end{array} $$ This becomes the Topelitz (constant diagonal) system, $$ \left( \begin{array} { cccccc } a^{sn} & a^{s(n-1)} & \ldots & \quad & a^s \quad 1 \\ a^{s(n+1)} & a^{sn} & \ldots & \quad & a^{s+1} \quad a^s \\ \ldots \\ a^{s(2n)} & a^{s(2n-1)} & \ldots & \quad & a^{s(n+1)} \quad a^{sn} \end{array} \right) \left( \begin{array} { c } c_{n} \\ c_{n-1} \\ \ldots \\ c_0 \end{array} \right) = \left( \begin{array} { c } 0 \\ 0 \\ \ldots \\ 0 \\ \end{array} \right) $$ If we project out the $k^{th}$ coefficient in each of the polynomial entries $a^{sk}$ in the matrix, we can solve the resulting integer Toeplitz system using the $O(n)$ Berlekamp-Massey algorithm (described in Berlekamp's book or most any book on error correction coding).Original document written by Sean Erik O'Connor.
Thanks to Martin Dowd for pointing out methods for finding all primitive polynomials given a single primitive polynomial.
Thanks to Tielin Liang for pointing out a mistake in one of my Lemmas in the theoretical section.
Thanks to Majid Bakhtiari for pointing out that Mersenne primes are an important special case for which we can quickly find extremely high degree polynomials.
last updated 08 Feb 15.