Section 19.1.2

Finite field and modular inverse

Definition

A finite field is a finite set of values on which addition, subtraction, multiplication and division by any nonzero value are defined, stay inside the set and obey the usual rules of arithmetic. The simplest example is the whole numbers 0 to p−1p - 1 for a prime pp, with every result replaced by its remainder after division by pp. The modular inverse of aa modulo mm is the number bb from 1 to m−1m - 1 for which a×ba \times b leaves remainder 1 on division by mm. Field division is multiplication by this inverse.

The idea

Cryptographic formulas contain divisions. Adding two points on an elliptic curve §19.1.5, a structure that two devices can use to agree on a secret key, divides one difference of coordinates by another. Threshold secret sharing §5.44, which splits a secret into shares so that a set number of them can rebuild it, divides by differences of share numbers when it rebuilds the secret. The values in such formulas must come from one fixed finite set, so that each fits in a fixed number of bytes. Every result must also be exact, so that two parties who evaluate the same formula get the same bits. Fractions are not in the set. Rounding is not exact. Ordinary integer division throws the remainder away, and with it the information the formula needs: 5 divided by 3 becomes 1, and 1×31 \times 3 is 3, not 5.

A finite field keeps what division is for. In ordinary arithmetic, dividing by 3 undoes multiplying by 3. A finite field defines 5 divided by 3 as the one element that gives 5 when it is multiplied by 3. Such an element always exists when the modulus, the number that every result is reduced by, is prime, because multiplying all the nonzero elements by one fixed nonzero element only rearranges them. Modulo 7, multiplying 3 by 1, 2, 3, 4, 5 and 6 gives 3, 6, 2, 5, 1 and 4: every nonzero value appears exactly once. No value can appear twice: if two different multipliers xx and yy gave the same value, 7 would divide 3×(x−y)3 \times (x - y), and a prime divides a product only if it divides one of the factors, yet 7 divides neither 3 nor the difference of two different numbers from 1 to 6. The value 1 appears at the multiplier 5. So 5 is the inverse of 3 modulo 7, and dividing by 3 is the same as multiplying by 5.

How it works

Take a prime pp. The elements of the field are the whole numbers 0 to p−1p - 1. Write xmodpx \bmod p for the remainder of xx after division by pp, taken in the range 0 to p−1p - 1.

  1. Add, subtract, multiply. Compute with ordinary whole numbers, then reduce. Modulo 17, 11+9=2011 + 9 = 20 and 20mod17=320 \bmod 17 = 3.

  2. Invert. For a nonzero element aa, find the element a−1a^{-1} with (a×a−1)modp=1(a \times a^{-1}) \bmod p = 1.

  3. Divide. Define c/ac / a as (c×a−1)modp(c \times a^{-1}) \bmod p.

Two standard methods find the inverse.

An inverse of aa modulo any whole number mm exists exactly when aa and mm have no common divisor larger than 1. When mm is prime, every nonzero element qualifies, which is why a prime modulus gives a field. Such a field is called a prime field. A modulus with factors, such as 15, does not give a field.

Finite fields exist with pkp^k elements for every prime pp and every whole number k≥1k \ge 1, and with no other sizes. For k>1k > 1 the elements are not whole numbers modulo pkp^k. Standards write the field with qq elements as GF(q)\mathrm{GF}(q), where GF stands for Galois field, another name for a finite field.

Worked example

Work modulo the prime 17 and compute 5 divided by 3.

The inverse of 3 by the extended Euclidean algorithm. Divide with remainder until the remainder is 1: 17=5×3+2,3=1×2+1.17 = 5 \times 3 + 2, \qquad 3 = 1 \times 2 + 1. Now build 1 out of 3 and 17 by substituting backwards: 1=3−2=3−(17−5×3)=6×3−17.1 = 3 - 2 = 3 - (17 - 5 \times 3) = 6 \times 3 - 17. So 6×3=18=17+16 \times 3 = 18 = 17 + 1, and the inverse of 3 is 6.

The same inverse by Fermat’s little theorem. Here p−2=15p - 2 = 15, and 15=8+4+2+115 = 8 + 4 + 2 + 1. Square repeatedly and reduce modulo 17 each time: 32=93^2 = 9; then 92=81=4×17+139^2 = 81 = 4 \times 17 + 13, so 343^4 is 13; then 132=169=9×17+1613^2 = 169 = 9 \times 17 + 16, so 383^8 is 16. Multiply the four powers 16, 13, 9 and 3 together and reduce at each step: 16×13=208=12×17+416 \times 13 = 208 = 12 \times 17 + 4, giving 4; 4×9=36=2×17+24 \times 9 = 36 = 2 \times 17 + 2, giving 2; 2×3=62 \times 3 = 6. The result is 6 again.

The division. 5 divided by 3 is 5 times the inverse of 3: 5×6=30=17+135 \times 6 = 30 = 17 + 13, so the answer is 13. Check it by multiplying back: 3×13=39=2×17+53 \times 13 = 39 = 2 \times 17 + 5. Ordinary integer division gives 1 instead, and 3×1=33 \times 1 = 3 does not return 5.

A modulus that is not prime. Modulo 15, the multiples of 3 are 0, 3, 6, 9 and 12, again and again. The value 1 never appears, so 3 has no inverse. Also 3×5=153 \times 5 = 15, which reduces to 0: two nonzero values multiply to zero. Division by 3 cannot be defined, and the whole numbers modulo 15 are not a field.

Stated precisely

A field is a set FF with two operations, addition and multiplication, and two different elements 0 and 1, such that:

A commutative group is a set with an operation that is associative (regrouping a chain of operations does not change the result) and commutative (swapping the two operands does not change the result), has an identity element and gives every element an inverse. A finite field is a field with finitely many elements. In a field, c/ac / a is defined as ca−1c a^{-1} for a≠0a \ne 0 and is undefined for a=0a = 0.

For whole numbers aa and mm with m≥2m \ge 2, a whole number bb is an inverse of aa modulo mm when ab≡1(modm)ab \equiv 1 \pmod{m}, which means that ab−1ab - 1 is a multiple of mm. The following statements are theorems. They rest on no unproven assumption.

  1. aa has an inverse modulo mm exactly when gcd(a,m)=1\gcd(a, m) = 1, where gcd\gcd is the greatest common divisor. Exactly one of the numbers 0 to m−1m - 1 is then an inverse.

  2. The whole numbers 0 to m−1m - 1 with arithmetic modulo mm form a field exactly when mm is prime.

  3. Fermat’s little theorem: if pp is prime and does not divide aa, then ap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p}, and so a−1≡ap−2(modp)a^{-1} \equiv a^{p-2} \pmod{p}.

  4. A finite field with qq elements exists exactly when qq is a power of a prime.

Limits and common mistakes

Zero has no inverse in any field. Code that divides must treat a divisor equal to zero as a separate case. In the formulas for adding points on an elliptic curve, a divisor equal to zero marks the cases that have their own rule.

Unsigned modular arithmetic §4.16, which is what a processor does with unsigned integers (non-negative whole numbers stored in a fixed number of bits), works modulo a power of two such as 282^8, 2322^{32} or 2642^{64} and is not field arithmetic. No even number has an inverse modulo 28=2562^8 = 256, because every multiple of an even number is even and so never leaves remainder 1 on division by 256. Two nonzero values can also multiply to zero: 2×128=2562 \times 128 = 256, which reduces to 0. The field with 256 elements is a different structure. The Advanced Encryption Standard (AES) uses it, and adds two of its elements by bitwise XOR (exclusive OR), which outputs a 1 bit wherever the two bytes differ. In hexadecimal notation, 0x57 plus 0x83 is 0xd4 in that field, while the sum modulo 256 is 0xda.

The operators / and % of the C programming language are not field operations. For integer operands, the C11 standard defines / to discard the fractional part of the quotient, and % can then return a negative number: -7 % 17 is −7-7, while the field element is −7+17=10-7 + 17 = 10.

Correct mathematics is not yet a safe implementation. The product of two 256-bit numbers needs 512 bits before it is reduced. When the operands are secret, the running time must not depend on them. The number of steps of the extended Euclidean algorithm depends on its inputs. The Fermat method uses the fixed, public exponent p−2p - 2, and the key agreement function X25519 is specified with that method. The entry Constant-time cryptographic implementation §5.36 treats this requirement.

Questions answered

Why cannot division in a finite field be ordinary integer division?

Division in a finite field cannot be ordinary integer division because integer division throws away the remainder, so it does not undo multiplication, and undoing multiplication is what a formula means when it divides. A finite field is a finite set of numbers with exact addition, subtraction, multiplication and division, such as the whole numbers 0 to 16 with every result reduced to its remainder after division by 17. In that field, 5 divided by 3 must be the element that gives 5 when it is multiplied by 3. That element is 13, because 3×13=39=2×17+53 \times 13 = 39 = 2 \times 17 + 5. Integer division gives 1, and 3×13 \times 1 is 3, not 5.

Field division is therefore defined as multiplication by the modular inverse: the inverse of 3 modulo 17 is 6, because 3×6=18=17+13 \times 6 = 18 = 17 + 1, and 5×6=30=17+135 \times 6 = 30 = 17 + 13. Every nonzero element has such an inverse exactly because the modulus is prime. A formula that divides, such as the slope in elliptic-curve point addition, keeps its meaning only under this definition. With integer division the two parties to a protocol would still compute something, but the equations that make the protocol correct would no longer hold.

Related entries: Threshold secret sharing (§5.44), Unsigned modular arithmetic (§4.16), Constant-time cryptographic implementation (§5.36), Group, cyclic subgroup, generator, and order (§19.1.1), Elliptic-curve Diffie–Hellman (ECDH) (§19.1.5). Further reading: [47, 78, 98].