Copyright © 1986-2013 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
for
but
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
but
The Phi or Totient function is defined by
and, for n > 1,
(number of integers between 1 and n-1 relatively prime to n).
(Integers a and b are relatively prime (coprime) if the greatest
Euler's Totient with upper and lower bounds.
common divisor of a and b is 1).
We have
for prime p.
In general, if n has the prime factorization
then
Example:
= number of integers between 1 and 17 relatively prime
to 18 = # {1, 5, 7, 11, 13, 17}
We have the bounds,
where the lower bound holds except for the number,
n = 2 3 5 7 11 13 17 19 23.
For our example, this is 4.239 ≤ φ ( n ) ≤ 18.
A more loose lower bound is φ ( n ) ≤ √ n for n ≥ 6.
If p is a prime and p does not divide a, then
and for any integer a,
For example,
Any finite field has
elements where n is 1 or greater and p is
prime.
All fields with the same number of elements are isomorphic.
A field with
elements is called Galois Field
The Galois field
is a quotient ring
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
is the set of polynomials
with addition and multiplication of
polynomials mod
and mod 3.
forms a cyclic group of order
under multiplication with the elements,
where the field element a is called a
generator of the group (or primitive element of the field).
Multiplication is defined by
For consistency, we define the zero
element of the field as
Zero is not a primitive element.
Example: We show that
is a primitive element of the field
by multiplying out powers of a.
The field element
is a primitive element iff
Thus, it's intuitively obvious there are
primitive elements.
In the example above,
has
primitive elements, and they are
Any field element a satisfies the equation,
and non-zero field elements satisfy,
Example:
Any polynomial
with coefficients in
obeys the identity
This implies, if
is any root of
in
then so are
Example:
is the smallest subfield of
It is the field of integers under modulo p
arithmetic.
is isomorphic to an element of
if it satisfies the equation,
For example, for a = 2x above,
The minimal polynomial
of a field element
in
is the unique polynomial of lowest degree which has
as a root and whose coefficients lie in
More precisely, the coefficients are allowed to be in
as long as they are isomorphic to elements of
Example:
is the minimal polynomial of
The conjugates of a field element
in
are the r elements,
where r <= n is the smallest integer for which
Example:
has r = 2 conjugates,
and
because
The conjugates of the field element
are exactly the roots of the minimal polynomial for
The minimal polynomial can then be written explicitly as,
Example: The minimal polynomial,
factors as
In the cyclic group
of order
the element
is a generator if and only if s and N are relatively prime.
That is why
were primitive elements of the field
There are
generators.
Suppose we are given a set S and m of its subsets,
Let
denote the number of elements in a set S.
Then we have the formula,
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
m = 9, and let
be the set of multiples of i+1 except itself.
Then
,
,
,
and
through
are empty.
and
All other intersections are empty as are all intersections
of 3 or more sets.
The inclusion and exclusion principle then gives us
which agrees with the direct calculation,
f(x) is a primitive polynomial iff the field element x generates the
cyclic group of non-zero field elements of the finite field.
where p is prime and
In particular, as a generator of the non-zero field elements, f(x) satisfies the equations
but
for
The probability that a random f(x) is a primitive polyomial is
which is bounded above and below by
Lower bound factor in parentheses.
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 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 (pn-1)/(p-1). For a primitive polynomial of degree 9 modulo 3, we test 2 conditions instead of 19,682: (39-1)/(3-1) = 13 * 757. Or for degree 10 modulo 2, we can test 3 conditions instead of 1023.
Step 0) Factor
into distinct primes:
Note that when r = (2n-1)/(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,
where we take care that all
possible combinations of coefficients are generated exactly once.
Step 2) Check that
is a primitive root of the prime p.
Use the quick test that a is a primitive root of prime p iff
for all prime
Of course,
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
When
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/2n and 1/n.
Step 5) Check if
for some integer a.
Most polynomials are rejected here.
Step 6) Check
Step 7) Check
for
But skip the test for
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 xn mod f(x), modulo 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]:=