Base Classes for Finite Fields¶
AUTHORS:
 Adrien Brochard, David Roe, Jeroen Demeyer, Julian Rueth, Niles Johnson, Peter Bruin, Travis Scrimshaw, Xavier Caruso: initial version

class
sage.rings.finite_rings.finite_field_base.
FiniteField
¶ Bases:
sage.rings.ring.Field
Abstract base class for finite fields.

algebraic_closure
(name='z', **kwds)¶ Return an algebraic closure of
self
.INPUT:
name
– string (default: ‘z’): prefix to use for variable names of subfieldsimplementation
– string (optional): specifies how to construct the algebraic closure. The only value supported at the moment is'pseudo_conway'
. For more details, seealgebraic_closure_finite_field
.
OUTPUT:
An algebraic closure of
self
. Note that mathematically speaking, this is only unique up to nonunique isomorphism. To obtain canonically defined algebraic closures, one needs an algorithm that also provides a canonical isomorphism between any two algebraic closures constructed using the algorithm.This nonuniqueness problem can in principle be solved by using Conway polynomials; see for example [CP]. These have the drawback that computing them takes a long time. Therefore Sage implements a variant called pseudoConway polynomials, which are easier to compute but do not determine an algebraic closure up to unique isomorphism.
The output of this method is cached, so that within the same Sage session, calling it multiple times will return the same algebraic closure (i.e. the same Sage object). Despite this, the nonuniqueness of the current implementation means that coercion and pickling cannot work as one might expect. See below for an example.
EXAMPLES:
sage: F = GF(5).algebraic_closure() sage: F Algebraic closure of Finite Field of size 5 sage: F.gen(3) z3
The default name is ‘z’ but you can change it through the option
name
:sage: Ft = GF(5).algebraic_closure('t') sage: Ft.gen(3) t3
Because Sage currently only implements algebraic closures using a nonunique definition (see above), it is currently impossible to implement pickling in such a way that a pickled and unpickled element compares equal to the original:
sage: F = GF(7).algebraic_closure() sage: x = F.gen(2) sage: loads(dumps(x)) == x False
Note
This is currently only implemented for prime fields.
REFERENCE:
[CP] Wikipedia entry on Conway polynomials, Wikipedia article Conway_polynomial_(finite_fields)

cardinality
()¶ Return the cardinality of
self
.Same as
order()
.EXAMPLES:
sage: GF(997).cardinality() 997

construction
()¶ Return the construction of this finite field, as a
ConstructionFunctor
and the base field.EXAMPLES:
sage: v = GF(3^3).construction(); v (AlgebraicExtensionFunctor, Finite Field of size 3) sage: v[0].polys[0] 3 sage: v = GF(2^1000, 'a').construction(); v[0].polys[0] a^1000 + a^5 + a^4 + a^3 + 1

dual_basis
(basis=None, check=True)¶ Return the dual basis of
basis
, or the dual basis of the power basis if no basis is supplied.If \(e = \{e_0, e_1, ..., e_{n1}\}\) is a basis of \(\GF{p^n}\) as a vector space over \(\GF{p}\), then the dual basis of \(e\), \(d = \{d_0, d_1, ..., d_{n1}\}\), is the unique basis such that \(\mathrm{Tr}(e_i d_j) = \delta_{i,j}, 0 \leq i,j \leq n1\), where \(\mathrm{Tr}\) is the trace function.
INPUT:
basis
– (default:None
): a basis of the finite fieldself
, \(\GF{p^n}\), as a vector space over the base field \(\GF{p}\). Uses the power basis \(\{x^i : 0 \leq i \leq n1\}\) as input if no basis is supplied, where \(x\) is the generator ofself
.check
– (default:True
): verifies thatbasis
is a valid basis ofself
.
ALGORITHM:
The algorithm used to calculate the dual basis comes from pages 110–111 of [FFCSE1987].
Let \(e = \{e_0, e_1, ..., e_{n1}\}\) be a basis of \(\GF{p^n}\) as a vector space over \(\GF{p}\) and \(d = \{d_0, d_1, ..., d_{n1}\}\) be the dual basis of \(e\). Since \(e\) is a basis, we can rewrite any \(d_c, 0 \leq c \leq n1\), as \(d_c = \beta_0 e_0 + \beta_1 e_1 + ... + \beta_{n1} e_{n1}\), for some \(\beta_0, \beta_1, ..., \beta_{n1} \in \GF{p}\). Using properties of the trace function, we can rewrite the \(n\) equations of the form \(\mathrm{Tr}(e_i d_c) = \delta_{i,c}\) and express the result as the matrix vector product: \(A [\beta_0, \beta_1, ..., \beta_{n1}] = i_c\), where the \(i,j\)th element of \(A\) is \(\mathrm{Tr(e_i e_j)}\) and \(i_c\) is the \(i\)th column of the \(n \times n\) identity matrix. Since \(A\) is an invertible matrix, \([\beta_0, \beta_1, ..., \beta_{n1}] = A^{1} i_c\), from which we can easily calculate \(d_c\).
EXAMPLES:
sage: F.<a> = GF(2^4) sage: F.dual_basis(basis=None, check=False) [a^3 + 1, a^2, a, 1]
We can test that the dual basis returned satisfies the defining property of a dual basis: \(\mathrm{Tr}(e_i d_j) = \delta_{i,j}, 0 \leq i,j \leq n1\)
sage: F.<a> = GF(7^4) sage: e = [4*a^3, 2*a^3 + a^2 + 3*a + 5, ....: 3*a^3 + 5*a^2 + 4*a + 2, 2*a^3 + 2*a^2 + 2] sage: d = F.dual_basis(e, check=True); d [3*a^3 + 4*a^2 + 6*a + 2, a^3 + 6*a + 5, 3*a^3 + 6*a^2 + 2*a + 5, 5*a^2 + 4*a + 3] sage: vals = [[(x * y).trace() for x in e] for y in d] sage: matrix(vals) == matrix.identity(4) True
We can test that if \(d\) is the dual basis of \(e\), then \(e\) is the dual basis of \(d\):
sage: F.<a> = GF(7^8) sage: e = [a^0, a^1, a^2, a^3, a^4, a^5, a^6, a^7] sage: d = F.dual_basis(e, check=False); d [6*a^6 + 4*a^5 + 4*a^4 + a^3 + 6*a^2 + 3, 6*a^7 + 4*a^6 + 4*a^5 + 2*a^4 + a^2, 4*a^6 + 5*a^5 + 5*a^4 + 4*a^3 + 5*a^2 + a + 6, 5*a^7 + a^6 + a^4 + 4*a^3 + 4*a^2 + 1, 2*a^7 + 5*a^6 + a^5 + a^3 + 5*a^2 + 2*a + 4, a^7 + 2*a^6 + 5*a^5 + a^4 + 5*a^2 + 4*a + 4, a^7 + a^6 + 2*a^5 + 5*a^4 + a^3 + 4*a^2 + 4*a + 6, 5*a^7 + a^6 + a^5 + 2*a^4 + 5*a^3 + 6*a] sage: F.dual_basis(d) [1, a, a^2, a^3, a^4, a^5, a^6, a^7]
We cannot calculate the dual basis if
basis
is not a valid basis.sage: F.<a> = GF(2^3) sage: F.dual_basis([a], check=True) Traceback (most recent call last): ... ValueError: basis length should be 3, not 1 sage: F.dual_basis([a^0, a, a^0 + a], check=True) Traceback (most recent call last): ... ValueError: value of 'basis' keyword is not a basis
REFERENCES:
[FFCSE1987] Robert J. McEliece. Finite Fields for Computer Scientists and Engineers. Kluwer Academic Publishers, 1987. AUTHOR:
 Thomas Gagne (20150616)

extension
(modulus, name=None, names=None, map=False, embedding=None, **kwds)¶ Return an extension of this finite field.
INPUT:
modulus
– a polynomial with coefficients inself
, or an integer.name
– string: the name of the generator in the new extensionmap
– boolean (default:False
): ifFalse
, return just the extension \(E\); ifTrue
, return a pair \((E, f)\), where \(f\) is an embedding ofself
into \(E\).embedding
– currently not used; for compatibility with otherAlgebraicExtensionFunctor
calls.**kwds
: further keywords, passed to the finite field constructor.
OUTPUT:
An extension of the given modulus, or pseudoConway of the given degree if
modulus
is an integer.EXAMPLES:
sage: k = GF(2) sage: R.<x> = k[] sage: k.extension(x^1000 + x^5 + x^4 + x^3 + 1, 'a') Finite Field in a of size 2^1000 sage: k = GF(3^4) sage: R.<x> = k[] sage: k.extension(3) Finite Field in z12 of size 3^12 sage: K = k.extension(2, 'a') sage: k.is_subring(K) True
An example using the
map
argument:sage: F = GF(5) sage: E, f = F.extension(2, 'b', map=True) sage: E Finite Field in b of size 5^2 sage: f Ring morphism: From: Finite Field of size 5 To: Finite Field in b of size 5^2 Defn: 1 > 1 sage: f.parent() Set of field embeddings from Finite Field of size 5 to Finite Field in b of size 5^2
Extensions of nonprime finite fields by polynomials are not yet supported: we fall back to generic code:
sage: k.extension(x^5 + x^2 + x  1) Univariate Quotient Polynomial Ring in x over Finite Field in z4 of size 3^4 with modulus x^5 + x^2 + x + 2

factored_order
()¶ Returns the factored order of this field. For compatibility with
integer_mod_ring
.EXAMPLES:
sage: GF(7^2,'a').factored_order() 7^2

factored_unit_order
()¶ Returns the factorization of
self.order()1
, as a 1tuple.The format is for compatibility with
integer_mod_ring
.EXAMPLES:
sage: GF(7^2,'a').factored_unit_order() (2^4 * 3,)

frobenius_endomorphism
(n=1)¶ INPUT:
n
– an integer (default: 1)
OUTPUT:
The \(n\)th power of the absolute arithmetic Frobenius endomorphism on this finite field.
EXAMPLES:
sage: k.<t> = GF(3^5) sage: Frob = k.frobenius_endomorphism(); Frob Frobenius endomorphism t > t^3 on Finite Field in t of size 3^5 sage: a = k.random_element() sage: Frob(a) == a^3 True
We can specify a power:
sage: k.frobenius_endomorphism(2) Frobenius endomorphism t > t^(3^2) on Finite Field in t of size 3^5
The result is simplified if possible:
sage: k.frobenius_endomorphism(6) Frobenius endomorphism t > t^3 on Finite Field in t of size 3^5 sage: k.frobenius_endomorphism(5) Identity endomorphism of Finite Field in t of size 3^5
Comparisons work:
sage: k.frobenius_endomorphism(6) == Frob True sage: from sage.categories.morphism import IdentityMorphism sage: k.frobenius_endomorphism(5) == IdentityMorphism(k) True
AUTHOR:
 Xavier Caruso (20120629)

gen
()¶ Return a generator of this field (over its prime field). As this is an abstract base class, this just raises a
NotImplementedError
.EXAMPLES:
sage: K = GF(17) sage: sage.rings.finite_rings.finite_field_base.FiniteField.gen(K) Traceback (most recent call last): ... NotImplementedError

is_conway
()¶ Return
True
if self is defined by a Conway polynomial.EXAMPLES:
sage: GF(5^3, 'a').is_conway() True sage: GF(5^3, 'a', modulus='adlemanlenstra').is_conway() False sage: GF(next_prime(2^16, 2), 'a').is_conway() False

is_field
(proof=True)¶ Returns whether or not the finite field is a field, i.e., always returns
True
.EXAMPLES:
sage: k.<a> = FiniteField(3^4) sage: k.is_field() True

is_perfect
()¶ Return whether this field is perfect, i.e., every element has a \(p\)th root. Always returns
True
since finite fields are perfect.EXAMPLES:
sage: GF(2).is_perfect() True

is_prime_field
()¶ Return
True
ifself
is a prime field, i.e., has degree 1.EXAMPLES:
sage: GF(3^7, 'a').is_prime_field() False sage: GF(3, 'a').is_prime_field() True

modulus
()¶ Return the minimal polynomial of the generator of
self
over the prime finite field.The minimal polynomial of an element \(a\) in a field is the unique monic irreducible polynomial of smallest degree with coefficients in the base field that has \(a\) as a root. In finite field extensions, \(\GF{p^n}\), the base field is \(\GF{p}\).
OUTPUT:
 a monic polynomial over \(\GF{p}\) in the variable \(x\).
EXAMPLES:
sage: F.<a> = GF(7^2); F Finite Field in a of size 7^2 sage: F.polynomial_ring() Univariate Polynomial Ring in a over Finite Field of size 7 sage: f = F.modulus(); f x^2 + 6*x + 3 sage: f(a) 0
Although \(f\) is irreducible over the base field, we can doublecheck whether or not \(f\) factors in \(F\) as follows. The command
F['x'](f)
coerces \(f\) as a polynomial with coefficients in \(F\). (Instead of a polynomial with coefficients over the base field.)sage: f.factor() x^2 + 6*x + 3 sage: F['x'](f).factor() (x + a + 6) * (x + 6*a)
Here is an example with a degree 3 extension:
sage: G.<b> = GF(7^3); G Finite Field in b of size 7^3 sage: g = G.modulus(); g x^3 + 6*x^2 + 4 sage: g.degree(); G.degree() 3 3
For prime fields, this returns \(x  1\) unless a custom modulus was given when constructing this field:
sage: k = GF(199) sage: k.modulus() x + 198 sage: var('x') x sage: k = GF(199, modulus=x+1) sage: k.modulus() x + 1
The given modulus is always made monic:
sage: k.<a> = GF(7^2, modulus=2*x^23, impl="pari_ffelt") sage: k.modulus() x^2 + 2

multiplicative_generator
()¶ Return a primitive element of this finite field, i.e. a generator of the multiplicative group.
You can use
multiplicative_generator()
orprimitive_element()
, these mean the same thing.Warning
This generator might change from one version of Sage to another.
EXAMPLES:
sage: k = GF(997) sage: k.multiplicative_generator() 7 sage: k.<a> = GF(11^3) sage: k.primitive_element() a sage: k.<b> = GF(19^32) sage: k.multiplicative_generator() b + 4

ngens
()¶ The number of generators of the finite field. Always 1.
EXAMPLES:
sage: k = FiniteField(3^4, 'b') sage: k.ngens() 1

order
()¶ Return the order of this finite field.
EXAMPLES:
sage: GF(997).order() 997

polynomial
(name=None)¶ Return the minimal polynomial of the generator of
self
over the prime finite field.INPUT:
name
– a variable name to use for the polynomial. By default, use the name given when constructing this field.
OUTPUT:
 a monic polynomial over \(\GF{p}\) in the variable
name
.
See also
Except for the
name
argument, this is identical to themodulus()
method.EXAMPLES:
sage: k.<a> = FiniteField(9) sage: k.polynomial('x') x^2 + 2*x + 2 sage: k.polynomial() a^2 + 2*a + 2 sage: F = FiniteField(9, 'a', impl='pari_ffelt') sage: F.polynomial() a^2 + 2*a + 2 sage: F = FiniteField(7^20, 'a', impl='pari_ffelt') sage: f = F.polynomial(); f a^20 + a^12 + 6*a^11 + 2*a^10 + 5*a^9 + 2*a^8 + 3*a^7 + a^6 + 3*a^5 + 3*a^3 + a + 3 sage: f(F.gen()) 0 sage: k.<a> = GF(2^20, impl='ntl') sage: k.polynomial() a^20 + a^10 + a^9 + a^7 + a^6 + a^5 + a^4 + a + 1 sage: k.polynomial('FOO') FOO^20 + FOO^10 + FOO^9 + FOO^7 + FOO^6 + FOO^5 + FOO^4 + FOO + 1 sage: a^20 a^10 + a^9 + a^7 + a^6 + a^5 + a^4 + a + 1

polynomial_ring
(variable_name=None)¶ Returns the polynomial ring over the prime subfield in the same variable as this finite field.
EXAMPLES:
sage: k.<alpha> = FiniteField(3^4) sage: k.polynomial_ring() Univariate Polynomial Ring in alpha over Finite Field of size 3

primitive_element
()¶ Return a primitive element of this finite field, i.e. a generator of the multiplicative group.
You can use
multiplicative_generator()
orprimitive_element()
, these mean the same thing.Warning
This generator might change from one version of Sage to another.
EXAMPLES:
sage: k = GF(997) sage: k.multiplicative_generator() 7 sage: k.<a> = GF(11^3) sage: k.primitive_element() a sage: k.<b> = GF(19^32) sage: k.multiplicative_generator() b + 4

random_element
(*args, **kwds)¶ A random element of the finite field. Passes arguments to
random_element()
function of underlying vector space.EXAMPLES:
sage: k = GF(19^4, 'a') sage: k.random_element() a^3 + 3*a^2 + 6*a + 9
Passes extra positional or keyword arguments through:
sage: k.random_element(prob=0) 0

some_elements
()¶ Returns a collection of elements of this finite field for use in unit testing.
EXAMPLES:
sage: k = GF(2^8,'a') sage: k.some_elements() # random output [a^4 + a^3 + 1, a^6 + a^4 + a^3, a^5 + a^4 + a, a^2 + a]

subfields
(degree=0, name=None)¶ Return all subfields of
self
of the givendegree
, or all possible degrees ifdegree
is \(0\).The subfields are returned as absolute fields together with an embedding into
self
.INPUT:
degree
– (default: \(0\)) an integername
– a string, a dictionary orNone
: If
degree
is nonzero, thenname
must be a string (orNone
, if this is a pseudoConway extension), and will be the variable name of the returned field.  If
degree
is zero, the dictionary should have keys the divisors of the degree of this field, with the desired variable name for the field of that degree as an entry.  As a shortcut, you can provide a string and the degree of each subfield will be appended for the variable name of that subfield.
 If
None
, uses the prefix of this field.
 If
OUTPUT:
A list of pairs
(K, e)
, whereK
ranges over the subfields of this field ande
gives an embedding ofK
intoself
.EXAMPLES:
sage: k = GF(2^21) sage: k.subfields() [(Finite Field of size 2, Ring morphism: From: Finite Field of size 2 To: Finite Field in z21 of size 2^21 Defn: 1 > 1), (Finite Field in z3 of size 2^3, Ring morphism: From: Finite Field in z3 of size 2^3 To: Finite Field in z21 of size 2^21 Defn: z3 > z21^20 + z21^19 + z21^17 + z21^15 + z21^11 + z21^9 + z21^8 + z21^6 + z21^2), (Finite Field in z7 of size 2^7, Ring morphism: From: Finite Field in z7 of size 2^7 To: Finite Field in z21 of size 2^21 Defn: z7 > z21^20 + z21^19 + z21^17 + z21^15 + z21^14 + z21^6 + z21^4 + z21^3 + z21), (Finite Field in z21 of size 2^21, Ring morphism: From: Finite Field in z21 of size 2^21 To: Finite Field in z21 of size 2^21 Defn: z21 > z21)]

unit_group_exponent
()¶ The exponent of the unit group of the finite field. For a finite field, this is always the order minus 1.
EXAMPLES:
sage: k = GF(2^10, 'a') sage: k.order() 1024 sage: k.unit_group_exponent() 1023

vector_space
(subfield=None, basis=None, map=False)¶ Return the vector space over the subfield isomorphic to this finite field as a vector space, along with the isomorphisms.
INPUT:
subfield
– a subfield of or a morphism into this finite field. If not given, the prime subfield is assumed. A subfield means a finite field with coercion to this finite field.basis
– a basis of the finite field as a vector space over the subfield. If not given, one is chosen automatically.map
– boolean (default:False
); ifTrue
, isomorphisms from and to the vector space are also returned.
The
basis
maps to the standard basis of the vector space by the isomorphisms.OUTPUT: if
map
isFalse
, vector space over the subfield or the domain of the morphism, isomorphic to this finite field.
and if
map
isTrue
, then also an isomorphism from the vector space to the finite field.
 the inverse isomorphism to the vector space from the finite field.
EXAMPLES:
sage: GF(27,'a').vector_space() Vector space of dimension 3 over Finite Field of size 3 sage: F = GF(8) sage: E = GF(64) sage: V, from_V, to_V = E.vector_space(F, map=True) sage: V Vector space of dimension 2 over Finite Field in z3 of size 2^3 sage: to_V(E.gen()) (0, 1) sage: all(from_V(to_V(e)) == e for e in E) True sage: all(to_V(e1 + e2) == to_V(e1) + to_V(e2) for e1 in E for e2 in E) True sage: all(to_V(c * e) == c * to_V(e) for e in E for c in F) True sage: basis = [E.gen(), E.gen() + 1] sage: W, from_W, to_W = E.vector_space(F, basis, map=True) sage: all(from_W(to_W(e)) == e for e in E) True sage: all(to_W(c * e) == c * to_W(e) for e in E for c in F) True sage: all(to_W(e1 + e2) == to_W(e1) + to_W(e2) for e1 in E for e2 in E) # long time True sage: to_W(basis[0]); to_W(basis[1]) (1, 0) (0, 1) sage: F = GF(9, 't', modulus=(x^2+x1)) sage: E = GF(81) sage: h = Hom(F,E).an_element() sage: V, from_V, to_V = E.vector_space(h, map=True) sage: V Vector space of dimension 2 over Finite Field in t of size 3^2 sage: V.base_ring() is F True sage: all(from_V(to_V(e)) == e for e in E) True sage: all(to_V(e1 + e2) == to_V(e1) + to_V(e2) for e1 in E for e2 in E) True sage: all(to_V(h(c) * e) == c * to_V(e) for e in E for c in F) True

zeta
(n=None)¶ Return an element of multiplicative order
n
in this finite field. If there is no such element, raiseValueError
.Warning
In general, this returns an arbitrary element of the correct order. There are no compatibility guarantees:
F.zeta(9)^3
may not be equal toF.zeta(3)
.EXAMPLES:
sage: k = GF(7) sage: k.zeta() 3 sage: k.zeta().multiplicative_order() 6 sage: k.zeta(3) 2 sage: k.zeta(3).multiplicative_order() 3 sage: k = GF(49, 'a') sage: k.zeta().multiplicative_order() 48 sage: k.zeta(6) 3 sage: k.zeta(5) Traceback (most recent call last): ... ValueError: no 5th root of unity in Finite Field in a of size 7^2
Even more examples:
sage: GF(9,'a').zeta_order() 8 sage: GF(9,'a').zeta() a sage: GF(9,'a').zeta(4) a + 1 sage: GF(9,'a').zeta()^2 a + 1
This works even in very large finite fields, provided that
n
can be factored (see trac ticket #25203):sage: k.<a> = GF(2^2000) sage: p = 8877945148742945001146041439025147034098690503591013177336356694416517527310181938001 sage: z = k.zeta(p) sage: z a^1999 + a^1996 + a^1995 + a^1994 + ... + a^7 + a^5 + a^4 + 1 sage: z ^ p 1

zeta_order
()¶ Return the order of the distinguished root of unity in
self
.EXAMPLES:
sage: GF(9,'a').zeta_order() 8 sage: GF(9,'a').zeta() a sage: GF(9,'a').zeta().multiplicative_order() 8


sage.rings.finite_rings.finite_field_base.
is_FiniteField
(x)¶ Return
True
ifx
is of type finite field, andFalse
otherwise.EXAMPLES:
sage: from sage.rings.finite_rings.finite_field_base import is_FiniteField sage: is_FiniteField(GF(9,'a')) True sage: is_FiniteField(GF(next_prime(10^10))) True
Note that the integers modulo n are not of type finite field, so this function returns
False
:sage: is_FiniteField(Integers(7)) False

sage.rings.finite_rings.finite_field_base.
unpickle_FiniteField_ext
(_type, order, variable_name, modulus, kwargs)¶ Used to unpickle extensions of finite fields. Now superseded (hence no doctest), but kept around for backward compatibility.

sage.rings.finite_rings.finite_field_base.
unpickle_FiniteField_prm
(_type, order, variable_name, kwargs)¶ Used to unpickle finite prime fields. Now superseded (hence no doctest), but kept around for backward compatibility.