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

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

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

This loads the package.
Click for copyable input
Add 4 and 5 mod 7. The default output form is subscripted.
Click for copyable input
Here is the FullForm of the expression. Note the automatic choice of irreducible polynomial.
Click for copyable input
Here is multiplication in the field of elements.
Click for copyable input
Field objects are automatically reduced to a canonical form.
Click for copyable input
Since the Wolfram Language 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.
Click for copyable input
It is nonsense to add elements of different finite fields, so this expression does not evaluate.
Click for copyable input
Integers are imported into finite fields.
Click for copyable input

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 the Wolfram Language 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 Null after arithmetic.
Click for copyable input
The sum of the field zeros from different fields also simplifies to Null.
Click for copyable input
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 Null with Null prime and Null 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.
Click for copyable input
Here is some arithmetic with elements from this field.
Click for copyable input
Represent each element as an integer in the range 0 to 8, rather than as a sequence of polynomial coefficients.
Click for copyable input
Here the arithmetic results are encoded.
Click for copyable input
This restores the default formatting for this field.
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.
Click for copyable input
A field extension is a vector space over the base field. ExtensionDegree gives the dimension of this vector space.
Click for copyable input
FieldSize gives the number of elements in the field.
Click for copyable input
Here is an identity.
Click for copyable input
This gives the irreducible polynomial associated with a field.
Click for copyable input
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 integervalued 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.
Click for copyable input
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 Null elements, where Null is a prime, then it is isomorphic to the integers mod Null. 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 Null elements, where Null, then it is isomorphic to the set of polynomials in some variable having degree less than Null and with coefficients in the integers mod Null. Addition is polynomial addition except that the coefficients are reduced modulo Null. For multiplication the product is also reduced modulo an irreducible (nonfactorable) polynomial over the integers modulo Null.

Taking multiplicative inverses is equivalent to finding the extended greatest common divisor modulo Null with the irreducible polynomial. In other words, given the field element elem and the field's irreducible polynomial irred, find polynomials Null and Null such that PolynomialGCD[elem,irred,Modulus->p] equals PolynomialMod[a elem + b irred,p]. If the result of PolynomialGCD is unity, then Null 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.
Click for copyable input
Although using a list of primitive element powers is an inefficient way to do GF[7,1] arithmetic, this enables the method anyway.
Click for copyable input
The position of {6} in PowerList is one greater than the index given by FieldInd.
Click for copyable input
FieldExp and FieldInd are inverses.
Click for copyable input
These definitions for field index and field exponent are not part of the conventional definitions, but may be useful in some applications.
Click for copyable input

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 Null Nulltuples of integers. The first Nulltuple is assumed to give the coefficients of the polynomial representation of the Null element of the field. This is used to determine whether the coefficients are listed constanttermfirst or constanttermlast. The second Nulltuple is assumed to represent a primitive element of the field. The succeeding Nulltuples 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.
Click for copyable input
Arithmetic works on fields defined using PowerListToField.
Click for copyable input
The field parameters are computed automatically.
Click for copyable input
FieldExp may be used to generate the list of elements corresponding to the original list.
Click for copyable input
This gives the original list consisting of the data parts of the elements.
Click for copyable input

Compatibility with Other Wolfram Language Functions

Most Wolfram Language 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].
Click for copyable input
Fractional powers do not work.
Click for copyable input
Factor fails because it makes the assumption that it is working over the rationals.
Click for copyable input
PolynomialGCD does not work either. Use PolynomialExtendedGCD defined in the package instead.
Click for copyable input
Set the format for GF[3,4].
Click for copyable input
PolynomialQuotient and PolynomialRemainder accept polynomials over a field.
Click for copyable input
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 the Wolfram Language does not treat GF objects as numbers.
Click for copyable input
Det works on matrices of field elements.
Click for copyable input
RowReduce works except that the 1 should be in the field.
Click for copyable input
Inverse and Dot also work with matrices of field elements.
Click for copyable input