An explorable essay
Inside Keccak-256
Let’s start with the hash as a black box. Put in abc, then capitalize just the first letter. Both answers are 32 bytes long, and there’s no obvious spot in the answer that corresponds to the byte we changed. So where did that one change go?
A digest alone won’t tell us how it was made. So we’ll look at the bytes in between, starting with a much smaller machine where we can follow a change by eye.
What carries over?
Here’s a first guess for hashing a message in chunks: mix each chunk on its own. But then a change in the first chunk has no way to reach the second chunk’s calculation. What if every chunk gets one extra input: the result from the chunk before it?
00. For each one-byte block, XOR it with the carried byte, then rotate the result left by one bit, wrapping the top bit around.00 XOR 61 = 61 → c2c2 XOR 62 = a0 → 4141 XOR 63 = 22 → 44
Try A instead of a. The second input is still 62, but look just to its left: the value arriving from step one has changed. The third step inherits that change too. We didn’t have to keep the original first byte around for it to matter at the end.
That handoff is the idea behind Merkle–Damgård: pad the message (including its length), then run a compression function on each block together with the previous state. “Compression” here means the block and old state go in, but only a new state of the same size comes out. This is a construction, not another name for MD5. MD5 and SHA-256 (in the SHA-2 family) both use this style of chaining, with different compression functions.
One state, one window
Can we keep the handoff without a function that takes a block and a state as separate inputs? Try laying the block over part of the state and flipping bits wherever the block has a 1. Then the mixer only needs one input: the state itself. It returns a state of the same size, but mixed. That’s a permutation, rather than a compression function.
Here’s the split Keccak-256 uses. The same window is used to put bytes in and read bytes out. The rest of the state never touches either directly, but the mixer can reach all of it. This arrangement is called a sponge. Keep an eye on that back part when we get to the real state: it should sit still when we put bytes in, then change once we mix.
Rate · 136 bytes
The input and output window
Capacity · 64 bytes
Never read or written directly by the sponge interface
That gives us a concrete place to poke at it. If the window is 136 bytes wide, what happens when the message is exactly that long?
Follow the real bytes
Let’s change one thing at a time. The three boundary presets below are all lowercase a repeated; only the length changes. The same message feeds every view down the page.
Text only, up to 500 UTF-16 code units (the browser’s length limit). The hash works on UTF-8 bytes. Nothing you type is sent to a server.
The block that shouldn’t be there
Start with 135 bytes. The preview ends in 61 61 81. Maybe 81 is a filler byte for the one empty slot. If so, 136 bytes should fit perfectly and need no filler at all. Try it. There are two blocks. Select the second one: the message already ended, so what’s in there?
Show all 136 bytes of this block
Read left to right in each group of eight. Each group is one lane, in state order. Byte positions in the summary count from 1.
The second block starts with 01 and ends with 80. What if those are the two bits we saw together in 81? At 137 bytes, that guess predicts that block two will start 61 01, but still end with 80. Check the preview again.
The padding rule we just found
For byte-aligned text, original Keccak puts 01 right after the message and sets the high bit (80) in the very last slot of the last block. The bytes in between are zero. If both bits use the same slot, you get 81; if there’s no slot left, you get a whole extra block. Padding marks the end of the message; it doesn’t just fill space.
Count bytes, not characters. Try Unicode: the é alone takes two UTF-8 bytes (c3 a9), even though it looks like one letter.
Watch the part we never wrote to
The 200 bytes are laid out as a 5×5 grid of lanes, each a 64-bit word. The first 17 lanes are the rate; the remaining eight are the capacity. Read across each row, starting at the top. Within a lane, the first byte goes in the least significant position: the bytes 61 62 63 01 00 00 00 00 make the hex number 0000000001636261.
Go back to block one and switch the grid from After XOR to After θ (theta). At first the last eight lanes are still zero, no matter what the message was. One step later they need not be. That’s the difference between putting bytes through the window and mixing the whole state.
Now look at the second block’s Before XOR. If every block started over, this grid would be all zeros. Instead it matches block one’s After round 24, lane for lane. It’s the handoff from the toy chain again: the first block is gone, but what it did is still here.
State for the selected block
Start with the incoming state, watch this block get XORed in, then step through round 1 and the remaining rounds. Each value is a 64-bit hex number.
Pull apart one round
Compare After ρ (rho) to After π (pi). Pick an easy-to-spot lane value at (1, 0). After π, it should be at (0, 2), unchanged. Now compare χ to ι: only lane (0, 0) changes. What looks like one big stir is five moves you can distinguish in the grid:
- θ: XOR together the five lanes in each column, then use the neighboring columns’ results to change every lane in that column.
- ρ: rotate the bits within each lane by a fixed offset for its position.
- π: move each rotated lane to another position, without changing its bits.
- χ: change each lane using two neighbors in its row. The bitwise AND in this step makes it nonlinear.
- ι: XOR this round’s constant into
(0, 0).
Those five moves, repeated for 24 rounds with different round constants, are Keccak-f[1600]. XORing a block into the rate and then running the permutation is one absorb step.
Find the digest in the grid
We started with a 32-byte answer. Is there one more mixing step to make it? Use Inspect the final state below. It jumps to the last round of the last block and outlines four lanes. Compare the rightmost byte of (0, 0) in the grid with the leftmost byte of the digest: they match. The lane is shown as a number, but its bytes come out least-significant first, just as they went in.
Four lanes at eight bytes each already give us 32 bytes. This read is the squeeze. A sponge could permute again after reading a full window to keep producing output; Keccak-256 doesn’t even need the whole first window.
Keccak-256 digest
Why not call it SHA3-256?
Keccak won NIST’s SHA-3 competition in 2012. FIPS 202 later standardized SHA3-256 with the same permutation, rate, capacity, and output length, but with a different domain suffix. In a byte-oriented implementation, SHA3-256 puts 0x06 where the original Keccak-256 on this page puts 0x01. Both set the final 0x80 bit. The very first change to the padded input sends the state down a different path, so the names aren’t interchangeable.
Further reading: The Keccak team’s sponge description and NIST FIPS 202.