How SHA-3 absorbs a message into a Keccak sponge

Short answer: SHA3-256 is a Keccak sponge, not a Merkle-Damgard hash. The state is twenty-five 64-bit lanes. SHA3-256 absorbs 136 bytes at a time and pads with the domain byte 0x06, zeros, and a final byte whose high bit is set. Each absorbed block is xored into the state, and the state is then permuted for twenty-four rounds. The digest of abc is 3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532.


What the problem actually asks

A sponge hashes by absorbing the message into part of a large state and squeezing the digest back out. The part that is xored with the message is the rate, 136 bytes for SHA3-256. The rest is the capacity. The permutation has five steps: theta mixes columns, rho rotates lanes, pi moves lanes, chi mixes each lane with its neighbors, and iota xors a round constant into the first lane. The padding domain byte is what separates SHA-3 from the other Keccak instances that use the same permutation.


What the algorithm guarantees

The same message produces the same 32-byte digest. The digest is the first 32 bytes of the state after the last permutation. SHA3-256 does not use the SHA-256 padding or the SHA-256 constants. A changed domain byte defines a different function. The capacity is the part of the state the message does not overwrite directly.


The working steps

  1. Start from a state of twenty-five zero lanes.
  2. Append 0x06, zero bytes, and set the high bit of the last byte so the padded length is a multiple of 136.
  3. Xor each 136-byte block into the state and run the twenty-four-round permutation.
  4. Read the first 32 bytes of the state as the digest.

Approaches that look simpler and fail

Approach Why it fails when it is treated as SHA-256 with different constants
Pad with the SHA-256 length suffix SHA-3 uses the domain byte and the high bit. The Merkle-Damgard length suffix is a different padding.
Squeeze more than 32 bytes and still call it SHA3-256 SHA3-256 is defined to squeeze 32 bytes. A longer squeeze is a different output length.
Skip the domain byte because the permutation is the same The domain byte is how SHA-3 is separated from other uses of Keccak.
Use the sponge as a password hash with no cost parameter The sponge is fast. A password store still wants a salt and a memory or iteration cost.

SHA-256 is the Merkle-Damgard hash. SHA3-256 is the sponge.


A working example

This Python fragment runs the Keccak permutation and squeezes a SHA3-256 digest of abc. It prints 3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532.

def _rotl64(x: int, n: int) -> int:
    n %= 64
    return ((x << n) | (x >> (64 - n))) & 0xFFFFFFFFFFFFFFFF

_RC = [1, 32898, 9223372036854808714, 9223372039002292224, 32907, 2147483649, 9223372039002292353, 9223372036854808585, 138, 136, 2147516425, 2147483658, 2147516555, 9223372036854775947, 9223372036854808713, 9223372036854808579, 9223372036854808578, 9223372036854775936, 32778, 9223372039002259466, 9223372039002292353, 9223372036854808704, 2147483649, 9223372039002292232]

_ROT = [[0, 36, 3, 41, 18], [1, 44, 10, 45, 2], [62, 6, 43, 15, 61], [28, 55, 25, 21, 56], [27, 20, 39, 8, 14]]

def _keccak_f(state: list[int]) -> None:
    rho = [[_ROT[x][y] for y in range(5)] for x in range(5)]
    for rc in _RC:
        lanes = [[state[x + 5 * y] for y in range(5)] for x in range(5)]
        c = [lanes[x][0] ^ lanes[x][1] ^ lanes[x][2] ^ lanes[x][3] ^ lanes[x][4] for x in range(5)]
        d = [c[(x - 1) % 5] ^ _rotl64(c[(x + 1) % 5], 1) for x in range(5)]
        for x in range(5):
            for y in range(5):
                lanes[x][y] ^= d[x]
        b = [[0] * 5 for _ in range(5)]
        for x in range(5):
            for y in range(5):
                b[y][(2 * x + 3 * y) % 5] = _rotl64(lanes[x][y], rho[x][y])
        for x in range(5):
            for y in range(5):
                lanes[x][y] = b[x][y] ^ ((~b[(x + 1) % 5][y]) & b[(x + 2) % 5][y] & 0xFFFFFFFFFFFFFFFF)
        lanes[0][0] ^= rc
        for x in range(5):
            for y in range(5):
                state[x + 5 * y] = lanes[x][y]

def sha3_256(data: bytes) -> bytes:
    rate = 136
    state = [0] * 25
    msg = data + b"\x06"
    msg += b"\x00" * ((rate - len(msg) % rate) % rate)
    msg = msg[:-1] + bytes([msg[-1] | 0x80])
    for off in range(0, len(msg), rate):
        block = msg[off : off + rate]
        for i in range(rate // 8):
            state[i] ^= int.from_bytes(block[i * 8 : (i + 1) * 8], "little")
        _keccak_f(state)
    out = b"".join(state[i].to_bytes(8, "little") for i in range(4))
    return out

digest = sha3_256(b"abc")
assert digest.hex() == "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"
print(digest.hex())

Where it is used

NIST SHA-3 is the Keccak sponge with the domain byte 0x06. Ethereum’s Keccak-256 is the same permutation with a different padding, so the two digests of one message differ. SHA-256 remains the hash most protocols already agreed on.


When you can drop this algorithm

Drop SHA3-256 as a password store and as a tag. Keep it as the 256-bit sponge digest. A tag built from it is a keyed construction, not the bare sponge.


Why this stays published

We publish this so a search for SHA-3 returns the Keccak sponge, the domain byte 0x06, and the digest of abc. SHA3-256 xors a message into a 136-byte rate, pads with the domain byte 0x06, and squeezes 32 bytes from the Keccak state.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *