Skip to content

MdStream x86-64: bound each indexed buffer store by clamping its index - #4745

Merged
alex merged 1 commit into
mainfrom
claude/mdstream-clamp
Nov 9, 2026
Merged

alex merged 1 commit into
mainfrom
claude/mdstream-clamp

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Groundwork for verified residue clearing on x86-64 (proving that a function leaves no secret in registers or on the stack when it returns). It changes no contract, spec or TCB.

What changes

Every store into the 64- or 128-byte block buffer of the streaming (Impl/MdStream/X86_64.lean) and one-shot (Impl/MdStream/X86_64/OneShot.lean) Merkle–Damgård code now goes through one memory operand, bufByte and blkByte. Its index is first clamped to the buffer:

mov32 lim, B − w ; cmp idx, lim ; cmovb lim, idx    ; then store at [base + lim]

Whenever the existing invariant holds (the index plus the width fits the buffer), this is the same index, so the stored bytes and every contract are unchanged. The proofs are the same but for that one lemma (Proof/MdStream/X86_64/Common.lean).

Why

The residue analysis can only tell that a store stays in the buffer from a bound on its index register. Without the clamp, the bound is a relation between two registers (r13 + rax ≤ 64), which it does not track. The analysis then assumes the store may hit any stack slot, and gives up on knowing which slots hold secrets. The clamp gives the bound directly.

Affected

Every x86-64 MD5, SHA-1, SHA-2 and SM3 function (streaming update/finalize, one-shot digest), and their HMAC, PBKDF2 and ECDSA-RFC 6979 callers' scratch helpers: the regenerated crates/verified-garbage-asm-x86_64/ files.

Checks

Speed

Benchmarks run 42525736855: x86-64, every VG_CPU_FEATURES configuration that chooses among these modules' implementations, against #4678. No benchmark slowed by more than 5%.

  • SHA-256 at every size from 32 B to 16 KiB: within 0.5%, both with every CPU feature and with none.
  • HMAC-SHA-256 and SHA-512: within 1%.
  • SHA-1: within 2.4%.
  • The none shard holding MD5, SHA-224, SHA-384 and SM3: within ±5% either way, where the spread between copies of the same binary was 3–17%.

A local run had shown SHA-256 1–4% slower with none; the runners do not reproduce it.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa

…x; re-prove

Each store into the buffer of the streaming (MdStream) and one-shot
(OneShot) Merkle–Damgård code goes through one memory operand, whose index
is first clamped to the buffer (mov32 lim; cmp idx, lim; cmovb lim, idx):
the same value whenever the existing invariant holds, so the proofs are the
same but for that lemma, and the residue analysis can tell the store stays
in the buffer. Covers MD5, SHA-1, SHA-2 and SM3 on x86-64.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
@github-actions github-actions Bot added the untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. label Nov 8, 2026
@alex
alex added this pull request to the merge queue Nov 9, 2026
Merged via the queue into main with commit 5fd4bd3 Nov 9, 2026
52 checks passed
@alex
alex deleted the claude/mdstream-clamp branch November 9, 2026 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified.

Development

Successfully merging this pull request may close these issues.

3 participants