Repository navigation
x86-64 AES-GCM: encrypt slices after an unaligned one straight to the output - #1467
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_stream_encrypt_to, whichvg_aes_gcm_seal_gather(AesGcm::encrypt) calls once per slice on its streaming path, encrypted a slice straight fromsrctodstonly when the text before it ended a block. A slice after one that ends mid-block was copied todstbyte by byte and encrypted in place byvg_aes_gcm_stream_encrypt, slow path and all. For example,encrypt(&[&p[..1], &p[1..]])copied almost the whole message one byte at a time.It now starts with a head step. If the text so far ends inside a block, the
(-text_len) mod 16bytes that finish it (or all oflen, if fewer) are copied and encrypted byvg_aes_gcm_stream_encrypt. Then the whole blocks after them go tovg_aes_gcm_encrypt_blocks_tostraight fromsrc, and at most 15 bytes are left for the copy. The head is skipped when the text so far plus the head would pass 2⁶⁴ bytes, so the text length stays exact.blocksnow works past the bytes done instead of from offset 0. Every instance ofstream_encrypt_togets this, for every variant: it is generic code.This is part of #1228. The workload is the one from the rustls analysis in #991 (
encryptwith a few slices, reused key).Measurements
AesGcm::encrypttimed with a reused key, 13-byte AAD and 12-byte nonce, on an AVX-512 Xeon (VAES, VPCLMULQDQ). Each cell is the best of 9 runs in ns, base → this PR. Texts under 768 bytes still take the copy-and-sealpath, so this change doesn't affect them.[n][1, n-1][n-1, 1][5, n-5][100, n-100]With a slice after one that ends mid-block,
encryptis now 2.1–6.0× faster: within 1.0–1.7× of a single slice instead of 2.5–6×. Inputs where every slice boundary is block-aligned ([n],[n-1, 1]) are unchanged.Proofs
Assembly comes only from the Lean code. Spec, TCB and contracts are unchanged; the proofs are against the existing
streamEncryptTocontract.Steps.lean: per-block lemmas for the new code (headLen,headOver,headPtrs,headArgs,headDone,blocksLoad,blocksCount).blocksArgsnow works from any offset.BlkCall.lean,EncCall.lean: the calls ofencrypt_blocks_toandstream_encryptnow take an offset and a count, not just "from 0" or "the rest".Blocks.lean:call_midandblocks_oknow start fromMid s oinstead ofMid s 0.Head.lean(new):head_ok, split into per-piece lemmas that the constant-time proof reuses. The head's byte count stays in the free slotwork + 72across its call.CT.lean:head_rel, plusblocks_relfrom any offset. Every branch in the head depends only ontext_lenandlen, which are public.Checks
lake build(all 13,546 jobs), 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, with all CPU features, and the AES-GCM tests again withVG_CPU_FEATURES=none.backends_agreenow also covers slices after one that ends mid-block: a slice shorter than the bytes that end the open block, one exactly that long, and one longer.🤖 Generated with Claude Code
https://claude.ai/code/session_01FoYWDDEDFq1DFev5X7n2aq
Generated by Claude Code