Skip to content

RISC-V RSA: the batched inverse's updates fused into one pass each - #4991

Merged
alex merged 21 commits into
mainfrom
claude/riscv64-inv-fused
Nov 12, 2026
Merged

alex merged 21 commits into
mainfrom
claude/riscv64-inv-fused

Conversation

@alex

@alex alex commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

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, with main merged in (0a5108c).

Each batch of the inverse used to update f, g and d, e in several passes: four lin linear combinations, two mred multiply-accumulate loops, two shifts and two copies. Each update is now one in-place pass, in the style of libsecp256k1:

  • FG pass (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 (mul plus mulhu, 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 two lin and two shift passes, about 76 instructions a word.
  • DE pass (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 two lin, two mred and two copies, about 100 instructions a word.
  • The passes produce exactly the values the old code did: mred's (t + km)/2^64 and the shift's top word. So IbInverse's invariant, InvLaw, BS and batch_ok are unchanged. Only updateG_ok and updateAB_ok are proved again, in IbLoops, IbOps and IbInverse.
  • The passes use more registers than the inverse may clobber. About 15 are saved to arrays 11 and 12 and restored around each update.

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 from Det's cb/ws, and RdVerified provides the cut (pre_ok).
  • KeyCT (key generation) cuts after dPar, whose dEven runs an inverse, and after crtPart, each re-pinned from KE. Key/Verified provides the cuts from Body's new dFront_ok and crtFront_ok, and Generate/CT uses the new key_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/ or Spec/. Only the riscv64 rsa.{rs,s} and internal/vg_rsa_keygen_key.{rs,s} change.

Measurements

qemu-riscv64 instructions per call, db7045a (#4987) → this branch:

function key #4987 this branch change
crt_values 2048 336,155 280,305 −16.6%
crt_values 3072 642,827 522,873 −18.7%
crt_values 4096 1,044,987 836,737 −19.9%
crt_values unbalanced 2048 1,599,942 1,537,402 −3.9%
recover_primes_det 2048 8,347,880 8,260,010 −1.1%
recover_primes_det 3072 17,527,848 17,336,234 −1.1%
recover_primes_det 4096 30,068,584 29,733,290 −1.1%

recover_primes_det's gcd runs only the FG pass. check_key doesn't run the inverse, so it is unchanged.

Benchmarks

Run 43164437857 (riscv64, module rsa, groups rsa_from_.*, base db7045a = #4987's head, with OpenSSL and aws-lc-rs):

  • rsa_from_primes (one crt_values) is 7.0–10.3% faster, now 2.4–2.5× faster than OpenSSL.
  • rsa_from_components is 0.5–2.6% faster.
  • rsa_from_crt runs no changed code. It is 0.3–2.8% faster, which is noise or placement.
Benchmark Base Head Change OpenSSL Head vs OpenSSL aws-lc-rs Head vs aws-lc-rs
rsa_from_components/256 2.22 ms 2.20 ms 0.5% faster 30.26 ms 13.7× faster – –
rsa_from_components/384 4.65 ms 4.53 ms 2.6% faster 103.58 ms 22.9× faster – –
rsa_from_components/512 7.86 ms 7.69 ms 2.2% faster 208.20 ms 27.1× faster – –
rsa_from_crt/256 97.58 µs 96.01 µs 1.6% faster 9.06 µs 10.6× slower 894.13 µs 9.3× faster
rsa_from_crt/384 179.16 µs 178.58 µs 0.3% faster 10.58 µs 16.9× slower 1.84 ms 10.3× faster
rsa_from_crt/512 291.37 µs 283.34 µs 2.8% faster 12.33 µs 23.0× slower 3.03 ms 10.7× faster
rsa_from_primes/256 210.50 µs 195.83 µs 7.0% faster 482.00 µs 2.5× faster – –
rsa_from_primes/384 386.67 µs 355.97 µs 7.9% faster 889.07 µs 2.5× faster – –
rsa_from_primes/512 618.47 µs 554.91 µs 10.3% faster 1.33 ms 2.4× faster – –

Checks

On ccd0418, one commit on db7045a:

  • Every registration builds. The full Emit.lean changes only the riscv64 files above, and Emit.lean --check exits 0.
  • Every Static-checks step passes, and so does check_lean_public_imports.py --base origin/claude/riscv64-inv64 on the edited modules.
  • Under qemu, riscv64 cargo test --release --features cpu-features-env rsa passes (46 + 1 + 18), both by default and with VG_CPU_FEATURES=none.
  • After the merge with main (0a5108c), every RISC-V Lean file and the generated verified-garbage-asm-riscv64 crate are byte-identical to ef2a003's, this branch's head before the merge. The merge differs from main only 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 runs Emit.lean --check.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh

…offsets

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
…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
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
Base automatically changed from claude/riscv64-inv64 to main November 12, 2026 21:35
@github-actions github-actions Bot removed the untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. label Nov 12, 2026
@github-actions

Copy link
Copy Markdown

This pull request has conflicts with main, found after dae9c3c landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

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
@github-actions github-actions Bot added the untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. label Nov 12, 2026
@alex
alex marked this pull request as ready for review November 12, 2026 21:48
@alex
alex added this pull request to the merge queue Nov 12, 2026
Merged via the queue into main with commit 0e01d1c Nov 12, 2026
22 checks passed
@alex
alex deleted the claude/riscv64-inv-fused branch November 12, 2026 21:56
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified.

Development

Successfully merging this pull request may close these issues.

2 participants