Path: blob/main/foundations/03-galois-fields-aes/sage/03a-binary-fields-gf2.ipynb
483 views
The Binary Field GF(2)
Module 03a | Galois Fields and AES
The simplest field has only two elements, and it powers all of modern computing.
Question: Every computer stores data as bits, 0s and 1s. You can XOR two bits and AND two bits. But did you know that XOR and AND are actually addition and multiplication in a field?
A field with just two elements, where . That sounds broken, but it's the foundation of AES, error-correcting codes, and all of GF() arithmetic.
By the end of this notebook, you will see that bits aren't just data, they are field elements.
Objectives
By the end of this notebook you will be able to:
Construct GF(2) in SageMath and list its elements
Verify all field axioms for GF(2) and explain why each holds
Identify XOR as field addition and AND as field multiplication
Explain why in GF(2) and why this is not a bug
Work with bit-vectors as vectors over GF(2)
Bridge from Module 02
In Module 02 you built rings and fields from scratch. You saw that a field is a ring where every nonzero element has a multiplicative inverse, and that is a field when is prime.
The smallest prime is . So should be a field with just two elements: . This is GF(2), and it is the starting point for everything in this module.
| Module 02 concept | Module 03 application |
|---|---|
| is a field | GF(2) = , the smallest field |
| Polynomial rings over a field | GF() = GF(2) / (irreducible) |
| Quotient rings | How we build GF(256) for AES |
SageMath confirms: GF(2) has two elements, characteristic 2, and is a field.
Characteristic 2 means . This single fact is the source of all the "weirdness" in binary fields, and also what makes them perfect for computers.
Addition in GF(2) Is XOR
The addition table for GF(2) is identical to the XOR truth table. Compare:
Checkpoint: In GF(2), addition and subtraction are the same operation. Why? Because (since for every element). This is why XOR is used for both encryption and decryption in stream ciphers: XOR is its own inverse.
Multiplication in GF(2) Is AND
Now let's look at multiplication:
Why : Characteristic 2
Common mistake: "If , then the field is broken, you can't have !" But GF(2) has no element called "2." The only elements are 0 and 1. When you add 1 to itself, the result must be one of . It can't be 1 (that would make 1 the additive identity, but 0 already is). So . This is called characteristic 2, the number of times you add 1 to get 0.
This is not a bug. It's what makes binary arithmetic work: carries disappear, addition has no "overflow" in the usual sense, and every element is its own negative.
Verifying All Field Axioms
GF(2) has only 2 elements, so we can check every axiom exhaustively. A field needs:
is an abelian group (identity 0, every element has an additive inverse)
is an abelian group (identity 1, every nonzero element has a multiplicative inverse)
The distributive law holds
With only 2 elements, this is a very small amount of checking:
Checkpoint: GF(2) is trivially a field because , there's only one nonzero element, and , so it's its own inverse. The real power of GF(2) isn't in the field itself, but in what you can build on top of it: polynomial rings, vector spaces, and extension fields like GF() = GF(256).
Bit-Vectors as GF(2) Vector Spaces
A byte (8 bits) is a vector in . This isn't just an analogy, it's the literal mathematical structure. Adding two bytes component-wise over GF(2) is XOR:
This is exactly what AES's AddRoundKey step does: XOR the state with the round key. It's vector addition in .
Polynomials over GF(2)
The real payoff of GF(2) comes when we build polynomial rings over it. A polynomial over GF(2) has coefficients that are 0 or 1, so it's just a bit-string read as polynomial coefficients:
Checkpoint: A byte like
0xA3is the bit-string10100011. As a GF(2) polynomial, what is it? Write it as . Check: which bits are 1? Those are the nonzero coefficients.
This byte ↔ polynomial correspondence is the key idea behind GF(256). In notebook 03b, we will take this polynomial ring and quotient out by an irreducible polynomial to create the finite field GF() that AES uses.
Exercises
Exercise 1 (Worked)
Verify that GF(2) satisfies the field axiom: for every nonzero , there exists with . Then show that for all in GF(2). (This property is called idempotence and is special to GF(2), it fails in every other field.)
Exercise 2 (Guided)
Convert the byte 0xCB (binary 11001011) to a polynomial over GF(2), and convert the polynomial back to a byte. Then add the two polynomials and verify the result matches XOR of the bytes.
Exercise 3 (Independent)
The Hamming weight of a bit-vector is the number of 1s. In GF(2) terms, it's the sum of the vector's components (computed in , not GF(2)!).
Write a function that takes a byte (0-255) and returns its Hamming weight.
How many bytes have Hamming weight exactly 4? (These form an important subset in coding theory.)
The XOR of two bytes has Hamming weight equal to the Hamming distance between and . Verify this for and .
Summary
| Concept | Key idea |
|---|---|
| GF(2) | The smallest field, just , with characteristic 2 so |
| Addition = XOR | GF(2) addition is exactly the XOR gate, and subtraction is the same operation |
| Multiplication = AND | GF(2) multiplication is exactly the AND gate |
| Self-inverse elements | Every element is its own additive inverse (), so addition and subtraction are identical |
| Bytes as vectors | A byte is a vector in , and XOR of bytes is vector addition over GF(2) |
| Bytes as polynomials | A byte is also a polynomial in of degree , with polynomial addition equal to XOR |
Crypto foreshadowing: AES operates entirely over GF(2) and its extension GF(). AddRoundKey is GF(2) vector addition (XOR). SubBytes, MixColumns, and the key schedule all use GF() arithmetic, which we build starting in notebook 03b.
Next: Extension Fields: GF(), how to build larger fields from GF(2) using irreducible polynomials.