cstef
/posts/sponge
TOP

SquarePants Sponge Hashing

A quick explaination of the sponge hashing algorithm, notably used in SHA-3

06/01/2025 (modified 15/01/2025) Β· 2 min read Β· sapling πŸͺ΄

In 2007, the NIST (National Institue of Standards and Technology) announced a Cryptographic Hash Algorithm Competition, with the winner being awarded the grant to be standardized as the new SHA-3. It ended in 2012 with #smallcaps("K​")script(#smallcaps("ECCAK"))#smallcaps("K​")script(#smallcaps("ECCAK")) being announced as the winner.

The function uses a sponge construction, but what the hell is even this thing? Let’s take a look!

We have a message MM as our input and the final hash HH. We then have two β€œregisters”: rr and cc, which both make up our state ss. These two are initialized with 00s in them.

The message MM is first padded so that it can fit in exactly k in NNk in NN parts m_im_i, each of size rr. The process of β€œabsorption” is the following:

  1. XOR (denoted xorxor) the message-part m_im_i with the register rr bit-by-bit
  2. Scramble the whole register ss (rr and cc) altogether to produce a new state s's' by applying ff The function ff needs to generate a pseudo-random permutation of the bits in ss

#import "@preview/fletcher:0.5.2" as fletcher: *

#import "@preview/fletcher:0.5.2" as fletcher: *

Example permutation via ff

  1. Pass on the state to the next iteration

We can now β€œsqueeze” our sponge to extract the hash from it, by applying ff again and taking a small chunk h_ih_i (rr bits) over and over until we have enough for our desired output length.

This whole process can be described with the following diagram:

#import "@preview/fletcher:0.5.2" as fletcher: *

#import "@preview/fletcher:0.5.2" as fletcher: *

Sponge construction algorithm, inspired by Keccak's diagram

This whole process can also be modified to fit streaming needs, by combining both absorption and squeezing steps at each iteration.

#import "@preview/fletcher:0.5.2" as fletcher: *

#import "@preview/fletcher:0.5.2" as fletcher: *