Skip to content

GCM x86-64: baseline vg_aes_gcm_prepare by calls of vg_ghash - #4538

Merged
alex merged 3 commits into
mainfrom
claude/gcm-x86-64-prepare-baseline
Nov 7, 2026
Merged

alex merged 3 commits into
mainfrom
claude/gcm-x86-64-prepare-baseline

Conversation

@alex

@alex alex commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

This is step (2a) of the GCM single-family plan: an x86-64 vg_aes_gcm_prepare that needs no CPU feature. It writes all 48 prepared powers (PreparedPowersRepr). With it, vg_aes_gcm_prepare_auto has a baseline to fall back to.

Code

Impl/Gcm/X86_64/PrepareCall.lean:

  • It calls the verified vg_ghash 47 times, each on one zero block: Y := Y • H.
  • Each power Hⁿ is first stored in its own slot of the context, as vg_ghash's big-endian accumulator. Step n copies it to the next power's slot, then turns it in place into preparedPower(Hⁿ), as a little-endian 128-bit word, with the baseline vg_ghash's own hInv. It then calls vg_ghash on the next slot.
  • The next slot is 16 bytes before an odd power's slot and 48 bytes after an even power's. So the code is 23 identical pairs of steps, then step 47 and the last conversion.
  • rsi holds the current slot and r8 holds the 312-byte scratch buffer. Neither vg_ghash nor vg_ghash_pclmul ever writes these two registers.
  • The scratch buffer holds vg_ghash's working space, a copy of H, the zero block and the saved r12–r14. It sits in a stack frame that is zeroed on return (withStackScratchWiped, 328 bytes of stack).

Proof

Proof/Gcm/X86_64/PrepareCall.lean proves it against Spec.Gcm.prepareContract.

  • St n is what holds before step n: Hⁿ is in its slot at rsi, the prepared powers below n are in theirs, and the scratch buffer holds its contents.
  • setup_ok establishes St 1.
  • step_ok takes St n to St (n+1), using:
    • copy_ok;
    • pp_ok, built on hInv_ok;
    • add_ok;
    • mulH_ok/call_ok, which use WP.call with the vg_ghash contract.
  • pairs_ok composes the 23 pairs by induction. final_ok handles H⁴⁸ and restores the registers.
  • Constant time is one taint_decide (about 0.2 s), with ctx, scratch and rsp public.
    • The callee never writes rsi or r8, so the slot and buffer addresses stay public through every call.
    • vg_ghash restores rbx, rbp and r12–r15 from memory, so a taint analysis can't keep those public after a call.
  • Verified.stackScratchWiped adds the stack frame. postOut_local shows that preparePost reads only the context.

Registration

The code is registered generic over vg_ghash's implementations in Generic/Ghash/X86_64/Prepare.lean, as CLAUDE.md requires of a caller of a function with variants.

Dispatch order

Dispatch.lean now ranks the bare _pclmul_avx suffix right after _vpclmul.

  • Before, it had no entry for _pclmul_avx and read it as its parts, which put it first in vg_aes_gcm_prepare_auto.
  • Every CPU with VPCLMULQDQ and AVX2 also has AVX and PCLMULQDQ, so _vpclmul was never chosen.
  • The order is now _vpclmul_avx512, _vpclmul, _pclmul_avx, then the baseline. No other dispatcher changes.

Scope

  • No TCB or Spec change, and no Rust change outside the generated crate.
  • No benchmark, because nothing in the library calls vg_aes_gcm_prepare_auto yet.

Checks

On the merge of main (daf7be3):

  • Full Emit.lean, whose audits pass (standard axioms only), with no generated file left to update.
  • Every Static checks step, with check_lean_single_importer.py --base origin/main.
  • check_lean_public_imports.py on the three new modules.
  • cargo fmt --check and cargo clippy --all-targets -D warnings on x86-64.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHFuGsz7F32Tm9DLkuP

The baseline vg_aes_gcm_prepare, writing the 48 prepared powers of a key
context, for the implementations of vg_ghash with no prepare of their own
(today the baseline vg_ghash). It calls vg_ghash 47 times on one zero
block: each power is first stored in its slot as vg_ghash's accumulator,
then turned in place into its prepared power with vg_ghash's own hInv.
The slot pointer (rsi) and the scratch buffer (r8, 312 bytes on the stack,
zeroed on return) are registers vg_ghash never writes, so they stay
public through the calls and constant time is one taint_decide.

Registered generic over vg_ghash's implementations
(Generic/Ghash/X86_64/Prepare.lean); vg_ghash_pclmul and
vg_ghash_vpclmul have none, as vg_aes_gcm_prepare_pclmul and
vg_aes_gcm_prepare_vpclmul compute the powers themselves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFuGsz7F32Tm9DLkuP
Dispatch.lean ranked `_vpclmul` but had no entry for a bare `_pclmul_avx`,
which it read as its parts and put first. Every CPU with VPCLMULQDQ and
AVX2 also has AVX and PCLMULQDQ, so vg_aes_gcm_prepare_auto never chose
the 256-bit `_vpclmul` code. The order is now `_vpclmul_avx512`,
`_vpclmul`, `_pclmul_avx`, then the baseline; no other dispatcher changes.

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

alex commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

Static checks fails only on ci/check_variants.py. It reports that vg_aes_gcm_prepare_pclmul_avx and vg_aes_gcm_prepare_vpclmul are "neither called by another generated function nor used by the Rust code".

This is expected for this PR. With a baseline vg_aes_gcm_prepare, those two count as variants, and nothing in Rust calls vg_aes_gcm_prepare_auto until step (3) of the GCM plan. That is why the PR is a draft: it lands with or after (3).

Pushed ba6ec1cb, which ranks the bare _pclmul_avx suffix after _vpclmul in Dispatch.lean. Before, vg_aes_gcm_prepare_auto tried _pclmul_avx first, so it never chose _vpclmul. The order is now _vpclmul_avx512, _vpclmul, _pclmul_avx, then the baseline, and no other dispatcher changes.


Generated by Claude Code

@alex
alex marked this pull request as ready for review November 7, 2026 22:21
@alex
alex added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit f1f27f3 Nov 7, 2026
33 checks passed
@alex
alex deleted the claude/gcm-x86-64-prepare-baseline branch November 7, 2026 22:34
alex pushed a commit that referenced this pull request Nov 7, 2026
With #4538's baseline vg_aes_gcm_prepare on main, the dispatcher also
tries vg_aes_gcm_prepare_pclmul, after _pclmul_avx and before the
baseline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHDjuK1puxAcLZKKff
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