# Compilation Modes¶

The galois library supports finite field arithmetic on NumPy arrays by just-in-time compiling custom NumPy ufuncs. It uses Numba to JIT compile ufuncs written in pure Python. The created FieldArray subclass GF intercepts NumPy calls to a given ufunc, JIT compiles the finite field ufunc (if not already cached), and then invokes the new ufunc on the input array(s).

There are two primary compilation modes: "jit-lookup" and "jit-calculate". The supported ufunc compilation modes of a given finite field are listed in ufunc_modes.

In : GF = galois.GF(3**5)

In : GF.ufunc_modes
Out: ['jit-lookup', 'jit-calculate']


Large finite fields, which have numpy.object_ data types, use "python-calculate" which utilizes non-compiled, pure-Python ufuncs.

In : GF = galois.GF(2**100)

In : GF.ufunc_modes
Out: ['python-calculate']


## Lookup tables¶

The lookup table compilation mode "jit-lookup" uses exponential, logarithm, and Zech logarithm lookup tables to speed up arithmetic computations. These tables are built once at FieldArray subclass-creation time during the call to GF().

The exponential and logarithm lookup tables map every finite field element to a power of the primitive element $$\alpha$$.

$x = \alpha^i$

$\textrm{log}_{\alpha}(x) = i$

With these lookup tables, many arithmetic operations are simplified. For instance, multiplication of two finite field elements is reduced to three lookups and one integer addition.

$\begin{split} x \cdot y &= \alpha^m \cdot \alpha^n \\ &= \alpha^{m + n} \end{split}$

The Zech logarithm is defined below. A similar lookup table is created for it.

$1 + \alpha^i = \alpha^{Z(i)}$

$Z(i) = \textrm{log}_{\alpha}(1 + \alpha^i)$

With Zech logarithms, addition of two finite field elements becomes three lookups, one integer addition, and one integer subtraction.

$\begin{split} x + y &= \alpha^m + \alpha^n \\ &= \alpha^m (1 + \alpha^{n - m}) \\ &= \alpha^m \alpha^{Z(n - m)} \\ &= \alpha^{m + Z(n - m)} \end{split}$

Finite fields with order less than $$2^{20}$$ use lookup tables by default. In the limited cases where explicit calculation is faster than table lookup, the explicit calculation is used.

In : GF = galois.GF(3**5)

In : GF.ufunc_mode
Out: 'jit-lookup'


## Explicit calculation¶

Finite fields with order greater than $$2^{20}$$ use explicit calculation by default. This eliminates the need to store large lookup tables. However, explicit calculation is usually slower than table lookup.

In : GF = galois.GF(2**24)

In : GF.ufunc_mode
Out: 'jit-calculate'


However, if memory is of no concern, even large fields can be compiled to use lookup tables. Initially constructing the lookup tables may take some time, however.

In : GF = galois.GF(2**24, compile="jit-lookup")

In : GF.ufunc_mode
Out: 'jit-lookup'


## Python explicit calculation¶

Large finite fields cannot use JIT compiled ufuncs. This is because they cannot use NumPy integer data types. This is either because the order of the field or an intermediate arithmetic result is larger than the max value of numpy.int64.

These finite fields use the numpy.object_ data type and have ufunc compilation mode "python-calculate". This mode does not compile the Python functions, but rather converts them into Python ufuncs using numpy.frompyfunc(). The lack of JIT compilation allows the ufuncs to operate on Python integers, which have unlimited size. This does come with a performance penalty, however.

In : GF = galois.GF(2**100)

In : GF.ufunc_mode
Out: 'python-calculate'


## Recompile the ufuncs¶

The compilation mode may be explicitly set during creation of the FieldArray subclass using the compile keyword argument to GF().

Here, the FieldArray subclass for $$\mathrm{GF}(3^5)$$ would normally select "jit-lookup" as its default compilation mode. However, we can intentionally choose explicit calculation.

In : GF = galois.GF(3**5, compile="jit-calculate")

In : GF.ufunc_mode
Out: 'jit-calculate'


After a FieldArray subclass has been created, its compilation mode may be changed using the compile() method.

In : GF.compile("jit-lookup")

In : GF.ufunc_mode
Out: 'jit-lookup'


This will not immediately recompile all of the ufuncs. The ufuncs are compiled on-demand (during their first invocation) and only if a cached version is not available.

Last update: Aug 06, 2023