Finite Fields Package
A field is an algebraic structure obeying the rules of ordinary arithmetic. In particular, a field has binary operations of addition and multiplication, both of which are commutative and associative. A field has two special elements, the additive identity 0, and multiplicative identity 1. This package adds rules to
Plus,
Times, and
Power so that arithmetic on field elements will be defined properly. It also provides lowlevel utilities for working with finite fields and for formatting finite field elements.
For any finite field there is a fixed prime number
p, called the characteristic of the field, such that the sum of
p of the 1 field elements gives the 0 field element. A finite field of characteristic
p has
p^{d} elements for some positive integer
d, which is loosely called the extension degree. This package represents field elements as polynomials in a single variable. The polynomials have degree at most
d1 and their coefficients are integers reduced mod
p.
The internal form of a finite field element is
GF[p, ilist][elist] where
GF stands for
Galois field,
p is the prime characteristic of the field,
ilist is the coefficient list of the irreducible polynomial which defines multiplication in the field, and
elist is the coefficient list of the polynomial representing the particular element. Because this form can be a bit clumsy, the package provides several shortcuts for entering finite field elements.
GF[p][{k}]  the integer k in the field of integers mod p 
GF[p,1][{k}]  another way of expressing the integer k in the field of integers mod p 
GF[p,{0,1}][{k}]  the full form of the integer k in the field of integers mod p 
GF[p,d]  a field that is a degree d extension of a prime field isomorphic to Z_{p} (the integers mod p); an irreducible polynomial is selected automatically 
GF[p,ilist][elist]  the full form of a finite field element, where ilist is a list of integers of length d+1 representing the coefficients of an irreducible polynomial, and elist is a list of integers of length d giving the coefficients of the polynomial representation of the element 
Ways to enter finite field elements.
Add 4 and 5 mod 7. The default output form is subscripted.
Out[2]=  

Here is multiplication in the field of 3^481 elements.
Out[4]=  

Field objects are automatically reduced to a canonical form.
Out[5]=  

Since Mathematica lists polynomials with the constant term first, the same is done for finite field elements. In this example, the lefthand zero is essential to give the constant term, but the righthand zero in the input is unnecessary.
Out[6]=  

It is nonsense to add elements of different finite fields, so this expression does not evaluate.
Out[7]=  

Integers are imported into finite fields.
Out[8]=  

Zero is special. This package makes the assumption that zero is zero, regardless of the field it is in. The main consequence of this is that a field zero is simplified to the integer 0 automatically. Many functions in
Mathematica work better with this assumption. The assumption is not rigorously correct, but will not produce nonsensical output unless nonsensical input is given.
Field zeros are simplified to the integer 0 after arithmetic.
Out[9]=  

The sum of the field zeros from different fields also simplifies to 0.
Out[10]=  

Field element formatting.
You may always input or output the
FullForm of a finite field element, however the package provides some shortcuts. The elements of a prime field may be written
GF[p][{k}] or
GF[p, 1][{k}], both of which are interpreted as
GF[p, {0, 1}][{k}]. In general if
sp^{d} with
p prime and
d a positive integer, then
GF[s] is interpreted as
GF[p, d]. The expression
GF[3, 4] stands for a degree 4 extension of the integers mod 3, that is, a field with 81 elements. When this form is used, the package automatically selects an irreducible polynomial, with coefficients given by
ilist and with the property that
GF[p, ilist][{0, 1}] is a
primitive element of the field. It is possible to provide your own irreducible polynomial by giving it explicitly, as in
GF[3, {1, 1, 1, 1, 2, 1}].
The default
OutputForm of a finite field element is a list of integers subscripted by the characteristic of the field. The length of the list is the degree of the field extension over the prime field. If you are working with only one representation of any field, then this will be sufficient to distinguish which field contains a given element.
Since it is possible to have fields of the same size using different irreducible polynomials, it is useful to be able to distinguish elements from these fields. It is also convenient to have a more compact way to type finite field elements. The function
SetFieldFormat establishes the input and output formats of field elements. You may always input field elements in the ways already described, or ask explicitly for the
FullForm of the output.
Note that a field head, such as
GF[2, 3] may be assigned to a symbol. The assignment
f8=GF[2, 3] allows you to refer to a specific field element using
f8[{1, 0, 1}].
Give a field with 9 elements a functional format. 
Here is some arithmetic with elements from this field.
Out[12]=  

Represent each element as an integer in the range 0 to 8, rather than as a sequence of polynomial coefficients. 
Here the arithmetic results are encoded.
Out[14]=  

This restores the default formatting for this field. 
Characteristic[f]  give the characteristic of field f 
ExtensionDegree[f]  give the degree of the extension of field f over its base field 
FieldSize[f]  give the number of elements in field f 
FieldIrreducible[f,s]  give the irreducible polynomial which defines multiplication in the field f, expressed in terms of the symbol s 
IrreduciblePolynomial[s,p,d]  find an irreducible polynomial of degree d over the integers mod prime p, expressed in terms of the symbol s 
Field parameter functions.
A field extension is a vector space over the base field. ExtensionDegree gives the dimension of this vector space.
Out[17]=  

This gives the irreducible polynomial associated with a field.
Out[20]=  

Successor[elem]  give the next element in a canonical ordering of the field elements (this function does not wrap, so the largest element has no successor) 
ReduceElement[elem]  give a field element in reduced form 
ToElementCode[elem]  give a nonnegative integer less than the field size that encodes the field element 
FromElementCode[f,code]  give the field element of f corresponding to code, a nonnegative integer less than the field size 
PolynomialToElement[f,poly]  give the field element of f corresponding to poly, a polynomial in one variable with integervalued coefficients 
ElementToPolynomial[elem,s]  give a polynomial in the symbol s corresponding to the field element elem 
Field element manipulation functions.
This gives the element as a polynomial.
Out[21]=  

PowerListQ[f]  give True if a list representing the powers of a primitive element of the field is used to do field arithmetic, False otherwise 
FieldExp[f,n]  give the value of the discrete exponential function associated with the field f for integer n 
FieldInd[elem]  give the value of the discrete logarithm function associated with the field for element elem 
PowerList[f]  give a list of the data parts of the nonzero elements of the field f, generated by raising a primitive element of the field to integer powers 0, 1, 2, ..., FieldSize[f]1 
PowerListToField[list]  give the field associated with the specified list of element data parts, where the elements are generated by successive powers of a primitive element 
Functions to support fast multiplication and division.
A finite field must have a prime power number of elements. If it has
p elements, where
p is a prime, then it is isomorphic to the integers mod
p. In this case the package does addition, subtraction, multiplication, and positive powers as usual over the integers and reduces the results using
Mod. For negative powers the package uses
PowerMod.
If the field has
p^{d} elements, where
d>1, then it is isomorphic to the set of polynomials in some variable having degree less than
d and with coefficients in the integers mod
p. Addition is polynomial addition except that the coefficients are reduced modulo
p. For multiplication the product is also reduced modulo an irreducible (nonfactorable) polynomial over the integers modulo
p.
Taking multiplicative inverses is equivalent to finding the extended greatest common divisor modulo
p with the irreducible polynomial. In other words, given the field element
elem and the field's irreducible polynomial
irred, find polynomials
a and
b such that
PolynomialGCD[elem, irred, Modulus>p] equals
PolynomialMod[a elem + b irred, p]. If the result of
PolynomialGCD is unity, then
a is the inverse of
elem.
There is a faster way to do multiplication and compute inverses, based on the fact that the multiplicative group of any finite field is cyclic. If you have a generator of the group along with a list of the positive powers of the generator in sequence, then multiplication and division can be reduced to adding and subtracting indices of the elements in the list.
The package supports this method of arithmetic by allowing you to either generate a field object from a list representing the powers of a primitive element (
PowerListToField), or generate a list representing the powers of a primitive element from a field object (
PowerList). Setting
PowerListQ to
True causes field arithmetic to use a list of primitive element powers, by defining the discrete exponential function
FieldExp and the discrete logarithm function
FieldInd. The
FieldInd function is often called the index function for the field. The list of powers will depend on the choice of primitive element and on which irreducible polynomial is used for the field representation. Any element of the field for which
FieldInd[elem] is relatively prime to
FieldSize[f]1 is also a primitive element of the field.
Field arithmetic using a list of primitive element powers has two disadvantages. It takes time to enter or compute the list, and the list can take considerable memory. In general, if you are doing only a few arithmetic operations, or if you are working in a large field, it is better not to create the list. Note that setting
PowerListQ to
False disables this method of doing arithmetic in a particular field, but it does not destroy the computed values of
FieldExp and
FieldInd.
This shows that 3 is a primitive element of the integers mod 7. The other primitive element is 5. Since GF[7, 1] is a prime field, it is inefficient to use a list of primitive element powers for multiplication.
Out[22]=  

Although using a list of primitive element powers is an inefficient way to do GF[7, 1] arithmetic, this enables the method anyway. 
These definitions for field index and field exponent are not part of the conventional definitions, but may be useful in some applications.
Out[26]=  

Some books contain tables equivalent to the list given by
PowerList. The function
PowerListToField allows such a list to be used to define a field. The argument of
PowerListToField is a list of
p^{d}1 dtuples of integers. The first
dtuple is assumed to give the coefficients of the polynomial representation of the
1 element of the field. This is used to determine whether the coefficients are listed constanttermfirst or constanttermlast. The second
dtuple is assumed to represent a primitive element of the field. The succeeding
dtuples are assumed to represent successive powers of the second element.
PowerListToField does some elementary error checking to verify that the input list is valid. If so,
PowerListToField computes parameters of the field such as the characteristic and the irreducible polynomial.
The field parameters are computed automatically.
Out[29]=  

FieldExp may be used to generate the list of elements corresponding to the original list.
Out[30]=  

This gives the original list consisting of the data parts of the elements.
Out[31]=  

Compatibility with Other Mathematica Functions
Most
Mathematica functions are not, indeed cannot be, designed to work over arbitrary fields. For instance, it is nonsensical to integrate an expression containing a finite field object. At best, such functions will not do anything when given such an expression.
You can generally expect these functions to treat the field object as an unknown symbol. Before using a function on an expression with finite field objects, it is wise to try some test cases first. The types of functions that might reasonably be expected to work are linear algebra and polynomial functions.
Set the format for GF[9]. 
Factor fails because it makes the assumption that it is working over the rationals.
Out[34]=  

Set the format for GF[3, 4]. 
PolynomialMod works on polynomials over the integers, not polynomials over a field. Also note that a polynomial over a field is not automatically sorted from lower power to higher power, because Mathematica does not treat GF objects as numbers.
Out[38]=  
