MD strengthening is suffix-free
ProvedCityMD.shaPad_suffixFreeFor , the SHA-2 style padding is suffix-free on bit strings of length .
Preamble
import Mathlib import Definitions.Def_CityMD open Cryptography.MerkleDamgard CityMD
Formal statement
/-- MD-strengthening padding is suffix-free on messages shorter than `2 ^ ℓ` bits. -/
theorem CityMD.shaPad_suffixFree (w ℓ : ℕ) (hw : 0 < w) :
SuffixFree (fun m : {m : List Bool // m.length < 2 ^ ℓ} => shaPad w ℓ m.1) := by sorryRead-back
What the Lean code literally says, in plain math · claude-opus-5-5
CityMD.shaPad_suffixFree
Read-back
For all w, ℓ with w > 0: on the set of bit strings m with |m| < 2^ℓ, the map m ↦ shaPad w ℓ m is suffix-free. That is, for messages m ≠ m′ (both shorter than 2^ℓ), the block list shaPad(m) is not a suffix of the block list shaPad(m′).
Audit notes
- True. All blocks have length exactly w. A block-list suffix therefore implies a bit-string suffix after flattening, so the last ℓ bits agree. That gives bitsBE ℓ |m| = bitsBE ℓ |m′|. Both lengths are below 2^ℓ, so |m| = |m′|. Then z agrees, so the two block lists have the same length. A suffix of the same length is equality, and equality forces m = m′.
- The bound |m| < 2^ℓ is genuinely needed. Without it, lengths wrap modulo 2^ℓ.
- ℓ = 0: only m = [] is allowed, so the domain is a singleton and the claim is trivially true. That is consistent.
- w = 0 is excluded by
hw. With w = 0 the whole string becomes one chunk and the claim would still hold, sohwis harmless. - Checked by evaluation:
shaPad 4 3 [1,0] = [[1,0,1,0],[0,0,1,0]], which is the message, a 1-bit, no zeros, and the length 2 written as010. A 7-bit message with w=5, ℓ=3 gives 3 blocks of length 5. - The docstring matches the code. Endianness is MSB-first, as SHA-2 uses.