COMPUTING PRIMITIVE POLYNOMIALS - THEORY AND ALGORITHM

Sean E. O'Connor

Copyright © 1986-2014 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

Introduction

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.

Fast Review of Number Theory, Finite Fields and Combinatorics

Here is an ultra-fast review of some basic results we'll need to use.

Number Theory

Primitive Roots

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)$

Euler Phi Function

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$$

Fig 3.

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$.

Fermat's Little Theorem

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)$

Finite Fields

Basic Properties

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)$.

Polynomial Representation

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.

Exponential Representation

$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$$

Primitive Elements of a Field

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 \}$

Fermat's Theorem for a Field

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 )$.

Polynomial Identity

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)$

Prime Subfield

$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$

Minimal Polynomial

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$

Conjugates

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$

Minimal Polynomial Formula

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))$

Group Theory: Generators of a Cyclic Group

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.

Combinatorics: Principle of Inclusion and Exclusion

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.$

Definition of Primitive Polynomial

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

Fig 4.

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.

Algorithm for Finding Primitive Polynomials

Instead of checking pn-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 pn 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.

Primitive Polynomial Search Example

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.

PrimitivePolynomialSearchExample

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("PrimitivePolynomialSearchExample_1.gif") is exponentially slow.  We show a much faster test, due to Alanen, Knuth and Sugimoto based on factoring "PrimitivePolynomialSearchExample_2.gif"

1. Preliminaries

First we have to set up a series of helper functions.  Let's try them out in GF("PrimitivePolynomialSearchExample_3.gif"), using the primitive polynomial,

In[325]:=

"PrimitivePolynomialSearchExample_4.gif"

The number of elements in the field, which is the total number of polynomials is

In[326]:=

"PrimitivePolynomialSearchExample_5.gif"

In[327]:=

"PrimitivePolynomialSearchExample_6.gif"

Out[327]=

"PrimitivePolynomialSearchExample_7.gif"

The number of distinct primitive polynomials is "PrimitivePolynomialSearchExample_8.gif"

In[328]:=

"PrimitivePolynomialSearchExample_9.gif"

In[329]:=

"PrimitivePolynomialSearchExample_10.gif"

Out[329]=

"PrimitivePolynomialSearchExample_11.gif"

The probability of a random polynomial being primitive is

In[330]:=

"PrimitivePolynomialSearchExample_12.gif"

Out[330]=

"PrimitivePolynomialSearchExample_13.gif"

The number of polynomials with linear factors is "PrimitivePolynomialSearchExample_14.gif"

In[331]:=

"PrimitivePolynomialSearchExample_15.gif"

In[332]:=

"PrimitivePolynomialSearchExample_16.gif"

Out[332]=

"PrimitivePolynomialSearchExample_17.gif"

The probability of a random polynomial having no linear factors is

In[333]:=

"PrimitivePolynomialSearchExample_18.gif"

Out[333]=

"PrimitivePolynomialSearchExample_19.gif"

Is a a primitive root of p, i.e. does a generate the multiplicative group GF(p)?

In[334]:=

"PrimitivePolynomialSearchExample_20.gif"

In[335]:=

"PrimitivePolynomialSearchExample_21.gif"

Out[335]=

"PrimitivePolynomialSearchExample_22.gif"

a = 4 is not a generator of GF(p)

In[336]:=

"PrimitivePolynomialSearchExample_23.gif"

Out[336]=

"PrimitivePolynomialSearchExample_24.gif"

In[337]:=

"PrimitivePolynomialSearchExample_25.gif"

Out[337]=

"PrimitivePolynomialSearchExample_26.gif"

But a = 3 is a generator of GF(p)

In[338]:=

"PrimitivePolynomialSearchExample_27.gif"

Out[338]=

"PrimitivePolynomialSearchExample_28.gif"

Extract a polynomial's constant term,

In[339]:=

"PrimitivePolynomialSearchExample_29.gif"

In[340]:=

"PrimitivePolynomialSearchExample_30.gif"

Out[340]=

"PrimitivePolynomialSearchExample_31.gif"

Test if a polynomial is constant,

In[341]:=

"PrimitivePolynomialSearchExample_32.gif"

In[342]:=

"PrimitivePolynomialSearchExample_33.gif"

Out[342]=

"PrimitivePolynomialSearchExample_34.gif"

Evaluate a polynomial at {0,...,p-1} to check if it has any zeros over GF(p),

In[343]:=

"PrimitivePolynomialSearchExample_35.gif"

In[344]:=

"PrimitivePolynomialSearchExample_36.gif"

Out[344]=

"PrimitivePolynomialSearchExample_37.gif"

In[345]:=

"PrimitivePolynomialSearchExample_38.gif"

Out[345]=

"PrimitivePolynomialSearchExample_39.gif"

Evaluate "PrimitivePolynomialSearchExample_40.gif"

In[346]:=

"PrimitivePolynomialSearchExample_41.gif"

In[347]:=

"PrimitivePolynomialSearchExample_42.gif"

Out[347]=

"PrimitivePolynomialSearchExample_43.gif"

Form the Q matrix from the rows of coefficients of the powers "PrimitivePolynomialSearchExample_44.gif"

In[348]:=

"PrimitivePolynomialSearchExample_45.gif"

In[349]:=

"PrimitivePolynomialSearchExample_46.gif"

Out[349]=

"PrimitivePolynomialSearchExample_47.gif"

In[350]:=

"PrimitivePolynomialSearchExample_48.gif"

In[351]:=

"PrimitivePolynomialSearchExample_49.gif"

Out[351]//MatrixForm=

"PrimitivePolynomialSearchExample_50.gif"

In[352]:=

"PrimitivePolynomialSearchExample_51.gif"

In[353]:=

"PrimitivePolynomialSearchExample_52.gif"

Out[353]=

"PrimitivePolynomialSearchExample_53.gif"

Determine the left nullspace of Q-I and its nullity.

In[354]:=

"PrimitivePolynomialSearchExample_54.gif"

In[355]:=

"PrimitivePolynomialSearchExample_55.gif"

Out[355]=

"PrimitivePolynomialSearchExample_56.gif"

In[356]:=

"PrimitivePolynomialSearchExample_57.gif"

In[357]:=

"PrimitivePolynomialSearchExample_58.gif"

Out[357]=

"PrimitivePolynomialSearchExample_59.gif"

2. Fast Algorithm for Finding a Primitive Polynomial

Here's a reasonably fast method for testing a polynomial modulo p for primitivity.

In[358]:=

"PrimitivePolynomialSearchExample_60.gif"

Test the method on several polynomials.  First, our example polynomial.

In[359]:=

"PrimitivePolynomialSearchExample_61.gif"

"PrimitivePolynomialSearchExample_62.gif"

"PrimitivePolynomialSearchExample_63.gif"

"PrimitivePolynomialSearchExample_64.gif"

"PrimitivePolynomialSearchExample_65.gif"

"PrimitivePolynomialSearchExample_66.gif"

"PrimitivePolynomialSearchExample_67.gif"

"PrimitivePolynomialSearchExample_68.gif"

"PrimitivePolynomialSearchExample_69.gif"

"PrimitivePolynomialSearchExample_70.gif"

"PrimitivePolynomialSearchExample_71.gif"

"PrimitivePolynomialSearchExample_72.gif"

"PrimitivePolynomialSearchExample_73.gif"

"PrimitivePolynomialSearchExample_74.gif"

"PrimitivePolynomialSearchExample_75.gif"

"PrimitivePolynomialSearchExample_76.gif"

"PrimitivePolynomialSearchExample_77.gif"

Now test the method on several polynomials.

In[360]:=

"PrimitivePolynomialSearchExample_78.gif"

"PrimitivePolynomialSearchExample_79.gif"

"PrimitivePolynomialSearchExample_80.gif"

"PrimitivePolynomialSearchExample_81.gif"

"PrimitivePolynomialSearchExample_82.gif"

In[361]:=

"PrimitivePolynomialSearchExample_83.gif"

"PrimitivePolynomialSearchExample_84.gif"

"PrimitivePolynomialSearchExample_85.gif"

"PrimitivePolynomialSearchExample_86.gif"

"PrimitivePolynomialSearchExample_87.gif"

"PrimitivePolynomialSearchExample_88.gif"

"PrimitivePolynomialSearchExample_89.gif"

"PrimitivePolynomialSearchExample_90.gif"

"PrimitivePolynomialSearchExample_91.gif"

In[362]:=

"PrimitivePolynomialSearchExample_92.gif"

"PrimitivePolynomialSearchExample_93.gif"

"PrimitivePolynomialSearchExample_94.gif"

"PrimitivePolynomialSearchExample_95.gif"

"PrimitivePolynomialSearchExample_96.gif"

"PrimitivePolynomialSearchExample_97.gif"

"PrimitivePolynomialSearchExample_98.gif"

"PrimitivePolynomialSearchExample_99.gif"

"PrimitivePolynomialSearchExample_100.gif"

"PrimitivePolynomialSearchExample_101.gif"

In[363]:=

"PrimitivePolynomialSearchExample_102.gif"

"PrimitivePolynomialSearchExample_103.gif"

"PrimitivePolynomialSearchExample_104.gif"

"PrimitivePolynomialSearchExample_105.gif"

"PrimitivePolynomialSearchExample_106.gif"

"PrimitivePolynomialSearchExample_107.gif"

"PrimitivePolynomialSearchExample_108.gif"

"PrimitivePolynomialSearchExample_109.gif"

"PrimitivePolynomialSearchExample_110.gif"

"PrimitivePolynomialSearchExample_111.gif"

"PrimitivePolynomialSearchExample_112.gif"

"PrimitivePolynomialSearchExample_113.gif"

"PrimitivePolynomialSearchExample_114.gif"

"PrimitivePolynomialSearchExample_115.gif"

"PrimitivePolynomialSearchExample_116.gif"

"PrimitivePolynomialSearchExample_117.gif"

"PrimitivePolynomialSearchExample_118.gif"

"PrimitivePolynomialSearchExample_119.gif"

In[370]:=

"PrimitivePolynomialSearchExample_120.gif"

"PrimitivePolynomialSearchExample_121.gif"

"PrimitivePolynomialSearchExample_122.gif"

"PrimitivePolynomialSearchExample_123.gif"

"PrimitivePolynomialSearchExample_124.gif"

"PrimitivePolynomialSearchExample_125.gif"

"PrimitivePolynomialSearchExample_126.gif"

"PrimitivePolynomialSearchExample_127.gif"

"PrimitivePolynomialSearchExample_128.gif"

"PrimitivePolynomialSearchExample_129.gif"

"PrimitivePolynomialSearchExample_130.gif"

"PrimitivePolynomialSearchExample_131.gif"

"PrimitivePolynomialSearchExample_132.gif"

"PrimitivePolynomialSearchExample_133.gif"

"PrimitivePolynomialSearchExample_134.gif"

"PrimitivePolynomialSearchExample_135.gif"

"PrimitivePolynomialSearchExample_136.gif"

3. Slow Algorithm for Finding a Primitive Polynomial

The exponentially slow (brute force) method is to try "PrimitivePolynomialSearchExample_137.gif" (mod f(x), p) and check we don't get 1 until k = "PrimitivePolynomialSearchExample_138.gif"-1.
Here are the first few powers,

In[365]:=

"PrimitivePolynomialSearchExample_139.gif"

Out[365]=

"PrimitivePolynomialSearchExample_140.gif"

Scan through all the powers, and make sure 1 is the last one!

In[366]:=

"PrimitivePolynomialSearchExample_141.gif"

In[367]:=

"PrimitivePolynomialSearchExample_142.gif"

"PrimitivePolynomialSearchExample_143.gif"

Spikey Created with Wolfram Mathematica 6

Theory Behind the Algorithm

More Properties of Primitive Polynomials

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, equation267. where the roots of f(x) are the n conjugates of a.

Proof. By the minimal polynomial formula, equation268. where r is the least integer such that equation269.

But a is a primitive element, so all the powers of a are distinct, until by Fermat's theorem for fields, equation270. Thus r=n.

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 equation271. i.e. it generates the field. x is a root because equation272. By the polynomial field identity, the powers of x, equation273. 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.

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.

Theorem. For any primitive polynomial f(x), equation274. If f(x) has zeros in GF(p), it would contains linear factors, contradicting irreducibility.

Algorithm For Finding Primitive Polynomials

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 equation275. if equation276. then equation277.

The same result holds if we replace the condition "= 1" with the condition "= integer".

Also, for any integer a and prime p, if n is the smallest number for which equation278. and equation279. then equation280.

Proof. Since n is the smallest such number, equation281. so by the division algorithm, equation282. which gives equation283. equation284.

But since r < n, this would contradict the minimality of n unless r=0. Thus equation285. The same exact reasoning based on minimal order holds for the other two conditions.

Theorem. Let r be the smallest number for which equation286. for some integer a and let k be the smallest number for which equation287. (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 equation288. then equation289.

Proof. Observe equation290. By the lemma, equation291. But since 1 is an integer, by the lemma, we must have equation292. From these divisibility results, we can write for some integers u and v, equation293. giving equation294. and by cancellation, equation295. But then equation296. By the lemma, equation297. But we already had equation298. from t=uv, so t=v, implying u=1 implying equation299.

Corollary. Given the definition of the numbers r and t as in the lemma above, if equation300. and equation301. then equation302. 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 equation303. and equation304.

Proof. Since f(x) is primitive, equation305. Now our first theorem says equation306. Next, by Fermat's theorem, equation307. so by the lemma, equation308. and then for some s, equation309. Combining all of this, equation310. Upon cancelling t, equation311. Now we show equation312. which will force the equality equation313. and prove the first part of the theorem.

A field element, equation314. is an element of equation315. iff its p-1 power is 1. equation316. by the primitivity of f(x), proving equation317. By the lemma, equation318. The value of t is equation319. 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 equation320. for some integer a is equation321. The smallest value of t such that equation322. is equation323. In other words a must be a primitive root of p.

The Polynomial's Constant Term

Theorem. Let f(x) be a primitive polynomial of degree n, modulo p. The the constant term of f is equation324.

Proof. By the lemma, the primitive polynomial can be written as equation325. and as the minimal polynomial equation326. Multiplying out and comparing constant terms gives equation327. which by geometric progression is equation328.

Corollary. equation329. where a is the integer of the theorem earlier. Also, a is a primitive root of p.

Proof.

equation330. Thus equation331. But a is a primitive root of p by the theorem, so equation332. is a primitive root of p as well.

Finding Minimal r such that equation333.

Let's show we don't need to test r conditions, saving considerable time.

Lemma. Suppose that r has prime factorization, equation334. If equation335. then equation336.

Proof. Assume to the contrary that there is some m < r for which equation337.

Let equation338. then by Euclid's algorithm there exist integers u and v for which equation339. yielding equation340. Now consider equation342. Since equation343. is an integer. But d is the greatest common divisor of m and r, where m < r, so d < r also. Thus equation344. contains at least one factor equation345. for some i making equation346. an integer. But then equation347. for some i, which is a contradiction.

Theorem. Factor r into primes, equation356. The k+1 conditions equation357. equation358. hold iff equation359. is the smallest number for which equation360.

Proof. Let's take the easy direction first. If r is the smallest number for which equation361. then it's intuitively obvious that equation362. Now take the hard direction. Assume all k+1 conditions hold. By the lemma, equation367. equation368. and by assumption we had equation369.

Testing if a Polynomial is Primitive

We'll tie together all the theory here. But first, we need a few more theorems.

Theorem. For equation370. if equation371. it implies equation372. is not a primitive root of p for any integer a.

Proof. By Fermat's theorem, if a isn't a multiple of p, equation373. But equation374. so for some s<p-1, equation375. giving equation376. Thus equation377. cannot be a primitive root of p. In the case, equation378. we'd have equation379. so again, equation380. is not a primitive root of p.

Theorem. Suppose equation381. where equation382. If equation383. is an integer and a primitive root of p, then equation384.

Proof. Suppose equation385. for some integer a. equation386. but the theorem above says equation387. is not a primitive root of p, a contradiction.

Theorem. Let the prime factorization of r be equation388. and let equation389. where equation390. 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 equation397. for some equation398.

References

Primitive Polynomial Computation

Conway Polynomials and Related Topics

Finite Fields

Number Theory and Arithmetic

Appendix A - Number of Primitive Polynomials

Theorem. The number of primitive polynomials of the field equation550. is equation552.

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 equation554. be the primitive elements of the field. They are precisely the generators of the cyclic group of non-zero elements of the field. Let equation556.

Lemma. equation558.

Proof. Suppose equation560. By definition of conjugate, for some k, equation562. But since equation564. is a generator of the group of non-zero field elements, the order of the generator, equation566. and the size of the group, equation568. must be relatively prime. But equation570. and equation572. have no common factors, making equation574. and equation576. relatively prime as well. Thus B is a generator and so equation578.

For the other direction, suppose equation580. Then equation582. for some i, so equation584. by definition.

Lemma. The sets of conjugates equation586. are either disjoint or identical.

Proof. Suppose equation588. and let equation590. and equation592. have some common element B. Then because B is a conjugate of both sets, for some u and v, equation594. If we keep raising this equation to the pth power repeatedly modulo equation596. the left side will cycle through all the elements of equation598. and the right side will cycle through all elements of equation600. Thus any element in one set is also in the other and they are identical.

Lemma. The number of distinct equation602. 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 equation604. )

Lemma. equation606. 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 equation608. where the equation610. are disjoint sets. It follows that equation612. equation614. and the number of primitive polynomials is equation616.

Corollary. The probability that a randomly chosen polynomial f(x) of degree n modulo p is primitive is equation618. and we have the bounds, equation620.

Proof. Use the fact that there are equation622. total polynomials and the bounds on the Euler phi function.

Appendix B - Number of Linear Factor Free Polynomials

When equation624. 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 equation626. and equation628.

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 equation630.

Now let equation632. 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 equation634. There is one choice for the first factor, and equation636. choices for the second factor giving equation638. There are equation640. possible sets equation642. one for each choice of root i.

The set equation644. 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 equation646. After picking i and j, there remain equation648. choices for the coefficients, giving equation650. The number of such sets is the number of ways of picking distinct i and j from among p possibilities, equation652. Similarly, there are equation654. many sets for distinct i, j, and k whose cardinality is equation656.

At some point, the intersections become empty. When equation658. we run out of roots to use for linear factors, and the last type of polynomial to consider has the form equation660. It has only one set containing equation662. polynomials.

On the other hand, if n < p, we end up factoring the polynomial completely: equation664. There is only one polynomial of this type, but the set contains equation666. elements.

Counting up the number of possibilities by the principle of inclusion and exclusion gives the formulas.

Example: Let n=3 and p=3.

equation668.

equation670.

equation672.

equation674.

equation676.

equation678.

equation680.

By inclusion-exclusion, the number of polynomials with no linear factors is

equation682.

Corollary. The number of polynomials of degree n modulo p with one or more linear factors is

equation684.

equation686.

Theorem. For the usual case of equation688. we have equation690.

Proof. By the binomial theorem,

equation692. so equation694. giving equation696.

Theorem. Pick an nth degree polynomial at random where equation698. The probability that it has one or more linear factors is equation700.

Proof. The total number of polynomials is equation702. Assuming each choice to be equally likely, the probability is equation704.

Lemma. The function equation706. is an increasing function.

Proof. We'll show equation708.

Taking natural logarithms gives equation710.

Differentiating, equation712.

equation714.

so equation716.

Since equation718. we need only show equation720. or, since equation722. that equation724. By definition of log base e, equation726.

Now we argue using the fact that

Fig 2.

Area under the natural log curve.

the natural log is the area under the 1/t curve.

We'll show that equation728. 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 equation730. Thus C > B. But C = (1/2) x base x height = equation732. Putting it all together, equation734. equation736.

Corollary. equation738. Is a decreasing function for equation740.

Theorem. For equation742. we have the bounds equation744.

Proof. From calculus, equation746. Giving equation748. equation750. 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...

Appendix C - Berlekamp Method for Testing Irreducibility

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

  1. We skip the initial stage which finds gcd( f(x), f'(x) ) to reduce f(x) to square-free form for the sake of speed.
  2. We stop as soon as we've found the number of distinct irreducible factors is ≥ 2.

So the standard proof will need to be modified.

Goal

Any monic polynomial f(x) factors into the product of r distinct irreducible polynomials, equation800. If equation802. then f(x) is reducible and can be rejected as a candidate for a primitive polynomial.

Sets of Congruences of the Factors

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, equation804. and equation806. are the identical. By the Chinese remainder theorem for polynomials, the set equation808. has equation810. solutions, but we can't find r because we don't know the irreducible factors. But the set equation812. is more tractable as we shall see.

Proof. equation814. because by the Chinese remainder theorem for polynomials, there is a unique v(x) for any given r integers, equation816. so there are equation818. solutions. equation820. because 0 and 1 are solutions. To prove equality, if equation822. then by Fermat's theorem, equation824. so that equation826.

On the other hand, if equation828. then equation830. so using the identity, equation832. we get equation834.

Since the ring of polynomials is a UFD, we can factor each factor into the product of unique monic irreducible polynomials as

equation836.

equation838.

where equation840. and some of the equation842. could be zero. Comparing factors, we have equation844. For equation846. we cannot have equation848. and equation850. simultaneously because we'd get equation852. and equation854. implying equation856. equation858. leading to equation860. which implies, since the polynomial is > 1, equation862. a contradiction. Thus if equation864. the sum collapses to one term, equation866. for some j and equation868. for some j. Thus for all i, equation870. for some j implying equation872.

Number of Solutions of a Simpler Equation

But S2 = S1, so let's find how many solutions v(x) solve the equation of S2 to get equation874.

Theorem. equation876. iff equation878. where

equation880.

equation882.

and equation884. and equation886. equation888.

Proof. equation890. and equation892. But in a field of characteristic p, we have the polynomial identity, equation894. and so we have equation896. Comparing equations, we have equation898. iff equation900. or equation902.

Number of Matrix Solutions

The number of elements in the set equation904. is the number of solutions v to matrix equation, equation906. or equivalently equation908. 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 equation910. Now we finally find r which is then equation912. = # 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.

Appendix D - Finding All Primtive Polynomials

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).

History

Original document written by Sean Erik O'Connor.

Acknowledgements

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 28 Jan 14.