Documentation

CslibFrontier.Crypto.Primitives.Hash.Keccak

Keccak-p[1600, 24], SHA3-512 and byte-aligned SHAKE #

This module implements the concrete permutation and byte-aligned extendable-output function from NIST FIPS 202. The state is a 5 × 5 array of 64-bit lanes. Lane bit z is the standard's A[x, y, z], and a flat state bit at position 64 * (5 * y + x) + z maps to that coordinate (Sections 3.1.2--3.1.3). The five round steps follow Algorithms 1--7 and Tables 1--2 in Sections 3.2--3.3; keccakP1600Rounds24 is Algorithm 8's underlying function as specified in Section 4.

byteSponge implements Algorithms 8--9 for byte-aligned messages at any lane-aligned Keccak-p[1600] rate/capacity split and explicit domain-separation suffix. Bytes are expanded least significant bit first, matching NIST's official example state dumps; the suffix and pad10*1 are appended; rate blocks are processed by the existing Cslib.Crypto.Sponge.hashBlocks; and output bytes are assembled in the same convention. sha3512 uses the 576-bit rate, suffix 01, and a 64-byte output. shake256 uses the 1088-bit rate and suffix 1111; SHAKE128 specializes the same machinery in CslibFrontier.Crypto.Primitives.Hash.Keccak.SHAKE128.

This is a checked executable specification, not a constant-time or efficient implementation, a cryptographic security proof, or a claim of validation under FIPS 202. It maps to the published August 2015 standard and does not claim agreement with a future revision or with FN-DSA / FIPS 206.

Main definitions #

Main results #

References #

@[reducible, inline]

A 64-bit lane in the Keccak-p[1600] state. Bit-vector position zero is coordinate z = 0.

@[reducible, inline]

The 25 lanes of a Keccak-p[1600] state in FIPS 202's flat order 5 * y + x.

The vector representation makes each round materialize all lanes once. This avoids the exponential recomputation that would result from composing 24 rounds as nested state functions.

A lane-aligned rate/capacity split of the 25-lane Keccak-p[1600] state.

  • rateLanes : ℕ

    The number of 64-bit lanes exposed by the rate.

  • capacityLanes : ℕ

    The number of 64-bit lanes retained as capacity.

  • rate_pos : 0 < self.rateLanes

    A byte XOF has a nonempty rate.

  • lane_count : self.rateLanes + self.capacityLanes = 25

    Rate and capacity together fill the Keccak-p[1600] state.

@[reducible, inline]

The rate and capacity bit-string views selected by a lane-aligned sponge layout.

The SHA3-512 layout: nine rate lanes and sixteen capacity lanes.

The SHAKE256 layout: 17 rate lanes and eight capacity lanes.

@[reducible, inline]

The sponge rate and capacity views used by SHAKE256.

Reduce a natural-number state coordinate modulo five.

The flat FIPS 202 lane index corresponding to state coordinates (x, y).

Read the Keccak state lane A[x, y].

The rotation offset for lane (x, y), from FIPS 202 Table 2, reduced modulo 64.

The 24 round constants for Keccak-f[1600], in round order.

FIPS 202 Algorithm 1, the column-parity step θ.

FIPS 202 Algorithm 2, the lane-rotation step ρ.

FIPS 202 Algorithm 3, the lane-permutation step π.

FIPS 202 Algorithm 4, the nonlinear row step χ.

def Cslib.Crypto.Keccak.iota (constant : Lane) (A : State) :

FIPS 202 Algorithm 6, the round-constant step ι.

def Cslib.Crypto.Keccak.round (constant : Lane) (A : State) :

One FIPS 202 round: ι ∘ χ ∘ π ∘ ρ ∘ θ.

FIPS 202 KECCAK-p[1600, 24], equivalently KECCAK-f[1600], on lane arrays.

Pack 64 least-significant-bit-first coordinates into a lane.

theorem Cslib.Crypto.Keccak.getLsbD_laneOfBits (bits : BitString 64) (i : ℕ) (hi : i < 64) :
BitVec.getLsbD (laneOfBits bits) i = bits ⟨i, hi⟩

Reading an in-range coordinate of laneOfBits returns the corresponding input bit.

Shared lane-aligned sponge adapter #

Convert a lane-aligned rate/capacity split into the FIPS 202 lane array.

theorem Cslib.Crypto.Keccak.get_ofSpongeView_rate (layout : SpongeLayout) (state : SpongeView layout) (lane : Fin 25) (h : ↑lane < layout.rateLanes) :
Vector.get (ofSpongeView layout state) lane = laneOfBits fun (z : Fin 64) => state.1 ⟨64 * ↑lane + ↑z, ⋯⟩

A rate lane reads the corresponding 64 rate coordinates.

theorem Cslib.Crypto.Keccak.get_ofSpongeView_capacity (layout : SpongeLayout) (state : SpongeView layout) (lane : Fin 25) (h : ¬↑lane < layout.rateLanes) :
Vector.get (ofSpongeView layout state) lane = laneOfBits fun (z : Fin 64) => state.2 ⟨64 * (↑lane - layout.rateLanes) + ↑z, ⋯⟩

A capacity lane reads the corresponding 64 capacity coordinates.

Convert the FIPS 202 lane array into a selected rate/capacity split.

theorem Cslib.Crypto.Keccak.toSpongeView_ofSpongeView (layout : SpongeLayout) (state : SpongeView layout) :
toSpongeView layout (ofSpongeView layout state) = state

Converting a selected sponge view to lanes and back preserves every state bit.

Converting a lane state to a selected sponge view and back preserves every lane.

Keccak-p[1600, 24] on a lane-aligned sponge view.

Shared byte-aligned sponge machinery #

Expand a byte into FIPS 202 bit-string order, least significant bit first.

Expand a byte string into FIPS 202 bit-string order.

Each serialized FIPS 202 byte contributes exactly eight bits.

FIPS 202 Algorithm 9, pad10*1(x, m).

Pack the first eight least-significant-bit-first coordinates into a byte.

@[simp]

Expanding one byte least-significant bit first and reading it back recovers the byte.

@[simp]

Reading an eight-bit word as a byte and expanding it recovers the word.

Packing an aligned bit string eight bits at a time and expanding recovers the string.

Little-endian expansion preserves all the information in a byte string.

The FIPS 202 little-endian bijection between a fixed byte string and its bit register.

The register has precisely the bits that the underlying SHAKE byte stream returns.

The first n bytes expand to the first 8 * n bits, including truncated inputs.

def Cslib.Crypto.Keccak.rateBytesFor (layout : SpongeLayout) (rate : BitString (64 * layout.rateLanes)) :
List (Fin 256)

Read the rate selected by layout as consecutive little-endian bytes.

@[simp]
theorem Cslib.Crypto.Keccak.length_rateBytesFor (layout : SpongeLayout) (rate : BitString (64 * layout.rateLanes)) :
(rateBytesFor layout rate).length = 8 * layout.rateLanes

A lane-aligned rate exposes eight bytes per lane.

def Cslib.Crypto.Keccak.squeezeBytesFor (layout : SpongeLayout) (blocks : List (BitString (64 * layout.rateLanes))) (count : ℕ) :
List (Fin 256)

Absorb padded blocks and expose count squeezed rate states as bytes.

@[simp]
theorem Cslib.Crypto.Keccak.length_squeezeBytesFor (layout : SpongeLayout) (blocks : List (BitString (64 * layout.rateLanes))) (count : ℕ) :
(squeezeBytesFor layout blocks count).length = 8 * layout.rateLanes * count

count squeezing blocks contain 8 * rateLanes * count bytes.

theorem Cslib.Crypto.Keccak.take_squeezeBytesFor (layout : SpongeLayout) (blocks : List (BitString (64 * layout.rateLanes))) {m n : ℕ} (h : m ≤ n) :
List.take (8 * layout.rateLanes * m) (squeezeBytesFor layout blocks n) = squeezeBytesFor layout blocks m

Requesting more squeezing blocks preserves every byte of the earlier blocks.

def Cslib.Crypto.Keccak.bitSponge (layout : SpongeLayout) (suffix input : List Bool) (outputBytes : ℕ) :
List (Fin 256)

Absorb an arbitrary bit string with a distinguished suffix and FIPS 202 padding, then squeeze the requested number of little-endian bytes.

def Cslib.Crypto.Keccak.byteSponge (layout : SpongeLayout) (suffix : List Bool) (input : List (Fin 256)) (outputBytes : ℕ) :
List (Fin 256)

Byte-aligned Keccak sponge with the rate and capacity selected by layout and the supplied domain-separation suffix, whose bits are listed in FIPS 202 bit-string order.

The output length is measured in bytes. The input bytes and output bytes are little-endian at the bit boundary, and FIPS 202's pad10*1 follows the suffix.

@[simp]
theorem Cslib.Crypto.Keccak.length_bitSponge (layout : SpongeLayout) (suffix input : List Bool) (outputBytes : ℕ) :
(bitSponge layout suffix input outputBytes).length = outputBytes

An arbitrary bit-string input produces exactly the requested number of output bytes.

@[simp]
theorem Cslib.Crypto.Keccak.length_byteSponge (layout : SpongeLayout) (suffix : List Bool) (input : List (Fin 256)) (outputBytes : ℕ) :
(byteSponge layout suffix input outputBytes).length = outputBytes

A byte-aligned sponge returns exactly the requested byte count.

theorem Cslib.Crypto.Keccak.take_bitSponge (layout : SpongeLayout) (suffix input : List Bool) {m n : ℕ} (h : m ≤ n) :
List.take m (bitSponge layout suffix input n) = bitSponge layout suffix input m

On arbitrary bit-string inputs, shorter sponge outputs are prefixes of longer ones.

theorem Cslib.Crypto.Keccak.take_byteSponge (layout : SpongeLayout) (suffix : List Bool) (input : List (Fin 256)) {m n : ℕ} (h : m ≤ n) :
List.take m (byteSponge layout suffix input n) = byteSponge layout suffix input m

For a fixed suffix, byte-aligned sponge output is prefix-consistent across requested lengths.

def Cslib.Crypto.Keccak.byteXof (layout : SpongeLayout) (input : List (Fin 256)) (outputBytes : ℕ) :
List (Fin 256)

Byte-aligned SHAKE with the rate and capacity selected by layout.

The output length is measured in bytes. Every specialization uses the SHAKE suffix 1111 and FIPS 202's pad10*1; layout changes only the rate/capacity split.

@[simp]
theorem Cslib.Crypto.Keccak.length_byteXof (layout : SpongeLayout) (input : List (Fin 256)) (outputBytes : ℕ) :
(byteXof layout input outputBytes).length = outputBytes

A byte-aligned XOF returns exactly the requested byte count.

theorem Cslib.Crypto.Keccak.take_byteXof (layout : SpongeLayout) (input : List (Fin 256)) {m n : ℕ} (h : m ≤ n) :
List.take m (byteXof layout input n) = byteXof layout input m

Byte-aligned XOF output is prefix-consistent across requested lengths.

SHA3-512 specialization #

def Cslib.Crypto.Keccak.sha3512 (input : List (Fin 256)) :
List (Fin 256)

Byte-aligned SHA3-512 from FIPS 202 Section 6.1.

The input bytes use the explicit little-endian bit convention of bytesToBitsLE. The suffix is the SHA-3 domain separator 01 in FIPS bit-string order, and the digest is 64 bytes.

@[simp]
theorem Cslib.Crypto.Keccak.length_sha3512 (input : List (Fin 256)) :
(sha3512 input).length = 64

SHA3-512 returns exactly 64 bytes.

SHAKE256 specialization #

Convert the SHAKE256 rate/capacity split into the FIPS 202 lane array.

theorem Cslib.Crypto.Keccak.get_ofSpongeState_rate (state : SpongeState) (lane : Fin 25) (h : ↑lane < 17) :
Vector.get (ofSpongeState state) lane = laneOfBits fun (z : Fin 64) => state.1 ⟨64 * ↑lane + ↑z, ⋯⟩

A SHAKE256 rate lane reads the corresponding 64 rate coordinates.

theorem Cslib.Crypto.Keccak.get_ofSpongeState_capacity (state : SpongeState) (lane : Fin 25) (h : ¬↑lane < 17) :
Vector.get (ofSpongeState state) lane = laneOfBits fun (z : Fin 64) => state.2 ⟨64 * (↑lane - 17) + ↑z, ⋯⟩

A SHAKE256 capacity lane reads the corresponding 64 capacity coordinates.

Convert the FIPS 202 lane array into SHAKE256's 1088-bit rate and 512-bit capacity split.

Converting a SHAKE256 sponge split to lanes and back preserves every state bit.

Converting a lane state to the SHAKE256 split and back preserves every lane.

FIPS 202 KECCAK-p[1600, 24] on SHAKE256's rate/capacity split.

Read the 1088-bit SHAKE256 rate as 136 consecutive little-endian bytes.

@[simp]

One SHAKE256 rate block contains exactly 136 output bytes.

def Cslib.Crypto.Keccak.squeezeBytes (blocks : List (BitString 1088)) (count : ℕ) :
List (Fin 256)

Squeeze SHAKE256 rate states as bytes.

@[simp]
theorem Cslib.Crypto.Keccak.length_squeezeBytes (blocks : List (BitString 1088)) (count : ℕ) :
(squeezeBytes blocks count).length = 136 * count

count SHAKE256 squeezing blocks contain exactly 136 * count bytes.

theorem Cslib.Crypto.Keccak.take_squeezeBytes (blocks : List (BitString 1088)) {m n : ℕ} (h : m ≤ n) :
List.take (136 * m) (squeezeBytes blocks n) = squeezeBytes blocks m

Requesting more SHAKE256 squeezing blocks preserves every byte of the earlier blocks.

def Cslib.Crypto.Keccak.shake256 (input : List (Fin 256)) (outputBytes : ℕ) :
List (Fin 256)

Byte-aligned SHAKE256 from FIPS 202 Section 6.2.

@[simp]
theorem Cslib.Crypto.Keccak.length_shake256 (input : List (Fin 256)) (outputBytes : ℕ) :
(shake256 input outputBytes).length = outputBytes

SHAKE256 returns exactly the requested number of bytes.

theorem Cslib.Crypto.Keccak.take_shake256 (input : List (Fin 256)) {m n : ℕ} (h : m ≤ n) :
List.take m (shake256 input n) = shake256 input m

SHAKE256 is prefix-consistent: requesting more output never changes preceding bytes.