Skip to content

x86-64 SHA-1, SHA-224, the SHA-512 family, MD5 and SM3 return without secret residue - #4842

Merged
reaperhulk merged 6 commits into
mainfrom
claude/residue-hashes
Nov 10, 2026
Merged

reaperhulk merged 6 commits into
mainfrom
claude/residue-hashes

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

The first burn-down after #4836. It converts the rest of the Merkle–Damgård hashes, taking 36 lines off ci/residue_allowlist.txt (861 → 825). No trust change: nothing under TCB/ or Spec/ changes.

What is converted

Each function is scrubbed the way #4836 scrubbed SHA-256. Its code is followed by scrubTail, which clears what the residue analysis finds may hold secrets, and the kernel checks that analysis with taint_decide in the module that registers the function.

  • SHA-1: update, finalize and digest, for the scalar, AVX/BMI and SHA-NI compression functions (9).
  • SHA-224: finalize and digest, over each SHA-256 compression function (6).
  • SHA-384, SHA-512, SHA-512/224 and SHA-512/256: scalar and SHA-NI (16).
    • SHA-512's update and finalize.
    • Each hash's finalize and digest.
  • MD5: update and finalize (2).
  • SM3: init, update and finalize (3).

What stays listed, and why

  • The SHA-512 AVX2 functions: the analysis does not yet accept them.
  • SHA-512's one-shot digest: Ed25519's key-pair generation calls vg_sha512_digest and vg_sha512_digest_shani by name.
  • The init functions of MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224 and SHA-512/256: HMAC, PBKDF2, DSA and RSA call each by its exported name.

In both of the last two cases, the emitter refuses a name whose code differs from the code its callers run. Converting them takes what #4836 did for Poly1305's init: the callers call the unscrubbed code under a name of its own (vg_poly1305_init_core). That changes every caller's generated code, so it gets its own PR.

Proofs

  • Hashes/Sha1Scrub.lean, Hashes/Sha512Scrub.lean, and SHA-224's part of Hashes/Sha256Scrub.lean: each PostOut fact says the function's postcondition reads memory on return only within its buffers. That is the streaming state (repr_congr) and the digest.
  • Hashes/Sha1.lean, Sha224.lean and Sha512.lean: each stream list is split into one definition per function, as x86-64: the residue analysis and scrub, an allowlist that only shrinks, and SHA-256, Poly1305 and ChaCha20 apply without residue #4836 did for SHA-256.
  • Variants/MdHash/X86_64/Sha1.lean, Sha512.lean and Sha512ShaNi.lean (new), plus the existing SHA-1 and SHA-256 variant files: the scrubs, each checked against its own variant's code.
  • Proof/Md5/X86_64/Scrub.lean and Proof/Sm3/X86_64/Scrub.lean: registrations built with withScrub, which Artifacts/{Md5,Sm3}/X86_64.lean bind.

Checks

  • lake build (all modules), Emit.lean --check, the Static checks job, check_lean_public_imports.py on the changed modules: pass.
  • cargo fmt --check, cargo clippy --all-targets -- -D warnings: pass.
  • cargo test with Wycheproof, with every CPU feature and with VG_CPU_FEATURES=none: pass.

Speed

Benchmarks run 42657421527: x86-64, against #4836's merge, on an AMD EPYC 7763, one interleaved run. It covers the md5 sha1 sha224 sha384 sha512 sha512-224 sha512-256 sm3 groups, in every configuration that chooses among their implementations.

The scrub is a fixed cost per call. No benchmark slowed by more than 4.0%, and none by more than 12 ns:

  • SHA-224 at 64 B: 4.0% slower with avx,avx2,bmi1,bmi2 (312 → 324 ns), 3.2% with sha,ssse3 (93 → 96 ns). At 1 KiB, 0.4%.
  • SHA-512 at 64 B with sha,ssse3 (the scalar compression function): 3.8% slower (257 → 266 ns).
  • Everything else: within ±2%, at every size and in every configuration where this PR changes the code. That covers SHA-1, MD5, SM3, SHA-384, SHA-512/224 and SHA-512/256.

The default and avx,avx2,bmi1,bmi2 configurations show SHA-384, SHA-512, SHA-512/224 and SHA-512/256 8–11% faster at 32–111 B. They run the SHA-512 AVX2 code, which this PR does not change, so I don't credit this PR with the speedup: it is noise or code layout.

The AVX-512 IFMA configuration was not measured, since the runner lacks it. It runs the same hash code as the default configuration.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa

…sidue

SHA-224's finalize and digest over each SHA-256 compression function,
SHA-1's update, finalize and digest over each of its own, and SHA-384's,
SHA-512's, SHA-512/224's and SHA-512/256's over the scalar and SHA-NI
compression functions, scrubbed as SHA-256's are (StreamFn.scrub): each
variant file checks the residue analysis of its own code with taint_decide,
and the PostOut lemmas of each spec say its postconditions read only the
buffers. The SHA-512 AVX2 functions stay listed: the analysis does not yet
accept them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
MD5's and SM3's init, update and finalize, and the init of SHA-1, SHA-224,
SHA-256, SHA-384, SHA-512, SHA-512/224 and SHA-512/256, each scrubbed with
withScrub and its residue analysis checked by taint_decide; their
registration files bind the results.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
HMAC, PBKDF2, DSA and RSA call each hash's init by its exported name, with
its unscrubbed code, so scrubbing those needs a separate name for the
callers' code (as vg_poly1305_init_core): a change of its own. SM3's init,
which nothing calls, stays converted. Removes the 37 converted functions'
lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
Ed25519's key-pair generation calls vg_sha512_digest (and its SHA-NI
variant) with its unscrubbed code, so it waits for a callers' name of its
own, as the hash inits do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 10, 2026
Merged via the queue into main with commit cc51087 Nov 10, 2026
31 checks passed
@reaperhulk
reaperhulk deleted the claude/residue-hashes branch November 10, 2026 04:52
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