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 the 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 low-level utilities for working with finite fields and for formatting finite field elements.

For any finite field there is a fixed prime number , called the characteristic of the field, such that the sum of of the 1 field elements gives the 0 field element. A finite field of characteristic has elements for some positive integer , which is loosely called the extension degree. This package represents field elements as polynomials in a single variable. The polynomials have degree at most and their coefficients are integers reduced mod .

The internal form of a finite field element is GF[p, ilist][elist] where GF stands for Galois field, 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 (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 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.

This loads the package.
In[1]:=
Click for copyable input
Add 4 and 5 mod 7. The default output form is subscripted.
In[2]:=
Click for copyable input
Out[2]=
Here is the FullForm of the expression. Note the automatic choice of irreducible polynomial.
In[3]:=
Click for copyable input
Out[3]//FullForm=
Here is multiplication in the field of elements.
In[4]:=
Click for copyable input
Out[4]=
Field objects are automatically reduced to a canonical form.
In[5]:=
Click for copyable input
Out[5]=
Since Mathematica lists polynomials with the constant term first, the same is done for finite field elements. In this example, the left-hand zero is essential to give the constant term, but the right-hand zero in the input is unnecessary.
In[6]:=
Click for copyable input
Out[6]=
It is nonsense to add elements of different finite fields, so this expression does not evaluate.
In[7]:=
Click for copyable input
Out[7]=
Integers are imported into finite fields.
In[8]:=
Click for copyable input
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 after arithmetic.
In[9]:=
Click for copyable input
Out[9]=
The sum of the field zeros from different fields also simplifies to .
In[10]:=
Click for copyable input
Out[10]=
SetFieldFormat[f,FormatType->Subscripted]set the field f to have a Subscripted output (default)
SetFieldFormat[f,FormatType->FullForm]set the field f to have a FullForm output
SetFieldFormat[f,FormatType->FunctionOfCoefficients[g]]set the field f to input and output a field element using g as the function name, with the arguments given by the coefficients of the polynomial representation of the field element
SetFieldFormat[f,FormatType->FunctionOfCode[g]]set the field f to input and output a field element using g as the function name, with the argument given by the integer code specifying the field element

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 with prime and 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 .

Give a field with 9 elements a functional format.
In[11]:=
Click for copyable input
Here is some arithmetic with elements from this field.
In[12]:=
Click for copyable input
Out[12]=
Represent each element as an integer in the range 0 to 8, rather than as a sequence of polynomial coefficients.
In[13]:=
Click for copyable input
Here the arithmetic results are encoded.
In[14]:=
Click for copyable input
Out[14]=
This restores the default formatting for this field.
In[15]:=
Click for copyable input
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.

The functions Characteristic, ExtensionDegree, FieldSize, FieldIrreducible, and IrreduciblePolynomial give important parameters about a finite field.

Characteristic gives the prime characteristic of the field.
In[16]:=
Click for copyable input
Out[16]=
A field extension is a vector space over the base field. ExtensionDegree gives the dimension of this vector space.
In[17]:=
Click for copyable input
Out[17]=
FieldSize gives the number of elements in the field.
In[18]:=
Click for copyable input
Out[18]=
Here is an identity.
In[19]:=
Click for copyable input
Out[19]=
This gives the irreducible polynomial associated with a field.
In[20]:=
Click for copyable input
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 non-negative integer less than the field size that encodes the field element
FromElementCode[f,code]give the field element of f corresponding to code, a non-negative integer less than the field size
PolynomialToElement[f,poly]give the field element of f corresponding to poly, a polynomial in one variable with integer-valued coefficients
ElementToPolynomial[elem,s]give a polynomial in the symbol s corresponding to the field element elem

Field element manipulation functions.

The functions Successor, ReduceElement, ToElementCode, FromElementCode, PolynomialToElement, and ElementToPolynomial are for working with finite field elements.

This gives the element as a polynomial.
In[21]:=
Click for copyable input
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 elements, where is a prime, then it is isomorphic to the integers mod . 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 elements, where , then it is isomorphic to the set of polynomials in some variable having degree less than and with coefficients in the integers mod . Addition is polynomial addition except that the coefficients are reduced modulo . For multiplication the product is also reduced modulo an irreducible (nonfactorable) polynomial over the integers modulo .

Taking multiplicative inverses is equivalent to finding the extended greatest common divisor modulo with the irreducible polynomial. In other words, given the field element elem and the field's irreducible polynomial irred, find polynomials and such that PolynomialGCD[elem, irred, Modulus->p] equals PolynomialMod[a elem + b irred, p]. If the result of PolynomialGCD is unity, then 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.
In[22]:=
Click for copyable input
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.
In[23]:=
Click for copyable input
The position of in PowerList is one greater than the index given by FieldInd.
In[24]:=
Click for copyable input
Out[24]=
FieldExp and FieldInd are inverses.
In[25]:=
Click for copyable input
Out[25]=
These definitions for field index and field exponent are not part of the conventional definitions, but may be useful in some applications.
In[26]:=
Click for copyable input
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 -tuples of integers. The first -tuple is assumed to give the coefficients of the polynomial representation of the element of the field. This is used to determine whether the coefficients are listed constant-term-first or constant-term-last. The second -tuple is assumed to represent a primitive element of the field. The succeeding -tuples 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.

Here is a field of order 9.
In[27]:=
Click for copyable input
Out[27]=
Arithmetic works on fields defined using PowerListToField.
In[28]:=
Click for copyable input
Out[28]=
The field parameters are computed automatically.
In[29]:=
Click for copyable input
Out[29]=
FieldExp may be used to generate the list of elements corresponding to the original list.
In[30]:=
Click for copyable input
Out[30]=
This gives the original list consisting of the data parts of the elements.
In[31]:=
Click for copyable input
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].
In[32]:=
Click for copyable input
Fractional powers do not work.
In[33]:=
Click for copyable input
Out[33]=
Factor fails because it makes the assumption that it is working over the rationals.
In[34]:=
Click for copyable input
Out[34]=
PolynomialGCD does not work either. Use PolynomialExtendedGCD defined in the package instead.
In[35]:=
Click for copyable input
Out[35]=
Set the format for GF[3, 4].
In[36]:=
Click for copyable input
PolynomialQuotient and PolynomialRemainder accept polynomials over a field.
In[37]:=
Click for copyable input
Out[37]=
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.
In[38]:=
Click for copyable input
Out[38]=
Det works on matrices of field elements.
In[39]:=
Click for copyable input
Out[39]=
RowReduce works except that the 1 should be in the field.
In[40]:=
Click for copyable input
Out[40]=
Inverse and Dot also work with matrices of field elements.
In[41]:=
Click for copyable input
Out[41]=
New to Mathematica? Find your learning path »
Have a question? Ask support »