Skip to content

x86-64: the residue analysis and scrub, an allowlist that only shrinks, and SHA-256, Poly1305 and ChaCha20 apply without residue - #4836

Merged
reaperhulk merged 5 commits into
mainfrom
claude/residue-framework
Nov 9, 2026
Merged

reaperhulk merged 5 commits into
mainfrom
claude/residue-framework

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Follows #4741, which added the property (NoResidue) and the allowlist to the TCB. This PR proves the property for the first x86-64 functions and adds the framework that proves it. No trust change: nothing under TCB/ or Spec/ changes.

The framework

  • The residue analysis (Proof/Framework/X86_64/ResidueTaint.lean, proven sound in ResidueTaintSound.lean). It extends the x86-64 taint analysis to the stack below the caller's stack pointer. It follows frames, calls and returns, and tracks:

    • bounded indices (clamped by cmp; cmovb, or masked);
    • pointers kept in public stack slots;
    • zeroing idioms;
    • buffer bytes the contract makes public.

    At the end it lists the registers and stack granules that may hold secrets.

  • The scrub (Impl/Scrub/X86_64.lean, Proof/Framework/X86_64/Scrub.lean, ResidueScrub.lean, ResidueKeep.lean). scrubReg follows a registration's code with a tail that:

    • clears the vector registers the code may write;
    • clears the general-purpose registers it may write, other than the result;
    • wipes the granules the analysis listed.

    It proves NoResidue and the same contract, and sets Registration.residue.

  • withScrub takes the analysis as a kernel-checked certificate (Analysed, by taint_decide), so nothing runs at emit time. Given none, it leaves the registration as it was.

The ratchet

ci/check_residue_allowlist.py, a new step of the Static checks job, rejects any line of ci/residue_allowlist.txt that the base branch does not have. The emitter already rejects a listed function that has a proof or no longer exists. So the list only shrinks, and converting a function removes its line. CLAUDE.md and the adding-a-primitive skill now say this.

Effect on other PRs: from now on, a PR that adds an x86-64 function proves the property instead of adding a line. That includes:

  • a new function;
  • a new variant, since variants are emitted for their generic callers under new names;
  • a rename.

First conversions (17 lines off the list, 881 → 864)

Each registration file only binds the scrubbed registration, built in Proof/. scrubReg copies every other field, extra and vendor included, so what dispatch chooses does not change.

Checks

  • lake build (all modules), Emit.lean --check, the Static checks job, check_lean_public_imports.py on the 19 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: the scrub adds instructions at the end of each converted function, a fixed cost per call. Benchmarks run 42645591964: x86-64, sha256 poly1305 chacha20 against TCB: x86-64 functions return without secret residue, but those an allowlist names #4741's merge, on an AMD EPYC 7763, one interleaved run. Nothing slowed by more than 5 ns:
    • SHA-256:
      • With none (scalar), SHA-NI, AVX-512 and AVX-512VL: within ±1.6% at every size from 32 B to 16 KiB.
      • With avx,avx2,bmi1,bmi2 (the AVX2 implementation): 3.4% slower at 32 B (140 → 145 ns), falling to 0.9% at 256 B.
    • Poly1305: the scalar update (VG_CPU_FEATURES=none) is 7.1% slower at 64 B (56 → 60 ns), 3.3% at 480 B, 2.1% at 1 KiB, and unchanged from 4 KiB. Configurations that choose a vector update (not converted) are within ±3.2%.
    • ChaCha20: at most 2.4% slower in every configuration and at every size. The larger moves are speedups at 5 B (up to 11.4% with none), which is noise.
    • The AVX-512 IFMA configuration was not measured: the runner lacks AVX-512 IFMA. It runs the same SHA-256 and ChaCha20 code as AVX-512VL. Poly1305 there uses the vector update, which is not converted.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa

RTaint extends the x86-64 taint analysis to the stack a function uses: it
follows frames, calls and returns, bounded indices (clamped, or masked),
pointers kept in public slots, zeroing idioms and buffer bytes the contract
makes public, and finds the registers and stack granules that may hold
secrets on return; it is proven sound (ResidueTaintSound). scrubReg follows
a registration's code with a tail (Impl.Scrub.X86_64.scrubTail) that clears
the vector registers and general-purpose registers the code may write and
wipes those granules, and proves NoResidue and the same contract.
withScrub does so given the analysis (Analysed, checked by the kernel with
taint_decide), and leaves the registration as it is without one. CLAUDE.md
and the adding-a-primitive skill say how to convert a function.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
ci/check_residue_allowlist.py, a step of the Static checks job, refuses a
line of ci/residue_allowlist.txt that the base branch does not have: a new
x86-64 function proves that it returns without secret residue rather than
joining the list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
…idue

Each scrubbed (StreamFn.scrub) for the scalar, AVX2 and SHA-NI compression
functions; their lines leave ci/residue_allowlist.txt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
Poly1305's init, finalize and update over the scalar vg_poly1305_blocks,
and ChaCha20's apply over each implementation of vg_chacha20_xor (scalar,
SSSE3, AVX2, AVX-512, AVX-512VL), scrubbed with withScrub. A generic
caller's instance takes its analysis from the implementation
(BlocksImpl.update, XorImpl.apply); the vector Poly1305 updates have none:
the analysis does not follow ldmxcsr. apply's analysis starts with the
state's count of keystream bytes left public, which its contract leaks
(pubOk). ChaCha20-Poly1305 calls Poly1305's init code as
vg_poly1305_init_core, since the exported vg_poly1305_init now clears what
it leaves. Their lines leave ci/residue_allowlist.txt.

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
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