Skip to content

x86-64 residue analysis: classify each instruction once, linear order and meet - #4962

Merged
alex merged 1 commit into
mainfrom
claude/residue-faster-analysis
Nov 11, 2026
Merged

alex merged 1 commit into
mainfrom
claude/residue-faster-analysis

Conversation

@reaperhulk

Copy link
Copy Markdown
Member

This makes the kernel's evaluation of the x86-64 residue analysis (rtaint) about twice as fast. No analysis result changes: each new function is proven equal to the one it replaces. The emitter writes no change under crates/.

It is framework only. It touches three files in Proof/Framework/X86_64/: ResidueTaint.lean, ResidueTaintSound.lean and ResidueScrub.lean. Nothing under TCB/ or Spec/ changes, nor X86_64/Taint.lean or the generic Taint.lean, so no constant-time proof is rebuilt. No user of the analysis is edited.

Why

Profiling the residue checks (callgrind, diagnostics, prototypes) found that about 95% of their cost is the kernel. Three things dominate it:

  • Repeated matching on the instruction: each RTaint.step unfolds about ten wide match i on Instr (writesSp, special, post, ubNew, the flags, storeW/writesMem in mbAfter, clobbers). On the same code, the base analysis alone takes 1.1 s of kernel time where rtaint takes 6.0 s.
  • A quadratic order: le is checked at every hint.
  • A quadratic meet: Taint.meet runs at every ite.

What

  • One classification per instruction: RTaint.stepFn : Instr → RStep is shaped like the base analysis's stepKDFn. Its one match on the instruction holds no taint, so the kernel evaluates it once per instruction. It has two fast paths:

    • fastFn, for instructions isPlain accepts: the base step, with the written register's bounds dropped and the flags cleared.
    • fastStore, for stores without an index register.

    Every other instruction falls back to step. stepFn_eq proves (stepFn i).run τ = step τ i.

  • Linear order: leF compares the slots as an ordered sublist, falling back to the quadratic check, and compares numbers with Nat.beq. leF_eq proves leF = le.

  • Cheaper meet: meetF keeps the slots as they are when both sides are equal, and otherwise filters with KList. meetF_eq proves meetF = meet.

  • Cheaper end check: RScrub.Analysed.ok_of now checks only the bytes sec lists (resOkF) and still concludes resOkB, through resOkB_of_resOkF. The two checks accept exactly the same taints, and the 216 users of ok_of in 90 files build unchanged.

  • Compiled code: like VG.KList, the three helpers written with List.rec (natsEq, subl, eqs3) carry @[implemented_by] for compiled code, each next to its equality lemma.

rtaint now uses stepF, meetF and leF. Its soundness fields reuse the existing proofs through these equalities, so step_sound is unchanged.

Cost

Measured locally with lean -j2 and the profiler, run one at a time on the same machine:

Module Before After
Camellia.X86_64.Scrub 199.8 s / 4.17 GB 92.7 s / 1.82 GB
Sm4.X86_64.Scrub 180.8 s / 2.49 GB 104.7 s / 1.48 GB
Variants.MdHash.X86_64.Sha512 163.7 s / 2.33 GB 78.0 s / 1.27 GB
Variants.MdHash.X86_64.Sha512ShaNi 93.7 s / 1.73 GB 33.2 s / 0.92 GB
Blowfish.X86_64.Scrub 29.4 s / 2.26 GB 14.6 s / 1.31 GB
Variants.MdHash.X86_64.Sha1 23.6 s / 1.57 GB 17.0 s / 1.08 GB
Ecdh.X86_64.P192.Verified (CPU) 16.1 s / 2.35 GB 13.2 s / 1.85 GB
Md5.X86_64.Scrub 3.8 s 2.9 s
  • All residue declarations: 648 matched between the two profiles. Their kernel time went from 1781 s to 1137 s, about 640 s or 36% less. This is a lower bound, because the new profile comes from a full parallel build.
  • The framework modules: they take about 4 s more, mostly ResidueTaintSound (14.8 → 18.3 s).

No benchmark run: the generated code is unchanged.

Checks

All passed locally:

  • full lake build;
  • lake env lean --run Emit.lean (no change under crates/), then --check;
  • every step of the Static checks job;
  • check_lean_public_imports.py --base origin/main on the three modules;
  • cargo fmt --check, cargo clippy --all-targets -- -D warnings;
  • cargo test, and the VG_CPU_FEATURES=none run.

Generated by Claude Code

The kernel spends nearly all of a residue check (`RTaint.rtaint`) on
evaluating the analysis: every `RTaint.step` unfolded about ten wide
matches on the instruction, `le` at hint taints compared slots
quadratically, and so did `meet` at every `ite`. `rtaint` now evaluates
functions proven equal to these (so every analysis, and every
`keep_for%`, is the same):

* `stepF`/`stepFn` classify the instruction before the taint is given, as
  the base analysis's `stepKDFn` does, so the kernel unfolds the matches
  on an instruction once; most instructions (`isPlain`) take the base
  step with the bounds of the register they write dropped, and stores
  without an index register a step of their own (`stepFn_eq`);
* `leF` compares slots in one pass when one list is a sublist of the
  other in order, and numbers with `Nat.beq` (`leF_eq`);
* `meetF` keeps equal slots as they are (`meetF_eq`, `meetT_eq`).

`RScrub.Analysed.ok_of` now takes the end check `resOkF`, which checks
only the window bytes `sec` lists (`resOkB_of_resOkF`), so its users
gain without edits. `step` names its flags `flOf`; nothing else in
`step`, `le`, `meet` or their soundness proofs changes. No TCB, Spec or
generated code changes.

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 11, 2026
@alex
alex enabled auto-merge November 11, 2026 15:49
@alex
alex added this pull request to the merge queue Nov 11, 2026
Merged via the queue into main with commit dc998e3 Nov 11, 2026
23 checks passed
@alex
alex deleted the claude/residue-faster-analysis branch November 11, 2026 16:08
reaperhulk pushed a commit that referenced this pull request Nov 11, 2026
#4962 moved resOkF, resOkB_of_resOkF and any_resOkB to ResidueScrub.lean;
drop the copies here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
reaperhulk pushed a commit that referenced this pull request Nov 11, 2026
… functions scrubbed

The residue analysis (`RTaint`) now tracks the argument area above the
return address: its length (`an`), its bytes that are the same in both
runs (`sa`, from the public stack words) and the words that hold the
address of a writable buffer passed on the stack (`sp`). A load
`mov`/`mov32 d, [rsp + disp]` from the area gives a public register or a
pointer into the buffer (`argPost`), so functions with more than six
arguments can store through their stack-passed buffers. `ScrubAbi` carries
the argument area and its no-wrap facts; `RScrub.init` sets `an`, `sa` and
`sp`. Soundness is proven in `ResidueTaintSound.lean`.

The kernel's fast paths (#4962) carry the new fields: `isPlain` excludes a
`mov32` load based on `rsp` without an index (which `argPost` may
change), `fastFn`/`fastStore` keep `an`/`sa`/`sp`, and `leF`/`meetF`
compare and intersect them; `stepFn_eq`, `leF_eq` and `meetF_eq` still
hold.

The one-pass XTS data-unit functions
`vg_aes_xts_{en,de}crypt_unit{,_to}_{vaes_vpclmul_avx512,vaes_vpclmul,aesni_pclmul}`
now prove `NoResidue` with no stack wipe; their allowlist lines are
removed. No TCB or Spec changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
reaperhulk pushed a commit that referenced this pull request Nov 11, 2026
… functions scrubbed

The residue analysis (`RTaint`) now tracks the argument area above the
return address: its length (`an`), its bytes that are the same in both
runs (`sa`, from the public stack words) and the words that hold the
address of a writable buffer passed on the stack (`sp`). A load
`mov`/`mov32 d, [rsp + disp]` from the area gives a public register or a
pointer into the buffer (`argPost`), so functions with more than six
arguments can store through their stack-passed buffers. `ScrubAbi` carries
the argument area and its no-wrap facts; `RScrub.init` sets `an`, `sa` and
`sp`. Soundness is proven in `ResidueTaintSound.lean`.

The kernel's fast paths (#4962) carry the new fields: `isPlain` excludes a
`mov32` load based on `rsp` without an index (which `argPost` may
change), `fastFn`/`fastStore` keep `an`/`sa`/`sp`, and `leF`/`meetF`
compare and intersect them; `stepFn_eq`, `leF_eq` and `meetF_eq` still
hold.

The one-pass XTS data-unit functions
`vg_aes_xts_{en,de}crypt_unit{,_to}_{vaes_vpclmul_avx512,vaes_vpclmul,aesni_pclmul}`
now prove `NoResidue` with no stack wipe; their allowlist lines are
removed. No TCB or Spec changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
reaperhulk pushed a commit that referenced this pull request Nov 12, 2026
… functions scrubbed

The residue analysis (`RTaint`) now tracks the argument area above the
return address: its length (`an`), its bytes that are the same in both
runs (`sa`, from the public stack words) and the words that hold the
address of a writable buffer passed on the stack (`sp`). A load
`mov`/`mov32 d, [rsp + disp]` from the area gives a public register or a
pointer into the buffer (`argPost`), so functions with more than six
arguments can store through their stack-passed buffers. `ScrubAbi` carries
the argument area and its no-wrap facts; `RScrub.init` sets `an`, `sa` and
`sp`. Soundness is proven in `ResidueTaintSound.lean`.

The kernel's fast paths (#4962) carry the new fields: `isPlain` excludes a
`mov32` load based on `rsp` without an index (which `argPost` may
change), `fastFn`/`fastStore` keep `an`/`sa`/`sp`, and `leF`/`meetF`
compare and intersect them; `stepFn_eq`, `leF_eq` and `meetF_eq` still
hold.

The one-pass XTS data-unit functions
`vg_aes_xts_{en,de}crypt_unit{,_to}_{vaes_vpclmul_avx512,vaes_vpclmul,aesni_pclmul}`
now prove `NoResidue` with no stack wipe; their allowlist lines are
removed. No TCB or Spec changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
reaperhulk pushed a commit that referenced this pull request Nov 12, 2026
#4962 moved resOkF, resOkB_of_resOkF and any_resOkB to ResidueScrub.lean;
drop the copies here.

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

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