Documentation

CslibFrontier.Crypto.Primitives.Hash.Sponge

The sponge construction #

The sponge construction ([BDPVA08]; [KL20], Section 7.3.3 and Construction 7.6) builds a hash function from a single unkeyed permutation P on an ℓ-bit state split into a rate part of r bits and a capacity part of c bits, r + c = ℓ. Starting from the all-zero state, each r-bit message block is added (XORed) into the rate part and the state is permuted (absorbing); afterwards the hash outputs part of the state, permutes, and repeats (squeezing).

The executable definitions and deterministic collision implications only apply P; they do not use its inverse or injectivity. Their parameter is therefore the underlying state function R × C → R × C. Permutations coerce to this function, while concrete clients such as FIPS 202's Keccak transformation need no unnecessary inverse wrapper.

We write the state as a pair (rate, capacity) : R × C, with the rate part R an additive group (for bits, BitString r, where + is exclusive or). This is the source's ℓ-bit state with y ⊕ (m ‖ 0^c) read as (y.1 + m, y.2). Each squeezing step outputs out y for an arbitrary output map out; Construction 7.6 takes the first v bits of the state, and the results below hold for every out.

Theorem 7.7 bounds the collision probability when P is a random permutation by splitting collisions into three bad events on the attacker's permutation queries (Claim 7.8). We prove the deterministic core of Claim 7.8 for permutation queries: if two distinct block sequences have equal first output, then among the permutation inputs evaluated while hashing them either two distinct inputs have images with equal output (event E1), some image has zero capacity part (event E2), or two distinct inputs have images with equal capacity part (event E3). Inverse queries and the probability bound of Claim 7.9 are not formalized here. Conversely, a collision in the capacity part always extends to a full collision, which is why the capacity governs security; we use this to solve [KL20], Exercise 7.26.

Main definitions #

Main results #

References #

Absorbing and squeezing #

def Cslib.Crypto.Sponge.absorbStep {R : Type u_1} {C : Type u_2} [AddGroup R] (P : R × C → R × C) (y : R × C) (m : R) :
R × C

One absorbing step: add the block m to the rate part of the state y, then permute, y ↦ P (y.1 + m, y.2).

def Cslib.Crypto.Sponge.absorb {R : Type u_1} {C : Type u_2} [AddGroup R] [Zero C] (P : R × C → R × C) (ms : List R) :
R × C

The absorbing phase: absorb the blocks ms in order, starting from the zero state.

def Cslib.Crypto.Sponge.squeeze {R : Type u_1} {C : Type u_2} {O : Type u_3} (P : R × C → R × C) (out : R × C → O) :
ℕ → R × C → List O

The squeezing phase: output out y, replace y by P y, and repeat, k outputs in total.

def Cslib.Crypto.Sponge.hashBlocks {R : Type u_1} {C : Type u_2} {O : Type u_3} [AddGroup R] [Zero C] (P : R × C → R × C) (out : R × C → O) (k : ℕ) (ms : List R) :

The sponge hash of a block sequence with k squeezing outputs.

@[simp]
theorem Cslib.Crypto.Sponge.absorb_nil {R : Type u_1} {C : Type u_2} [AddGroup R] [Zero C] (P : R × C → R × C) :
absorb P [] = 0
theorem Cslib.Crypto.Sponge.absorb_append {R : Type u_1} {C : Type u_2} [AddGroup R] [Zero C] (P : R × C → R × C) (a b : List R) :
absorb P (a ++ b) = List.foldl (absorbStep P) (absorb P a) b
@[simp]
theorem Cslib.Crypto.Sponge.absorb_concat {R : Type u_1} {C : Type u_2} [AddGroup R] [Zero C] (P : R × C → R × C) (a : List R) (m : R) :
absorb P (a ++ [m]) = P ((absorb P a).1 + m, (absorb P a).2)
@[simp]
theorem Cslib.Crypto.Sponge.length_squeeze {R : Type u_1} {C : Type u_2} {O : Type u_3} (P : R × C → R × C) (out : R × C → O) (k : ℕ) (y : R × C) :
(squeeze P out k y).length = k
theorem Cslib.Crypto.Sponge.take_squeeze {R : Type u_1} {C : Type u_2} {O : Type u_3} (P : R × C → R × C) (out : R × C → O) {m n : ℕ} (h : m ≤ n) (y : R × C) :
List.take m (squeeze P out n y) = squeeze P out m y

Squeezing more blocks preserves the entire previously requested prefix.

theorem Cslib.Crypto.Sponge.take_hashBlocks {R : Type u_1} {C : Type u_2} {O : Type u_3} [AddGroup R] [Zero C] (P : R × C → R × C) (out : R × C → O) {m n : ℕ} (h : m ≤ n) (ms : List R) :
List.take m (hashBlocks P out n ms) = hashBlocks P out m ms

Requesting more sponge output blocks preserves the entire previously requested prefix.

theorem Cslib.Crypto.Sponge.hashBlocks_succ {R : Type u_1} {C : Type u_2} {O : Type u_3} [AddGroup R] [Zero C] (P : R × C → R × C) (out : R × C → O) (k : ℕ) (ms : List R) :
hashBlocks P out (k + 1) ms = out (absorb P ms) :: squeeze P out k (P (absorb P ms))
def Cslib.Crypto.Sponge.IsInput {R : Type u_1} {C : Type u_2} [AddGroup R] [Zero C] (P : R × C → R × C) (ms : List R) (x : R × C) :

x is one of the permutation inputs x_i = (y_{i-1}.1 + m_i, y_{i-1}.2) evaluated while absorbing ms.

theorem Cslib.Crypto.Sponge.IsInput.append_right {R : Type u_1} {C : Type u_2} [AddGroup R] [Zero C] {P : R × C → R × C} {a : List R} {x : R × C} (h : IsInput P a x) (b : List R) :
IsInput P (a ++ b) x
theorem Cslib.Crypto.Sponge.isInput_concat {R : Type u_1} {C : Type u_2} [AddGroup R] [Zero C] (P : R × C → R × C) (a : List R) (m : R) :
IsInput P (a ++ [m]) ((absorb P a).1 + m, (absorb P a).2)
def Cslib.Crypto.Sponge.ImageCollision {R : Type u_1} {C : Type u_2} {D : Type u_4} (P : R × C → R × C) (f : R × C → D) (Q : R × C → Prop) :

Two distinct inputs in Q whose images under P agree under f. With f = out this is event E1 of [KL20], Claim 7.8; with f = Prod.snd (the capacity part) it is event E3.

def Cslib.Crypto.Sponge.ZeroCapacity {R : Type u_1} {C : Type u_2} [Zero C] (P : R × C → R × C) (Q : R × C → Prop) :

Some input in Q whose image under P has zero capacity part: event E2 of [KL20], Claim 7.8.

theorem Cslib.Crypto.Sponge.ImageCollision.mono {R : Type u_1} {C : Type u_2} {D : Type u_4} {P : R × C → R × C} {f : R × C → D} {Q Q' : R × C → Prop} (h : ImageCollision P f Q) (hQ : ∀ (x : R × C), Q x → Q' x) :
theorem Cslib.Crypto.Sponge.ZeroCapacity.mono {R : Type u_1} {C : Type u_2} [Zero C] {P : R × C → R × C} {Q Q' : R × C → Prop} (h : ZeroCapacity P Q) (hQ : ∀ (x : R × C), Q x → Q' x) :
theorem Cslib.Crypto.Sponge.capacityCollision_or_zeroCapacity {R : Type u_1} {C : Type u_2} [AddGroup R] [Zero C] (P : R × C → R × C) (ms ms' : List R) :
ms ≠ ms' → (absorb P ms).2 = (absorb P ms').2 → (ImageCollision P Prod.snd fun (x : R × C) => IsInput P ms x ∨ IsInput P ms' x) ∨ ZeroCapacity P fun (x : R × C) => IsInput P ms x ∨ IsInput P ms' x

Distinct block sequences with equal capacity part after absorbing trigger event E3 or E2 among their evaluated permutation inputs.

theorem Cslib.Crypto.Sponge.badEvent_of_out_eq {R : Type u_1} {C : Type u_2} {O : Type u_3} [AddGroup R] [Zero C] (P : R × C → R × C) (out : R × C → O) {ms ms' : List R} (hms : ms ≠ []) (hms' : ms' ≠ []) (hne : ms ≠ ms') (h : out (absorb P ms) = out (absorb P ms')) :
(ImageCollision P out fun (x : R × C) => IsInput P ms x ∨ IsInput P ms' x) ∨ (ZeroCapacity P fun (x : R × C) => IsInput P ms x ∨ IsInput P ms' x) ∨ ImageCollision P Prod.snd fun (x : R × C) => IsInput P ms x ∨ IsInput P ms' x

Claim 7.8 for forward queries ([KL20], Section 7.3.3): if two distinct nonempty block sequences have equal first output, then among the permutation inputs evaluated while absorbing them, event E1 (two distinct inputs whose images have equal output), event E2 (an image with zero capacity part) or event E3 (two distinct inputs whose images have equal capacity part) occurs.

theorem Cslib.Crypto.Sponge.absorb_eq_of_capacity_eq {R : Type u_1} {C : Type u_2} [AddGroup R] [Zero C] (P : R × C → R × C) {a a' : List R} (h : (absorb P a).2 = (absorb P a').2) (m : R) (s : List R) :
absorb P (a ++ m :: s) = absorb P (a' ++ (-(absorb P a').1 + ((absorb P a).1 + m)) :: s)

A capacity collision extends to a full collision. If a and a' reach states with equal capacity part, then appending m to a and the corrected block -(absorb P a').1 + ((absorb P a).1 + m) to a' reaches the same state, so any common suffix gives equal hashes.

The sponge hash on messages #

def Cslib.Crypto.Sponge.hash {r c : ℕ} {O : Type u_1} (P : BitString r × BitString c → BitString r × BitString c) (out : BitString r × BitString c → O) (k : ℕ) (msg : List Bool) :

The sponge hash of Construction 7.6 with k squeezing outputs: pad the message with pad10star, parse it into r-bit blocks and run hashBlocks. The output is the list [h₁, …, h_k]; with out the first v bits of the state, its flattening is h₁ ‖ ⋯ ‖ h_k.

theorem Cslib.Crypto.Sponge.badEvent_of_hash_eq {r c : ℕ} {O : Type u_1} (hr : 0 < r) (P : BitString r × BitString c → BitString r × BitString c) (out : BitString r × BitString c → O) (k : ℕ) {msg msg' : List Bool} (hne : msg ≠ msg') (h : hash P out (k + 1) msg = hash P out (k + 1) msg') :

Claim 7.8 for forward queries, on messages: a collision between distinct messages triggers event E1, E2 or E3 among the permutation inputs evaluated while absorbing their padded block sequences.

theorem Cslib.Crypto.Sponge.hash_eq_of_capacity_eq {r c : ℕ} {O : Type u_1} (hr : 0 < r) (P : BitString r × BitString c → BitString r × BitString c) {u u' : List Bool} (hu : r ∣ u.length) (hlen : u.length = u'.length) (hne : u ≠ u') (h : (absorb P (BitString.toBlocks r u)).2 = (absorb P (BitString.toBlocks r u')).2) (m : BitString r) (out : BitString r × BitString c → O) (k : ℕ) :
u ++ List.ofFn m ≠ u' ++ List.ofFn (-(absorb P (BitString.toBlocks r u')).1 + ((absorb P (BitString.toBlocks r u)).1 + m)) ∧ hash P out k (u ++ List.ofFn m) = hash P out k (u' ++ List.ofFn (-(absorb P (BitString.toBlocks r u')).1 + ((absorb P (BitString.toBlocks r u)).1 + m)))

Generic capacity-collision attack: if distinct full-block messages u ≠ u' of equal length reach states with equal capacity part, then extending them by one block each (m, and the corrected block for u') gives two distinct messages with equal hashes, for every output map and number of squeezing outputs.

theorem Cslib.Crypto.Sponge.hash_prodCongr_collision {r c : ℕ} {O : Type u_1} (hr : 0 < r) (S : Equiv.Perm (BitString r)) (T : Equiv.Perm (BitString c)) (m : BitString r) (out : BitString r × BitString c → O) (k : ℕ) :
(List.ofFn fun (x : Fin r) => false) ++ List.ofFn m ≠ (List.ofFn fun (x : Fin r) => true) ++ List.ofFn ((-S fun (x : Fin r) => true) + ((S fun (x : Fin r) => false) + m)) ∧ hash (⇑(Equiv.prodCongr S T)) out k ((List.ofFn fun (x : Fin r) => false) ++ List.ofFn m) = hash (⇑(Equiv.prodCongr S T)) out k ((List.ofFn fun (x : Fin r) => true) ++ List.ofFn ((-S fun (x : Fin r) => true) + ((S fun (x : Fin r) => false) + m)))

Exercise 7.26 ([KL20], Section 7.3.3): if the permutation acts separately on the rate and capacity parts, P = S × T, the capacity part after absorbing never depends on the message, so the one-block messages 0^r and 1^r already have equal capacity part and extend to an explicit collision. (The exercise's P = S₁ ‖ ⋯ ‖ S₈ with r = c = 4n is such a product.)