Path: blob/main/foundations/03-galois-fields-aes/sage/03d-aes-sbox-construction.ipynb
483 views
AES S-box Construction
Module 03d | Galois Fields and AES
Field inverse + affine transform = the most important lookup table in cryptography.
Question: The AES S-box maps each byte to another byte, a simple 256-entry table. But it's not random. It's constructed from exactly two mathematical operations: (1) take the multiplicative inverse in GF(256), and (2) apply an affine map over GF(2). Why these two steps? What properties do they give the S-box that a random table wouldn't?
By the end of this notebook, you'll build the S-box from scratch and understand why it resists both linear and differential cryptanalysis.
Objectives
By the end of this notebook you will be able to:
Compute the GF(256) multiplicative inverse of any byte
Apply the AES affine transformation as a matrix-vector product over GF(2)
Construct the complete 256-entry AES S-box from first principles
Verify your S-box matches the official AES specification
Explain why the S-box provides nonlinearity and high algebraic degree
Bridge from 03c
In 03c you built GF(256) arithmetic: addition (XOR), multiplication (polynomial product mod ), and inversion. You also built the complete 256-byte inverse table.
The S-box takes this inverse table and applies one more transformation, an affine map, to break the algebraic structure and create the nonlinearity that AES needs.
Setup: GF(256) with the AES Polynomial
Step 1: The Multiplicative Inverse
The first step of the S-box maps each byte to its multiplicative inverse in GF(256). By convention, (since 0 has no inverse).
Why the inverse? Because it provides maximum nonlinearity, the inverse function over GF() achieves the theoretical best nonlinearity score among all power maps.
Step 2: The Affine Transformation
After inverting, AES applies an affine map over GF(2). This is a matrix multiplication plus a constant vector:
where is a specific matrix over GF(2) and is the constant vector 0x63.
Why this specific matrix and constant? The designers of AES (Daemen and Rijmen) chose and to satisfy three constraints simultaneously:
Invertibility, must be invertible over GF(2) so that decryption can undo the affine step via .
Circulant structure, each row is a rotation of the first row. This means the transformation can be implemented as a fixed bit-rotation + XOR pattern, which is extremely efficient in hardware.
Resistance to known attacks, the combination of and was chosen so the full S-box has no fixed points (), no "opposite" fixed points (), and maximum nonlinearity among circulant affine maps.
The constant breaks the symmetry that the inverse alone would give (since under inversion), ensuring the S-box has no zero fixed point.
The matrix is a circulant matrix based on the polynomial (= 0x1F):
Checkpoint: Why does need to be invertible? Because AES must be decryptable. The inverse S-box uses to undo the affine step: .
Building the Complete S-box
Verification Against the Official S-box
Let's verify our construction matches the AES specification (FIPS 197):
Why Both Steps Are Needed
Common mistake: "The inverse alone would be a fine S-box." No! The inverse is a power map (), which means it satisfies algebraic equations over GF(256). Algebraic attacks (like XL and Gröbner basis methods) can exploit this. The affine step breaks the pure algebraic structure.
Conversely, an affine map alone would be linear, trivially breakable. You need both: the inverse for nonlinearity, and the affine map to disrupt algebraic relations.
The Inverse S-box
For decryption, AES needs the inverse S-box: .
Checkpoint: The S-box and inverse S-box are both permutations of . If you compose them, apply S-box then inverse S-box, you get the identity. This is essential for AES decryption to work correctly.
Exercises
Exercise 1 (Worked)
Compute S-box(0x53) by hand, showing both the inverse step and the affine step.
Exercise 2 (Guided)
Build the inverse S-box using the formula (not by inverting the forward table). Verify it matches.
Exercise 3 (Independent)
The AES S-box has no fixed points ( for all ). Verify this. Is this property guaranteed by the construction, or is it a coincidence of the specific matrix and constant ?
Construct an alternative S-box using a different affine constant instead of . Does it still have no fixed points? What is its nonlinearity?
What happens if you skip the affine step entirely and use only the inverse as the S-box? Count fixed points and compute the algebraic degree. (Hint: the algebraic degree of over GF() is 7, while the full S-box has degree 7 too, but the affine step changes which degree-7 polynomial it is.)
Summary
| Concept | Key idea |
|---|---|
| S-box construction | GF(256) multiplicative inverse followed by an affine transformation over GF(2) |
| Inverse provides nonlinearity | The field inverse achieves near-optimal nonlinearity (112/120), resisting linear and differential attacks |
| Affine map breaks algebraic structure | Without it, the pure inverse satisfies exploitable algebraic equations. The affine step disrupts these relations |
| No fixed points | and for all , thanks to the specific choice of matrix and constant |
| Bijection | The S-box is a permutation of all 256 bytes, essential for AES decryption to work |
| Inverse S-box | Uses to undo the affine step, then takes the GF(256) inverse again |
Crypto foreshadowing: The S-box is SubBytes, just one of four AES round operations. In notebook 03e, we'll see MixColumns, which uses matrix multiplication over GF(256). The interplay between SubBytes (nonlinear, byte-level) and MixColumns (linear, column-level) is what makes AES secure.
Next: AES MixColumns as Field Ops, matrix multiplication over GF(256).