Finite Fields GF(2^n)
Computers love bits, and fields of elements are made of bits: their elements are bit strings, addition is XOR and multiplication is "multiply polynomials, then reduce". AES, GCM authentication, CRC checksums, Reed-Solomon codes (QR codes, RAID 6) and many post-quantum schemes all compute in such fields.
Polynomials over GF(2)
with arithmetic modulo 2 (so ). A polynomial over , such as , is stored as the bit string of its coefficients, 0b10010011. Then:
- Addition is XOR (coefficients add modulo 2, no carries), and subtraction is the same as addition.
- Multiplication is "carry-less": shift-and-XOR instead of shift-and-add.
def clmul(a, b):
"""Carry-less multiplication of bit-polynomials."""
r = 0
while b:
if b & 1:
r ^= a
a <<= 1
b >>= 1
return r
def poly_str(a):
terms = [("x^%d" % i if i > 1 else "x" if i == 1 else "1") for i in range(a.bit_length() - 1, -1, -1) if a >> i & 1]
return " + ".join(terms) or "0"
a, b = 0b1011, 0b0110 # x^3 + x + 1 and x^2 + x
print(poly_str(a), " + ", poly_str(b), " = ", poly_str(a ^ b))
print(poly_str(a), " * ", poly_str(b), " = ", poly_str(clmul(a, b)))
print("integer product:", a * b, " carry-less product:", clmul(a, b))x^3 + x + 1 + x^2 + x = x^3 + x^2 + 1
x^3 + x + 1 * x^2 + x = x^5 + x^4 + x^3 + x
integer product: 66 carry-less product: 58
Building the field
Just as is a field when is prime, polynomials modulo an irreducible polynomial of degree form a field with elements, . Irreducible means it has no factors of lower degree, the polynomial analogue of a prime. Every element is a polynomial of degree below , that is an -bit string.
AES uses and (0x11B).
AES_POLY = 0x11B
def gf_mul(a, b, poly=AES_POLY, n=8):
r = 0
while b:
if b & 1:
r ^= a
b >>= 1
a <<= 1
if a >> n & 1: # degree reached n: subtract (XOR) the modulus
a ^= poly
return r
print(hex(gf_mul(0x57, 0x83)), hex(gf_mul(0x57, 0x13))) # FIPS-197 examples
print("x * x^7 =", hex(gf_mul(0x02, 0x80)), "(that is x^4 + x^3 + x + 1)")0xc1 0xfe
x * x^7 = 0x1b (that is x^4 + x^3 + x + 1)
The FIPS-197 standard gives and ; the code reproduces both.
Inverses and the multiplicative group
has 255 elements and, like , it is cyclic. So and for , the analogue of Fermat's little theorem. The element (0x03) is a generator for the AES field.
def gf_mul(a, b):
r = 0
while b:
if b & 1: r ^= a
b >>= 1; a <<= 1
if a & 0x100: a ^= 0x11B
return r
def gf_pow(a, e):
r = 1
while e:
if e & 1: r = gf_mul(r, a)
a = gf_mul(a, a); e >>= 1
return r
inv53 = gf_pow(0x53, 254)
print("inverse of 0x53:", hex(inv53), " check:", hex(gf_mul(0x53, inv53)))
powers, x = set(), 1
for _ in range(255):
powers.add(x); x = gf_mul(x, 0x03)
print("0x03 generates", len(powers), "nonzero elements")
print("0x02 generates", len({gf_pow(0x02, k) for k in range(255)}), "elements")inverse of 0x53: 0xca check: 0x1
0x03 generates 255 nonzero elements
0x02 generates 51 elements
Irreducibility
Not every degree-8 polynomial gives a field. If factors, there are zero divisors, exactly as in . A small test: of degree is irreducible iff it has no factor of degree at most .
def poly_mod(a, m):
dm = m.bit_length() - 1
while a.bit_length() - 1 >= dm:
a ^= m << (a.bit_length() - 1 - dm)
return a
def irreducible(m):
n = m.bit_length() - 1
return all(poly_mod(m, d) != 0 for d in range(2, 1 << (n // 2 + 1)))
print("0x11B (AES):", irreducible(0x11B))
print("0x11D (Reed-Solomon, QR codes):", irreducible(0x11D))
print("x^8 + 1:", irreducible(0x101))
print("number of irreducible degree-8 polynomials:", sum(irreducible(m) for m in range(0x100, 0x200)))0x11B (AES): True
0x11D (Reed-Solomon, QR codes): True
x^8 + 1: False
number of irreducible degree-8 polynomials: 30
CRC: remainders of polynomials
A CRC checksum is the remainder of the message (as a huge polynomial, times ) divided by a fixed degree- generator polynomial. It detects all burst errors shorter than bits, but it is linear and offers no security against deliberate tampering.
def crc8(data, poly=0x107): # CRC-8 with generator x^8 + x^2 + x + 1
r = 0
for byte in data:
r ^= byte
for _ in range(8):
r = ((r << 1) ^ poly) & 0xFF if r & 0x80 else (r << 1) & 0xFF
return r
print(hex(crc8(b"123456789")), hex(crc8(b"123456788")))0xf4 0xf3
In software
- AES's S-box and MixColumns (Inside AES); GCM's GHASH multiplies in modulo , accelerated by the CPU's
PCLMULQDQcarry-less multiply instruction. - Reed-Solomon over protects QR codes, CDs, DVDs and RAID 6; CRC-32 guards Ethernet frames, ZIP and PNG files.
Exercises
Exercise 1: Add and multiply
In (AES), compute and .
Answer
and .
Solution
. Multiplying by shifts left: , which has degree 8, so XOR with : .
Exercise 2: Characteristic 2
Why is for every element of , and what does that imply for ?
Answer
Coefficients are mod 2; .
Solution
Each coefficient doubles to . Then , the "freshman's dream", which makes squaring a linear map in characteristic 2.
Exercise 3: Inverse table (code)
Build the table of inverses of with the AES polynomial and check that every nonzero element has exactly one inverse.
Answer
255 inverses, each unique; the map is its own inverse.
Solution
def gf_mul(a, b):
r = 0
while b:
if b & 1: r ^= a
b >>= 1; a <<= 1
if a & 0x100: a ^= 0x11B
return r
inv = {a: next(b for b in range(1, 256) if gf_mul(a, b) == 1) for a in range(1, 256)}
print(len(inv), len(set(inv.values())), all(inv[inv[a]] == a for a in inv), hex(inv[0x53]))255 255 True 0xca
Test yourself
Addition in is computed with…
- integer addition
- XOR
- AND
- multiplication modulo
Why must the modulus polynomial be irreducible?
- to make it shorter
- otherwise there are zero divisors and not every element has an inverse
- for faster XOR
- it need not be
In , the inverse of a nonzero equals…