Repository navigation
RISC-V RSA: the batched inverse's updates fused into one pass each - #4991
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
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
This was referenced Nov 12, 2026
|
This pull request has conflicts with Posted by |
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
alex
marked this pull request as ready for review
November 12, 2026 21:48
alex
pushed a commit
that referenced
this pull request
Nov 12, 2026
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
pushed a commit
that referenced
this pull request
Nov 12, 2026
…ds-pr 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
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 RISC-V counterpart of the fused inverses on the other targets. It was stacked on #4987 (the 64-bit RISC-V inverse), which has merged; this PR now targets
main, withmainmerged in (0a5108c).Each batch of the inverse used to update f, g and d, e in several passes: four
linlinear combinations, twomredmultiply-accumulate loops, two shifts and two copies. Each update is now one in-place pass, in the style of libsecp256k1:updateG), about 50 instructions a word. It loads f_i and g_i once and builds u·f + v·g and q·f + r·g in two signed 128-bit accumulators (mulplusmulhu, less a precomputed sign mask & word). It stores word i−1 shifted by 59 from a carried register,(L_i << 5) | (L_{i−1} >> 59). This replaces twolinand two shift passes, about 76 instructions a word.updateAB), about 62 instructions a word. k₁ and k₂ = t₀·(−m⁻¹) come from the peeled word 0. Each word then runs the same two accumulators plus k·m_i, added by an unsigned carry chain, and is stored at word i−1. The division by 2^64 is the one-word offset. This replaces twolin, twomredand two copies, about 100 instructions a word.mred's (t + km)/2^64 and the shift's top word. SoIbInverse's invariant,InvLaw,BSandbatch_okare unchanged. OnlyupdateG_okandupdateAB_okare proved again, inIbLoops,IbOpsandIbInverse.Constant time
The taint analysis still accepts the inverse itself. The registers restored from arrays 11/12 come back tainted, and only code after the inverse reads them. So each caller cuts right after the inverse and pins those registers again from its correctness proof, as
crt_values' proof already does:RdCT(recover_primes_det) is taint-checked up to the factors. Its stores and restore are pinned fromDet'scb/ws, andRdVerifiedprovides the cut (pre_ok).KeyCT(key generation) cuts afterdPar, whosedEvenruns an inverse, and aftercrtPart, each re-pinned fromKE.Key/Verifiedprovides the cuts fromBody's newdFront_okandcrtFront_ok, andGenerate/CTuses the newkey_ct_full.These cuts depend only on the inverse's correctness contract (
Keep kRegs), which is unchanged, so a later change inside the inverse touches none of them.Trust
No change to
TCB/orSpec/. Only the riscv64rsa.{rs,s}andinternal/vg_rsa_keygen_key.{rs,s}change.Measurements
qemu-riscv64 instructions per call, db7045a (#4987) → this branch:
crt_valuescrt_valuescrt_valuescrt_valuesrecover_primes_detrecover_primes_detrecover_primes_detrecover_primes_det's gcd runs only the FG pass.check_keydoesn't run the inverse, so it is unchanged.Benchmarks
Run 43164437857 (riscv64, module
rsa, groupsrsa_from_.*, base db7045a = #4987's head, with OpenSSL and aws-lc-rs):rsa_from_primes(onecrt_values) is 7.0–10.3% faster, now 2.4–2.5× faster than OpenSSL.rsa_from_componentsis 0.5–2.6% faster.rsa_from_crtruns no changed code. It is 0.3–2.8% faster, which is noise or placement.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 ccd0418, one commit on db7045a:
Emit.leanchanges only the riscv64 files above, andEmit.lean --checkexits 0.check_lean_public_imports.py --base origin/claude/riscv64-inv64on the edited modules.cargo test --release --features cpu-features-env rsapasses (46 + 1 + 18), both by default and withVG_CPU_FEATURES=none.main(0a5108c), every RISC-V Lean file and the generatedverified-garbage-asm-riscv64crate are byte-identical to ef2a003's, this branch's head before the merge. The merge differs frommainonly in RISC-V files, and no TCB, emitter or lakefile file changed. A text merge of the generated files mixed both sides'vg_rsa_recover_primes_det, so they were taken whole from ef2a003. CI rebuilds and runsEmit.lean --check.🤖 Generated with Claude Code
https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh