Skip to content

SHA-1 on x86-64: AVX and BMI compression variant (_avx_bmi) - #4405

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

alex merged 1 commit into
mainfrom
claude/perf-4141-sha1-simd

Conversation

@alex

@alex alex commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Part of #4141 (finding 7 of the audit).

What was slow, and why

SHA-1's only x86-64 variant was _shani. Without the SHA extensions (Skylake to Comet Lake clients, Skylake-SP/Cascade Lake servers, older CPUs), it ran vg_sha1_compress. That function computes the schedule one word at a time through memory, with about 18 instructions a round. The audit measured it at 1.7–1.9× aws-lc-rs at 1–16 KiB, and HMAC-SHA-1 at 1.5–1.7×.

What changed

There is a new compression function, vg_sha1_compress_avx_bmi, for AVX + BMI1 + BMI2, using the technique of OpenSSL's AVX SHA-1:

  • The message schedule is computed four words at a time in SSE registers (VEX.128).
  • W + K goes into the same 64-byte window of the scratch space the scalar function uses, and each round adds it from there.
  • Ch is (¬b ∧ d) + (c ∧ b) (with andn) and Maj is (b ∧ c) + ((b ⊕ c) ∧ d). Both are sums of masks with no bit in common.
  • The rotations are rorx.
  • The rounds run about 9 instructions each.

It uses the scalar function's 112 bytes of scratch and saves the same registers. So it is one more variant of MdHash on x86-64 (Variants/MdHash/X86_64/Sha1AvxBmi.lean). The streaming functions, the one-shot digest, HMAC-SHA-1, PBKDF2-HMAC-SHA-1, DSA's parameter search (×SHA-256 ×Miller–Rabin) and the hash tables follow it through their generic registrations. Sha1Backend gets an AvxBmi arm in Rust, and select_backends in the DSA search gets its six combinations (with tests). ShaNi stays first in the selection order.

No TCB or Spec change: every instruction is already in the model. The proof is Proof/Sha1/X86_64/AvxBmi/Compress.lean. It is proven against the scalar function's contract (compressX86_64) and reuses its precondition, block lemmas and save/restore. It covers:

  • xupd_quad: one register of the schedule gives the next four words of the specification.
  • round_ok/roundV_eq: one round, executed symbolically once per f.
  • The W + K window invariant over the 20 groups.
  • Constant time by taint_decide.

Tests: the Haswell and Skylake-X SDE lines in ci/rust_matrix.toml already run sha1, dsa and the RSA hash groups unrestricted. On those CPUs (no SHA-NI) the new variant is now selected, so the matrix is unchanged. Locally, cargo test passes with VG_CPU_FEATURES set to avx,avx2,bmi1,bmi2, none and sha,ssse3.

The README tables were regenerated: SHA-1, HMAC-SHA-1 and PBKDF2-HMAC-SHA-1 on x86-64 now list "SHA extensions, AVX, BMI1, BMI2".

Numbers

Benchmarks workflow (x86_64 runner, VG_CPU_FEATURES=avx,avx2,bmi1,bmi2, modules sha1 hmac_sha1 pbkdf2_sha1 dsa):

Benchmark Base Head Change
sha1/64 227 ns 130 ns 42.6% faster
sha1/1024 1.87 µs 966 ns 48.2% faster
sha1/16384 28.11 µs 14.39 µs 48.8% faster
hmac-sha1/64 581 ns 348 ns 40.2% faster
hmac-sha1/1024 2.22 µs 1.20 µs 46.2% faster
hmac-sha1/16384 28.47 µs 14.70 µs 48.4% faster
pbkdf2-hmac-sha1/1024 230.20 µs 138.66 µs 39.8% faster
pbkdf2-hmac-sha1/16384 3.68 ms 2.21 ms 39.8% faster

DSA and the RSA-OAEP/PSS groups: within ±5% (OAEP with MGF1-SHA-1 encrypt 2–5% faster). The DSA public/private-key accessors in tens of ns are noise. No benchmark slowed by more than the threshold. That run measured OpenSSL and aws-lc-rs only without VG_CPU_FEATURES.

Local run on an Intel(R) Xeon(R) Processor @ 2.10GHz (has SHA-NI, masked here), using ci/bench_compare.py with VG_CPU_FEATURES=avx,avx2,bmi1,bmi2 and OPENSSL_ia32cap masking SHA (--openssl-with-features):

Benchmark Base Head Change aws-lc-rs Head vs aws-lc-rs
sha1/64 256 ns 169 ns 33.9% faster 198 ns 1.2× faster
sha1/1024 2.24 µs 1.40 µs 37.3% faster 1.58 µs 1.1× faster
sha1/16384 31.37 µs 20.30 µs 35.3% faster 23.21 µs 1.1× faster
hmac-sha1/1024 2.80 µs 1.97 µs 29.8% faster 2.24 µs 1.1× faster
hmac-sha1/16384 36.46 µs 22.11 µs 39.4% faster 28.86 µs 1.3× faster
pbkdf2-hmac-sha1/1024 267.55 µs 187.03 µs 30.1% faster 246.15 µs 1.3× faster
pbkdf2-hmac-sha1/16384 4.44 ms 3.08 ms 30.6% faster 4.34 ms 1.4× faster

On the bare compression function, before writing the proof, I timed the generated code against vg_sha1_compress in a harness: 765 vs 465 MiB/s, 1.65×.

An SSSE3-only variant (pre-Haswell CPUs) is not included. Every CPU the audit named has AVX2 and BMI2.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHEFnN3Cg4JT9EcrSqB

Without the SHA extensions, SHA-1 ran vg_sha1_compress, whose schedule
goes through memory one word at a time. vg_sha1_compress_avx_bmi computes
the message schedule four words at a time in SSE registers (VEX.128) and
keeps W + K in the same 64-byte window of the scratch space, from which
each round adds it to e; Ch and Maj are sums of two disjoint masks (andn
for Ch) and the rotations are rorx. It needs AVX, BMI1 and BMI2, uses the
scalar function's 112 bytes of scratch space and saves the same
registers, so the streaming, HMAC, PBKDF2 and DSA parameter-search
functions follow it through their generic registrations with no change
to their working space.

Proven against the scalar function's contract
(Proof/Sha1/X86_64/AvxBmi/Compress.lean), reusing its precondition,
prologue and epilogue.

Part of #4141.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHEFnN3Cg4JT9EcrSqB
@alex
alex enabled auto-merge November 7, 2026 08:53
@alex
alex added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit e7c5123 Nov 7, 2026
38 checks passed
@alex
alex deleted the claude/perf-4141-sha1-simd branch November 7, 2026 09:48
alex pushed a commit that referenced this pull request Nov 7, 2026
`_avx_bmi` (#4405) after `_shani`, as `src/hashes/sha1.rs` chooses it, and
the generated crates regenerated with main's 3DES and PPC64LE ChaCha20
functions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHE2dqL6Q2PDogxfD5f
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