Galois Fields for NumPy
The `galois` library is a performant Python package that extends NumPy arrays to operate over finite fields (Galois fields) for various mathematical and cryptographic applications. It leverages Numba and LLVM for just-in-time compilation to optimize finite field arithmetic, often outperforming native NumPy operations for modular arithmetic. It supports all Galois fields GF(p^m), offering functionalities for linear algebra, polynomials, forward error correction codes (BCH, Reed-Solomon), and number theoretic functions. Currently at version 0.4.10, the library maintains an active release cadence, with updates typically arriving monthly or bi-monthly.
Common errors
-
AttributeError: module 'galois' has no attribute 'Field'
cause Attempting to use the `galois.Field()` class factory after it has been removed in a future version (v0.5.0).fixReplace `galois.Field(...)` with `galois.GF(...)`. -
TypeError: galois.GF() takes no positional arguments for 'irreducible_poly'
cause Passing keyword-only arguments like `irreducible_poly` or `primitive_element` positionally to `galois.GF()` (or `galois.Field()`) since v0.3.0.fixEnsure all such arguments are passed by keyword, e.g., `galois.GF(2**8, irreducible_poly=poly)`. -
TypeError: arithmetic operation between FieldArray instances with different irreducible polynomials of order GF(p^m)
cause Performing arithmetic between two `FieldArray` instances that represent elements from different Galois fields. Even if they have the same order (p^m), they must be defined with the exact same irreducible polynomial to be compatible.fixEnsure both `FieldArray` instances are created from the *exact same* `GF` class. If necessary, explicitly cast one array to the field of the other using `GF_target(array_source)` if the conversion is meaningful and defined. -
ImportError: DLL load failed while importing unicodedata: The specified module could not be found.
cause This issue was specifically reported with Python 3.10.1, likely due to a specific incompatibility between Numba's compiled components and that Python patch version.fixIf on Python 3.10.1, try upgrading to a later Python 3.10.x release, Python 3.11+, or downgrading to Python 3.9. Ensure Numba and NumPy are also updated to their latest compatible versions.
Warnings
- breaking The `galois.Field()` class factory function was deprecated in `galois` v0.4.10 and is scheduled for removal in v0.5.0.
- breaking As of `galois` v0.3.0, several keyword arguments for `galois.GF()` and `galois.Field()` (e.g., `irreducible_poly`, `primitive_element`, `verify`, `compile`, `repr`) can no longer be passed as positional arguments.
- gotcha The algorithms implemented in `galois` are designed for performance, not constant-time execution. This means the library could be vulnerable to side-channel timing attacks.
- gotcha `galois` depends heavily on Numba, which has historically had tight and sometimes incompatible dependencies with specific NumPy versions. This can lead to issues during installation or runtime if the installed NumPy and Numba versions are not compatible.
Install
-
pip install galois
Imports
- galois
import galois
- GF
GF = galois.Field(p**m)
GF = galois.GF(p**m)
- Poly
P = galois.Poly([1, 0, 1], field=GF)
Quickstart
import galois
import numpy as np
# Define a Galois field, e.g., GF(3^5)
GF = galois.GF(3**5)
print(f"Galois Field properties:\n{GF.properties}")
# Create FieldArray instances (which are subclasses of np.ndarray)
x = GF([236, 87, 38, 112])
y = GF(np.array([109, 201, 2, 45]))
print(f"\nx (type: {type(x)}): {x}")
print(f"y (type: {type(y)}): {y}")
# Perform arithmetic operations in the finite field
result_add = x + y
result_mul = x * y
print(f"\nx + y: {result_add}")
print(f"x * y: {result_mul}")