Finite Fields

Module Contents

Classes

Field

A type protocol for defining fields.

PrimeField

Superclass for integers modulo a prime. Not intended to be used

GaloisField

Superclass for defining finite fields. Not intended to be used

Attributes

F

T

U

Module Details

F

F
F = TypeVar("F", bound="Field")

Field

A type protocol for defining fields.

class Field

Bases: typing_extensions.Protocol

__slots__ = []
classmethod zero()F
classmethod from_int(n: int)F
__radd__(left: F)F
__add__(right: F)F
__iadd__(right: F)F
__sub__(right: F)F
__rsub__(left: F)F
__mul__(right: F)F
__rmul__(left: F)F
__imul__(right: F)F
__pow__(exponent: int)F
__ipow__(right: int)F
__neg__()F
__truediv__(right: F)F

T

T
T = TypeVar("T", bound="PrimeField")

PrimeField

Superclass for integers modulo a prime. Not intended to be used directly, but rather to be subclassed.

class PrimeField

Bases: int, Field

__slots__ = []
PRIME :int
__floordiv__
__rfloordiv__
__ifloordiv__
__divmod__
__rdivmod__
__rpow__
__and__
__or__
__xor__
__rxor__
__ixor__
__rshift__
__lshift__
__irshift__
__ilshift__
classmethod from_be_bytes(buffer: ethereum.base_types.Bytes)T

Converts a sequence of bytes into a element of the field. :param buffer: Bytes to decode.

Returns

self – Unsigned integer decoded from buffer.

Return type

T

classmethod zero()T
classmethod from_int(n: int)T
__radd__(left: T)T

Return value+self.

__add__(right: T)T

Return self+value.

__iadd__(right: T)T
__sub__(right: T)T

Return self-value.

__rsub__(left: T)T

Return value-self.

__mul__(right: T)T

Return self*value.

__rmul__(left: T)T

Return value*self.

__imul__(right: T)T
__pow__(exponent: int)T

Return pow(self, value, mod).

__ipow__(right: int)T
__neg__()T

-self

__truediv__(right: T)T

Return self/value.

multiplicative_inverse()T
to_be_bytes32()ethereum.base_types.Bytes32

Converts this arbitrarily sized unsigned integer into its big endian representation with exactly 32 bytes. :returns: big_endian – Big endian (most significant bits first) representation. :rtype: Bytes32

U

U
U = TypeVar("U", bound="GaloisField")

GaloisField

Superclass for defining finite fields. Not intended to be used directly, but rather to be subclassed.

Fields are represented as F_p[x]/(x^n + …) where the MODULUS is a tuple of the non-leading coefficients of the defining polynomial. For example x^3 + 2x^2 + 3x + 4 is (2, 3, 4).

In practice the polynomial is likely to be be sparse and you should overload the __mul__() function to take advantage of this fact.

class GaloisField

Bases: tuple, Field

__slots__ = []
PRIME :int
MODULUS :Tuple[int, Ellipsis]
FROBENIUS_COEFFICIENTS :Tuple[GaloisField, Ellipsis]
classmethod zero()U
classmethod from_int(n: int)U
__add__(right: U)U

Return self+value.

__radd__(left: U)U
__iadd__(right: U)U
__sub__(right: U)U
__rsub__(left: U)U
__mul__(right: U)U

Return self*value.

__rmul__(left: U)U

Return value*self.

__imul__(right: U)U
__truediv__(right: U)U
__neg__()U
scalar_mul(x: int)U

Multiply a field element by a integer. This is faster than using from_int() and field multiplication.

deg()int

This is a support function for multiplicative_inverse().

multiplicative_inverse()U

Calculate the multiplicative inverse. Uses the Euclidian algorithm.

__pow__(exponent: int)U
classmethod calculate_frobenius_coefficients()Tuple[U, Ellipsis]

Calculate the coefficients needed by frobenius().

frobenius()U

Returns self ** p. This function is known as the Frobenius endomorphism and has many special mathematical properties. In particular it is extremely cheap to compute compared to other exponentiations.