Skip to content

RISC-V RSA: lazy long division, 63 bits a step, for every word-division user - #4996

Merged
alex merged 25 commits into
mainfrom
claude/riscv64-divw-lazy
Nov 12, 2026
Merged

alex merged 25 commits into
mainfrom
claude/riscv64-divw-lazy

Conversation

@alex

@alex alex commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

It was stacked on #4991 (RISC-V fused inverse passes) and #4987, which have both merged; this PR now targets main, with main merged in (64cdeb9).

RISC-V's word division (WordDiv.divW, used by crt_values, check_key and the key import's checks) and R² mod n (private_precompute) used to share a 64-bit step:

  • the remainder was reduced to [0, n) at each step, by an add-back pass whenever the estimate was one too large;
  • divW started 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:

  • Lazy remainder. V = r·2^63 + chunk, with r kept in [0, 2n) rather than reduced. The existing 3-by-2 estimate is lowered by one, so t = V − q̂·n never goes negative and needs no add-back (WordStepLazy.lazy_step).
  • Fused shift. mulSub writes 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.
  • One finish. One pass unshifts and compares (fused with the r ≥ n borrow), and one subtracts n under a mask. That replaces three passes.
  • Shifted start. divW starts 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, so divW and R² share the start, the steps and the finish, and differ only in where the chunks come from.

wordStep, wordStepU, addBack, mulSubU and stepsSetup are deleted. Every RISC-V word-division user goes through Precompute.step.

Proofs

  • Proof/Bignum/WordStepLazy.lean (new, target-independent): dec_bounds, lazy_step, shr_or_shl and shr_words. The chunk recurrence is the existing WordDiv.wordDiv_step/wordDiv_digits at W = 63.

  • Proof/Bignum/RISCV64/PreR2Lazy.lean (new):

    • mulSub_ok, step_ok (generic over how the chunk is made) and finish_ok;
    • steps_ok, the generic loop, with the invariant V = r·2^63 + chunk and r < 2n;
    • r2Init_ok and r2Words_ok.

    PreR2Words keeps the estimates.

  • KeyWordDiv: divSetup_ok, copyWord_ok, divInit_ok, clamp_val, chunk_ok and divW_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 from PreC.w_bounds.
    • divWRegs gains t1 and a5, which were already in dwRegs.
  • Constant time, by taint_decide, for all six RISC-V RSA registrations.

Trust

No change to TCB/ or Spec/. In crates/verified-garbage-asm-riscv64, only rsa.{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.

function 2048 3072 4096
crt_values 280,305 → 248,859 (−11.2%) 522,873 → 451,731 (−13.6%) 836,737 → 710,155 (−15.1%)
check_key 154,258 → 104,326 (−32.4%) 311,906 → 206,670 (−33.7%) 525,618 → 344,882 (−34.4%)
private_precompute 113,458 → 101,848 (−10.2%) 228,106 → 193,336 (−15.2%) 382,306 → 314,648 (−17.7%)

With an unbalanced 2048-bit key, crt_values and check_key are unchanged (that path does no word division) and private_precompute is 1.4% faster.

Benchmarks

Run 43194632598: riscv64, module rsa, groups rsa_from_.*, base ccd0418 (#4991's head), with OpenSSL and aws-lc-rs.

  • rsa_from_crt (check_key and private_precompute) is 17.3%, 20.9% and 21.2% faster at 2048, 3072 and 4096 bits.
  • rsa_from_primes (crt_values, check_key and private_precompute) is 14.7–16.4% faster, and 2.7–2.9× faster than OpenSSL.
  • rsa_from_components is 0.9–1.7% faster. Its time is mostly recover_primes_det, which does no word division.
Benchmark Base Head Change OpenSSL Head vs OpenSSL aws-lc-rs Head vs aws-lc-rs
rsa_from_components/256 2.20 ms 2.16 ms 1.7% faster 30.64 ms 14.2× faster – –
rsa_from_components/384 4.61 ms 4.55 ms 1.1% faster 103.44 ms 22.7× faster – –
rsa_from_components/512 7.73 ms 7.66 ms 0.9% faster 209.31 ms 27.3× faster – –
rsa_from_crt/256 96.08 µs 79.43 µs 17.3% faster 9.05 µs 8.8× slower 899.01 µs 11.3× faster
rsa_from_crt/384 181.09 µs 143.23 µs 20.9% faster 10.50 µs 13.6× slower 1.89 ms 13.2× faster
rsa_from_crt/512 287.51 µs 226.49 µs 21.2% faster 12.54 µs 18.1× slower 3.06 ms 13.5× faster
rsa_from_primes/256 195.21 µs 166.46 µs 14.7% faster 480.98 µs 2.9× faster – –
rsa_from_primes/384 359.73 µs 306.81 µs 14.7% faster 878.35 µs 2.9× faster – –
rsa_from_primes/512 570.70 µs 477.25 µs 16.4% faster 1.29 ms 2.7× faster – –

Checks

On 8f3aa40:

  • The six RISC-V RSA registrations (Rsa, RsaKeyGen, RsaOaep, RsaPkcs1Enc, RsaPkcs1Sig and RsaPss) build, constant-time proofs included.
  • The full Emit.lean and Emit.lean --check exit 0.
  • Every step of the Static checks passes, and so does check_lean_public_imports.py on the edited modules.
  • cargo test rsa on riscv64 (qemu) with Wycheproof passes with default CPU features and with VG_CPU_FEATURES=none.
  • Before proving, the prototype gave byte-identical results to the old division on 7,357 adversarial cases (divisor top words 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.
  • After the merge with main (64cdeb9), every RISC-V Lean file and the generated verified-garbage-asm-riscv64 crate 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 from main only in RISC-V files and this PR's Proof/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 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
…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
@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
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
Base automatically changed from claude/riscv64-inv-fused to main November 12, 2026 21:56
@github-actions

Copy link
Copy Markdown

This pull request has conflicts with main, found after 0e01d1c 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.

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
alex marked this pull request as ready for review November 12, 2026 22:48
@alex
alex added this pull request to the merge queue Nov 12, 2026
Merged via the queue into main with commit 9a5ee10 Nov 12, 2026
23 checks passed
@alex
alex deleted the claude/riscv64-divw-lazy branch November 12, 2026 23:12
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
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