Skip to content

Argon2 and BLAKE2: one implementation per CPU level, chosen by their dispatchers - #4502

Merged
alex merged 2 commits into
mainfrom
claude/argon2-pairs
Nov 7, 2026
Merged

alex merged 2 commits into
mainfrom
claude/argon2-pairs

Conversation

@alex

@alex alex commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

What changes

On x86-64, Argon2 was built for every BLAKE2b backend paired with every implementation of G, nine functions in all. src/kdf/argon2.rs matched on both backends to pick one of five. It marked the other four as never chosen and excluded them from coverage. Those four are combinations no CPU would ever pick: they existed only because Argon2 was generic over the full product of the two.

The generic registration (Generic/Blake2b/Argon2Compress/X86_64/Argon2.lean) now pairs each BLAKE2b backend only with G's implementation of the same suffix: the baseline, _avx2 and _avx512. Any other pair needs the features of one of these three, which runs both of its parts with them. That leaves three functions, vg_argon2, vg_argon2_blake2b_avx2_g_avx2 and vg_argon2_blake2b_avx512_g_avx512, and one generated dispatcher. src/kdf/argon2.rs calls vg_argon2_auto. CompressBackend, its choice, its compile-time assertions, the coverage exclusion and its unit test are all removed.

Choices

CPU Before After
AVX-512F + AVX512VL _avx512 + _avx512 same
AVX2, no AVX-512F _avx2 + _avx2 same
none of these baseline same
AVX-512F without AVX512VL (Knights Landing) BLAKE2b _avx2 + G _avx512 _avx2 + _avx2
AVX-512F without AVX2 (no such CPU; only VG_CPU_FEATURES) BLAKE2b baseline + G _avx512 baseline

The ci/rust_matrix.toml line avx,avx512f, which existed only to select that last pair, is removed. The comment describing the avx,avx2,avx512f line now names Argon2's AVX2 code.

Benchmark

Run of argon2 on x86_64 against da38ca4, this branch's base. AArch64 and x86 have only one Argon2 implementation.

The run has three jobs: all features, avx,avx2,bmi1,bmi2,adx, and none. Each covers the 14 Argon2 benchmarks, and none regresses:

Job Median Worst row Best row
1 −0.1% +1.1% −1.5%
2 0.0% +1.1% −0.5%
3 +0.9% +3.3% −2.3%

These numbers come from the jobs' uploaded samples: the median estimate of each benchmark, head against base. The artifacts don't record which job ran which configuration, so the rows are numbered rather than named. Every CPU these runners have runs the same machine code as before, since only the never-chosen pairs and the Rust choice went.

Also: BLAKE2 (#4511, merged into this branch)

#4511 was stacked on this PR and merged into its branch, so this PR lands both. blake2! (BLAKE2b and BLAKE2s) calls vg_blake2{b,s}_{update,finalize}_auto, and Blake2bBackend and Blake2sBackend are removed: once Argon2 calls its own dispatcher, it was their last user outside src/hashes. Benchmark: https://github.com/pyca/verified-garbage/actions/runs/42398981897.

No TCB or Spec change.

Checks

  • lake build; Emit.lean --check
  • check_variants.py and every other static check
  • cargo fmt --check; cargo clippy --all-targets -D warnings on x86-64, AArch64, i686, ARMv7, RISC-V 64 and PPC64LE
  • argon2 and rfc9106 tests (release) with VG_CPU_FEATURES unset and set to:
    • avx,avx2,avx512f,avx512vl
    • avx,avx2,avx512f
    • avx,avx2
    • none

https://claude.ai/code/session_01TKUFHE2dqL6Q2PDogxfD5f


Generated by Claude Code

Argon2 on x86-64 was instantiated for every BLAKE2b backend with every
implementation of G, nine functions, of which the Rust code chose five
(listing four as never chosen) by matching on both backends. The generic
registration now pairs each BLAKE2b backend with G's of the same suffix,
the baseline, `_avx2` and `_avx512`: any other pair needs the features of
one of these, which runs both its parts with them. `src/kdf/argon2.rs`
calls `vg_argon2_auto`; `CompressBackend`, its choice and its tests go.

A CPU with AVX-512F but not AVX512VL (Knights Landing) ran BLAKE2b's
`_avx2` with G's `_avx512` and now runs `_avx2` for both; one with
AVX-512F but not AVX2 (none exists) runs the baseline. The matrix's
`avx,avx512f` line, which chose that pair, goes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHE2dqL6Q2PDogxfD5f
`blake2!` calls `vg_blake2<b|s>_{init,update,finalize}_auto`
(`crate::arch::dispatch::blake2<b|s>`) instead of matching on
`Blake2bBackend` or `Blake2sBackend`, which it chose in Rust when a
computation started. The enums, their `REQUIRES` and `select`, and the
computations' `backend` field go; Argon2, their last other user, calls its
own dispatcher (#4502).

The choice is today's: on x86-64, BLAKE2b's `_avx512`, then `_avx2`, and
BLAKE2s's `_avx`; the baseline elsewhere. `update` and `finalize` each
check their own features, so they could differ only on feature sets no
CPU has; both have the same contract over the same state.


Claude-Session: https://claude.ai/code/session_01TKUFHFuGsz7F32Tm9DLkuP

Co-authored-by: Claude <noreply@anthropic.com>
@alex alex changed the title Argon2: one implementation per CPU level, chosen by its dispatcher Argon2 and BLAKE2: one implementation per CPU level, chosen by their dispatchers Nov 7, 2026
@alex
alex added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit 4a24441 Nov 7, 2026
42 of 43 checks passed
@alex
alex deleted the claude/argon2-pairs branch November 7, 2026 18:59
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