Skip to content

Hashes, MACs and KDFs: remove dead declarations and modules - #4349

Merged
reaperhulk merged 1 commit into
mainfrom
claude/unify-random-bytes-crypto-xqmg0z-deadcode-hashes
Nov 6, 2026
Merged

reaperhulk merged 1 commit into
mainfrom
claude/unify-random-bytes-crypto-xqmg0z-deadcode-hashes

Conversation

@reaperhulk

Copy link
Copy Markdown
Member

Like #4305 (DSA) and #4314 (ECDSA), this removes the proof and implementation declarations that nothing reachable uses, for the hashes, MACs and KDFs: SHA-1, SHA-2 (SHA-256, SHA-512), SHA-3, SM3, MD5, BLAKE2, the shared Merkle–Damgård streaming code (MdStream), HMAC, PBKDF2, scrypt and Argon2. That is 1,135 declarations, including 7 whole modules, under Proof/ and Impl/ of those algorithms.

  • Nothing in Spec/, TCB/, src/ or the generated crates changes. Emit.lean --check passes, so the emitted code is identical.
  • MdHashTable and HashTable had nothing dead to remove, and there is no Sha2 directory.

366 files, +95 / −9,423.

How "dead" was decided

The method of tools/deadcode/README.md (on the tooling branch), with SCOPE set to the directories above:

  • Graph. A declaration-level graph built from the .ilean files of a full build.
  • Live roots. Every declaration in Artifacts/, Generic/, Variants/, TCB/ and VerifiedGarbageTest/; the non-Spec modules the lakefile names; the modules the top-level *.lean files import.
  • Uses the .ilean files don't record, kept conservatively:
    • Literals found by name: every module-level command of a module the roots import is live, with what it references (materialize_*, taint_summary), and Lit modules that live code imports stay.
    • Tactics, macros and notation: a module that defines syntax is never deleted.
    • Attributes and instances: anything carrying a simp-set or instance attribute in a module the roots import is live.
  • Removal set. Dead declarations of the scope whose every referrer is removed too, iterated to a fixed point; modules that other algorithms' registrations import are trimmed, never deleted.
  • Final check. The full build is the arbiter.

The analysis also proposed the auxiliary declarations of notations and syntax; I took those out of the removal set by hand (see "Kept on purpose"), since their uses leave no reference.

Removed

7 modules:

  • Proof/MdStream/X86/ArgWord
  • Proof/Pbkdf2/Hmac
  • Proof/Scrypt/Contract, Proof/Sm3/Contract (the per-routine contracts only the removed Verified proofs used)
  • Proof/Sha256/AArch64/Stream/Common, Proof/Sha256/Arm/Stream/Common
  • Proof/Sha3/AArch64/Neon/Rounds

1,128 further declarations from 343 modules that remain, mostly unused helpers, *_ct/*_wp/*_ok lemmas, contracts and satisfying states of the removed proofs, and unused Impl definitions in 12 files (code shapes nothing emits or proves, such as compressAt, referenceBlockMix, progWith, *Main := *With .scalar). By algorithm: scrypt 227, SHA-256 145, PBKDF2 134, Argon2 128, SHA-3 119, SHA-512 113, BLAKE2 78, SM3 65, SHA-1 57, MD5 35, MdStream 25, HMAC 8, HmacTable 1.

146 top-level Verified proofs of routines that no registration exports. Each proves that an internal routine meets a contract (usually the shared one, with widened scratch) as if it were exported on its own. No registration file (Artifacts/, Generic/, Variants/) refers to any of them on any target: the routines are only internal callees of what is exported (update/finalize, HMAC, PBKDF2, scrypt, Argon2), and their correctness and constant-time proofs, which those callers use, stay. The emitted functions are unchanged.

  • SHA-1 (16), SHA-256 (16), SHA-512 (16), SM3 (12), MD5 (5): the compression function against the shared contract (Shared.compress, compressWide_verified, compress_of, compressWide_of, compress_shani, compress_avx2) on each target; SHA-512's X86_64.compressWide_verified and X86_64.Avx2.compress_verified; and the per-file update_verified/finalize_verified of SHA-1 (AArch64, x86-64, and x86-64's Compress) and SHA-256 (AArch64).
  • SHA-3 (10): the permutation (permute_verified, permute_verified', Shared.permute) on AArch64 (including Scalar.Lazy and Hybrid.X3), ARMv7, PPC64LE, RISC-V, x86 and x86-64.
  • BLAKE2 (20): compressB_verified/compressS_verified (and their primed forms) on AArch64, PPC64LE and RISC-V, ARMv7's and x86's compress_verified/compressB_verified, and the scratch-taking stream functions on RISC-V and x86-64 (updateScratchB_verified, finalizeScratchB_verified, updateB_verified, finalizeB_verified).
  • PBKDF2 (18): the per-hash iterate on PPC64LE (8) and RISC-V (7), the generic PPC64LE.verified and RISCV64.verified, and Sha1.RISCV64.iterate_verified.
  • scrypt (19): salsa_verified, blockMix_verified and roMix_verified on every target (including AArch64's Resident and x86-64's Fused and DirectFill).
  • Argon2 (14): the compression function's compress_verified (AArch64, ARMv7, PPC64LE, x86's compressShared/compressWide, x86-64 scalar, AVX2 and AVX-512) and H′'s verified/hPrimeShared_verified/hPrimeWide_verified (AArch64, ARMv7, PPC64LE, x86, x86-64).

Outside these directories, only imports of deleted modules are dropped:

  • HMAC's and PBKDF2's AArch64 registrations, Generic/MdHash/AArch64/{Hmac,Pbkdf2}.lean (check_registrations passes);
  • Proof/Variants/Sha1/X86/{Scalar,ShaNi}.lean;
  • Proof/Ed448/X86/SignCached/Hash.lean, Proof/Ecdsa/Rfc6979/AArch64/Hash.lean, Proof/RsaPkcs1Enc/{Arm,X86}/DecKdk.lean and six Proof/MlKem/AArch64/ modules.

Other edits that come with the removal:

  • Proof/Pbkdf2/Memory now imports Proof/Sha256/Stream itself, which the deleted Pbkdf2/Hmac passed on.
  • Tactic templates and macros that named removed declarations: compressWide is dropped from the narrow simp sets of SHA-256 (ARMv7) and SHA-512 (ARMv7, x86); SM3 x86's narrowC and the narrow macros of Argon2 x86 and scrypt x86 (Salsa, BlockMixVerified, RoMixCT), which only the removed proofs used, are removed.
  • Dangling omit … in, include … in, open … in and set_option … in lines, empty sections and namespace blocks, and headings with nothing left under them are removed.
  • Doc comments that named removed declarations are reworded (module headers of the Shared files, PBKDF2's per-hash iterate files, scrypt's and Argon2's Verified files, and others).
  • ci/lean_speed_allowed.py lowers its counts for Sha512/X86_64/Avx2/Compress.lean and drops one for Blake2/AArch64/Compress.lean, as check_lean_speed.py asked.

Kept on purpose

Rerunning the analysis after the removal leaves 106 dead declarations in scope, all kept for these reasons:

  • Notation and syntax (20): the auxiliary declarations of the local +++ notation (Argon2, BLAKE2, SHA-3 and SHA-512 Impl files on ARMv7/x86) and the property notation (Argon2 DeriveVerified on AArch64 and x86-64), and SHA-3 AArch64's sponge_taint_summaries command and sponge_taint_decide tactic (Proof/Sha3/AArch64/Sums) with the helpers their elaborators run (spongeSumCmds, spongeSumNames, spongeSumSavingCmds, mlkemSumNames, spongeSaved). Their uses leave no reference, so the analysis cannot see them.
  • Scrypt.PPC64LE.Whole.fr_addr, which the frame_run macro's simp set names.
  • Used by other algorithms' (dead) code (85): SHA-3 AArch64's scalar permutation proof (Permutation.scalar and what it needs, with Proof/Sha3/{Env,Lanes} lemmas) and Impl.Sha3.AArch64.Stream.{absorb,pad,squeeze}, which ML-KEM, ML-KEM-1024 and ML-DSA AArch64 proofs use; SHA-256 x86's Shared.compressWide{,_of,_implies} with their helpers, and Proof.Sha256.compressContract/compressSig, which Proof/Variants/Sha256/X86/ShaNi's compress_shared uses. Removing these belongs with those algorithms' passes.

Dead Spec/ definitions noticed in scope (not removed here)

Listed only; a separate PR removes dead Spec definitions.

  • Spec/Sha3/Length.lean: flatMap_lanes_length, sha3_256_length, shake256_length, squeezeBlocks_length, squeeze_length, toBytes_length
  • Spec/Blake2/Contract.lean: blake2s_eq_finalHash
  • Spec/HashTable.lean: index_hashes

Checks

  • Lean:
    • the full lake build --log-level=warning passes, before (28,920 jobs) and after (28,828 jobs) rebasing onto current main (5d3313ab7);
    • Emit.lean --check passes with the standard axiom audit, with no change to any crate;
    • check_lean_public_imports.py --base origin/main -m on the 358 modified modules passes (it checks the one whose imports changed, Proof/Pbkdf2/Memory).
  • Static checks: every step of the Static checks job passes, with check_lean_single_importer.py --base origin/main, including run_unit_tests (641 tests).
  • Rust: cargo fmt --check and cargo clippy --all-targets -- -D warnings pass. No Rust changed.
  • No escape hatches: nothing adds sorry, native_decide, bv_decide or maxHeartbeats.
  • Re-analysis: after the removal, nothing dead is left in scope beyond what is kept above.

No benchmark: the emitted code is unchanged.


Generated by Claude Code

Remove the 1,135 proof and implementation declarations of the hash, MAC
and KDF algorithms (SHA-1, SHA-2, SHA-3, SM3, MD5, BLAKE2, the shared
Merkle-Damgard streaming code, HMAC, PBKDF2, scrypt, Argon2) that nothing
reachable from the registrations uses, including 7 whole modules and 146
top-level `Verified` proofs of routines no registration exports.

Nothing in Spec/, TCB/, src/ or the generated crates changes; Emit.lean
--check passes, so the emitted code is identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWu41XvSg4zgCD1juXF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants