Skip to content

AES-XTS x86-64: one-pass AES-NI functions without AVX - #4400

Merged
alex merged 1 commit into
mainfrom
claude/perf-4141-aes-xts
Nov 7, 2026
Merged

alex merged 1 commit into
mainfrom
claude/perf-4141-aes-xts

Conversation

@alex

@alex alex commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

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:

  1. Westmere tier (ssse3,aes,pclmulqdq, no AVX): a real gap. The one-pass vg_aes_xts_*_aesni_pclmul functions required AVX, but only for a few VEX instructions in their tweak handling:

    • vpxor with a memory operand, used to XOR in the tweaks;
    • vmovq;
    • the three-operand vpsrldq, vpclmulqdq, vpslldq and vpxor of the ×α⁸ tweak step.

    Without AVX, XTS fell back to the two-pass _aesni functions. Those write every tweak into the data, call vg_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).

  2. AVX and AVX2 tiers: not a code gap. Out of place, _aesni_pclmul already 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-place group.

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

  • The 128-bit tweak helpers are now legacy SSE: X86_64Vaes.tPre/tPost, X86_64VaesTo.tPre, and X86_64AesNi.stepT/constK.
    • The tweak is loaded into xmm9 and XORed with a register pxor, because legacy pxor m128 requires an aligned operand.
    • movdqa copies the tweak before the shift, and movq (SSE2) loads 0x87.
  • _aesni_pclmul's features are now aes, pclmulqdq, so select picks it on Westmere-class CPUs too.
  • The VAES functions' 1–7-block tails share these helpers. They run after vzeroupper, so there is no SSE/AVX transition penalty.
  • Proofs: tPre1_ok/tPost1_ok/tPre1To_ok and their list forms now carry xmm9 in their frames ((.xmm9 :: regs).Nodup, r ∉ .xmm9 :: regs). OnePassNi.stepT_ok is reproven for the legacy sequence, and the now-unused VEX lane lemmas are removed.
  • Rust: select_features now asserts that PCLMULQDQ without AVX picks AesNiPclmul. In ci/rust_matrix.toml, aes_xts moves from the aes,avx,pclmulqdq,ssse3 line to aes,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.py reports 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_FEATURES benchmark (1 MiB) base head change
aes,pclmulqdq,ssse3 aes-xts-128 out of place 274.8 µs 173.1 µs 37.0% faster
aes-xts-128 in place 276.0 µs 202.2 µs 26.7% faster
aes-xts-256 out of place 322.5 µs 217.5 µs 32.6% faster
aes-xts-256 in place 320.7 µs 253.7 µs 20.9% faster
aes,avx,pclmulqdq,ssse3 all four within 0.1–0.3%
aes,avx,avx2,pclmulqdq,ssse3 aes-xts-128 out of place 170.6 µs 112.8 µs 33.9% faster
aes-xts-256 out of place 204.5 µs 149.4 µs 27.0% faster

In 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_ia32cap was left empty).

Local

Sapphire Rapids (Intel Xeon @ 2.10 GHz, 4 vCPU). ci/bench_compare.py against main, 3 interleaved rounds, 1 MiB:

VG_CPU_FEATURES benchmark main this PR change
ssse3,aes,pclmulqdq aes-xts-128 out of place 328.6 µs 144.6 µs 56% faster
aes-xts-128 in place 325.7 µs 181.1 µs 44% faster
aes-xts-256 out of place 382.6 µs 188.1 µs 51% faster
aes-xts-256 in place 375.4 µs 226.6 µs 40% faster
…,avx all four within 0.1–0.9%
…,avx,avx2,bmi1,bmi2 all four within 0.4–2.2%

OpenSSL 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, then Emit.lean --check: clean.
  • check_lean_public_imports.py on the edited modules: OK.
  • Every step of the Static checks job: pass.
  • cargo fmt --check and clippy -D warnings: clean.
  • XTS tests (CAVP and Wycheproof) pass under VG_CPU_FEATURES = native, none, aes,ssse3, aes,pclmulqdq,ssse3, aes,avx,pclmulqdq,ssse3, aes,avx,avx2,ssse3,vaes, and aes,avx,avx2,pclmulqdq,ssse3,vaes,vpclmulqdq.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHEHakcDWS9PDNhTTa7

…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
@alex
alex enabled auto-merge November 7, 2026 08:40
@alex
alex added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit f0a8c57 Nov 7, 2026
41 checks passed
@alex
alex deleted the claude/perf-4141-aes-xts branch November 7, 2026 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants