Repository navigation
SHA-1 on x86-64: AVX and BMI compression variant (_avx_bmi) - #4405
Merged
Merged
Conversation
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
enabled auto-merge
November 7, 2026 08:53
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
6 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 ranvg_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:W + Kgoes into the same 64-byte window of the scratch space the scalar function uses, and each round adds it from there.Chis(¬b ∧ d) + (c ∧ b)(withandn) andMajis(b ∧ c) + ((b ⊕ c) ∧ d). Both are sums of masks with no bit in common.rorx.It uses the scalar function's 112 bytes of scratch and saves the same registers. So it is one more variant of
MdHashon 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.Sha1Backendgets anAvxBmiarm in Rust, andselect_backendsin the DSA search gets its six combinations (with tests).ShaNistays 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 perf.W + Kwindow invariant over the 20 groups.taint_decide.Tests: the Haswell and Skylake-X SDE lines in
ci/rust_matrix.tomlalready runsha1,dsaand the RSA hash groups unrestricted. On those CPUs (no SHA-NI) the new variant is now selected, so the matrix is unchanged. Locally,cargo testpasses withVG_CPU_FEATURESset toavx,avx2,bmi1,bmi2,noneandsha,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, modulessha1 hmac_sha1 pbkdf2_sha1 dsa):sha1/64sha1/1024sha1/16384hmac-sha1/64hmac-sha1/1024hmac-sha1/16384pbkdf2-hmac-sha1/1024pbkdf2-hmac-sha1/16384DSA 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), usingci/bench_compare.pywithVG_CPU_FEATURES=avx,avx2,bmi1,bmi2andOPENSSL_ia32capmasking SHA (--openssl-with-features):sha1/64sha1/1024sha1/16384hmac-sha1/1024hmac-sha1/16384pbkdf2-hmac-sha1/1024pbkdf2-hmac-sha1/16384On the bare compression function, before writing the proof, I timed the generated code against
vg_sha1_compressin 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