Base class for finite field elements - Finite Rings

Skip to content
Back to top
View this page
Edit this page

# Base class for finite field elements

AUTHORS:

- David Roe (2010-01-14): factored out of sage.structure.element

- Sebastian Oehms (2018-07-19): added `conjugate()` (see Issue #26761)

class sage.rings.finite_rings.element_base.Cache_base

  Bases: `SageObject`

  fetch_int(number)

    Given an integer less than \(p^n\) with base \(2\) representation \(a_0 + a_1 \cdot 2 + \cdots + a_k 2^k\), this returns \(a_0 + a_1 x + \cdots + a_k x^k\), where \(x\) is the generator of this finite field.

    EXAMPLES:

    Sage

        sage: k.<a> = GF(2^48)
        sage: k._cache.fetch_int(2^33 + 2 + 1)                                      # needs sage.libs.ntl
        a^33 + a + 1

    Python

        >>> from sage.all import *
        >>> k = GF(Integer(2)**Integer(48), names=('a',)); (a,) = k._first_ngens(1)
        >>> k._cache.fetch_int(Integer(2)**Integer(33) + Integer(2) + Integer(1))                                      # needs sage.libs.ntl
        a^33 + a + 1

class sage.rings.finite_rings.element_base.FinitePolyExtElement

  Bases: `FiniteRingElement`

  Elements represented as polynomials modulo a given ideal.

  additive_order()

    Return the additive order of this finite field element.

    EXAMPLES:

    Sage

        sage: k.<a> = FiniteField(2^12, 'a')
        sage: b = a^3 + a + 1
        sage: b.additive_order()
        2
        sage: k(0).additive_order()
        1

    Python

        >>> from sage.all import *
        >>> k = FiniteField(Integer(2)**Integer(12), 'a', names=('a',)); (a,) = k._first_ngens(1)
        >>> b = a**Integer(3) + a + Integer(1)
        >>> b.additive_order()
        2
        >>> k(Integer(0)).additive_order()
        1

  charpoly(var='x', algorithm='pari')

    Return the characteristic polynomial of `self` as a polynomial with given variable.

    INPUT:

    - `var` – string (default: `'x'`)

    - `algorithm` – string (default: `'pari'`):

      - `'pari'` – use pari’s charpoly

      - `'matrix'` – return the charpoly computed from the matrix of left multiplication by `self`

    The result is not cached.

    EXAMPLES:

    Sage

        sage: from sage.rings.finite_rings.element_base import FinitePolyExtElement
        sage: k.<a> = FiniteField(19^2)
        sage: parent(a)
        Finite Field in a of size 19^2
        sage: b = a**20
        sage: p = FinitePolyExtElement.charpoly(b, "x", algorithm='pari')
        sage: q = FinitePolyExtElement.charpoly(b, "x", algorithm='matrix')         # needs sage.modules
        sage: q == p                                                                # needs sage.modules
        True
        sage: p
        x^2 + 15*x + 4
        sage: factor(p)
        (x + 17)^2
        sage: b.minpoly('x')
        x + 17

    Python

        >>> from sage.all import *
        >>> from sage.rings.finite_rings.element_base import FinitePolyExtElement
        >>> k = FiniteField(Integer(19)**Integer(2), names=('a',)); (a,) = k._first_ngens(1)
        >>> parent(a)
        Finite Field in a of size 19^2
        >>> b = a**Integer(20)
        >>> p = FinitePolyExtElement.charpoly(b, "x", algorithm='pari')
        >>> q = FinitePolyExtElement.charpoly(b, "x", algorithm='matrix')         # needs sage.modules
        >>> q == p                                                                # needs sage.modules
        True
        >>> p
        x^2 + 15*x + 4
        >>> factor(p)
        (x + 17)^2
        >>> b.minpoly('x')
        x + 17

  conjugate()

    This methods returns the result of the Frobenius morphism in the case where the field is a quadratic extension, say \(GF(q^2)\), where \(q=p^k\) is a prime power and \(p\) the characteristic of the field.

    OUTPUT:

    Instance of this class representing the image under the Frobenius morphisms.

    EXAMPLES:

    Sage

        sage: F.<a> = GF(16)
        sage: b = a.conjugate(); b
        a + 1
        sage: a == b.conjugate()
        True

        sage: F.<a> = GF(27)
        sage: a.conjugate()
        Traceback (most recent call last):
        ...
        TypeError: cardinality of the field must be a square number

    Python

        >>> from sage.all import *
        >>> F = GF(Integer(16), names=('a',)); (a,) = F._first_ngens(1)
        >>> b = a.conjugate(); b
        a + 1
        >>> a == b.conjugate()
        True

        >>> F = GF(Integer(27), names=('a',)); (a,) = F._first_ngens(1)
        >>> a.conjugate()
        Traceback (most recent call last):
        ...
        TypeError: cardinality of the field must be a square number

  frobenius(k=1)

    alias of `pth_power()`.

  is_square()

    Return `True` if and only if this element is a perfect square.

    EXAMPLES:

    Sage

        sage: k.<a> = FiniteField(9, implementation='givaro', modulus='primitive')            # needs sage.libs.linbox
        sage: a.is_square()                                                         # needs sage.libs.linbox
        False
        sage: (a**2).is_square()                                                    # needs sage.libs.linbox
        True
        sage: k.<a> = FiniteField(4, implementation='ntl', modulus='primitive')               # needs sage.libs.ntl
        sage: (a**2).is_square()                                                    # needs sage.libs.ntl
        True
        sage: k.<a> = FiniteField(17^5, implementation='pari_ffelt', modulus='primitive')     # needs sage.libs.pari
        sage: a.is_square()
        False
        sage: (a**2).is_square()                                                    # needs sage.libs.pari
        True

    Python

        >>> from sage.all import *
        >>> k = FiniteField(Integer(9), implementation='givaro', modulus='primitive', names=('a',)); (a,) = k._first_ngens(1)# needs sage.libs.linbox
        >>> a.is_square()                                                         # needs sage.libs.linbox
        False
        >>> (a**Integer(2)).is_square()                                                    # needs sage.libs.linbox
        True
        >>> k = FiniteField(Integer(4), implementation='ntl', modulus='primitive', names=('a',)); (a,) = k._first_ngens(1)# needs sage.libs.ntl
        >>> (a**Integer(2)).is_square()                                                    # needs sage.libs.ntl
        True
        >>> k = FiniteField(Integer(17)**Integer(5), implementation='pari_ffelt', modulus='primitive', names=('a',)); (a,) = k._first_ngens(1)# needs sage.libs.pari
        >>> a.is_square()
        False
        >>> (a**Integer(2)).is_square()                                                    # needs sage.libs.pari
        True

    Sage

        sage: k(0).is_square()                                                      # needs sage.libs.linbox
        True

    [Python]

        >>> from sage.all import *
        >>> k(Integer(0)).is_square()                                                      # needs sage.libs.linbox
        True

  list()

    Return the list of coefficients (in little-endian) of this finite field element when written as a polynomial in the generator.

    Equivalent to calling `list()` on this element.

    EXAMPLES:

    Sage

        sage: x = polygen(GF(71))
        sage: F.<u> = GF(71^7, modulus=x^7 + x + 1)
        sage: a = 3 + u + 3*u^2 + 3*u^3 + 7*u^4
        sage: a.list()
        [3, 1, 3, 3, 7, 0, 0]
        sage: a.list() == list(a) == [a[i] for i in range(F.degree())]
        True

    Python

        >>> from sage.all import *
        >>> x = polygen(GF(Integer(71)))
        >>> F = GF(Integer(71)**Integer(7), modulus=x**Integer(7) + x + Integer(1), names=('u',)); (u,) = F._first_ngens(1)
        >>> a = Integer(3) + u + Integer(3)*u**Integer(2) + Integer(3)*u**Integer(3) + Integer(7)*u**Integer(4)
        >>> a.list()
        [3, 1, 3, 3, 7, 0, 0]
        >>> a.list() == list(a) == [a[i] for i in range(F.degree())]
        True

    The coefficients returned are those of a fully reduced representative of the finite field element:

    Sage

        sage: b = u^777
        sage: b.list()
        [9, 69, 4, 27, 40, 10, 56]
        sage: (u.polynomial()^777).list()
        [0, 0, 0, 0, ..., 0, 1]

    [Python]

        >>> from sage.all import *
        >>> b = u**Integer(777)
        >>> b.list()
        [9, 69, 4, 27, 40, 10, 56]
        >>> (u.polynomial()**Integer(777)).list()
        [0, 0, 0, 0, ..., 0, 1]

  matrix(reverse=False)

    Return the matrix of left multiplication by the element on the power basis \(1, x, x^2, \ldots, x^{d-1}\) for the field extension.

    Thus the emph{columns} of this matrix give the images of each of the \(x^i\).

    INPUT:

    - `reverse` – if `True`, act on vectors in reversed order

    EXAMPLES:

    Sage

        sage: # needs sage.modules
        sage: k.<a> = GF(2^4)
        sage: b = k.random_element()
        sage: vector(a*b) == a.matrix() * vector(b)
        True
        sage: (a*b)._vector_(reverse=True) == a.matrix(reverse=True) * b._vector_(reverse=True)
        True

    Python

        >>> from sage.all import *
        >>> # needs sage.modules
        >>> k = GF(Integer(2)**Integer(4), names=('a',)); (a,) = k._first_ngens(1)
        >>> b = k.random_element()
        >>> vector(a*b) == a.matrix() * vector(b)
        True
        >>> (a*b)._vector_(reverse=True) == a.matrix(reverse=True) * b._vector_(reverse=True)
        True

  minimal_polynomial(var='x')

    Return the minimal polynomial of this element (over the corresponding prime subfield).

    EXAMPLES:

    Sage

        sage: k.<a> = FiniteField(3^4)
        sage: parent(a)
        Finite Field in a of size 3^4
        sage: b=a**20;p=charpoly(b,"y");p
        y^4 + 2*y^2 + 1
        sage: factor(p)
        (y^2 + 1)^2
        sage: b.minimal_polynomial('y')
        y^2 + 1

    Python

        >>> from sage.all import *
        >>> k = FiniteField(Integer(3)**Integer(4), names=('a',)); (a,) = k._first_ngens(1)
        >>> parent(a)
        Finite Field in a of size 3^4
        >>> b=a**Integer(20);p=charpoly(b,"y");p
        y^4 + 2*y^2 + 1
        >>> factor(p)
        (y^2 + 1)^2
        >>> b.minimal_polynomial('y')
        y^2 + 1

  minpoly(var='x', algorithm='pari')

    Return the minimal polynomial of this element (over the corresponding prime subfield).

    INPUT:

    - `var` – string (default: `'x'`)

    - `algorithm` – string (default: `'pari'`):

      - `'pari'` – use pari’s minpoly

      - `'matrix'` – return the minpoly computed from the matrix of left multiplication by self

    EXAMPLES:

    Sage

        sage: from sage.rings.finite_rings.element_base import FinitePolyExtElement
        sage: k.<a> = FiniteField(19^2)
        sage: parent(a)
        Finite Field in a of size 19^2
        sage: b=a**20
        sage: p=FinitePolyExtElement.minpoly(b,"x", algorithm='pari')
        sage: q=FinitePolyExtElement.minpoly(b,"x", algorithm='matrix')
        sage: q == p
        True
        sage: p
        x + 17

    Python

        >>> from sage.all import *
        >>> from sage.rings.finite_rings.element_base import FinitePolyExtElement
        >>> k = FiniteField(Integer(19)**Integer(2), names=('a',)); (a,) = k._first_ngens(1)
        >>> parent(a)
        Finite Field in a of size 19^2
        >>> b=a**Integer(20)
        >>> p=FinitePolyExtElement.minpoly(b,"x", algorithm='pari')
        >>> q=FinitePolyExtElement.minpoly(b,"x", algorithm='matrix')
        >>> q == p
        True
        >>> p
        x + 17

  multiplicative_order()

    Return the multiplicative order of this field element.

    EXAMPLES:

    Sage

        sage: S.<a> = GF(5^3); S
        Finite Field in a of size 5^3
        sage: a.multiplicative_order()
        124
        sage: (a^8).multiplicative_order()
        31
        sage: S(0).multiplicative_order()
        Traceback (most recent call last):
        ...
        ArithmeticError: multiplicative order of 0 not defined

    Python

        >>> from sage.all import *
        >>> S = GF(Integer(5)**Integer(3), names=('a',)); (a,) = S._first_ngens(1); S
        Finite Field in a of size 5^3
        >>> a.multiplicative_order()
        124
        >>> (a**Integer(8)).multiplicative_order()
        31
        >>> S(Integer(0)).multiplicative_order()
        Traceback (most recent call last):
        ...
        ArithmeticError: multiplicative order of 0 not defined

  norm()

    Return the norm of `self` down to the prime subfield.

    This is the product of the Galois conjugates of `self`.

    EXAMPLES:

    Sage

        sage: S.<b> = GF(5^2); S
        Finite Field in b of size 5^2
        sage: b.norm()
        2
        sage: b.charpoly('t')
        t^2 + 4*t + 2

    Python

        >>> from sage.all import *
        >>> S = GF(Integer(5)**Integer(2), names=('b',)); (b,) = S._first_ngens(1); S
        Finite Field in b of size 5^2
        >>> b.norm()
        2
        >>> b.charpoly('t')
        t^2 + 4*t + 2

    Next we consider a cubic extension:

    Sage

        sage: S.<a> = GF(5^3); S
        Finite Field in a of size 5^3
        sage: a.norm()
        2
        sage: a.charpoly('t')
        t^3 + 3*t + 3
        sage: a * a^5 * (a^25)
        2

    [Python]

        >>> from sage.all import *
        >>> S = GF(Integer(5)**Integer(3), names=('a',)); (a,) = S._first_ngens(1); S
        Finite Field in a of size 5^3
        >>> a.norm()
        2
        >>> a.charpoly('t')
        t^3 + 3*t + 3
        >>> a * a**Integer(5) * (a**Integer(25))
        2

  nth_root(n, extend=False, all=False, algorithm=None, cunningham=False)

    Return an \(n\)-th root of `self`.

    INPUT:

    - `n` – integer \(\geq 1\)

    - `extend` – boolean (default: `False`); if `True`, return an \(n\)-th root in an extension ring, if necessary. Otherwise, raise a `ValueError` if the root is not in the base ring. Warning: this option is not implemented!

    - `all` – boolean (default: `False`); if `True`, return all \(n\)-th roots of `self`, instead of just one

    - `algorithm` – string (default: `None`); `'Johnston'` is the only currently supported option. For IntegerMod elements, the problem is reduced to the prime modulus case using CRT and \(p\)-adic logs, and then this algorithm used.

    OUTPUT:

    If `self` has an \(n\)-th root, returns one (if `all` is `False`) or a list of all of them (if `all` is `True`). Otherwise, raises a `ValueError` (if `extend` is `False`) or a `NotImplementedError` (if `extend` is `True`).

    Warning

    The `extend` option is not implemented (yet).

    EXAMPLES:

    Sage

        sage: K = GF(31)
        sage: a = K(22)
        sage: K(22).nth_root(7)
        13
        sage: K(25).nth_root(5)
        5
        sage: K(23).nth_root(3)
        29

        sage: K.<a> = GF(625)
        sage: (3*a^2+a+1).nth_root(13)**13
        3*a^2 + a + 1

        sage: k.<a> = GF(29^2)
        sage: b = a^2 + 5*a + 1
        sage: b.nth_root(11)
        3*a + 20
        sage: b.nth_root(5)
        Traceback (most recent call last):
        ...
        ValueError: no nth root
        sage: b.nth_root(5, all = True)
        []
        sage: b.nth_root(3, all = True)
        [14*a + 18, 10*a + 13, 5*a + 27]

        sage: k.<a> = GF(29^5)
        sage: b = a^2 + 5*a + 1
        sage: b.nth_root(5)
        19*a^4 + 2*a^3 + 2*a^2 + 16*a + 3
        sage: b.nth_root(7)
        Traceback (most recent call last):
        ...
        ValueError: no nth root
        sage: b.nth_root(4, all=True)
        []

    Python

        >>> from sage.all import *
        >>> K = GF(Integer(31))
        >>> a = K(Integer(22))
        >>> K(Integer(22)).nth_root(Integer(7))
        13
        >>> K(Integer(25)).nth_root(Integer(5))
        5
        >>> K(Integer(23)).nth_root(Integer(3))
        29

        >>> K = GF(Integer(625), names=('a',)); (a,) = K._first_ngens(1)
        >>> (Integer(3)*a**Integer(2)+a+Integer(1)).nth_root(Integer(13))**Integer(13)
        3*a^2 + a + 1

        >>> k = GF(Integer(29)**Integer(2), names=('a',)); (a,) = k._first_ngens(1)
        >>> b = a**Integer(2) + Integer(5)*a + Integer(1)
        >>> b.nth_root(Integer(11))
        3*a + 20
        >>> b.nth_root(Integer(5))
        Traceback (most recent call last):
        ...
        ValueError: no nth root
        >>> b.nth_root(Integer(5), all = True)
        []
        >>> b.nth_root(Integer(3), all = True)
        [14*a + 18, 10*a + 13, 5*a + 27]

        >>> k = GF(Integer(29)**Integer(5), names=('a',)); (a,) = k._first_ngens(1)
        >>> b = a**Integer(2) + Integer(5)*a + Integer(1)
        >>> b.nth_root(Integer(5))
        19*a^4 + 2*a^3 + 2*a^2 + 16*a + 3
        >>> b.nth_root(Integer(7))
        Traceback (most recent call last):
        ...
        ValueError: no nth root
        >>> b.nth_root(Integer(4), all=True)
        []

    ALGORITHM:

    The default is currently an algorithm described in [Joh1999].

    AUTHOR:

    - David Roe (2010-02-13)

  pth_power(k=1)

    Return the \((p^k)\)-th power of self, where \(p\) is the characteristic of the field.

    INPUT:

    - `k` – integer (default: 1, must fit in C int type)

    Note that if \(k\) is negative, then this computes the appropriate root.

    EXAMPLES:

    Sage

        sage: F.<a> = GF(29^2)
        sage: z = a^2 + 5*a + 1
        sage: z.pth_power()
        19*a + 20
        sage: z.pth_power(10)
        10*a + 28
        sage: z.pth_power(-10) == z
        True
        sage: F.<b> = GF(2^12)
        sage: y = b^3 + b + 1
        sage: y == (y.pth_power(-3))^(2^3)
        True
        sage: y.pth_power(2)
        b^7 + b^6 + b^5 + b^4 + b^3 + b

    Python

        >>> from sage.all import *
        >>> F = GF(Integer(29)**Integer(2), names=('a',)); (a,) = F._first_ngens(1)
        >>> z = a**Integer(2) + Integer(5)*a + Integer(1)
        >>> z.pth_power()
        19*a + 20
        >>> z.pth_power(Integer(10))
        10*a + 28
        >>> z.pth_power(-Integer(10)) == z
        True
        >>> F = GF(Integer(2)**Integer(12), names=('b',)); (b,) = F._first_ngens(1)
        >>> y = b**Integer(3) + b + Integer(1)
        >>> y == (y.pth_power(-Integer(3)))**(Integer(2)**Integer(3))
        True
        >>> y.pth_power(Integer(2))
        b^7 + b^6 + b^5 + b^4 + b^3 + b

  pth_root(k=1)

    Return the \((p^k)\)-th root of self, where \(p\) is the characteristic of the field.

    INPUT:

    - `k` – integer (default: 1, must fit in C int type)

    Note that if \(k\) is negative, then this computes the appropriate power.

    EXAMPLES:

    Sage

        sage: F.<b> = GF(2^12)
        sage: y = b^3 + b + 1
        sage: y == (y.pth_root(3))^(2^3)
        True
        sage: y.pth_root(2)
        b^11 + b^10 + b^9 + b^7 + b^5 + b^4 + b^2 + b

    Python

        >>> from sage.all import *
        >>> F = GF(Integer(2)**Integer(12), names=('b',)); (b,) = F._first_ngens(1)
        >>> y = b**Integer(3) + b + Integer(1)
        >>> y == (y.pth_root(Integer(3)))**(Integer(2)**Integer(3))
        True
        >>> y.pth_root(Integer(2))
        b^11 + b^10 + b^9 + b^7 + b^5 + b^4 + b^2 + b

  sqrt(extend=False, all=False)

    See `square_root()`.

    EXAMPLES:

    Sage

        sage: k.<a> = GF(3^17)
        sage: (a^3 - a - 1).sqrt()
        a^16 + 2*a^15 + a^13 + 2*a^12 + a^10 + 2*a^9 + 2*a^8 + a^7 + a^6 + 2*a^5 + a^4 + 2*a^2 + 2*a + 2

    Python

        >>> from sage.all import *
        >>> k = GF(Integer(3)**Integer(17), names=('a',)); (a,) = k._first_ngens(1)
        >>> (a**Integer(3) - a - Integer(1)).sqrt()
        a^16 + 2*a^15 + a^13 + 2*a^12 + a^10 + 2*a^9 + 2*a^8 + a^7 + a^6 + 2*a^5 + a^4 + 2*a^2 + 2*a + 2

  square_root(extend=False, all=False)

    The square root function.

    INPUT:

    - `extend` – boolean (default: `True`); if `True`, return a square root in an extension ring, if necessary. Otherwise, raise a `ValueError` if the root is not in the base ring.

      > Warning

      > This option is not implemented!

    - `all` – boolean (default: `False`); if `True`, return all square roots of `self`, instead of just one

    Warning

    The `'extend'` option is not implemented (yet).

    EXAMPLES:

    Sage

        sage: F = FiniteField(7^2, 'a')
        sage: F(2).square_root()
        4
        sage: F(3).square_root()
        2*a + 6
        sage: F(3).square_root()**2
        3
        sage: F(4).square_root()
        2
        sage: K = FiniteField(7^3, 'alpha', implementation='pari_ffelt')
        sage: K(3).square_root()
        Traceback (most recent call last):
        ...
        ValueError: must be a perfect square.

    Python

        >>> from sage.all import *
        >>> F = FiniteField(Integer(7)**Integer(2), 'a')
        >>> F(Integer(2)).square_root()
        4
        >>> F(Integer(3)).square_root()
        2*a + 6
        >>> F(Integer(3)).square_root()**Integer(2)
        3
        >>> F(Integer(4)).square_root()
        2
        >>> K = FiniteField(Integer(7)**Integer(3), 'alpha', implementation='pari_ffelt')
        >>> K(Integer(3)).square_root()
        Traceback (most recent call last):
        ...
        ValueError: must be a perfect square.

  to_bytes(byteorder='big')

    Return an array of bytes representing an integer.

    Internally relies on the python `int.to_bytes()` method. Length of byte array is determined from the field’s order.

    INPUT:

    - `byteorder` – string (default: `'big'`); determines the byte order of the output; can only be `'big'` or `'little'`

    EXAMPLES:

    Sage

        sage: F.<z5> = GF(3^5)
        sage: a = z5^4 + 2*z5^3 + 1
        sage: a.to_bytes()
        b'\x88'

    Python

        >>> from sage.all import *
        >>> F = GF(Integer(3)**Integer(5), names=('z5',)); (z5,) = F._first_ngens(1)
        >>> a = z5**Integer(4) + Integer(2)*z5**Integer(3) + Integer(1)
        >>> a.to_bytes()
        b'\x88'

    Sage

        sage: F.<z3> = GF(163^3)
        sage: a = 136*z3^2 + 10*z3 + 125
        sage: a.to_bytes()
        b'7)\xa3'

    [Python]

        >>> from sage.all import *
        >>> F = GF(Integer(163)**Integer(3), names=('z3',)); (z3,) = F._first_ngens(1)
        >>> a = Integer(136)*z3**Integer(2) + Integer(10)*z3 + Integer(125)
        >>> a.to_bytes()
        b'7)\xa3'

  to_integer(reverse=False)

    Return an integer representation of this finite field element obtained by lifting its representative polynomial to \(\ZZ\) and evaluating it at the characteristic \(p\).

    If `reverse` is set to `True` (default: `False`), the list of coefficients is reversed prior to evaluation.

    Inverse of `sage.rings.finite_rings.finite_field_base.FiniteField.from_integer()`.

    EXAMPLES:

    Sage

        sage: F.<t> = GF(7^5)
        sage: F(5).to_integer()
        5
        sage: t.to_integer()
        7
        sage: (t^2).to_integer()
        49
        sage: (t^2+1).to_integer()
        50
        sage: (t^2+t+1).to_integer()
        57

    Python

        >>> from sage.all import *
        >>> F = GF(Integer(7)**Integer(5), names=('t',)); (t,) = F._first_ngens(1)
        >>> F(Integer(5)).to_integer()
        5
        >>> t.to_integer()
        7
        >>> (t**Integer(2)).to_integer()
        49
        >>> (t**Integer(2)+Integer(1)).to_integer()
        50
        >>> (t**Integer(2)+t+Integer(1)).to_integer()
        57

    Sage

        sage: F.<t> = GF(2^8)
        sage: u = F.from_integer(0xd1)
        sage: bin(u.to_integer(False))
        '0b11010001'
        sage: bin(u.to_integer(True))
        '0b10001011'

    [Python]

        >>> from sage.all import *
        >>> F = GF(Integer(2)**Integer(8), names=('t',)); (t,) = F._first_ngens(1)
        >>> u = F.from_integer(Integer(0xd1))
        >>> bin(u.to_integer(False))
        '0b11010001'
        >>> bin(u.to_integer(True))
        '0b10001011'

  trace()

    Return the trace of this element, which is the sum of the Galois conjugates.

    EXAMPLES:

    Sage

        sage: S.<a> = GF(5^3); S
        Finite Field in a of size 5^3
        sage: a.trace()
        0
        sage: a.charpoly('t')
        t^3 + 3*t + 3
        sage: a + a^5 + a^25
        0
        sage: z = a^2 + a + 1
        sage: z.trace()
        2
        sage: z.charpoly('t')
        t^3 + 3*t^2 + 2*t + 2
        sage: z + z^5 + z^25
        2

    Python

        >>> from sage.all import *
        >>> S = GF(Integer(5)**Integer(3), names=('a',)); (a,) = S._first_ngens(1); S
        Finite Field in a of size 5^3
        >>> a.trace()
        0
        >>> a.charpoly('t')
        t^3 + 3*t + 3
        >>> a + a**Integer(5) + a**Integer(25)
        0
        >>> z = a**Integer(2) + a + Integer(1)
        >>> z.trace()
        2
        >>> z.charpoly('t')
        t^3 + 3*t^2 + 2*t + 2
        >>> z + z**Integer(5) + z**Integer(25)
        2

class sage.rings.finite_rings.element_base.FiniteRingElement

  Bases: `CommutativeRingElement`

  canonical_associate()

    Return a canonical associate.

    Implemented here because not all finite field elements inherit from FieldElement.

    EXAMPLES:

    Sage

        sage: GF(7)(1).canonical_associate()
        (1, 1)
        sage: GF(7)(3).canonical_associate()
        (1, 3)
        sage: GF(7)(0).canonical_associate()
        (0, 1)
        sage: IntegerModRing(15)(7).canonical_associate()
        NotImplemented

    Python

        >>> from sage.all import *
        >>> GF(Integer(7))(Integer(1)).canonical_associate()
        (1, 1)
        >>> GF(Integer(7))(Integer(3)).canonical_associate()
        (1, 3)
        >>> GF(Integer(7))(Integer(0)).canonical_associate()
        (0, 1)
        >>> IntegerModRing(Integer(15))(Integer(7)).canonical_associate()
        NotImplemented

  to_bytes(byteorder='big')

    Return an array of bytes representing an integer.

    Internally relies on the python `int.to_bytes()` method. Length of byte array is determined from the field’s order.

    INPUT:

    - `byteorder` – string (default: `'big'`); determines the byte order of `input_bytes`; can only be `'big'` or `'little'`

    EXAMPLES:

    Sage

        sage: F = GF(65537)
        sage: a = F(8726)
        sage: a.to_bytes()
        b'\x00"\x16'
        sage: a.to_bytes(byteorder='little')
        b'\x16"\x00'

    Python

        >>> from sage.all import *
        >>> F = GF(Integer(65537))
        >>> a = F(Integer(8726))
        >>> a.to_bytes()
        b'\x00"\x16'
        >>> a.to_bytes(byteorder='little')
        b'\x16"\x00'