Repository navigation
x86-64 RSA inverse: each batch's updates fused into one pass per pair - #4995
Merged
Merged
Conversation
The batch of 59 divsteps now updates f, g and a, b in place, in one pass each over their words: (f, g) with two-word accumulators, the shift by 2^59 folded into the store of the word before, and (a, b) with three-word accumulators of u·a + v·b + k·m, the multiple k of m taken from column 0 and the sign words added in the last column. This replaces lin, shr, mred and copyW, and their proofs, for every caller (crt_values, recover_primes_det and key generation's inverse). Counts stay in registers, so the batch is still constant time (taint_decide). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
alex
marked this pull request as ready for review
November 12, 2026 09:11
alex
pushed a commit
that referenced
this pull request
Nov 12, 2026
Merges origin/main (c7f68f6), which contains #4990 (squash-merged as f36e35a; this branch was stacked on it) and #4995 (x86-64 fused inverse). Squash double-apply: for each file below, main's version is byte-identical to #4990's head (6c4ad41), so the branch's version is taken: - lean/VerifiedGarbage/Proof/Rsa/X86/KcWord.lean (conflicted) - Impl/Rsa/{X86_64,AArch64}/CheckKeyCore.lean, Proof/Rsa/AArch64/{CkCT,CkPieces,CkSteps}.lean, Proof/Rsa/X86_64/{KeyCTPieces,KeyChecks,KeySteps}.lean (auto-merged without conflict, but with #4990's definitions duplicated, e.g. mulPQ) Impl/Rsa/X86_64/Keys.lean auto-merged, keeping both this branch's changes and #4995's. The conflicting generated files (rsa.s and rsa.rs of the x86, x86_64 and powerpc64le crates) and every other crate were regenerated with the full emitter (Emit.lean); Emit.lean --check passes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGGAywWt7REP7CHZ6u
This was referenced Nov 12, 2026
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.
The x86-64 counterpart of #4989 (i686), #4991 (RISC-V) and #4992 (AArch64). Before this PR, each batch of the batched inverse (
Inv.batch) made separate passes over the words:lin, the matrix rows;shr, the shift of (f, g) by 59;mred, the Montgomery reduction of (a, b);copyW, the copies between them.It now makes one in-place pass for each pair:
fgRow: (u f + v g) / 2^59 and (q f + r g) / 2^59 from one read of each word, with two-word accumulators. The division by 2^59 is folded into the store of word j − 1.abRow: u a + v b + k m for both rows, with three-word accumulators, stored a word down. k comes from column 0 (thekd/keslots), and the last column adds the sign words.Every caller of
Inv.batchuses it:crt_values(inverseN/inverseNS),recover_primes_det(inverse) and keygen (inverseK).lin,shr,mred,copyWandabsMare deleted, along with their proofs.Proofs
Proof/Rsa/X86_64/InvB/Rows.lean, withfgRow_okandabRow_ok. OnlyBatchimports it.batch_oknow composes the two rows.BSgains one fact: word n ofaPis 0.InvLaw,inverse_ok) is unchanged, so no caller's proof changes beyond the batch.r15and recompute n from it, so every branch and address is a function of registers that taint tracks as public. A variant kept entirely in registers, which held n and the end pointer in memory slots, was 1% faster (238k instead of 240k at 2048). It failstaint_decide, because the analysis makes values loaded from memory secret, so this PR keeps the version that passes.Trust
No change to
TCB/orSpec/. Incrates/verified-garbage-asm-x86_64, onlyrsaandinternal/vg_rsa_keygen_keychange.Measurements
Callgrind, one call through the emitted crate, main (3170b02) → this branch:
crt_valuesrecover_primes_detrecover_primes_detis dominated by work other than the inverse. Keygen's time is the random prime search: its saving of about 100k instructions per call is far below run-to-run variance.Benchmarks
Run 43187425116: x86_64, module
rsa, groupsrsa_from_.*, base 3170b02 (main), with OpenSSL and aws-lc-rs. Each configuration is one interleaved run on its own runner. The inverse runs the same code in every configuration, so the rows are six samples of one change.rsa_from_primesis mostly onecrt_valuescall.avx,avx2,bmi2,adx(EPYC 9V74)none(EPYC 9V74)avx,avx2,bmi1,bmi2,adx(EPYC 7763)avx,avx2,bmi1,bmi2(Xeon Platinum 8573C)sha,ssse3(EPYC 7763)The default job's 4096-bit row (82.9 → 88.7 µs median) disagrees with its own 2048- and 3072-bit rows, with the other five runs (on three other CPU models), and with callgrind's −15.1%. Its head samples are also unusually spread: a standard deviation of 24.4 µs, against 1.7 µs for base. So it reads as disturbance during that one round rather than a slowdown. It has not been rerun.
rsa_from_components(recover_primes_det) moves by 0.1–1.5% either way, as expected for a 0.5% change in instructions.rsa_from_crtdoes not run the inverse.Default configuration:
rsa_from_components/256rsa_from_components/384rsa_from_components/512rsa_from_crt/256rsa_from_crt/384rsa_from_crt/512rsa_from_primes/256rsa_from_primes/384rsa_from_primes/512The
avx512ifmaconfiguration was not measured, because its runner (EPYC 7763) lacks those features.Checks
On ca02889, one commit on main (3170b02):
lake buildpasses, and so do the fullEmit.leanandEmit.lean --check(exit 0).check_mcdt.pyandcheck_lean_public_imports.pyon the edited modules.cargo test --releasewith Wycheproof passes with default CPU features, and withVG_CPU_FEATURES=none(cpu-features-env).This PR does not depend on #4989, #4990 or #4993.
Impl/Rsa/X86_64/Keys.leanand the generated x86-64rsafiles.Keys.leanmerges cleanly with both. The generated files conflict with RSA check_key: word steps sized to the operands on x86, x86-64, AArch64 and PPC64LE #4993's and merge textually with RSA key import: the p·q = n product over the primes' words on every architecture but ARMv7 #4990's. Either way, whichever of these PRs merges second gets main merged in and the crate regenerated by the full emitter.🤖 Generated with Claude Code
https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh