Repository navigation
AES-XTS x86-64: one-pass AES-NI functions without AVX - #4400
Merged
Merged
Conversation
…t AVX
`vg_aes_xts_{en,de}crypt[_to]_aesni_pclmul`, the one-pass functions for
CPUs with AES-NI and PCLMULQDQ but not VAES, required AVX only for a few
VEX instructions in their tweak handling: `vpxor` with a memory operand
(the tweaks XORed in), `vmovq`, and the three-operand `vpsrldq`,
`vpclmulqdq`, `vpslldq` and `vpxor` of the tweak step. Without AVX
(Westmere-class CPUs, `VG_CPU_FEATURES=ssse3,aes,pclmulqdq`) XTS fell back
to the two-pass `_aesni` functions, which write every tweak to the data,
call the block function and XOR them in again: 2.2x OpenSSL at 1 MiB.
The 128-bit tweak helpers (`X86_64Vaes.tPre`/`tPost`, `X86_64VaesTo.tPre`)
and the AES-NI loop's `stepT` and `constK` are now legacy SSE, with the
tweak loaded into `xmm9` (legacy `pxor` requires an aligned memory
operand) and `movdqa` copying it before the shift. The `_aesni_pclmul`
functions now need only AES-NI and PCLMULQDQ, so `select` chooses them
without AVX. The VAES functions' tails (after `vzeroupper`) use the same
helpers.
Interleaved A/B against main (ci/bench_compare.py, 3 rounds, 1 MiB):
ssse3,aes,pclmulqdq: AES-128 out of place 328.6 -> 144.6 us (56% faster),
in place 325.7 -> 181.1 us; AES-256 out of place 382.6 -> 188.1 us.
With AVX and with AVX2: within 0.1-2.2% (noise).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHEHakcDWS9PDNhTTa7
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.
Part of #4141: the audit's "AES-XTS without VAES: 1.3–1.55× OpenSSL at 1 MiB" (findings).
What was slow, and why
There are two separate causes:
Westmere tier (
ssse3,aes,pclmulqdq, no AVX): a real gap. The one-passvg_aes_xts_*_aesni_pclmulfunctions required AVX, but only for a few VEX instructions in their tweak handling:vpxorwith a memory operand, used to XOR in the tweaks;vmovq;vpsrldq,vpclmulqdq,vpslldqandvpxorof the ×α⁸ tweak step.Without AVX, XTS fell back to the two-pass
_aesnifunctions. Those write every tweak into the data, callvg_aes_encrypt_blocks_aesni, then compute the tweaks again and XOR them in. On this machine that was 2.2× OpenSSL (338 µs vs 152 µs, AES-128, 1 MiB).AVX and AVX2 tiers: not a code gap. Out of place,
_aesni_pclmulalready matches OpenSSL: 145 µs vs 150–165 µs (AES-128) and 188 µs vs 189–200 µs (AES-256). Both are at the AES-NI port limit. The in-place benchmark (aes-xts-*/1048576) also times copying 1 MiB into the buffer before each in-place operation (bench/benches/primitives/aes_xts.rs). OpenSSL's side runs out of place and pays no copy, and that copy is the 30–40 µs left in the audit's 1.3–1.5× ratios. I haven't changed the benchmark: the like-for-like comparison is the-out-of-placegroup.Interleave width isn't the problem. The loop already runs 8 blocks at a time with AES-NI's two ports saturated, and the tweak step costs about the same as OpenSSL's.
What changed
X86_64Vaes.tPre/tPost,X86_64VaesTo.tPre, andX86_64AesNi.stepT/constK.xmm9and XORed with a registerpxor, because legacypxor m128requires an aligned operand.movdqacopies the tweak before the shift, andmovq(SSE2) loads0x87._aesni_pclmul's features are nowaes, pclmulqdq, soselectpicks it on Westmere-class CPUs too.vzeroupper, so there is no SSE/AVX transition penalty.tPre1_ok/tPost1_ok/tPre1To_okand their list forms now carryxmm9in their frames ((.xmm9 :: regs).Nodup,r ∉ .xmm9 :: regs).OnePassNi.stepT_okis reproven for the legacy sequence, and the now-unused VEX lane lemmas are removed.select_featuresnow asserts that PCLMULQDQ without AVX picksAesNiPclmul. Inci/rust_matrix.toml,aes_xtsmoves from theaes,avx,pclmulqdq,ssse3line toaes,pclmulqdq,ssse3, which is now the configuration that first picks it.No TCB or spec change: every instruction used was already in the x86-64 model. The README tables are unchanged (
algorithms_table.pyreports them current), since the variant set is the same.Numbers
Benchmarks workflow
Run 42356240341: x86_64,
aes_xts, one interleaved round per job. Each job ran on its own runner, so compare within a job, not across jobs.VG_CPU_FEATURESaes,pclmulqdq,ssse3aes,avx,pclmulqdq,ssse3aes,avx,avx2,pclmulqdq,ssse3In that run, the 64-byte cases are within ±5%: they are below the 8-block loop and differ only in the tail's extra load. The OpenSSL column of that run is not a like-for-like reference, because it ran with all of the runner's features (
openssl_ia32capwas left empty).Local
Sapphire Rapids (Intel Xeon @ 2.10 GHz, 4 vCPU).
ci/bench_compare.pyagainstmain, 3 interleaved rounds, 1 MiB:VG_CPU_FEATURESssse3,aes,pclmulqdq…,avx…,avx,avx2,bmi1,bmi2OpenSSL in the same no-AVX tier (masked
OPENSSL_ia32cap) measured 152–186 µs for AES-128 and 197–215 µs for AES-256 across runs. Our out-of-place times are now at or below OpenSSL's in all three tiers.Checks
lake build, thenEmit.lean --check: clean.check_lean_public_imports.pyon the edited modules: OK.cargo fmt --checkandclippy -D warnings: clean.VG_CPU_FEATURES= native,none,aes,ssse3,aes,pclmulqdq,ssse3,aes,avx,pclmulqdq,ssse3,aes,avx,avx2,ssse3,vaes, andaes,avx,avx2,pclmulqdq,ssse3,vaes,vpclmulqdq.🤖 Generated with Claude Code
https://claude.ai/code/session_01TKUFHEHakcDWS9PDNhTTa7