TExcelTAD
Home › Number Theory & Cryptography › Finite Fields GF(2^n)

Finite Fields GF(2^n)

Computers love bits, and fields of 2n2^n 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)

GF(2)={0,1}GF(2) = \{0, 1\} with arithmetic modulo 2 (so 1+1=01 + 1 = 0). A polynomial over GF(2)GF(2), such as x7+x4+x+1x^7 + x^4 + x + 1, 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.
python · try it yourself
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 Z/p\mathbb{Z}/p is a field when pp is prime, polynomials modulo an irreducible polynomial m(x)m(x) of degree nn form a field with 2n2^n elements, GF(2n)GF(2^n). Irreducible means it has no factors of lower degree, the polynomial analogue of a prime. Every element is a polynomial of degree below nn, that is an nn-bit string.

AES uses n=8n = 8 and m(x)=x8+x4+x3+x+1m(x) = x^8 + x^4 + x^3 + x + 1 (0x11B).

python · try it yourself
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 {57}⋅{83}={c1}\{57\} \cdot \{83\} = \{c1\} and {57}⋅{13}={fe}\{57\} \cdot \{13\} = \{fe\}; the code reproduces both.

Inverses and the multiplicative group

GF(28)×GF(2^8)^\times has 255 elements and, like Zp×\mathbb{Z}_p^\times, it is cyclic. So a255=1a^{255} = 1 and a−1=a254a^{-1} = a^{254} for a≠0a \ne 0, the analogue of Fermat's little theorem. The element x+1x + 1 (0x03) is a generator for the AES field.

python · try it yourself
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 m(x)m(x) factors, there are zero divisors, exactly as in Z/6\mathbb{Z}/6. A small test: mm of degree nn is irreducible iff it has no factor of degree at most n/2n/2.

python · try it yourself
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 xrx^{r}) divided by a fixed degree-rr generator polynomial. It detects all burst errors shorter than rr bits, but it is linear and offers no security against deliberate tampering.

python · try it yourself
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 GF(2128)GF(2^{128}) modulo x128+x7+x2+x+1x^{128} + x^7 + x^2 + x + 1, accelerated by the CPU's PCLMULQDQ carry-less multiply instruction.
  • Reed-Solomon over GF(28)GF(2^8) protects QR codes, CDs, DVDs and RAID 6; CRC-32 guards Ethernet frames, ZIP and PNG files.

Exercises

Exercise 1: Add and multiply

In GF(28)GF(2^8) (AES), compute {53}+{CA}\{53\} + \{CA\} and {02}⋅{87}\{02\} \cdot \{87\}.

Answer

{99}\{99\} and {15}\{15\}.

Solution

0x53⊕0xCA=0x990x53 \oplus 0xCA = 0x99. Multiplying by xx shifts left: 0x87≪1=0x10E0x87 \ll 1 = 0x10E, which has degree 8, so XOR with 0x11B0x11B: 0x10E⊕0x11B=0x0150x10E \oplus 0x11B = 0x015.

Exercise 2: Characteristic 2

Why is a+a=0a + a = 0 for every element of GF(2n)GF(2^n), and what does that imply for (a+b)2(a + b)^2?

Answer

Coefficients are mod 2; (a+b)2=a2+b2(a + b)^2 = a^2 + b^2.

Solution

Each coefficient doubles to 2c≡02c \equiv 0. Then (a+b)2=a2+2ab+b2=a2+b2(a + b)^2 = a^2 + 2ab + b^2 = a^2 + b^2, the "freshman's dream", which makes squaring a linear map in characteristic 2.

Exercise 3: Inverse table (code)

Build the table of inverses of GF(28)GF(2^8) 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
python · try it yourself
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 GF(2n)GF(2^n) is computed with…

  1. integer addition
  2. XOR
  3. AND
  4. multiplication modulo 2n2^n

Why must the modulus polynomial be irreducible?

  1. to make it shorter
  2. otherwise there are zero divisors and not every element has an inverse
  3. for faster XOR
  4. it need not be

In GF(28)GF(2^8), the inverse of a nonzero aa equals…

  1. a254a^{254}
  2. a256a^{256}
  3. 255−a255 - a
  4. a⊕0xFFa \oplus 0xFF