Repository navigation
RISC-V RSA: lazy long division, 63 bits a step, for every word-division user - #4996
Merged
Merged
Conversation
…offsets Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
…roof Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
…orrectness) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
…emmas in KeyWordDiv Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
…d key The proofs of the word divisions in vg_rsa_crt_values, and its constant-time proof split after them, since the registers the divisions restore from memory are secret to the taint analysis. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
…lanced keys Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
…ail by one shift and divW (tail_ok) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
…sion by words for balanced keys Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
The five conflicts come from #4982's squash: main's versions of CrtValues.lean, CrtValCorrect.lean, CrtValVerified.lean and the riscv64 rsa.{rs,s} equal #4982's head 307f394, which this branch is built on, so each takes this branch's side. Nothing else main changed imports the modules this branch changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh
The inverse modulo an odd m (InvBatch: inverse, inverseS, gcd) now runs batches of 59 divsteps on 64-bit words over nu = ceil(n / 2) words, as x86-64's: 3 nu + 2 batches of Proof/Divstep/TcS.lean's batchS 59, with a and b kept in (-m, 2m) and normalized once at the end. lin multiplies a signed coefficient by a word with mul, and mulhu less the word where the coefficient is negative. InvLaw is unchanged but for inverseS's factor, now 2^(5 (3 nu + 2)). Every caller uses it: crt_values (its tail's K is 15 (w / 2) + 10, and its shift's carry is (x >> 1) >> (63 - T), so that T = K mod 64 = 0, for 5376-bit keys, is handled), recover_primes_det's gcd and key generation. The RISC-V 32-bit inverse is replaced; IbBatch and Instances are folded into IbInverse, which supplies the InvLaw instance, so that TcS's algebra reaches few modules. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
main is #4976 and the squash of #4984, whose head (16a23a3) this branch is built on: every file #4984 changed is identical on main and at 16a23a3, so each takes this branch's version (Instances.lean, folded into IbInverse here, stays deleted). The merged tree is this branch's plus exactly #4976's files as on main (PPC64LE, and MontCheckMath.lean, which only PPC64LE imports). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh
Each batch of the 64-bit batched inverse now applies its matrix in two in-place passes, in the shape of libsecp256k1's update_fg and update_de: * updateG: (u f + v g) / 2^59 and (q f + r g) / 2^59 from one read of each word, two signed accumulators, the low words with the five bits before stored a word down; * updateAB: (t + k m) / 2^64 for both rows, k m added to each low word by an unsigned carry, the sums stored a word down. They replace lin, shr, mred and their copies. The passes borrow twelve registers, kept in arrays 11 and 12. updateG_ok and updateAB_ok keep their statements, so BS, InvLaw and batch_ok are unchanged. The taint analysis takes the restored registers as secret, so the constant-time proofs of recover_primes_det (RdCT) and keygen (KeyCT) now cut after the inverses and pin the registers again from correctness, as crt_values' proof already does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
…on user Divide 63 bits a step with the remainder kept in [0, 2n) instead of reduced: V = r 2^63 + c < n 2^64, the existing 3-by-2 estimate lowered by one, and t = V - q̂ n needs no add-back (WordStep.lazy_step). mulSub writes t shifted up by 63 bits with the next chunk below it, so a step is 21 instructions a word and the next chunk 16. One final pass unshifts and compares, and one subtracts n by a mask. divW starts from the dividend's top words, ⌊X / 2^(63 (S - 1))⌋, S = v + 1 + ⌈(v + 1) / 63⌉, instead of 2v word steps from zero; R² mod n is the case X = 2^(128 w) with zero chunks. Both share Precompute.step and finish, and wordStep, wordStepU and addBack are gone. Target-independent arithmetic in Proof/Bignum/WordStepLazy.lean; the RISC-V steps in Proof/Bignum/RISCV64/PreR2Lazy.lean and the division's start and chunks in Proof/Rsa/RISCV64/KeyWordDiv.lean. divW_ok now takes w ≤ 256 and v + 1 zero words of [r], which its callers have. qemu instructions (2048/3072/4096): crt_values -11.2/-13.6/-15.1%, check_key -32.4/-33.7/-34.4%, private_precompute -10.2/-15.2/-17.7%. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
Conflicts only in the generated riscv64 crate (assembly/rsa.s, src/rsa.rs), against #4990's RISC-V check_key/crt_values p·q change; took main's side and regenerated every crate with the full emitter (Emit.lean --check passes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHESL7oGBqaM9Weq9ha
…the fused inverse Conflicts only in the generated riscv64 crate (assembly/rsa.s, src/rsa.rs); took the base branch's side and regenerated every crate with the full emitter (Emit.lean --check passes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHESL7oGBqaM9Weq9ha
…he lazy word division Not a squash double-apply: main's #4990 changed RISC-V check_key's p·q product to the primes' w/2 words, and this branch replaced the word division (divW_ok now takes w ≤ 256 and v + 1 zero words of [r]). Kept both by hand: - Proof/Rsa/RISCV64/KeyChkWord.lean: crtChecksW_ok takes this branch's hwl : g.w ≤ 128 and #4990's hpB/hqB bounds on p and q; its body (auto-merged) calls #4990's mulEqH_ok and threads hwl to mulModOne_ok. - Proof/Rsa/RISCV64/KeyChkCk.lean: ck_valid keeps #4990's e32/mulQH step and bounds, and passes this branch's hwl to crtChecksW_ok and dChecksW_ok. The generated riscv64 crate (assembly/rsa.s, src/rsa.rs) conflicted too; took the base branch's side and regenerated every crate with the full emitter (Emit.lean --check passes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHESL7oGBqaM9Weq9ha
Every conflict was #4987's squash against this branch's copy of the same commits; main's RISC-V Lean equals #4987's final head, on which this branch is built, so this branch's side is kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh
…divw-lazy Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh
|
This pull request has conflicts with Posted by |
main's RISC-V Lean equals #4991's final head, on which this branch is built; the generated RISC-V crate is this branch's, whose inputs are unchanged by the merge. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh
alex
marked this pull request as ready for review
November 12, 2026 22:48
alex
pushed a commit
that referenced
this pull request
Nov 12, 2026
…ds-pr main's RISCV64 Lean equals #4996's final head, on which this branch is built, so this branch's side is kept; the generated riscv64 crate is this branch's, whose inputs the merge leaves unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh
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.
It was stacked on #4991 (RISC-V fused inverse passes) and #4987, which have both merged; this PR now targets
main, withmainmerged in (64cdeb9).RISC-V's word division (
WordDiv.divW, used bycrt_values,check_keyand the key import's checks) and R² mod n (private_precompute) used to share a 64-bit step:divWstarted from zero and ran 2v steps, the first v − 1 of which only shifted.Both now use one lazy step,
Precompute.step, 63 bits at a time:WordStepLazy.lazy_step).mulSubwrites t already shifted up by 63 bits, with the next chunk below it. A step is 21 instructions per word, and building the next chunk takes 16.divWstarts from a shifted copy of the dividend's top words, ⌊X / 2^(63(S − 1))⌋, and runs S = v + 1 + ⌈(v + 1)/63⌉ steps instead of 2v. R² mod n is the case X = 2^(128w) with zero chunks, sodivWand R² share the start, the steps and the finish, and differ only in where the chunks come from.wordStep,wordStepU,addBack,mulSubUandstepsSetupare deleted. Every RISC-V word-division user goes throughPrecompute.step.Proofs
Proof/Bignum/WordStepLazy.lean(new, target-independent):dec_bounds,lazy_step,shr_or_shlandshr_words. The chunk recurrence is the existingWordDiv.wordDiv_step/wordDiv_digitsat W = 63.Proof/Bignum/RISCV64/PreR2Lazy.lean(new):mulSub_ok,step_ok(generic over how the chunk is made) andfinish_ok;steps_ok, the generic loop, with the invariant V = r·2^63 + chunk and r < 2n;r2Init_okandr2Words_ok.PreR2Wordskeeps the estimates.KeyWordDiv:divSetup_ok,copyWord_ok,divInit_ok,clamp_val,chunk_okanddivW_ok.divW_ok's contract is unchanged except for two new hypotheses: w ≤ 256 (for the exact ⌈(v + 1)/63⌉) and v + 1 zero words of [r]. Its callers already have both, threading w ≤ 128 fromPreC.w_bounds.divWRegsgains t1 and a5, which were already indwRegs.Constant time, by
taint_decide, for all six RISC-V RSA registrations.Trust
No change to
TCB/orSpec/. Incrates/verified-garbage-asm-riscv64, onlyrsa.{rs,s}change.Measurements
qemu-riscv64 instructions per call, #4991 (ccd0418) → this branch.
recover_primes_det, which does no word division, is identical and serves as the control.crt_valuescheck_keyprivate_precomputeWith an unbalanced 2048-bit key,
crt_valuesandcheck_keyare unchanged (that path does no word division) andprivate_precomputeis 1.4% faster.Benchmarks
Run 43194632598: riscv64, module
rsa, groupsrsa_from_.*, base ccd0418 (#4991's head), with OpenSSL and aws-lc-rs.rsa_from_crt(check_keyandprivate_precompute) is 17.3%, 20.9% and 21.2% faster at 2048, 3072 and 4096 bits.rsa_from_primes(crt_values,check_keyandprivate_precompute) is 14.7–16.4% faster, and 2.7–2.9× faster than OpenSSL.rsa_from_componentsis 0.9–1.7% faster. Its time is mostlyrecover_primes_det, which does no word division.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/512Checks
On 8f3aa40:
Emit.leanandEmit.lean --checkexit 0.check_lean_public_imports.pyon the edited modules.cargo test rsaon riscv64 (qemu) with Wycheproof passes with default CPU features and withVG_CPU_FEATURES=none.1000…,0111…, all ones and small; dividends just below k·n). Every R² mod n, dP, dQ and qInv was checked against Python. 2,232 more cases ran through the final emitted code, with no mismatch.main(64cdeb9), every RISC-V Lean file and the generatedverified-garbage-asm-riscv64crate are byte-identical to this branch's before the merge:main's RISC-V Lean equals RISC-V RSA: the batched inverse's updates fused into one pass each #4991's final head (0a5108c), on which this branch is built. The merge differs frommainonly in RISC-V files and this PR'sProof/Bignum/WordStepLazy.lean, and no TCB, emitter or lakefile file changed. The generated files are this branch's, not a text merge. CI rebuilds and runsEmit.lean --check.🤖 Generated with Claude Code
https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh