galois.Poly

class galois.Poly(coeffs, field=None, order='desc')[source]

Bases: object

Create a polynomial \(p(x)\) over \(\mathrm{GF}(q)\).

The polynomial \(p(x) = a_{d}x^{d} + a_{d-1}x^{d-1} + \dots + a_1x + a_0\) has coefficients \(\{a_{d}, a_{d-1}, \dots, a_1, a_0\}\) in \(\mathrm{GF}(q)\).

Parameters
  • coeffs (array_like) – List of polynomial coefficients \(\{a_{d}, a_{d-1}, \dots, a_1, a_0\}\) with type galois.GFArray, numpy.ndarray, list, or tuple. The first element is the highest-degree element if order="desc" or the first element is the 0-th degree element if order="asc".

  • field (galois.GFArray, optional) – The field \(\mathrm{GF}(q)\) the polynomial is over. The default is None which represents galois.GF2. If coeffs is a Galois field array, then that field is used and the field argument is ignored.

  • order (str, optional) – The interpretation of the coefficient degrees, either "desc" (default) or "asc". For "desc", the first element of coeffs is the highest degree coefficient \(x^{d}\)) and the last element is the 0-th degree element \(x^0\).

Returns

The polynomial \(p(x)\).

Return type

galois.Poly

Examples

Create a polynomial over \(\mathrm{GF}(2)\).

In [82]: galois.Poly([1,0,1,1])
Out[82]: Poly(x^3 + x + 1, GF(2))

In [83]: galois.Poly.Degrees([3,1,0])
Out[83]: Poly(x^3 + x + 1, GF(2))

Create a polynomial over \(\mathrm{GF}(2^8)\).

In [84]: GF = galois.GF(2**8)

In [85]: galois.Poly([124,0,223,0,0,15], field=GF)
Out[85]: Poly(124x^5 + 223x^3 + 15, GF(2^8))

# Alternate way of constructing the same polynomial
In [86]: galois.Poly.Degrees([5,3,0], coeffs=[124,223,15], field=GF)
Out[86]: Poly(124x^5 + 223x^3 + 15, GF(2^8))

Polynomial arithmetic using binary operators.

In [87]: a = galois.Poly([117,0,63,37], field=GF); a
Out[87]: Poly(117x^3 + 63x + 37, GF(2^8))

In [88]: b = galois.Poly([224,0,21], field=GF); b
Out[88]: Poly(224x^2 + 21, GF(2^8))

In [89]: a + b
Out[89]: Poly(117x^3 + 224x^2 + 63x + 48, GF(2^8))

In [90]: a - b
Out[90]: Poly(117x^3 + 224x^2 + 63x + 48, GF(2^8))

# Compute the quotient of the polynomial division
In [91]: a / b
Out[91]: Poly(202x, GF(2^8))

# True division and floor division are equivalent
In [92]: a / b == a // b
Out[92]: True

# Compute the remainder of the polynomial division
In [93]: a % b
Out[93]: Poly(198x + 37, GF(2^8))

# Compute both the quotient and remainder in one pass
In [94]: divmod(a, b)
Out[94]: (Poly(202x, GF(2^8)), Poly(198x + 37, GF(2^8)))

Constructors

Coeffs(coeffs[, field, order])

Constructs a polynomial over \(\mathrm{GF}(q)\) from its coefficients.

Degrees(degrees[, coeffs, field])

Constructs a polynomial over \(\mathrm{GF}(q)\) from its non-zero degrees.

Identity([field])

Constructs the identity polynomial \(p(x) = x\) over \(\mathrm{GF}(q)\).

Integer(integer[, field])

Constructs a polynomial over \(\mathrm{GF}(q)\) from its integer representation.

One([field])

Constructs the one polynomial \(p(x) = 1\) over \(\mathrm{GF}(q)\).

Random(degree[, field])

Constructs a random polynomial over \(\mathrm{GF}(q)\) with degree \(d\).

Roots(roots[, multiplicities, field])

Constructs a monic polynomial in \(\mathrm{GF}(q)[x]\) from its roots.

Zero([field])

Constructs the zero polynomial \(p(x) = 0\) over \(\mathrm{GF}(q)\).

Methods

derivative([k])

Computes the \(k\)-th formal derivative \(\frac{d^k}{dx^k} p(x)\) of the polynomial \(p(x)\).

roots([multiplicity])

Calculates the roots \(r\) of the polynomial \(p(x)\), such that \(p(r) = 0\).

Attributes

coeffs

The coefficients of the polynomial in degree-descending order.

degree

The degree of the polynomial, i.e. the highest degree with non-zero coefficient.

degrees

An array of the polynomial degrees in degree-descending order.

field

The Galois field to which the coefficients belong.

integer

The integer representation of the polynomial.

nonzero_coeffs

The non-zero coefficients of the polynomial in degree-descending order.

nonzero_degrees

An array of the polynomial degrees that have non-zero coefficients, in degree-descending order.

classmethod Coeffs(coeffs, field=None, order='desc')[source]

Constructs a polynomial over \(\mathrm{GF}(q)\) from its coefficients.

Alias of galois.Poly constructor.

Parameters
  • coeffs (array_like) – List of polynomial coefficients \(\{a_{d}, a_{d-1}, \dots, a_1, a_0\}\) with type galois.GFArray, numpy.ndarray, list, or tuple. The first element is the highest-degree element if order="desc" or the first element is the 0-th degree element if order="asc".

  • field (galois.GFArray, optional) – The field \(\mathrm{GF}(q)\) the polynomial is over. The default is None which represents galois.GF2. If coeffs is a Galois field array, then that field is used and the field argument is ignored.

  • order (str, optional) – The interpretation of the coefficient degrees, either "desc" (default) or "asc". For "desc", the first element of coeffs is the highest degree coefficient \(x^{d}\)) and the last element is the 0-th degree element \(x^0\).

Returns

The polynomial \(p(x)\).

Return type

galois.Poly

classmethod Degrees(degrees, coeffs=None, field=None)[source]

Constructs a polynomial over \(\mathrm{GF}(q)\) from its non-zero degrees.

Parameters
  • degrees (list) – List of polynomial degrees with non-zero coefficients.

  • coeffs (array_like, optional) – List of corresponding non-zero coefficients. The default is None which corresponds to all one coefficients, i.e. [1,]*len(degrees).

  • field (galois.GFArray, optional) – The field \(\mathrm{GF}(q)\) the polynomial is over. The default is`None` which represents galois.GF2.

Returns

The polynomial \(p(x)\).

Return type

galois.Poly

Examples

Construct a polynomial over \(\mathrm{GF}(2)\) by specifying the degrees with non-zero coefficients.

In [95]: galois.Poly.Degrees([3,1,0])
Out[95]: Poly(x^3 + x + 1, GF(2))

Construct a polynomial over \(\mathrm{GF}(2^8)\) by specifying the degrees with non-zero coefficients.

In [96]: GF = galois.GF(2**8)

In [97]: galois.Poly.Degrees([3,1,0], coeffs=[251,73,185], field=GF)
Out[97]: Poly(251x^3 + 73x + 185, GF(2^8))
classmethod Identity(field=<class 'galois.gf2.GF2'>)[source]

Constructs the identity polynomial \(p(x) = x\) over \(\mathrm{GF}(q)\).

Parameters

field (galois.GFArray, optional) – The field \(\mathrm{GF}(q)\) the polynomial is over. The default is galois.GF2.

Returns

The polynomial \(p(x)\).

Return type

galois.Poly

Examples

Construct the identity polynomial over \(\mathrm{GF}(2)\).

In [98]: galois.Poly.Identity()
Out[98]: Poly(x, GF(2))

Construct the identity polynomial over \(\mathrm{GF}(2^8)\).

In [99]: GF = galois.GF(2**8)

In [100]: galois.Poly.Identity(field=GF)
Out[100]: Poly(x, GF(2^8))
classmethod Integer(integer, field=<class 'galois.gf2.GF2'>)[source]

Constructs a polynomial over \(\mathrm{GF}(q)\) from its integer representation.

The integer value \(i\) represents the polynomial \(p(x) = a_{d}x^{d} + a_{d-1}x^{d-1} + \dots + a_1x + a_0\) over field \(\mathrm{GF}(q)\) if \(i = a_{d}q^{d} + a_{d-1}q^{d-1} + \dots + a_1q + a_0\) using integer arithmetic, not finite field arithmetic.

Parameters
  • integer (int) – The integer representation of the polynomial \(p(x)\).

  • field (galois.GFArray, optional) – The field \(\mathrm{GF}(q)\) the polynomial is over. The default is galois.GF2.

Returns

The polynomial \(p(x)\).

Return type

galois.Poly

Examples

Construct a polynomial over \(\mathrm{GF}(2)\) from its integer representation.

In [101]: galois.Poly.Integer(5)
Out[101]: Poly(x^2 + 1, GF(2))

Construct a polynomial over \(\mathrm{GF}(2^8)\) from its integer representation.

In [102]: GF = galois.GF(2**8)

In [103]: galois.Poly.Integer(13*256**3 + 117, field=GF)
Out[103]: Poly(13x^3 + 117, GF(2^8))
classmethod One(field=<class 'galois.gf2.GF2'>)[source]

Constructs the one polynomial \(p(x) = 1\) over \(\mathrm{GF}(q)\).

Parameters

field (galois.GFArray, optional) – The field \(\mathrm{GF}(q)\) the polynomial is over. The default is galois.GF2.

Returns

The polynomial \(p(x)\).

Return type

galois.Poly

Examples

Construct the one polynomial over \(\mathrm{GF}(2)\).

In [104]: galois.Poly.One()
Out[104]: Poly(1, GF(2))

Construct the one polynomial over \(\mathrm{GF}(2^8)\).

In [105]: GF = galois.GF(2**8)

In [106]: galois.Poly.One(field=GF)
Out[106]: Poly(1, GF(2^8))
classmethod Random(degree, field=<class 'galois.gf2.GF2'>)[source]

Constructs a random polynomial over \(\mathrm{GF}(q)\) with degree \(d\).

Parameters
  • degree (int) – The degree of the polynomial.

  • field (galois.GFArray, optional) – The field \(\mathrm{GF}(q)\) the polynomial is over. The default is galois.GF2.

Returns

The polynomial \(p(x)\).

Return type

galois.Poly

Examples

Construct a random degree-\(5\) polynomial over \(\mathrm{GF}(2)\).

In [107]: galois.Poly.Random(5)
Out[107]: Poly(x^5 + x, GF(2))

Construct a random degree-\(5\) polynomial over \(\mathrm{GF}(2^8)\).

In [108]: GF = galois.GF(2**8)

In [109]: galois.Poly.Random(5, field=GF)
Out[109]: Poly(190x^5 + 246x^4 + 125x^3 + 65x^2 + 136x + 91, GF(2^8))
classmethod Roots(roots, multiplicities=None, field=None)[source]

Constructs a monic polynomial in \(\mathrm{GF}(q)[x]\) from its roots.

The polynomial \(p(x)\) with \(d\) roots \(\{r_0, r_1, \dots, r_{d-1}\}\) is:

\[ \begin{align}\begin{aligned}p(x) &= (x - r_0) (x - r_1) \dots (x - r_{d-1})\\p(x) &= a_{d}x^{d} + a_{d-1}x^{d-1} + \dots + a_1x + a_0\end{aligned}\end{align} \]
Parameters
  • roots (array_like) – List of roots in \(\mathrm{GF}(q)\) of the desired polynomial.

  • multiplicities (array_like, optional) – List of multiplicity of each root. The default is None which corresponds to all ones.

  • field (galois.GFArray, optional) – The field \(\mathrm{GF}(q)\) the polynomial is over. The default is`None` which represents galois.GF2.

Returns

The polynomial \(p(x)\).

Return type

galois.Poly

Examples

Construct a polynomial over \(\mathrm{GF}(2)\) from a list of its roots.

In [110]: roots = [0, 0, 1]

In [111]: p = galois.Poly.Roots(roots); p
Out[111]: Poly(x^3 + x^2, GF(2))

In [112]: p(roots)
Out[112]: GF([0, 0, 0], order=2)

Construct a polynomial over \(\mathrm{GF}(2^8)\) from a list of its roots.

In [113]: GF = galois.GF(2**8)

In [114]: roots = [121, 198, 225]

In [115]: p = galois.Poly.Roots(roots, field=GF); p
Out[115]: Poly(x^3 + 94x^2 + 174x + 89, GF(2^8))

In [116]: p(roots)
Out[116]: GF([0, 0, 0], order=2^8)
classmethod Zero(field=<class 'galois.gf2.GF2'>)[source]

Constructs the zero polynomial \(p(x) = 0\) over \(\mathrm{GF}(q)\).

Parameters

field (galois.GFArray, optional) – The field \(\mathrm{GF}(q)\) the polynomial is over. The default is galois.GF2.

Returns

The polynomial \(p(x)\).

Return type

galois.Poly

Examples

Construct the zero polynomial over \(\mathrm{GF}(2)\).

In [117]: galois.Poly.Zero()
Out[117]: Poly(0, GF(2))

Construct the zero polynomial over \(\mathrm{GF}(2^8)\).

In [118]: GF = galois.GF(2**8)

In [119]: galois.Poly.Zero(field=GF)
Out[119]: Poly(0, GF(2^8))
derivative(k=1)[source]

Computes the \(k\)-th formal derivative \(\frac{d^k}{dx^k} p(x)\) of the polynomial \(p(x)\).

For the polynomial

\[p(x) = a_{d}x^{d} + a_{d-1}x^{d-1} + \dots + a_1x + a_0\]

the first formal derivative is defined as

\[p'(x) = (d) \cdot a_{d} x^{d-1} + (d-1) \cdot a_{d-1} x^{d-2} + \dots + (2) \cdot a_{2} x + a_1\]

where \(\cdot\) represents scalar multiplication (repeated addition), not finite field multiplication, e.g. \(3 \cdot a = a + a + a\).

Parameters

k (int, optional) – The number of derivatives to compute. 1 corresponds to \(p'(x)\), 2 corresponds to \(p''(x)\), etc. The default is 1.

Returns

The \(k\)-th formal derivative of the polynomial \(p(x)\).

Return type

galois.Poly

References

Examples

Compute the derivatives of a polynomial over \(\mathrm{GF}(2)\).

In [120]: p = galois.Poly.Random(7); p
Out[120]: Poly(x^7 + x^5 + x^4 + x^2, GF(2))

In [121]: p.derivative()
Out[121]: Poly(x^6 + x^4, GF(2))

# k derivatives of a polynomial where k is the Galois field's characteristic will always result in 0
In [122]: p.derivative(2)
Out[122]: Poly(0, GF(2))

Compute the derivatives of a polynomial over \(\mathrm{GF}(7)\).

In [123]: GF = galois.GF(7)

In [124]: p = galois.Poly.Random(11, field=GF); p
Out[124]: Poly(4x^11 + 3x^10 + 6x^9 + 4x^8 + 4x^7 + 3x^6 + 2x^5 + 4x^4 + 6x + 4, GF(7))

In [125]: p.derivative()
Out[125]: Poly(2x^10 + 2x^9 + 5x^8 + 4x^7 + 4x^5 + 3x^4 + 2x^3 + 6, GF(7))

In [126]: p.derivative(2)
Out[126]: Poly(6x^9 + 4x^8 + 5x^7 + 6x^4 + 5x^3 + 6x^2, GF(7))

In [127]: p.derivative(3)
Out[127]: Poly(5x^8 + 4x^7 + 3x^3 + x^2 + 5x, GF(7))

# k derivatives of a polynomial where k is the Galois field's characteristic will always result in 0
In [128]: p.derivative(7)
Out[128]: Poly(0, GF(2))

Compute the derivatives of a polynomial over \(\mathrm{GF}(2^8)\).

In [129]: GF = galois.GF(2**8)

In [130]: p = galois.Poly.Random(7, field=GF); p
Out[130]: Poly(250x^7 + 27x^6 + 161x^5 + 67x^4 + 79x^3 + 207x^2 + 186x + 31, GF(2^8))

In [131]: p.derivative()
Out[131]: Poly(250x^6 + 161x^4 + 79x^2 + 186, GF(2^8))

# k derivatives of a polynomial where k is the Galois field's characteristic will always result in 0
In [132]: p.derivative(2)
Out[132]: Poly(0, GF(2^8))
roots(multiplicity=False)[source]

Calculates the roots \(r\) of the polynomial \(p(x)\), such that \(p(r) = 0\).

This implementation uses Chien’s search to find the roots \(\{r_0, r_1, \dots, r_{k-1}\}\) of the degree-\(d\) polynomial

\[p(x) = a_{d}x^{d} + a_{d-1}x^{d-1} + \dots + a_1x + a_0,\]

where \(k \le d\). Then, \(p(x)\) can be factored as

\[p(x) = (x - r_0)^{m_0} (x - r_1)^{m_1} \dots (x - r_{k-1})^{m_{k-1}},\]

where \(m_i\) is the multiplicity of root \(r_i\) and

\[\sum_{i=0}^{k-1} m_i = d.\]

The Galois field elements can be represented as \(\mathrm{GF}(q) = \{0, 1, \alpha, \alpha^2, \dots, \alpha^{q-2}\}\), where \(\alpha\) is a primitive element of \(\mathrm{GF}(q)\).

\(0\) is a root of \(p(x)\) if:

\[a_0 = 0\]

\(1\) is a root of \(p(x)\) if:

\[\sum_{j=0}^{d} a_j = 0\]

The remaining elements of \(\mathrm{GF}(q)\) are powers of \(\alpha\). The following equations calculate \(p(\alpha^i)\), where \(\alpha^i\) is a root of \(p(x)\) if \(p(\alpha^i) = 0\).

\[ \begin{align}\begin{aligned}p(\alpha^i) &= a_{d}(\alpha^i)^{d} + a_{d-1}(\alpha^i)^{d-1} + \dots + a_1(\alpha^i) + a_0\\p(\alpha^i) &\overset{\Delta}{=} \lambda_{i,d} + \lambda_{i,d-1} + \dots + \lambda_{i,1} + \lambda_{i,0}\\p(\alpha^i) &= \sum_{j=0}^{d} \lambda_{i,j}\end{aligned}\end{align} \]

The next power of \(\alpha\) can be easily calculated from the previous calculation.

\[ \begin{align}\begin{aligned}p(\alpha^{i+1}) &= a_{d}(\alpha^{i+1})^{d} + a_{d-1}(\alpha^{i+1})^{d-1} + \dots + a_1(\alpha^{i+1}) + a_0\\p(\alpha^{i+1}) &= a_{d}(\alpha^i)^{d}\alpha^d + a_{d-1}(\alpha^i)^{d-1}\alpha^{d-1} + \dots + a_1(\alpha^i)\alpha + a_0\\p(\alpha^{i+1}) &= \lambda_{i,d}\alpha^d + \lambda_{i,d-1}\alpha^{d-1} + \dots + \lambda_{i,1}\alpha + \lambda_{i,0}\\p(\alpha^{i+1}) &= \sum_{j=0}^{d} \lambda_{i,j}\alpha^j\end{aligned}\end{align} \]
Parameters

multiplicity (bool, optional) – Optionally return the multiplicity of each root. The default is False, which only returns the unique roots.

Returns

  • galois.GFArray – Galois field array of roots of \(p(x)\).

  • np.ndarray – The multiplicity of each root. Only returned if multiplicity=True.

References

Examples

Find the roots of a polynomial over \(\mathrm{GF}(2)\).

In [133]: p = galois.Poly.Roots([0,]*7 + [1,]*13); p
Out[133]: Poly(x^20 + x^19 + x^16 + x^15 + x^12 + x^11 + x^8 + x^7, GF(2))

In [134]: p.roots()
Out[134]: GF([0, 1], order=2)

In [135]: p.roots(multiplicity=True)
Out[135]: (GF([0, 1], order=2), array([ 7, 13]))

Find the roots of a polynomial over \(\mathrm{GF}(2^8)\).

In [136]: GF = galois.GF(2**8)

In [137]: p = galois.Poly.Roots([18,]*7 + [155,]*13 + [227,]*9, field=GF); p
Out[137]: Poly(x^29 + 106x^28 + 27x^27 + 155x^26 + 230x^25 + 38x^24 + 78x^23 + 8x^22 + 46x^21 + 210x^20 + 248x^19 + 214x^18 + 172x^17 + 152x^16 + 82x^15 + 237x^14 + 172x^13 + 230x^12 + 141x^11 + 63x^10 + 103x^9 + 167x^8 + 199x^7 + 127x^6 + 254x^5 + 95x^4 + 93x^3 + 3x^2 + 4x + 208, GF(2^8))

In [138]: p.roots()
Out[138]: GF([ 18, 155, 227], order=2^8)

In [139]: p.roots(multiplicity=True)
Out[139]: (GF([ 18, 155, 227], order=2^8), array([ 7, 13,  9]))
property coeffs

The coefficients of the polynomial in degree-descending order. The entries of galois.Poly.degrees are paired with galois.Poly.coeffs.

Examples

In [140]: GF = galois.GF(7)

In [141]: p = galois.Poly([3, 0, 5, 2], field=GF)

In [142]: p.coeffs
Out[142]: GF([3, 0, 5, 2], order=7)
Type

galois.GFArray

property degree

The degree of the polynomial, i.e. the highest degree with non-zero coefficient.

Examples

In [143]: GF = galois.GF(7)

In [144]: p = galois.Poly([3, 0, 5, 2], field=GF)

In [145]: p.degree
Out[145]: 3
Type

int

property degrees

An array of the polynomial degrees in degree-descending order. The entries of galois.Poly.degrees are paired with galois.Poly.coeffs.

Examples

In [146]: GF = galois.GF(7)

In [147]: p = galois.Poly([3, 0, 5, 2], field=GF)

In [148]: p.degrees
Out[148]: array([3, 2, 1, 0])
Type

numpy.ndarray

property field

The Galois field to which the coefficients belong. The galois.Poly.field property is a subclass of galois.GFArray. This property is settable.

Examples

In [149]: GF = galois.GF(2**8)

# The primitive polynomial of the field GF(p^m) is degree-m over GF(p)[x]
In [150]: prim_poly = GF.prim_poly; prim_poly
Out[150]: Poly(x^8 + x^4 + x^3 + x^2 + 1, GF(2^8))

In [151]: prim_poly.field
Out[151]: galois.GF2^8

# Convert the primitive polynomial from GF(p)[x] to GF(p^m)[x]
In [152]: prim_poly.field = GF; prim_poly
Out[152]: Poly(x^8 + x^4 + x^3 + x^2 + 1, GF(2^8))

# The primitive element alpha is a root of the primitive polynomial in GF(p^m)
In [153]: prim_poly(GF.alpha)
Out[153]: GF(0, order=2^8)
Type

type

property integer

The integer representation of the polynomial. For polynomial \(p(x) = a_{d}x^{d} + a_{d-1}x^{d-1} + \dots + a_1x + a_0\) with elements in \(a_k \in \mathrm{GF}(q)\), the integer representation is \(i = a_{d} q^{d} + a_{d-1} q^{d-1} + \dots + a_1 q + a_0\) (using integer arithmetic, not finite field arithmetic).

Examples

In [154]: GF = galois.GF(7)

In [155]: p = galois.Poly([3, 0, 5, 2], field=GF)

In [156]: p.integer
Out[156]: 1066

In [157]: p.integer == 3*7**3 + 5*7**1 + 2*7**0
Out[157]: True
Type

int

property nonzero_coeffs

The non-zero coefficients of the polynomial in degree-descending order. The entries of galois.Poly.nonzero_degrees are paired with galois.Poly.nonzero_coeffs.

Examples

In [158]: GF = galois.GF(7)

In [159]: p = galois.Poly([3, 0, 5, 2], field=GF)

In [160]: p.nonzero_coeffs
Out[160]: GF([3, 5, 2], order=7)
Type

galois.GFArray

property nonzero_degrees

An array of the polynomial degrees that have non-zero coefficients, in degree-descending order. The entries of galois.Poly.nonzero_degrees are paired with galois.Poly.nonzero_coeffs.

Examples

In [161]: GF = galois.GF(7)

In [162]: p = galois.Poly([3, 0, 5, 2], field=GF)

In [163]: p.nonzero_degrees
Out[163]: array([3, 1, 0])
Type

numpy.ndarray