# Intro to Prime Fields¶

A Galois field is a finite field named in honor of Évariste Galois, one of the fathers of group theory. A field is a set that is closed under addition, subtraction, multiplication, and division. To be closed under an operation means that performing the operation on any two elements of the set will result in another element from the set. A finite field is a field with a finite set of elements.

Galois proved that finite fields exist only when their order (or size of the set) is a prime power $$p^m$$. Accordingly, finite fields can be broken into two categories: prime fields $$\mathrm{GF}(p)$$ and extension fields $$\mathrm{GF}(p^m)$$. This tutorial will focus on prime fields.

## Prime field¶

In this tutorial, we will consider the prime field $$\mathrm{GF}(7)$$. Using the galois library, the FieldArray subclass GF7 is created using the class factory GF().

In : GF7 = galois.GF(7)

In : print(GF7.properties)
Galois Field:
name: GF(7)
characteristic: 7
degree: 1
order: 7
irreducible_poly: x + 4
is_primitive_poly: True
primitive_element: 3

In : GF7 = galois.GF(7, display="power")

In : print(GF7.properties)
Galois Field:
name: GF(7)
characteristic: 7
degree: 1
order: 7
irreducible_poly: x + 4
is_primitive_poly: True
primitive_element: 3


Note

In this tutorial, we use the display="int" default to display the elements. However, sometimes it is useful to view elements in their power representation $$\{0, 1, \alpha, \alpha^2, \dots, \alpha^{p^m - 2}\}$$. Switch the display between these two representations using the tabbed sections. Note, the polynomial representation is not shown because it is identical to the integer representation for prime fields.

See Element Representation for more details.

## Elements¶

The elements of the finite field $$\mathrm{GF}(p)$$ are naturally represented as the integers $$\{0, 1, \dots, p - 1\}$$.

The elements of the finite field are retrieved in a 1-D array using the Elements() classmethod.

In : GF7.Elements()
Out: GF([0, 1, 2, 3, 4, 5, 6], order=7)

In : GF7.Elements()
Out: GF([  0,   1, α^2,   α, α^4, α^5, α^3], order=7)


## Arithmetic¶

Addition, subtraction, and multiplication in $$\mathrm{GF}(p)$$ is equivalent to integer addition, subtraction, and multiplication reduced modulo $$p$$. Mathematically speaking, this is the integer ring $$\mathbb{Z} / p\mathbb{Z}$$.

In this tutorial, consider two field elements $$a = 3$$ and $$b = 5$$. We will use galois to perform explicit modular integer arithmetic and then prime field arithmetic.

Here are $$a$$ and $$b$$ represented as Python integers.

In : a_int = 3

In : b_int = 5

In : p = GF7.characteristic; p
Out: 7


Here are $$a$$ and $$b$$ represented as prime field elements. See Array Creation for more details.

In : a = GF7(3); a
Out: GF(3, order=7)

In : b = GF7(5); b
Out: GF(5, order=7)

In : a = GF7(3); a
Out: GF(α, order=7)

In : b = GF7(5); b
Out: GF(α^5, order=7)


We can see that $$3 + 5 \equiv 1\ (\textrm{mod}\ 7)$$. So accordingly, $$3 + 5 = 1$$ in $$\mathrm{GF}(7)$$.

In : (a_int + b_int) % p
Out: 1

In : a + b
Out: GF(1, order=7)

In : (a_int + b_int) % p
Out: 1

In : a + b
Out: GF(1, order=7)


The galois library includes the ability to display the arithmetic tables for any finite field. The table is only readable for small fields, but nonetheless the capability is provided. Select a few computations at random and convince yourself the answers are correct.

In : print(GF7.arithmetic_table("+"))
+-------+---+---+---+---+---+---+---+
| x + y | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
+-------+---+---+---+---+---+---+---+
|     0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
+-------+---+---+---+---+---+---+---+
|     1 | 1 | 2 | 3 | 4 | 5 | 6 | 0 |
+-------+---+---+---+---+---+---+---+
|     2 | 2 | 3 | 4 | 5 | 6 | 0 | 1 |
+-------+---+---+---+---+---+---+---+
|     3 | 3 | 4 | 5 | 6 | 0 | 1 | 2 |
+-------+---+---+---+---+---+---+---+
|     4 | 4 | 5 | 6 | 0 | 1 | 2 | 3 |
+-------+---+---+---+---+---+---+---+
|     5 | 5 | 6 | 0 | 1 | 2 | 3 | 4 |
+-------+---+---+---+---+---+---+---+
|     6 | 6 | 0 | 1 | 2 | 3 | 4 | 5 |
+-------+---+---+---+---+---+---+---+

In : print(GF7.arithmetic_table("+"))
+-------+-----+-----+-----+-----+-----+-----+-----+
| x + y |   0 |   1 |   α | α^2 | α^3 | α^4 | α^5 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|     0 |   0 |   1 |   α | α^2 | α^3 | α^4 | α^5 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|     1 |   1 | α^2 | α^4 |   α |   0 | α^5 | α^3 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|     α |   α | α^4 | α^3 | α^5 | α^2 |   0 |   1 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|   α^2 | α^2 |   α | α^5 | α^4 |   1 | α^3 |   0 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|   α^3 | α^3 |   0 | α^2 |   1 | α^5 |   α | α^4 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|   α^4 | α^4 | α^5 |   0 | α^3 |   α |   1 | α^2 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|   α^5 | α^5 | α^3 |   1 |   0 | α^4 | α^2 |   α |
+-------+-----+-----+-----+-----+-----+-----+-----+


### Subtraction¶

As with addition, we can see that $$3 - 5 \equiv 5\ (\textrm{mod}\ 7)$$. So accordingly, $$3 - 5 = 5$$ in $$\mathrm{GF}(7)$$.

In : (a_int - b_int) % p
Out: 5

In : a - b
Out: GF(5, order=7)

In : (a_int - b_int) % p
Out: 5

In : a - b
Out: GF(α^5, order=7)


Here is the subtraction table for completeness.

In : print(GF7.arithmetic_table("-"))
+-------+---+---+---+---+---+---+---+
| x - y | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
+-------+---+---+---+---+---+---+---+
|     0 | 0 | 6 | 5 | 4 | 3 | 2 | 1 |
+-------+---+---+---+---+---+---+---+
|     1 | 1 | 0 | 6 | 5 | 4 | 3 | 2 |
+-------+---+---+---+---+---+---+---+
|     2 | 2 | 1 | 0 | 6 | 5 | 4 | 3 |
+-------+---+---+---+---+---+---+---+
|     3 | 3 | 2 | 1 | 0 | 6 | 5 | 4 |
+-------+---+---+---+---+---+---+---+
|     4 | 4 | 3 | 2 | 1 | 0 | 6 | 5 |
+-------+---+---+---+---+---+---+---+
|     5 | 5 | 4 | 3 | 2 | 1 | 0 | 6 |
+-------+---+---+---+---+---+---+---+
|     6 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
+-------+---+---+---+---+---+---+---+

In : print(GF7.arithmetic_table("-"))
+-------+-----+-----+-----+-----+-----+-----+-----+
| x - y |   0 |   1 |   α | α^2 | α^3 | α^4 | α^5 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|     0 |   0 | α^3 | α^4 | α^5 |   1 |   α | α^2 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|     1 |   1 |   0 | α^5 | α^3 | α^2 | α^4 |   α |
+-------+-----+-----+-----+-----+-----+-----+-----+
|     α |   α | α^2 |   0 |   1 | α^4 | α^3 | α^5 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|   α^2 | α^2 |   1 | α^3 |   0 |   α | α^5 | α^4 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|   α^3 | α^3 | α^5 |   α | α^4 |   0 | α^2 |   1 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|   α^4 | α^4 |   α |   1 | α^2 | α^5 |   0 | α^3 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|   α^5 | α^5 | α^4 | α^2 |   α | α^3 |   1 |   0 |
+-------+-----+-----+-----+-----+-----+-----+-----+


### Multiplication¶

Similarly, we can see that $$3 \cdot 5 \equiv 1\ (\textrm{mod}\ 7)$$. So accordingly, $$3 \cdot 5 = 1$$ in $$\mathrm{GF}(7)$$.

In : (a_int * b_int) % p
Out: 1

In : a * b
Out: GF(1, order=7)

In : (a_int * b_int) % p
Out: 1

In : a * b
Out: GF(1, order=7)


Here is the multiplication table for completeness.

In : print(GF7.arithmetic_table("*"))
+-------+---+---+---+---+---+---+---+
| x * y | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
+-------+---+---+---+---+---+---+---+
|     0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+-------+---+---+---+---+---+---+---+
|     1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
+-------+---+---+---+---+---+---+---+
|     2 | 0 | 2 | 4 | 6 | 1 | 3 | 5 |
+-------+---+---+---+---+---+---+---+
|     3 | 0 | 3 | 6 | 2 | 5 | 1 | 4 |
+-------+---+---+---+---+---+---+---+
|     4 | 0 | 4 | 1 | 5 | 2 | 6 | 3 |
+-------+---+---+---+---+---+---+---+
|     5 | 0 | 5 | 3 | 1 | 6 | 4 | 2 |
+-------+---+---+---+---+---+---+---+
|     6 | 0 | 6 | 5 | 4 | 3 | 2 | 1 |
+-------+---+---+---+---+---+---+---+

In : print(GF7.arithmetic_table("*"))
+-------+-----+-----+-----+-----+-----+-----+-----+
| x * y |   0 |   1 |   α | α^2 | α^3 | α^4 | α^5 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|     0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|     1 |   0 |   1 |   α | α^2 | α^3 | α^4 | α^5 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|     α |   0 |   α | α^2 | α^3 | α^4 | α^5 |   1 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|   α^2 |   0 | α^2 | α^3 | α^4 | α^5 |   1 |   α |
+-------+-----+-----+-----+-----+-----+-----+-----+
|   α^3 |   0 | α^3 | α^4 | α^5 |   1 |   α | α^2 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|   α^4 |   0 | α^4 | α^5 |   1 |   α | α^2 | α^3 |
+-------+-----+-----+-----+-----+-----+-----+-----+
|   α^5 |   0 | α^5 |   1 |   α | α^2 | α^3 | α^4 |
+-------+-----+-----+-----+-----+-----+-----+-----+


### Multiplicative inverse¶

Division in $$\mathrm{GF}(p)$$ is a little more difficult. Division can’t be as simple as taking $$a / b\ (\textrm{mod}\ p)$$ because many integer divisions do not result in integers! The division $$a / b$$ can be reformulated into $$a b^{-1}$$, where $$b^{-1}$$ is the multiplicative inverse of $$b$$. Let’s first learn the multiplicative inverse before returning to division.

Euclid discovered an efficient algorithm to solve the Bézout Identity, which is used to find the multiplicative inverse. It is now called the Extended Euclidean Algorithm. Given two integers $$x$$ and $$y$$, the Extended Euclidean Algorithm finds the integers $$s$$ and $$t$$ such that $$xs + yt = \textrm{gcd}(x, y)$$. This algorithm is implemented in egcd().

If $$x = 5$$ is a field element of $$\mathrm{GF}(7)$$ and $$y = 7$$ is the prime characteristic, then $$s = x^{-1}$$ in $$\mathrm{GF}(7)$$. Note, the GCD will always be $$1$$ because $$y$$ is prime.

# Returns (gcd, s, t)
In : galois.egcd(b_int, p)
Out: (1, 3, -2)


The galois library uses the Extended Euclidean Algorithm to compute multiplicative inverses (and division) in prime fields. The inverse of $$5$$ in $$\mathrm{GF}(7)$$ can be easily computed in the following way.

In : b ** -1
Out: GF(3, order=7)

In : np.reciprocal(b)
Out: GF(3, order=7)

In : b ** -1
Out: GF(α, order=7)

In : np.reciprocal(b)
Out: GF(α, order=7)


### Division¶

Now let’s return to division in finite fields. As mentioned earlier, $$a / b$$ is equivalent to $$a b^{-1}$$, and we have already learned multiplication and multiplicative inversion in finite fields.

To compute $$3 / 5$$ in $$\mathrm{GF}(7)$$, we can equivalently compute $$3 \cdot 5^{-1}$$ in $$\mathrm{GF}(7)$$.

In : _, b_inv_int, _ = galois.egcd(b_int, p)

In : (a_int * b_inv_int) % p
Out: 2

In : a * b**-1
Out: GF(2, order=7)

In : a / b
Out: GF(2, order=7)

In : _, b_inv_int, _ = galois.egcd(b_int, p)

In : (a_int * b_inv_int) % p
Out: 2

In : a * b**-1
Out: GF(α^2, order=7)

In : a / b
Out: GF(α^2, order=7)


Here is the division table for completeness. Notice that division is not defined for $$y = 0$$.

In : print(GF7.arithmetic_table("/"))
+-------+---+---+---+---+---+---+
| x / y | 1 | 2 | 3 | 4 | 5 | 6 |
+-------+---+---+---+---+---+---+
|     0 | 0 | 0 | 0 | 0 | 0 | 0 |
+-------+---+---+---+---+---+---+
|     1 | 1 | 4 | 5 | 2 | 3 | 6 |
+-------+---+---+---+---+---+---+
|     2 | 2 | 1 | 3 | 4 | 6 | 5 |
+-------+---+---+---+---+---+---+
|     3 | 3 | 5 | 1 | 6 | 2 | 4 |
+-------+---+---+---+---+---+---+
|     4 | 4 | 2 | 6 | 1 | 5 | 3 |
+-------+---+---+---+---+---+---+
|     5 | 5 | 6 | 4 | 3 | 1 | 2 |
+-------+---+---+---+---+---+---+
|     6 | 6 | 3 | 2 | 5 | 4 | 1 |
+-------+---+---+---+---+---+---+

In : print(GF7.arithmetic_table("/"))
+-------+-----+-----+-----+-----+-----+-----+
| x / y |   1 |   α | α^2 | α^3 | α^4 | α^5 |
+-------+-----+-----+-----+-----+-----+-----+
|     0 |   0 |   0 |   0 |   0 |   0 |   0 |
+-------+-----+-----+-----+-----+-----+-----+
|     1 |   1 | α^5 | α^4 | α^3 | α^2 |   α |
+-------+-----+-----+-----+-----+-----+-----+
|     α |   α |   1 | α^5 | α^4 | α^3 | α^2 |
+-------+-----+-----+-----+-----+-----+-----+
|   α^2 | α^2 |   α |   1 | α^5 | α^4 | α^3 |
+-------+-----+-----+-----+-----+-----+-----+
|   α^3 | α^3 | α^2 |   α |   1 | α^5 | α^4 |
+-------+-----+-----+-----+-----+-----+-----+
|   α^4 | α^4 | α^3 | α^2 |   α |   1 | α^5 |
+-------+-----+-----+-----+-----+-----+-----+
|   α^5 | α^5 | α^4 | α^3 | α^2 |   α |   1 |
+-------+-----+-----+-----+-----+-----+-----+


## Primitive elements¶

A property of finite fields is that some elements produce the non-zero elements of the field by their powers.

A primitive element $$g$$ of $$\mathrm{GF}(p)$$ is an element such that $$\mathrm{GF}(p) = \{0, 1, g, g^2, \dots, g^{p - 2}\}$$. The non-zero elements $$\{1, g, g^2, \dots, g^{p - 2}\}$$ form the cyclic multiplicative group $$\mathrm{GF}(p)^{\times}$$. A primitive element has multiplicative order $$\textrm{ord}(g) = p - 1$$.

In prime fields $$\mathrm{GF}(p)$$, the generators or primitive elements of $$\mathrm{GF}(p)$$ are primitive roots mod p.

### Primitive roots mod $$p$$¶

An integer $$g$$ is a primitive root mod p if every number coprime to $$p$$ can be represented as a power of $$g$$ mod $$p$$. Namely, every $$a$$ coprime to $$p$$ can be represented as $$g^k \equiv a\ (\textrm{mod}\ p)$$ for some $$k$$. In prime fields, since $$p$$ is prime, every integer $$1 \le a < p$$ is coprime to $$p$$.

Finding primitive roots mod $$p$$ is implemented in primitive_root() and primitive_roots().

In : galois.primitive_root(7)
Out: 3


### A primitive element¶

In galois, a primitive element of a finite field is provided by the primitive_element class property.

In : print(GF7.properties)
Galois Field:
name: GF(7)
characteristic: 7
degree: 1
order: 7
irreducible_poly: x + 4
is_primitive_poly: True
primitive_element: 3

In : g = GF7.primitive_element; g
Out: GF(3, order=7)

In : print(GF7.properties)
Galois Field:
name: GF(7)
characteristic: 7
degree: 1
order: 7
irreducible_poly: x + 4
is_primitive_poly: True
primitive_element: 3

In : g = GF7.primitive_element; g
Out: GF(α, order=7)


The galois package allows you to easily display all powers of an element and their equivalent polynomial, vector, and integer representations using repr_table(). Let’s ignore the polynomial and vector representations for now. They will become useful for extension fields.

Here is the representation table using the default generator $$g = 3$$. Notice its multiplicative order is $$p - 1$$.

In : g.multiplicative_order()
Out: 6

In : print(GF7.repr_table())
+-------+------------+--------+---------+
| Power | Polynomial | Vector | Integer |
+-------+------------+--------+---------+
|   0   |     0      |     |    0    |
+-------+------------+--------+---------+
|  3^0  |     1      |     |    1    |
+-------+------------+--------+---------+
|  3^1  |     3      |     |    3    |
+-------+------------+--------+---------+
|  3^2  |     2      |     |    2    |
+-------+------------+--------+---------+
|  3^3  |     6      |     |    6    |
+-------+------------+--------+---------+
|  3^4  |     4      |     |    4    |
+-------+------------+--------+---------+
|  3^5  |     5      |     |    5    |
+-------+------------+--------+---------+


### Other primitive elements¶

There are multiple primitive elements of any finite field. All primitive elements are provided in the primitive_elements class property.

In : list(galois.primitive_roots(7))
Out: [3, 5]

In : GF7.primitive_elements
Out: GF([3, 5], order=7)

In : g = GF7(5); g
Out: GF(5, order=7)

In : list(galois.primitive_roots(7))
Out: [3, 5]

In : GF7.primitive_elements
Out: GF([  α, α^5], order=7)

In : g = GF7(5); g
Out: GF(α^5, order=7)


This means that $$3$$ and $$5$$ generate the multiplicative group $$\mathrm{GF}(7)^\times$$. We can examine this by viewing the representation table using different generators.

Here is the representation table using a different generator $$g = 5$$. Notice it also has multiplicative order $$p- 1$$.

In : g.multiplicative_order()
Out: 6

In : print(GF7.repr_table(g))
+-------+------------+--------+---------+
| Power | Polynomial | Vector | Integer |
+-------+------------+--------+---------+
|   0   |     0      |     |    0    |
+-------+------------+--------+---------+
|  5^0  |     1      |     |    1    |
+-------+------------+--------+---------+
|  5^1  |     5      |     |    5    |
+-------+------------+--------+---------+
|  5^2  |     4      |     |    4    |
+-------+------------+--------+---------+
|  5^3  |     6      |     |    6    |
+-------+------------+--------+---------+
|  5^4  |     2      |     |    2    |
+-------+------------+--------+---------+
|  5^5  |     3      |     |    3    |
+-------+------------+--------+---------+


### Non-primitive elements¶

All other elements of the field cannot generate the multiplicative group. They have multiplicative orders less than $$p - 1$$.

For example, the element $$e = 2$$ is not a primitive element.

In : e = GF7(2); e
Out: GF(2, order=7)

In : e = GF7(2); e
Out: GF(α^2, order=7)


It has $$\textrm{ord}(e) = 3$$. Notice elements $$3$$, $$5$$, and $$6$$ are not represented by the powers of $$e$$.

In : e.multiplicative_order()
Out: 3

In : print(GF7.repr_table(e))
+-------+------------+--------+---------+
| Power | Polynomial | Vector | Integer |
+-------+------------+--------+---------+
|   0   |     0      |     |    0    |
+-------+------------+--------+---------+
|  2^0  |     1      |     |    1    |
+-------+------------+--------+---------+
|  2^1  |     2      |     |    2    |
+-------+------------+--------+---------+
|  2^2  |     4      |     |    4    |
+-------+------------+--------+---------+
|  2^3  |     1      |     |    1    |
+-------+------------+--------+---------+
|  2^4  |     2      |     |    2    |
+-------+------------+--------+---------+
|  2^5  |     4      |     |    4    |
+-------+------------+--------+---------+


Last update: May 18, 2022