Skip to content

AES-GCM: call the dispatchers, not a table of selected functions - #4629

Merged
alex merged 4 commits into
mainfrom
claude/charming-archimedes-aj5193-aes-gcm
Nov 8, 2026
Merged

alex merged 4 commits into
mainfrom
claude/charming-archimedes-aj5193-aes-gcm

Conversation

@alex

@alex alex commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Stacked on #4623 (only the top commit is this PR's).

AesGcm held fns: &'static Fns, a table of the implementations its functions' dispatchers would run, which it filled once per process from their selectors (<function>_select) behind a hand-written once-cell (Selected: an AtomicU8 state machine, UnsafeCell<MaybeUninit<Fns>>, an unsafe impl Sync), so that no call tested the CPU's features again. Since #4623 every dispatcher chooses once itself and calls through a function pointer, so all of that is now what _auto does, and the key calls the dispatchers like every other module:

  • removes Fns, Selected, SELECTED, the verified_fn! macro and the eleven function-pointer types (−248 lines, +38);
  • removes the key's fns field (one pointer smaller per key, and one dependent load less per call: the dispatcher reads a static, not the key's pointer to the table and then the table);
  • the stream's update picks _stream_encrypt…_auto or _stream_decrypt…_auto on the DECRYPT const instead of a field.

No change to what runs: each call goes to the implementation _select returned before. Rust only; VG.Dispatch.selected and the _select functions are removed from the TCB by #4635.

Testing

  • cargo clippy --all-targets -D warnings for x86-64, aarch64 (Linux, Apple), i686, armv7, riscv64 and powerpc64le.
  • cargo test --release with Wycheproof on x86-64 (AVX-512, VAES), with all features, VG_CPU_FEATURES=none, and aes,pclmulqdq,ssse3.

Benchmarks

Run 42488035545: this PR with #4623 vs. their base, x86-64 and AArch64, every configuration of aes_gcm, groups aes-(128|256)-gcm-tls-(seal|open) and aes-128-gcm-stream: the median change is within ±0.1% in every job, and aes-128-gcm-stream/64 is 3–5% faster on most x86-64 configurations. One outlier (aes-128-gcm-tls-seal/256 +10% under aes,avx,avx2,pclmulqdq,ssse3) did not reproduce when this PR alone was measured on the same runner type (+2.1%, with neighbouring sizes 2–3% faster). Details in the comments.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHFCSTvQuX83u32Ae29

Each generated dispatcher (`<function>_auto`, `VG.Rust.dispatcher`) used
to test the CPU's features against each of its implementations' on every
call, inlined into every caller. It now holds a pointer to the
implementation to call, `CHOSEN`, which starts out pointing at `first`: a
function of the implementations' type that chooses one (`select`, with
the same conditions in the same order as before), records it in `CHOSEN`
and runs it. Every later call is a load and an indirect call, however many
implementations the function has, as in an ELF IFUNC but portable to
every target and object format.

The pointer, `select` and `first` are items inside the dispatcher, so
their names are its own and its lines are still one span from its `pub
unsafe fn` to its closing `}` for `ci/merge_rust_coverage.py`.

This is a change to the trusted base (`TCB/Rust.lean`): which
implementation runs is chosen exactly as before, and `detected()` already
returned the same features for the life of the process, so the choice is
the same; only when it is made changes.

Fixes #4609.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFCSTvQuX83u32Ae29
…ms case

`main` dropped `Rank.skip` (#4618), so a dispatcher with variants always
has arms: the case of none, which called the base directly, is gone. The
generated files are regenerated over main's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFCSTvQuX83u32Ae29
@alex
alex force-pushed the claude/charming-archimedes-aj5193-aes-gcm branch from 02b9927 to 28bd86f Compare November 8, 2026 14:23
A dispatcher's `first` (`VG.Rust.dispatcher`) is an `extern` function
inside it, which `fnNames` read as a function of the file named `first`,
and `unreadFns` then refused every dispatchers' file with one ("cannot
read the names of its functions"). Read only the lines that are not
indented, as every item of a generated file is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFCSTvQuX83u32Ae29

alex commented Nov 8, 2026

Copy link
Copy Markdown
Member Author

Benchmarks: run 42488035545, this PR with #4623 under it vs. their base (3cf1f61), x86-64 and AArch64, every VG_CPU_FEATURES configuration of aes_gcm, groups aes-(128|256)-gcm-tls-(seal|open) and aes-128-gcm-stream (119 rows per job). Negative is faster.

Job Median Rows >2% slower / faster Largest slowdown
x86-64 (all features: VAES, VPCLMULQDQ, AVX-512) 0.0% 0 / 1 aes-128-gcm-tls-open/255 +1.0%
x86-64 aes,avx,avx2,pclmulqdq,ssse3,vaes,vpclmulqdq 0.0% 2 / 2 aes-256-gcm-tls-seal/31 +3.4%
x86-64 aes,avx,avx2,pclmulqdq,ssse3 +0.1% 7 / 1 aes-128-gcm-tls-seal/256 +10.1%
x86-64 aes,pclmulqdq,ssse3 0.0% 0 / 2 +0.7%
x86-64 aes,ssse3 −0.1% 0 / 0 +0.5%
x86-64 none 0.0% 26 / 33 ±7% both ways (noisy)
AArch64 (all features) 0.0% 0 / 0 +1.2%
AArch64 aes +0.1% 0 / 0 +0.9%
AArch64 none 0.0% 0 / 0 +0.1%

(The other x86-64 configurations are the same: medians within ±0.1%.) aes-128-gcm-stream/64 is 3–5% faster in most x86-64 jobs.

One outlier to explain: under aes,avx,avx2,pclmulqdq,ssse3 (the _aesni_pclmul_avx code), AES-128 seals of 256–1025 bytes are about 15 ns slower (146 → 161 ns at 256, 328 → 344 ns at 1024), with tight spreads, while AES-256 seals and every open are within 2%. The call path is the same for both key sizes, so I'm isolating it with two narrow runs on that configuration: #4623 alone vs. main (which changes nothing about how AES-GCM is called, so any difference there is code layout), and this PR vs. #4623. Results to follow.


Generated by Claude Code

A key used to hold a pointer to a table of the implementations its
functions' dispatchers would run (`Fns`), chosen once per process by their
selectors (`<function>_select`) behind a hand-written once-cell
(`Selected`), so that no call tested the CPU's features again. The
dispatchers now choose once themselves and call through a function
pointer (`VG.Rust.dispatcher`), so the key calls them as every other
module does, and the table, the once-cell, the function pointer types and
the key's pointer to the table go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFCSTvQuX83u32Ae29
@alex
alex force-pushed the claude/charming-archimedes-aj5193-aes-gcm branch from 28bd86f to 1ee0a6e Compare November 8, 2026 14:38

alex commented Nov 8, 2026

Copy link
Copy Markdown
Member Author

The aes,avx,avx2,pclmulqdq,ssse3 outlier did not reproduce. Two narrow runs on that configuration, groups aes-(128|256)-gcm-tls-seal:

  • PR vs. pyca/verified-garbage#4623 (this PR's change alone), on the same kind of runner as the outlier (146 ns base at 256 B): median −0.1%, 2 rows >2% slower and 6 >2% faster. aes-128-gcm-tls-seal/256 146 → 149 ns (+2.1%), /257 +1.9%, /287 −3.4%, /447 −2.5%, /1024 +1.4%, /1025 +1.2%; the largest change is aes-256-gcm-tls-seal/255 5.4% faster.
  • pyca/verified-garbage#4623 vs. main, which doesn't change how AES-GCM is called: median +0.1%, worst aes-256-gcm-tls-seal/4096 +2.5%. (This job drew a faster runner CPU, 95 ns at 256 B, so it says less about the outlier's machine.)

So the +10% at 256–257 B in the first run was that job's draw, not the change: the same rows on the same runner type are within ±3.5% both ways, about the spread of this configuration. Every other configuration in the first run was within ±0.1% median.


Generated by Claude Code

Base automatically changed from claude/charming-archimedes-aj5193 to main November 8, 2026 14:54
@alex
alex added this pull request to the merge queue Nov 8, 2026
Merged via the queue into main with commit 5584968 Nov 8, 2026
43 checks passed
@alex
alex deleted the claude/charming-archimedes-aj5193-aes-gcm branch November 8, 2026 15:07
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