galois.GF

class galois.GF(order, irreducible_poly=None, primitive_element=None, verify_irreducible=True, verify_primitive=True, mode='auto', target='cpu')

Factory function to construct a Galois field array class of type \(\mathrm{GF}(p^m)\).

The created class will be a subclass of galois.FieldArray with metaclass galois.FieldMeta. The galois.FieldArray inheritance provides the numpy.ndarray functionality. The galois.FieldMeta metaclass provides a variety of class attributes and methods relating to the finite field.

Parameters
  • order (int) – The order \(p^m\) of the field \(\mathrm{GF}(p^m)\). The order must be a prime power.

  • irreducible_poly (int, galois.Poly, optional) – Optionally specify an irreducible polynomial of degree \(m\) over \(\mathrm{GF}(p)\) that will define the Galois field arithmetic. An integer may be provided, which is the integer representation of the irreducible polynomial. Default is None which uses the Conway polynomial \(C_{p,m}\) obtained from galois.conway_poly().

  • primitive_element (int, galois.Poly, optional) – Optionally specify a primitive element of the field \(\mathrm{GF}(p^m)\). A primitive element is a generator of the multiplicative group of the field. For prime fields \(\mathrm{GF}(p)\), the primitive element must be an integer and is a primitive root modulo \(p\). For extension fields \(\mathrm{GF}(p^m)\), the primitive element is a polynomial of degree less than \(m\) over \(\mathrm{GF}(p)\) or its integer representation. The default is None which uses galois.primitive_root(p) for prime fields and galois.primitive_element(irreducible_poly) for extension fields.

  • verify_irreducible (bool, optional) – Indicates whether to verify that the specified irreducible polynomial is in fact irreducible. The default is True. For large irreducible polynomials that are already known to be irreducible (and may take a long time to verify), this argument can be set to False.

  • verify_primitive (bool, optional) – Indicates whether to verify that the specified primitive element is in fact a generator of the multiplicative group. The default is True.

  • mode (str, optional) – The type of field computation, either "auto", "jit-lookup", or "jit-calculate". The default is "auto". The “jit-lookup” mode will use Zech log, log, and anti-log lookup tables for efficient calculation. The “jit-calculate” mode will not store any lookup tables, but instead perform field arithmetic on the fly. The “jit-calculate” mode is designed for large fields that cannot or should not store lookup tables in RAM. Generally, “jit-calculate” mode will be slower than “jit-lookup”. The “auto” mode will determine whether to use “jit-lookup” or “jit-calculate” based on the field’s size. In “auto” mode, field’s with order <= 2**16 will use the “jit-lookup” mode.

  • target (str, optional) – The target keyword argument from numba.vectorize(), either "cpu", "parallel", or "cuda".

Returns

A new Galois field array class that is a subclass of galois.FieldArray with galois.FieldMeta metaclass.

Return type

galois.FieldMeta

Examples

Construct a Galois field array class with default irreducible polynomial and primitive element.

# Construct a GF(2^m) class
In [198]: GF256 = galois.GF(2**8)

# Notice the irreducible polynomial is primitive
In [199]: print(GF256.properties)
GF(2^8):
  structure: Finite Field
  characteristic: 2
  degree: 8
  order: 256
  irreducible_poly: Poly(x^8 + x^4 + x^3 + x^2 + 1, GF(2))
  is_primitive_poly: True
  primitive_element: GF(2, order=2^8)

In [200]: poly = GF256.irreducible_poly

Construct a Galois field specifying a specific irreducible polynomial.

# Field used in AES
In [201]: GF256_AES = galois.GF(2**8, irreducible_poly=galois.Poly.Degrees([8,4,3,1,0]))

In [202]: print(GF256_AES.properties)
GF(2^8):
  structure: Finite Field
  characteristic: 2
  degree: 8
  order: 256
  irreducible_poly: Poly(x^8 + x^4 + x^3 + x + 1, GF(2))
  is_primitive_poly: False
  primitive_element: GF(3, order=2^8)

# Construct a GF(p) class
In [203]: GF571 = galois.GF(571); print(GF571.properties)
GF(571):
  structure: Finite Field
  characteristic: 571
  degree: 1
  order: 571

# Construct a very large GF(2^m) class
In [204]: GF2m = galois.GF(2**100); print(GF2m.properties)
GF(2^100):
  structure: Finite Field
  characteristic: 2
  degree: 100
  order: 1267650600228229401496703205376
  irreducible_poly: Poly(x^100 + x^57 + x^56 + x^55 + x^52 + x^48 + x^47 + x^46 + x^45 + x^44 + x^43 + x^41 + x^37 + x^36 + x^35 + x^34 + x^31 + x^30 + x^27 + x^25 + x^24 + x^22 + x^20 + x^19 + x^16 + x^15 + x^11 + x^9 + x^8 + x^6 + x^5 + x^3 + 1, GF(2))
  is_primitive_poly: True
  primitive_element: GF(2, order=2^100)

# Construct a very large GF(p) class
In [205]: GFp = galois.GF(36893488147419103183); print(GFp.properties)
GF(36893488147419103183):
  structure: Finite Field
  characteristic: 36893488147419103183
  degree: 1
  order: 36893488147419103183

See galois.FieldArray for more examples of what Galois field arrays can do.