Skip to content

AES-GCM x86-64: one-pass out-of-place decryption on the AVX-512 loop - #4390

Merged
reaperhulk merged 5 commits into
claude/gcm-dec-store-modefrom
claude/gcm-dec-store-mode-z
Nov 7, 2026
Merged

reaperhulk merged 5 commits into
claude/gcm-dec-store-modefrom
claude/gcm-dec-store-mode-z

Conversation

@reaperhulk

Copy link
Copy Markdown
Member

Stacked on pyca/verified-garbage#4388 (review only the commits after it). On x86-64, vg_aes_gcm_open_to_prepared_vaes_vpclmul_avx512 now decrypts from src to dst in one pass from 1024 bytes on, as pyca/verified-garbage#4346 did for the 256-bit variant. It uses the AVX-512 variant's own 512-bit loop, out of place.

The 256-bit out-of-place loop was slower in this variant than a copy followed by the 512-bit loop in place, which is why #4346 left the variant alone. With its own loop out of place, out-of-place decryption here now runs about as fast as out-of-place encryption.

Code. As #4388 did for the 256-bit loop, StitchZH.batch, dbody, dbody48 and bigDP take the store mode ix (default false). Each register of four blocks (64 bytes) is stored back to rdx + 64 j, or, out of place, to rdx + r8 + 64 j (StitchZH.xorDataZM, using Stitch.dstK).

StitchZH.decFromR is decR's loops out of place: the setup setupPFrom (StitchFrom.setupCFrom in place of setupC), the round keys cached, the 48-block loop, the 16-block loop, and the stores. It takes StitchFrom.decR's arguments, for a multiple of 16 blocks. The in-place functions emit the same code as before. Of the exported functions, only vg_aes_gcm_open_to_prepared_vaes_vpclmul_avx512 changes. It now calls two new internal functions: vg_aes_gcm_open_split_prepared_vaes_vpclmul_avx512 and vg_aes_gcm_decrypt_blocks_to_prepared_vaes_vpclmul_avx512.

Proofs. The AVX-512 counterparts of #4388's generic lemmas, with the in-place lemmas now their false cases:

  • Cached/Aes.lean: xorDataZM_ok and batchM_ok (batch_ok).
  • StitchZ/Aes.lean: AInvM, the invariant on four lanes, with its conversions to and from AInv.
  • StitchZ/Loop.lean: QG.keepM (QG.data), keepPM, DInvM and dfinalM_ok (dfinal_ok). setupZ_run is setupZ_ok's execution, taken out so that the out-of-place setup can use it.
  • StitchZ/Loop48.lean: QG48.keepM, AInvM.gpr2, AInvM.pow, keepWM, reloadM_ok, DInv3M and DInvM.of_eq.
  • Cached/Loop.lean, Cached/Loop48.lean, Cached/RemD.lean and Cached/Prepared.lean: dbodyM_ok, dbody48M_ok, loopD48M_ok, bigDRestM_ok, loopDRM_ok and bigDPM_ok. The instruction checks the loops need, that they keep the cached keys, are now lemmas (dbody_keeps, dbody48_keeps) instead of decide +kernel.
  • Cached/DecFrom.lean (new): setupFrom_ok and decFromR_ok : StitchFromOkM CtxMode.prepared decFromR, given the field facts PowFinZ, which Instances.lean proves, as it does PowFinD.
  • Stitch.LPre gains d_p, which the 48-block loop's power tables need.

Variants. PieceFromR is now indexed by the loop's name (StitchToName.decFromR), and VaesVpclmulAvx512 names its loop. GcmVariant.stitchFromR picks each name's loop and proof. For vg_aes_gcm_prepare16 contexts, the 512-bit loop needs more powers than the context holds, so that variant's open_to_prepared16 keeps the copy.

Trust. No Spec/ or TCB/ changes. The new functions are internal, and the exported function keeps its contract. The Rust unit test backends_agree decrypts out of place at 1024, 1300 and 3072 bytes on every backend the CPU runs. On AVX-512 runners, that reaches both loops (3072 bytes is 192 blocks, over bigMinP's 96).

Benchmarks. The scoped run Benchmarks run 42316770570 (x86-64, aes_gcm, aes-128-gcm-decrypt-out-of-place, against #4388's head) landed on a runner without AVX-512: its CPU flags lack avx512f. It measured the 256-bit code, which is unchanged, and found every size within 1.5%, so it says nothing about the AVX-512 loop. Rather than rerun it in the hope of an AVX-512 runner, I measured on an AVX-512 machine (VAES, VPCLMULQDQ, AVX-512F/BW/VL) with ci/bench_compare.py --modules aes_gcm --groups aes-128-gcm-decrypt-out-of-place, base #4388's head, in two runs:

size base head change
1024 204 ns, 256 ns 198 ns, 253 ns 2.9%, 1.3% faster
16384 1.00 µs, 1.13 µs 1.02 µs, 1.19 µs 1.7%, 5.6% slower
1048576 90.25 µs, 107.73 µs 59.72 µs, 63.57 µs 33.8%, 41.0% faster

That machine is a noisy VM: the copies of one binary differ by up to 20% at 16 KiB, and by more in the first run. So only the 1 MiB result is clear. At 1 MiB, out-of-place decryption now runs about as fast as out-of-place encryption (55 µs in the first run), instead of paying for the copy. At 16 KiB, the copy stays in cache and costs little, and I measured no gain. Below 1024 bytes both sides copy, and the differences (both ways, up to 20%) are within the noise. I kept the 1024-byte threshold shared with the 256-bit variant. A run on a quieter AVX-512 machine could set its own threshold. No benchmark slowed down by more than 35%.


Generated by Claude Code

…(WIP)

StitchZH.batch, dbody, dbody48 and bigDP take the store mode; the proofs of
the batch, the GHASH states, the loop bodies and the 48-block loop are
generic over it, the in-place lemmas their cases. StitchZH.decFromR is the
out-of-place loop (its proof follows).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvqXkQxFyWZxghqxBG
The VaesVpclmulAvx512 variant names its out-of-place loop
(StitchToName.decFromR, StitchZH.decFromR) for prepared key contexts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvqXkQxFyWZxghqxBG
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvqXkQxFyWZxghqxBG
The out-of-place loop's proof no longer imports the field algebra; the
instances prove PowFinZ, as they do PowFinD.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvqXkQxFyWZxghqxBG
@github-actions github-actions Bot added the untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. label Nov 7, 2026
@reaperhulk
reaperhulk added this pull request to stack #4395 November 7, 2026 07:54
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit bdf59a6 Nov 7, 2026
26 checks passed
@reaperhulk
reaperhulk deleted the claude/gcm-dec-store-mode-z branch November 7, 2026 08:44
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.

2 participants