Repository navigation
x86-64 AES-GCM gather: copy a slice's last 16 bytes at once - #1474
Merged
Merged
Conversation
…(WIP: asm not yet regenerated) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FoYWDDEDFq1DFev5X7n2aq
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.
What
vg_aes_gcm_seal_gather(AesGcm::encrypt) copies each slice to the output and seals it there when the whole text is short (under 768 bytes on AVX-512). This copy also runs in the_preparedinstance. The copy moved 64 bytes at a time, then 16, then up to 15 more one byte at a time. That byte loop cost about 1.5 ns per byte, so a 15-byte tail cost 20–25 ns, much of the gap betweenencryptand copy-then-encrypt_in_place.Now, when a slice of at least 16 bytes has a remainder after the 16-byte loop, a single 16-byte move copies its last 16 bytes (
copyLast). The bytes before the remainder are copied a second time with the same values. Slices shorter than 16 bytes still go one byte at a time.Part of #1228, for the small, few-slice encryptions from the rustls analysis in #991.
Measurements
AesGcm::encryptwith a reused key, 13-byte AAD and 12-byte nonce, on an AVX-512 Xeon (VAES, VPCLMULQDQ). Each cell is the better of two best-of-11 runs, in ns, base → this PR.[2]splits the text atn/2 + 3, so both slices have a tail.encrypt_in_placeis shown for reference.encrypt_in_placeencrypt[n]encrypt[2]Two slices of 100–128 bytes are about 20–25% faster, and a 255-byte text about 16%. Block-aligned single slices (64, 128) are unchanged.
Proofs
Proof/AesGcm/X86_64/Gather/Copy.lean:copyLast_okproves that, afterj ≥ n - 16bytes, one more 16-byte write atn - 16leaves exactly thenbytes copied. It uses a new lemma,writeBytes_over: writingysoverxsfrom the same address, withysat least as long, is just writingys.setupLast_okcovers then < 16test, andCInv.readW'reads the source at any offset. The constant-time proof is unchanged in shape:copy_checkis re-checked bytaint_decide, and the new branch depends only on the slice length. Spec, TCB and contracts are unchanged.Checks
lake build, then the emitter. Onlysrc/asm/x86_64/gcm.rschanges.ci/check_lean_speed.py,check_lean_imports.py,check_vectors.py,check_arch_gates.py,check_variants.py,check_mcdt.py,cpu_tests.py lint,algorithms_table.py.cargo fmt --check,cargo clippy --all-targets -- -D warnings.cargo testwith Wycheproof, and the AES-GCM tests withVG_CPU_FEATURES=none.out_of_placeandbackends_agreealready cover slices with and without a tail, and shorter than 16 bytes.🤖 Generated with Claude Code
https://claude.ai/code/session_01FoYWDDEDFq1DFev5X7n2aq
Generated by Claude Code