Repository navigation
x86-64 AES-GCM: out-of-place interleaved loop for VAES on 256-bit registers - #1374
Merged
Merged
Conversation
…isters vg_aes_gcm_encrypt_blocks_to_vaes_vpclmul now encrypts from src to dst in the interleaved AES/GHASH loop (Impl.Gcm.X86_64.StitchTo), as the AVX-512 variant does, instead of copying the blocks to dst and encrypting them there in place. The loop is Stitch.enc with the setup of StitchZTo (r8 holds src - dst) and the plaintext loaded from [rdx + r8 + 32 j]. The proof (Proof/Gcm/X86_64/StitchTo, and stitchTo_ok in StitchZ/OkP) reuses the in-place loop's lemmas through the in-place view of the out-of-place state, as the AVX-512 one does. seal_gather's copy threshold is now chosen by the variant's out-of-place loop: 768 bytes for AVX-512, as before, and 16 KiB for this loop, below which a slice ending within a block costs the streaming functions more than the copy saves. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FoYWDDEDFq1DFev5X7n2aq
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.
This comes from the rustls provider analysis in #991 (#1228). It is the first PR replacing the dispatch of #1350/#1354 with real speedups in the assembly:
AesGcm::encryptstays a thin call intovg_aes_gcm_seal_gather, and there is no Rust change at all.What changes
vg_aes_gcm_encrypt_blocks_to_vaes_vpclmul(VAES + VPCLMULQDQ on 256-bit registers) now encrypts fromsrctodstin the interleaved AES/GHASH loop (Impl.Gcm.X86_64.StitchTo). Before, it copied the blocks todstand then encrypted them there in place. This is what the AVX-512 variant already does (StitchZTo).The loop is
Stitch.encwithStitchZTo's setup tail (setupCTo). That setsr8 ← src - dstand pointsrdxatdst. Each pair of plaintext blocks is loaded from[rdx + r8 + 32 j]and the ciphertext is stored to[rdx + 32 j], where the GHASH loads read it back as before.seal_gather's copy threshold (copyBelow) is now chosen by the variant's out-of-place loop:main.The threshold comes down once the streaming path handles a block split across slices without buffering it, which is the next PR.
No Spec or TCB change. In
src/asm/x86_64/gcm.rs, onlyvg_aes_gcm_encrypt_blocks_to_vaes_vpclmul(the new loop) andvg_aes_gcm_seal_gather_vaes_vpclmul(its threshold, and its documentation) change.Proofs
Proof/Gcm/X86_64/StitchTo/reuses the in-place loop's lemmas through the in-place view of the out-of-place state (StitchZTo.dst), asStitchZTodoes:Base.lean: the out-of-place XOR of the keystream (xorDataKTo_ok).Aes.lean: a batch of eight blocks (batchTo_ok).Loop.lean: the loop invariant, the body (the batches with the previous group's GHASH between their rounds), the first group, the loop, and the final stores.stitchTo_ok(inStitchZ/OkP.lean, which already imports the field algebra, to keepPoly's importers withincheck_lean_speed's limit): the contractStitchToOkM Mfor every kind of key context.toPart) are checked bytaint_decideanddecide +kernel, as for AVX-512.Measurements
These are
AesGcm::encryptof two pieces (n bytes, then 1 byte), with a 13-byte AAD and a warmed key, usingVG_CPU_FEATURES=aes,avx,avx2,pclmulqdq,ssse3,vaes,vpclmulqdqon an Ice Lake–class Xeon. Each value is the minimum of 33 interleaved samples, in ns. "copy+ip" copies the pieces and callsencrypt_in_place.Below 16 KiB the code path is the same as
main's (within noise). From 16 KiB, encrypting from the pieces is 5–10% faster thanmainat 16 KiB and 21–24% faster at 64–128 KiB, also beating copy+ip. The other backends' code is byte-identical.Validation
lake build(full) andlake env lean --run Emit.lean(axiom and compiler audits; the generated code is committed).cargo testwith Wycheproof on the default backend, and the AES-GCM tests withVG_CPU_FEATURESset to the VAES-AVX2 configuration and tonone.prepared_gather_tailsencrypts 16 KiB plus a suffix from pieces on every backend, which reaches the new loop.cargo fmt --check,cargo clippy --all-targets -- -D warnings.ci/check_lean_imports.py,check_lean_speed.py,check_vectors.py,check_arch_gates.py,check_variants.py,check_mcdt.py,cpu_tests.py lint,algorithms_table.py --check.🤖 Generated with Claude Code
https://claude.ai/code/session_01FoYWDDEDFq1DFev5X7n2aq
Generated by Claude Code