Finite Fields (Galois Fields: GF(p) and GF(2^n))
- •
What it is
- •
A field is a set with two operations (addition and multiplication) where both behave like ordinary arithmetic — associative, commutative, distributive — and, critically, every nonzero element has a multiplicative inverse (division works, except by zero). A finite field (Galois field, written or ) exists precisely when is a prime or a prime power. Two shapes show up constantly in cryptography: prime fields — ordinary integers mod a prime , the setting for Diffie-Hellman, RSA's underlying number theory, and quadratic-residue attacks — and binary extension fields — polynomials with coefficients in , added by XOR and multiplied modulo a fixed irreducible polynomial, the setting for AES's S-box/MixColumns arithmetic and GCM's GHASH. Despite looking different, both are genuine fields: 's "addition" is bitwise XOR (its own inverse — no separate subtraction needed), and its "multiplication" wraps around using a fixed reduction polynomial the same way integer multiplication mod wraps around using .
- •
- •
When to apply
- •
Any time arithmetic is described as happening "in " or "in "/"" — recognizing which kind of field you're in tells you which arithmetic rules actually apply (ordinary mod-p arithmetic vs. XOR-based polynomial arithmetic), which is easy to get wrong by assuming one when the code actually implements the other.
- •
- •
Symbols and assumptions
- •
is arithmetic modulo prime . consists of binary polynomials of degree below , reduced modulo a chosen irreducible polynomial of degree . It is not integer arithmetic modulo .
- •
- •
Math — step by step
- •
- In a prime field, add or multiply integers and reduce modulo . Every nonzero denominator has an inverse, so division means multiplying by that inverse.
- •
- In a binary field, coefficient addition is XOR: . Multiplication is polynomial multiplication, followed by replacing multiples of with zero.
- •
- Irreducibility matters: it prevents zero divisors and guarantees inverses for nonzero elements. Bit strings are just the chosen polynomial representation; integer multiplication of their encodings is generally wrong.
- •
- •
Worked example
- •
Python
- •
# GF(2^n) multiplication via shift-and-XOR (here: GF(2^8), AES's reduction polynomial 0x11b) def gf28_mul(a, b): p = 0 for _ in range(8): if b & 1: p ^= a hi = a & 0x80 a = (a << 1) & 0xFF if hi: a ^= 0x1b b >>= 1 return p
- •
- •
SageMath
- •
F = GF(2^8, modulus=x^8 + x^4 + x^3 + x + 1, names='a') # binary extension field Fp = GF(p) # prime field
- •
- •
- •
Cards
- •
What's the key difference between how GF(p) and GF(2^n) implement "addition"?
- •
GF(p) addition is ordinary integer addition mod p; GF(2^n) addition is bitwise XOR (which is its own inverse, so there's no separate subtraction).
- •
- •
What role does a "reduction polynomial" play in GF(2^n) arithmetic?
- •
It's the fixed irreducible polynomial that multiplication results are reduced modulo, playing the same structural role that the prime p plays for reducing products mod p in GF(p).
- •
- •