Path: blob/main/foundations/03-galois-fields-aes/sage/03f-full-aes-round.ipynb
483 views
Full AES Round
Module 03f | Galois Fields and AES
SubBytes, ShiftRows, MixColumns, AddRoundKey, the complete cipher round.
Question: You've built every piece of AES separately, the S-box (03d), MixColumns (03e), and the GF(256) arithmetic underneath (03a-03c). Now: can you put them together into a working AES round and trace a plaintext byte through all four operations?
In this notebook, you'll build a complete AES round from scratch and watch the avalanche effect unfold.
Objectives
By the end of this notebook you will be able to:
Implement all four AES round operations from scratch
Compose them into a complete AES round
Trace a byte through the round and explain each transformation
Demonstrate the avalanche effect: one bit change → half the output bits flip
Verify your implementation against known AES test vectors
Bridge from 03e
In 03d you built SubBytes (nonlinear, per-byte). In 03e you built MixColumns (linear, per-column). Now we add the two remaining operations, ShiftRows and AddRoundKey, and compose all four into a single round. This is the heart of AES.
Setup: GF(256) and S-box
The Four AES Round Operations
Each AES round applies four operations in order:
SubBytes, Apply S-box to each byte (nonlinear, per-byte)
ShiftRows, Cyclically shift each row of the state (permutation)
MixColumns, Matrix multiply each column over GF(256) (linear, per-column)
AddRoundKey, XOR the state with the round key
The state is a 4×4 matrix of bytes, stored column-major.
Operation 1: SubBytes
Apply the S-box to every byte in the state. This is the only nonlinear operation.
Operation 2: ShiftRows
Cyclically shift row left by positions:
Row 0: no shift
Row 1: shift left by 1
Row 2: shift left by 2
Row 3: shift left by 3
This ensures that each column of the output depends on bytes from all four columns of the input (after MixColumns).
Checkpoint: ShiftRows is a simple permutation, no arithmetic, no field operations. But it's essential: without it, MixColumns would only mix within columns, and bytes in different columns would never interact. ShiftRows ensures cross-column diffusion.
Operation 3: MixColumns
Operation 4: AddRoundKey
Complete AES Round
Now let's compose all four operations into a single round function:
The Avalanche Effect
A good cipher should exhibit the avalanche effect: flipping one input bit should change approximately half the output bits. Let's test this after just one round.
Common mistake: "More rounds = more security, so why not 100 rounds?" Each round adds computational cost. AES-128 uses 10 rounds, the minimum needed for full diffusion and security margin. This was determined by extensive cryptanalysis. Adding rounds beyond 10 doesn't significantly improve security but does slow down encryption.
Anatomy of a Round: Why Each Step Matters
| Operation | Type | Purpose |
|---|---|---|
| SubBytes | Nonlinear, per-byte | Confusion, resist linear/differential attacks |
| ShiftRows | Permutation | Cross-column mixing, break column isolation |
| MixColumns | Linear, per-column | Diffusion, spread each byte across the column |
| AddRoundKey | XOR with key | Key dependence, without it, AES is key-independent |
Remove any one and the cipher breaks.
Exercises
Exercise 1 (Worked)
Trace byte 0x32 (position [0,0] of the plaintext) through one complete round.
Exercise 2 (Guided)
Implement the inverse round (for decryption): InvShiftRows, InvSubBytes, InvMixColumns, AddRoundKey. Apply it to the round 1 output and verify you recover the round 0 state.
Exercise 3 (Independent)
Run 4 rounds of AES (you'll need to implement a simple key schedule, or use fixed round keys). After how many rounds does a single-bit plaintext change affect all 128 output bits?
What happens if you remove ShiftRows? Apply SubBytes → MixColumns → AddRoundKey for 10 rounds. Can you identify a structural weakness? (Hint: each column stays independent.)
Compute the branch number of MixColumns experimentally: for random nonzero input differences, what is the minimum number of nonzero bytes in (input difference + output difference)?
Summary
| Concept | Key idea |
|---|---|
| SubBytes | Nonlinear, per-byte confusion from GF(256) inversion and an affine map |
| ShiftRows | A simple row permutation that ensures cross-column mixing, breaking column isolation |
| MixColumns | Linear, per-column diffusion through MDS matrix multiplication over GF(256) |
| AddRoundKey | XOR with the round key (GF(2) vector addition), providing key dependence |
| Avalanche effect | After one round, a single bit change already spreads across the state. After 10 rounds, the output is indistinguishable from random |
| Everything is field theory | Bytes = GF(256) elements, S-box = GF(256) inversion, MixColumns = GF(256) matrix multiplication, AddRoundKey = GF(2) vector addition |
Every operation in AES is field theory in disguise. Remove any one of the four operations and the cipher breaks.
Crypto foreshadowing: AES is the most widely deployed symmetric cipher in the world, it protects TLS, Wi-Fi (WPA), disk encryption, and more. In Module 04, you'll study RSA, which uses a completely different mathematical foundation (number theory instead of Galois fields). But the underlying principle is the same: build cryptographic security on top of algebraic hardness.
This completes Module 03. Next: Module 04: Number Theory and RSA