Repository navigation
AES-GCM x86-64: one-pass out-of-place decryption for long inputs (open_split) - #4346
Merged
Merged
Conversation
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
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
marked this pull request as ready for review
November 6, 2026 21:06
github-merge-queue
Bot
removed this pull request from the merge queue due to a conflict with the base branch
Nov 6, 2026
|
This pull request has conflicts with Posted by |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follows pyca/verified-garbage#4307 (merged), which added
vg_aes_gcm_open_toby a copy. On x86-64,vg_aes_gcm_open_to_prepared16_vaes_vpclmulnow decrypts fromsrctodstin one pass from 1024 bytes on, rather than copying the ciphertext todstand decrypting it there. Below 1024 bytes it still copies, and the otheropen_toinstances are unchanged. The PR also adds 1 MiB to the one-shot AES-GCM benchmarks (LARGE_SIZESinbench/benches/primitives/aes_gcm.rs).This is the counterpart of
seal_split(decrypt instead of encrypt). Likeseal_split, it is built from three verified layers.1. An out-of-place decryption loop on 256-bit registers.
Impl/Gcm/X86_64/StitchFrom.leanholdsdecR, the VAES/VPCLMULQDQ loop. It decrypts 16 blocks at a time fromsrctodstand absorbs the ciphertext into GHASH fromsrc, which it never writes. Its proof is againstStitchFromOkM, the decryption counterpart ofStitchToOkM(Proof/Gcm/X86_64/StitchFrom/Loop.lean). The field fact it needs,PowFinD, is proved inInstances.lean, as for the other prepared loops.2.
vg_aes_gcm_decrypt_blocks_to_prepared16_vaes_vpclmul. This is the decryption counterpart ofencrypt_blocks_to(Impl/AesGcm/X86_64/BlocksFrom.lean,Proof/AesGcm/X86_64/BlocksFrom/):decRon the first16 ⌊n / 16⌋blocks.vg_aes_gcm_decrypt_blocksin place.3.
vg_aes_gcm_open_split_prepared16_vaes_vpclmul. This isopen, withseal_split'sprefixcallingdecrypt_blocks_toin place ofoneBlocks(Impl/AesGcm/X86_64/OpenSplit.lean,Proof/AesGcm/X86_64/OpenSplit/). It is an internal function with its contract inProof/. On a wrong tag it returns 0 and leavesdstfor its caller to zero.The caller:
open_tofor prepared contexts.Impl/AesGcm/X86_64/OpenToS.lean(proofs inProof/AesGcm/X86_64/OpenToS/) has two parts:openToSkeepsopen_to's frame and its wipe ofdston a wrong tag. Instead of the copy, it storesopen_split's seven stack arguments and calls it.withStackArgScratchgives it its 2560-byte working space, whichVerified.stackArgScratchcovers. The contracts with the working space appended are inProof/AesGcm/ScratchOpenTo.lean.openToLcompareslenwith the threshold on entry and branches to the copy path or the one-pass path, usingVerified.entryBranch:raxand the flags, which no contract reads.len, and the branch depends only onlen, which is public.Variants with the loop register 2776 bytes of stack, up from 2728. The others keep
open_toby a copy, at 2728 bytes (Instances.openToCodeR(16)).Variants. Only the
VaesVpclmulvariant names the loop (StitchToPart.pieceFromR), so of the exported functions onlyvg_aes_gcm_open_to_prepared16_vaes_vpclmulchanges, and the two internal functions above are new. I tried it inVaesVpclmulAvx512as 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/orTCB/changes: the new functions are internal and their contracts are inProof/. 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 testbackends_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_gcmonly, againstbench/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 areaes-128-gcm-decrypt-out-of-place/<size>.With
VG_CPU_FEATURES=aes,avx,avx2,pclmulqdq,ssse3,vaes,vpclmulqdq(the_prepared16VAES instance this PR changes):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