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 for a prime , with every result replaced by its remainder after division by . The modular inverse of modulo is the number from 1 to for which leaves remainder 1 on division by . 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 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 and gave the same value, 7 would divide , 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 . The elements of the field are the whole numbers 0 to . Write for the remainder of after division by , taken in the range 0 to .
Add, subtract, multiply. Compute with ordinary whole numbers, then reduce. Modulo 17, and .
Invert. For a nonzero element , find the element with .
Divide. Define as .
Two standard methods find the inverse.
The extended Euclidean algorithm §14.1 divides with remainder again and again, as in the school method for the greatest common divisor (the largest whole number that divides both of two given numbers), and keeps track of how each remainder is built from and . It ends with whole numbers and , either of which may be negative, such that . Then is the inverse.
Fermat’s little theorem says that for every nonzero element . So is the inverse, because .
An inverse of modulo any whole number exists exactly when and have no common divisor larger than 1. When 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 elements for every prime and every whole number , and with no other sizes. For the elements are not whole numbers modulo . Standards write the field with elements as , 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: Now build 1 out of 3 and 17 by substituting backwards: So , and the inverse of 3 is 6.
The same inverse by Fermat’s little theorem. Here , and . Square repeatedly and reduce modulo 17 each time: ; then , so is 13; then , so is 16. Multiply the four powers 16, 13, 9 and 3 together and reduce at each step: , giving 4; , giving 2; . The result is 6 again.
The division. 5 divided by 3 is 5 times the inverse of 3: , so the answer is 13. Check it by multiplying back: . Ordinary integer division gives 1 instead, and 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 , 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 with two operations, addition and multiplication, and two different elements 0 and 1, such that:
under addition, is a commutative group with identity 0;
under multiplication, the elements of other than 0 are a commutative group with identity 1;
for all , and in .
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, is defined as for and is undefined for .
For whole numbers and with , a whole number is an inverse of modulo when , which means that is a multiple of . The following statements are theorems. They rest on no unproven assumption.
has an inverse modulo exactly when , where is the greatest common divisor. Exactly one of the numbers 0 to is then an inverse.
The whole numbers 0 to with arithmetic modulo form a field exactly when is prime.
Fermat’s little theorem: if is prime and does not divide , then , and so .
A finite field with elements exists exactly when 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 , or and is not field arithmetic. No even number has an inverse modulo , 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: , 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 , while the field element is .
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 , 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 . Integer division gives 1, and is 3, not 5.
Field division is therefore defined as multiplication by the modular inverse: the inverse of 3 modulo 17 is 6, because , and . 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].