Skip to content

AES-CTR x86-64: eight blocks at a time in SSE2 registers in the baseline vg_aes_ctr32 - #4410

Merged
alex merged 2 commits into
mainfrom
claude/perf-4141-aes-baseline-kernel
Nov 7, 2026
Merged

alex merged 2 commits into
mainfrom
claude/perf-4141-aes-baseline-kernel

Conversation

@alex

@alex alex commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Part of #4141 (findings: "Baseline AES at none: AES-GCM is 1.1–1.9× aws-lc's constant-time code. AES-CTR is 1.45× at 16 KiB").

What was slow, and why

Without AES-NI, x86-64's vg_aes_ctr32 (which AES-CTR, -GCM, -CCM, -GCM-SIV, -SIV and CMAC all call) bitsliced four blocks into eight 64-bit general-purpose registers (BearSSL's aes_ct64). With only two free temporaries, the S-box spilled heavily, and every layer handled four blocks per instruction. aws-lc's aes_nohw does eight blocks in SSE2 registers.

What changed

The baseline vg_aes_ctr32 now runs groups of eight blocks in xmm0 … xmm7, with the same ct64 layout in each quadword: quadword 0 holds blocks 0–3, quadword 1 blocks 4–7. Everything is SSE2, the x86-64 baseline. The other x86-64 implementations (AES-NI, VAES) are unchanged.

  • S-box: Circuit.sbox, allocated by the DES circuits' SSE allocator (BitsliceAllocX) to xmm0 … xmm14, with spills to scratch.
  • ShiftRows: rotates each 16-bit row with psrlw/psllw and blends with two masks kept in scratch.
  • MixColumns: rotates by 16 bits with a pair of shifts, and by 32 bits with pshufd 0xb1.
  • AddRoundKey: loads the 64-bit bitsliced round-key word and copies it to both quadwords (punpcklqdq).
  • Reused as is: the prologue, the key bitslicing loop (same key format) and the 4-block group. The 4-block group now runs only for the last one to four blocks, so short inputs cost what they did. The 32-bit counter, scratch layout and contract are unchanged.

No TCB or Spec changes. Proofs (no sorry, no new axioms):

  • Proof/Framework/X86_64/StraightX.lean: the quadword-lane evaluator gains psrlw/psllw (a quadword shift masked to its words) and pshufd 0xb1 (a rotation of each quadword by 32), each with its per-quadword lemma from the TCB semantics. LinearX.lean is Linear.lean's lane-domain check, for SSE registers.
  • Proof/Aes/X86_64/Sse/Layers.lean: each layer is checked per quadword against the existing target-independent bit specs (sboxT, toBsG, srG, mcG, …), so Bitsliced.lean's bs_* lemmas apply unchanged.
  • Proof/Aes/X86_64/Sse/Group.lean:
    • encrypt8_ok;
    • the counter blocks, as Group.lean's ctr_inRel for groups 2g and 2g + 1;
    • 16-byte keystream XOR;
    • sseGroup_ok, which keeps Group.lean's loop invariant GInv from g to g + 2;
    • the mixed loop.
  • Proof/Aes/X86_64/Ctr32.lean: the whole-function proof, now with the mask setup and the mixed loop. The constant-time proof is the same taint_decide over the new code.

Numbers

Benchmarks workflow (x86_64 runner, VG_CPU_FEATURES=none, base 5e5cbf56): https://github.com/pyca/verified-garbage/actions/runs/42358850299

Benchmark Base Head Change
aes-ctr-128/64 600 ns 587 ns 2.1% faster
aes-ctr-128/1024 3.62 µs 3.08 µs 14.9% faster
aes-ctr-128/16384 51.26 µs 43.49 µs 15.2% faster
aes-128-gcm-encrypt/64 1.34 µs 1.34 µs 0.4% slower
aes-128-gcm-encrypt/1024 5.99 µs 5.57 µs 7.1% faster
aes-128-gcm-encrypt/16384 80.13 µs 73.10 µs 8.8% faster
aes-128-gcm-encrypt/1048576 5.07 ms 4.61 ms 9.1% faster

That run's OpenSSL and aws-lc-rs columns weren't feature-masked (they ran with AES-NI, about 1.5 µs at 16 KiB), so they aren't a like-for-like comparison.

This machine (Intel Xeon @ 2.30GHz; ci/bench_compare.py --openssl --openssl-with-features, with VG_CPU_FEATURES=none and OPENSSL_ia32cap as bench/gcm_siv_features.py scalar), where the gain is larger:

Benchmark Base Head Change vs OpenSSL vs aws-lc-rs
aes-ctr-128/64 750 ns 734 ns 2.1% faster 1.3× faster 1.6× faster
aes-ctr-128/1024 4.47 µs 3.16 µs 29.3% faster 2.2× faster 1.2× faster
aes-ctr-128/16384 64.97 µs 43.38 µs 33.2% faster 2.2× faster 1.1× faster
aes-128-gcm-encrypt/64 1.70 µs 1.69 µs 0.8% faster 1.2× slower 1.1× faster
aes-128-gcm-encrypt/1024 7.47 µs 6.47 µs 13.3% faster 1.2× faster about the same
aes-128-gcm-encrypt/16384 106.24 µs 85.44 µs 19.6% faster 1.3× faster about the same
aes-128-gcm-encrypt/1048576 6.77 ms 5.25 ms 22.5% faster 1.4× faster about the same

Most of GCM's remaining time at none is GHASH without PCLMULQDQ (about 42 µs of the 85 µs at 16 KiB here), which this PR doesn't touch.

Checks

  • lake build (all modules) and Emit.lean: only vg_aes_ctr32 changes in the generated code.
  • Every Static checks step, and check_lean_public_imports.py on the edited modules.
  • cargo test with Wycheproof, at the default features and at VG_CPU_FEATURES=none with cpu-features-env.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHFAHEwRUgwg8TfgQXC

…ine vg_aes_ctr32

WIP: proofs; crates not yet regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFAHEwRUgwg8TfgQXC
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFyzdwwyDWb9QSQqR1
@github-actions github-actions Bot added the untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. label Nov 7, 2026
@alex
alex enabled auto-merge November 7, 2026 09:16
@alex
alex added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit d1d4432 Nov 7, 2026
30 checks passed
@alex
alex deleted the claude/perf-4141-aes-baseline-kernel branch November 7, 2026 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified.

Development

Successfully merging this pull request may close these issues.

2 participants