Finite field arithmetic

In mathematics, finite field arithmetic is arithmetic in a finite field (a field containing a finite number of elements) contrary to arithmetic in a field with an infinite number of elements, like the field of rational numbers.

There are infinitely many different finite fields. Their number of elements is necessarily of the form pn where p is a prime number and n is a positive integer, and two finite fields of the same size are isomorphic. The prime p is called the characteristic of the field, and the positive integer n is called the dimension of the field over its prime field.

Finite fields are used in a variety of applications, including in classical coding theory in linear block codes such as BCH codes and Reed–Solomon error correction, in cryptography algorithms such as the Rijndael (AES) encryption algorithm, in tournament scheduling, and in the design of experiments.

Effective polynomial representation
The finite field with pn elements is denoted GF(pn) and is also called the Galois field of order pn, in honor of the founder of finite field theory, Évariste Galois. GF(p), where p is a prime number, is simply the ring of integers modulo p. That is, one can perform operations (addition, subtraction, multiplication) using the usual operation on integers, followed by reduction modulo p. For instance, in GF(5), 4 + 3 = 7 is reduced to 2 modulo 5. Division is multiplication by the inverse modulo p, which may be computed using the extended Euclidean algorithm.

A particular case is GF(2), where addition is exclusive OR (XOR) and multiplication is AND. Since the only invertible element is 1, division is the identity function.

Elements of GF(pn) may be represented as polynomials of degree strictly less than n over GF(p). Operations are then performed modulo m(x) where m(x) is an irreducible polynomial of degree n over GF(p), for instance using polynomial long division. Addition is the usual addition of polynomials, but the coefficients are reduced modulo p. Multiplication is also the usual multiplication of polynomials, but with coefficients multiplied modulo p and polynomials multiplied modulo the polynomial m(x). This representation in terms of polynomial coefficients is called a monomial basis (a.k.a. 'polynomial basis').

There are other representations of the elements of GF(pn); some are isomorphic to the polynomial representation above and others look quite different (for instance, using matrices). Using a normal basis may have advantages in some contexts.

When the prime is 2, it is conventional to express elements of GF(pn) as binary numbers, with the coefficient of each term in a polynomial represented by one bit in the corresponding element's binary expression. Braces ( "{" and "}" ) or similar delimiters are commonly added to binary numbers, or to their hexadecimal equivalents, to indicate that the value gives the coefficients of a basis of a field, thus representing an element of the field. For example, the following are equivalent representations of the same value in a characteristic 2 finite field:

Primitive polynomials
There are many irreducible polynomials (sometimes called reducing polynomials) that can be used to generate a finite field, but they do not all give rise to the same representation of the field.

A monic irreducible polynomial of degree $n$ having coefficients in the finite field GF($q$), where $q = p^{t}$ for some prime $p$ and positive integer $t$, is called a primitive polynomial if all of its roots are primitive elements of GF($q^{n}$). In the polynomial representation of the finite field, this implies that $q$ is a primitive element. There is at least one irreducible polynomial for which $q$ is a primitive element. In other words, for a primitive polynomial, the powers of $x$ generate every nonzero value in the field.

In the following examples it is best not to use the polynomial representation, as the meaning of $x$ changes between the examples. The monic irreducible polynomial $x^{8} + x^{4} + x^{3} + x + 1$ over GF(2) is not primitive. Let $λ$ be a root of this polynomial (in the polynomial representation this would be $x$), that is, $λ^{8} + λ^{4} + λ^{3} + λ + 1 = 0$. Now $λ^{51} = 1$, so $λ$ is not a primitive element of GF(28) and generates a multiplicative subgroup of order 51. The monic irreducible polynomial $x^{8} + x^{4} + x^{3} + x^{2} + 1$ over GF(2) is primitive, and all 8 roots are generators of $GF(2^{8})$.

All GF(28) have a total of 128 generators (see Number of primitive elements), and for a primitive polynomial, 8 of them are roots of the reducing polynomial. Having $x$ as a generator for a finite field is beneficial for many computational mathematical operations.

Addition and subtraction
Addition and subtraction are performed by adding or subtracting two of these polynomials together, and reducing the result modulo the characteristic.

In a finite field with characteristic 2, addition modulo 2, subtraction modulo 2, and XOR are identical. Thus,

Under regular addition of polynomials, the sum would contain a term 2x6. This term becomes 0x6 and is dropped when the answer is reduced modulo 2.

Here is a table with both the normal algebraic sum and the characteristic 2 finite field sum of a few polynomials:

In computer science applications, the operations are simplified for finite fields of characteristic 2, also called GF(2n) Galois fields, making these fields especially popular choices for applications.

Multiplication
Multiplication in a finite field is multiplication modulo an irreducible reducing polynomial used to define the finite field. (I.e., it is multiplication followed by division using the reducing polynomial as the divisor&mdash;the remainder is the product.) The symbol "•" may be used to denote multiplication in a finite field.

Rijndael's (AES) finite field
Rijndael (standardised as AES) uses the characteristic 2 finite field with 256 elements, which can also be called the Galois field GF(28). It employs the following reducing polynomial for multiplication:


 * x8 + x4 + x3 + x + 1.

For example, {53} • {CA} = {01} in Rijndael's field because




 * || (x6 + x4 + x + 1)(x7 + x6 + x3 + x)
 * = || (x13 + x12 + x9 + x7) + (x11 + x10 + x7 + x5) + (x8 + x7 + x4 + x2) + (x7 + x6 + x3 + x)
 * = || x13 + x12 + x9 + x11 + x10 + x5 + x8 + x4 + x2 + x6 + x3 + x
 * = || x13 + x12 + x11 + x10 + x9 + x8 + x6 + x5 + x4 + x3 + x2 + x
 * }
 * = || x13 + x12 + x9 + x11 + x10 + x5 + x8 + x4 + x2 + x6 + x3 + x
 * = || x13 + x12 + x11 + x10 + x9 + x8 + x6 + x5 + x4 + x3 + x2 + x
 * }
 * }

and




 * || x13 + x12 + x11 + x10 + x9 + x8 + x6 + x5 + x4 + x3 + x2 + x mod x8 + x4 + x3 + x1 + 1
 * = || (11111101111110 mod 100011011)
 * = || {3F7E mod 11B} = {01}
 * = || 1 (decimal)
 * }
 * = || {3F7E mod 11B} = {01}
 * = || 1 (decimal)
 * }
 * }

The latter can be demonstrated through long division (shown using binary notation, since it lends itself well to the task. Notice that exclusive OR is applied in the example and not arithmetic subtraction, as one might use in grade-school long division.):

11111101111110 (mod) 100011011 ^100011011               01110000011110           ^ 100011011                0110110101110            ^100011011                010101110110             ^100011011                00100011010               ^100011011                  000000001

(The elements {53} and {CA} are multiplicative inverses of one another since their product is 1.)

Multiplication in this particular finite field can also be done using a modified version of the "peasant's algorithm". Each polynomial is represented using the same binary notation as above. Eight bits is sufficient because only degrees 0 to 7 are possible in the terms of each (reduced) polynomial.

This algorithm uses three variables (in the computer programming sense), each holding an eight-bit representation. a and b are initialized with the multiplicands; p accumulates the product and must be initialized to 0.

At the start and end of the algorithm, and the start and end of each iteration, this invariant is true: a b + p is the product. This is obviously true when the algorithm starts. When the algorithm terminates, a or b will be zero so p will contain the product.


 * Run the following loop eight times (once per bit). It is OK to stop when a or b is zero before an iteration:
 * If the rightmost bit of b is set, exclusive OR the product p by the value of a. This is polynomial addition.
 * Shift b one bit to the right, discarding the rightmost bit, and making the leftmost bit have a value of zero. This divides the polynomial by x, discarding the x0 term.
 * Keep track of whether the leftmost bit of a is set to one and call this value carry.
 * Shift a one bit to the left, discarding the leftmost bit, and making the new rightmost bit zero. This multiplies the polynomial by x, but we still need to take account of carry which represented the coefficient of x7.
 * If carry had a value of one, exclusive or a with the hexadecimal number  (00011011 in binary).   corresponds to the irreducible polynomial with the high term eliminated. Conceptually, the high term of the irreducible polynomial and carry add modulo 2 to 0.
 * p now has the product

This algorithm generalizes easily to multiplication over other fields of characteristic 2, changing the lengths of a, b, and p and the value  appropriately.

Multiplicative inverse
The multiplicative inverse for an element a of a finite field can be calculated a number of different ways:


 * By multiplying a by every number in the field until the product is one. This is a brute-force search.
 * Since the nonzero elements of GF(pn) form a finite group with respect to multiplication, a = 1 (for a ≠ 0), thus the inverse of a is a.
 * By using the extended Euclidean algorithm.
 * By making logarithm and exponentiation tables for the finite field, subtracting the logarithm from pn − 1 and exponentiating the result.
 * By making a modular multiplicative inverse table for the finite field and doing a lookup.
 * By mapping to a composite field where inversion is simpler, and mapping back.
 * By constructing a special integer (in case of a finite field of a prime order) or a special polynomial (in case of a finite field of a non-prime order) and dividing it by a.

Generator based tables
When developing algorithms for Galois field computation on small Galois fields, a common performance optimization approach is to find a generator g and use the identity:


 * $$ab = g^{\log_g(ab)} = g^{\log_g(a) + \log_g (b)}$$

to implement multiplication as a sequence of table look ups for the logg(a) and gy functions and an integer addition operation. This exploits the property that every finite field contains generators. In the Rijndael field example, the polynomial x + 1 (or {03}) is one such generator. A necessary but not sufficient condition for a polynomial to be a generator is to be irreducible.

An implementation must test for the special case of a or b being zero, as the product will also be zero.

This same strategy can be used to determine the multiplicative inverse with the identity:


 * $$a^{-1} = g^{\log_g\left(a^{-1}\right)} = g^{-\log_g(a)} = g^{|g| - \log_g(a)}$$

Here, the order of the generator, $x$, is the number of non-zero elements of the field. In the case of GF(28) this is 28 − 1 = 255. That is to say, for the Rijndael example: (x + 1)255 = 1. So this can be performed with two look up tables and an integer subtract. Using this idea for exponentiation also derives benefit:


 * $$a^n = g^{\log_g\left(a^n\right)} = g^{n\log_g(a)} = g^{n\log_g(a) \pmod{|g|}}$$

This requires two table look ups, an integer multiplication and an integer modulo operation. Again a test for the special case a = 0 must be performed.

However, in cryptographic implementations, one has to be careful with such implementations since the cache architecture of many microprocessors leads to variable timing for memory access. This can lead to implementations that are vulnerable to a timing attack.

Carryless multiply
For binary fields GF(2n), field multiplication can be implemented using a carryless multiply such as CLMUL instruction set, which is good for n ≤ 64. A multiplication uses one carryless multiply to produce a product (up to 2n − 1 bits), another carryless multiply of a pre-computed inverse of the field polynomial to produce a quotient = ⌊product / (field polynomial)⌋, a multiply of the quotient by the field polynomial, then an xor: result = product &oplus; ((field polynomial) ⌊product / (field polynomial)⌋). The last 3 steps (pclmulqdq, pclmulqdq, xor) are used in the Barrett reduction step for fast computation of CRC using the x86 pclmulqdq instruction.

Composite exponent
When k is a composite number, there will exist isomorphisms from a binary field GF(2k) to an extension field of one of its subfields, that is, GF((2m)n) where k = m n. Utilizing one of these isomorphisms can simplify the mathematical considerations as the degree of the extension is smaller with the trade off that the elements are now represented over a larger subfield. To reduce gate count for hardware implementations, the process may involve multiple nesting, such as mapping from GF(28) to GF(((22)2)2).

C programming example
Here is some C code which will add and multiply numbers in the characteristic 2 finite field of order 28, used for example by Rijndael algorithm or Reed–Solomon, using the Russian peasant multiplication algorithm:

This example has cache, timing, and branch prediction side-channel leaks, and is not suitable for use in cryptography.

D programming example
This D program will multiply numbers in Rijndael's finite field and generate a PGM image:

This example does not use any branches or table lookups in order to avoid side channels and is therefore suitable for use in cryptography.