Skip to content

AES-GCM x86-64: one-pass out-of-place decryption for long inputs (open_split) - #4346

Merged
reaperhulk merged 15 commits into
mainfrom
claude/gcm-open-to-onepass
Nov 6, 2026
Merged

reaperhulk merged 15 commits into
mainfrom
claude/gcm-open-to-onepass

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Follows pyca/verified-garbage#4307 (merged), which added vg_aes_gcm_open_to by a copy. On x86-64, vg_aes_gcm_open_to_prepared16_vaes_vpclmul now decrypts from src to dst in one pass from 1024 bytes on, rather than copying the ciphertext to dst and decrypting it there. Below 1024 bytes it still copies, and the other open_to instances are unchanged. The PR also adds 1 MiB to the one-shot AES-GCM benchmarks (LARGE_SIZES in bench/benches/primitives/aes_gcm.rs).

This is the counterpart of seal_split (decrypt instead of encrypt). Like seal_split, it is built from three verified layers.

1. An out-of-place decryption loop on 256-bit registers. Impl/Gcm/X86_64/StitchFrom.lean holds decR, the VAES/VPCLMULQDQ loop. It decrypts 16 blocks at a time from src to dst and absorbs the ciphertext into GHASH from src, which it never writes. Its proof is against StitchFromOkM, the decryption counterpart of StitchToOkM (Proof/Gcm/X86_64/StitchFrom/Loop.lean). The field fact it needs, PowFinD, is proved in Instances.lean, as for the other prepared loops.

2. vg_aes_gcm_decrypt_blocks_to_prepared16_vaes_vpclmul. This is the decryption counterpart of encrypt_blocks_to (Impl/AesGcm/X86_64/BlocksFrom.lean, Proof/AesGcm/X86_64/BlocksFrom/):

  • It runs decR on the first 16 ⌊n / 16⌋ blocks.
  • For the remaining blocks, it copies them and calls vg_aes_gcm_decrypt_blocks in place.
  • Without the loop, it would copy all the blocks and decrypt them in place. No emitted variant takes that path today: only variants with the loop call it.

3. vg_aes_gcm_open_split_prepared16_vaes_vpclmul. This is open, with seal_split's prefix calling decrypt_blocks_to in place of oneBlocks (Impl/AesGcm/X86_64/OpenSplit.lean, Proof/AesGcm/X86_64/OpenSplit/). It is an internal function with its contract in Proof/. On a wrong tag it returns 0 and leaves dst for its caller to zero.

The caller: open_to for prepared contexts. Impl/AesGcm/X86_64/OpenToS.lean (proofs in Proof/AesGcm/X86_64/OpenToS/) has two parts:

  • openToS keeps open_to's frame and its wipe of dst on a wrong tag. Instead of the copy, it stores open_split's seven stack arguments and calls it.
  • withStackArgScratch gives it its 2560-byte working space, which Verified.stackArgScratch covers. The contracts with the working space appended are in Proof/AesGcm/ScratchOpenTo.lean.

openToL compares len with the threshold on entry and branches to the copy path or the one-pass path, using Verified.entryBranch:

  • The comparison writes only rax and the flags, which no contract reads.
  • It leaks only the address of len, and the branch depends only on len, which is public.

Variants with the loop register 2776 bytes of stack, up from 2728. The others keep open_to by a copy, at 2728 bytes (Instances.openToCodeR(16)).

Variants. Only the VaesVpclmul variant names the loop (StitchToPart.pieceFromR), so of the exported functions only vg_aes_gcm_open_to_prepared16_vaes_vpclmul changes, and the two internal functions above are new. I tried it in VaesVpclmulAvx512 as well. There it was slower than the copy plus that variant's 512-bit in-place loop: at 16 KiB, 1.95 µs against 1.49 µs. So that variant is left as it was.

Trust. No Spec/ or TCB/ changes: the new functions are internal and their contracts are in Proof/. The exported functions keep their names and contracts, so nothing changes on the Rust side. Wycheproof's vectors (up to 513 bytes) reach the copy path. The one-pass path is reached by the unit test backends_agree, which decrypts out of place at 1024, 1300 and 3072 bytes on every backend the CPU runs and checks that a wrong tag zeroes the output.

Benchmarks. Benchmarks run 42271893897 covers x86-64, aes_gcm only, against bench/gcm-open-to-base, which was pyca/verified-garbage#4307 with this PR's benchmark change (1 MiB one-shot sizes, LARGE_SIZES), so 1 MiB has a baseline. The numbers are aes-128-gcm-decrypt-out-of-place/<size>.

With VG_CPU_FEATURES=aes,avx,avx2,pclmulqdq,ssse3,vaes,vpclmulqdq (the _prepared16 VAES instance this PR changes):

size base head change
384 220 ns 223 ns 1.3% slower
1024 305 ns 302 ns 0.9% faster
16384 2.56 µs 2.23 µs 13.0% faster
1048576 179.69 µs 125.69 µs 30.1% faster

At 1 MiB, out-of-place decryption becomes as fast as out-of-place encryption (132 µs), instead of as slow as in-place decryption with its copy (181 µs). 384 bytes is below the threshold, so it takes the copy path in both; the 1.3% there is the cost of the length comparison, or noise. In the default configuration (AVX-512, whose code this PR does not change), decryption in place and out of place, and encryption out of place, are within 0.2% of the base at 384 B, 1 KiB, 16 KiB and 1 MiB. The run reports no benchmark slowed down by more than 35%.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWvqXkQxFyWZxghqxBG

claude added 14 commits November 6, 2026 11:10
Implements the contracts of Spec/Gcm/OutOfPlace.lean, Prepared.lean and
Prepared16.lean (#4292) on x86-64: vg_aes_gcm_open_to and its _prepared
and _prepared16 instances copy the ciphertext from src to dst (32 bytes at
a time with AVX's vmovdqu where the variant has AVX, else 16 with SSE2's
movdqu), decrypt it there in place by a call of the variant's
vg_aes_gcm_open, and zero dst if the tag is wrong. Every variant has one,
generic over the open it calls.

AesGcm::decrypt and decrypt_truncated decrypt out of place, by
vg_aes_gcm_open_to on x86-64, and elsewhere by copying the ciphertext and
decrypting in place (zeroing the output on a wrong tag as well). Tests:
Wycheproof at several offsets, CAVP with truncated tags, every backend in
the unit tests; a benchmark group next to OpenSSL's and aws-lc-rs's.

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

StitchFrom.decR decrypts whole groups of 16 blocks from src to dst in one
pass, hashing the ciphertext as it reads it: rdx points at the ciphertext,
which the GHASH loads and the XOR both read and nothing writes, and each
pair of plaintext blocks is stored at rdx + r8 (r8 = dst - src).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvqXkQxFyWZxghqxBG
The code of vg_aes_gcm_encrypt_blocks_to, with an out-of-place decryption
loop for the whole groups of 16 blocks, if the variant has one, and the
blocks after them copied and decrypted in place by vg_aes_gcm_decrypt_blocks;
GHASH absorbs the ciphertext at src. Its contract is internal (only verified
code calls it). Not yet called.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvqXkQxFyWZxghqxBG
Its code, and the proof of its whole blocks: seal_split's prefix with a call
of vg_aes_gcm_decrypt_blocks_to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvqXkQxFyWZxghqxBG
open of the ciphertext at src, with the plaintext written to dst: open's
entry, AAD and tag comparison, with seal_split's prefix (calling
vg_aes_gcm_decrypt_blocks_to) in place of oneBlocks, and no undo of the
whole blocks on a wrong tag (the caller zeroes dst).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvqXkQxFyWZxghqxBG
…n constant time

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
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvqXkQxFyWZxghqxBG
The VaesVpclmul variant names the out-of-place decryption loop
(StitchToPart.pieceFromR), so vg_aes_gcm_open_to_prepared16_vaes_vpclmul
calls vg_aes_gcm_open_split from 1024 bytes on (OpenToS.thr), in a frame
holding its working space; the other variants keep open_to by a copy.
Regenerate the crate.

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
Resolve the import list of Proof/AesGcm/X86_64/Instances.lean (both
sides' imports), and restore preparedPowersRepr(16)_zeroMem and
preparedPowersRepr(16)_of_zeroMem, which #4345 removed as unused and
open_to's satisfiability proofs use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvqXkQxFyWZxghqxBG
Resolve the import list of Proof/AesGcm/X86_64/Instances.lean, and state
saved_crFrame, which #4345 removed from Proof/AesGcm/X86_64/Fn.lean as
unused, in OpenSplit/Run.lean, its only user.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvqXkQxFyWZxghqxBG
@reaperhulk
reaperhulk marked this pull request as ready for review November 6, 2026 21:06
@reaperhulk
reaperhulk added this pull request to stack #4371 November 6, 2026 21:08
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 6, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Nov 6, 2026
Base automatically changed from claude/happy-goodall-uzpm6i to main November 6, 2026 22:02
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

This pull request has conflicts with main, found after b58e27c landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

@reaperhulk
reaperhulk added this pull request to the merge queue Nov 6, 2026
Merged via the queue into main with commit 7adfaed Nov 6, 2026
27 checks passed
@reaperhulk
reaperhulk deleted the claude/gcm-open-to-onepass branch November 6, 2026 23:29
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