Skip to content

RISC-V: reduce Montgomery and private RSA overhead - #4514

Merged
reaperhulk merged 4 commits into
mainfrom
perf/riscv-rsa-private
Nov 7, 2026
Merged

reaperhulk merged 4 commits into
mainfrom
perf/riscv-rsa-private

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Private RSA repeatedly copies whole table entries while selecting each exponent window and serializes Montgomery product work. Scan all 16 entries for each output word and store that word once, schedule independent mulhu/mul pairs high first in FIOS multiplication, and pipeline squaring pairs using only the caller-saved a4 temporary. The square schedule overlaps the next low product with the current carry chain without stack spills or additional instructions.

Extend the row and window-selection proofs through the existing RSA correctness, memory-safety, ABI, and constant-time contracts. Regenerate all RISC-V consumers of the shared Montgomery routines, including DSA and Miller–Rabin. No specification, ISA model, workspace size, or stack allocation changes.

Native cached-key PKCS#1 SHA-256 signing and verification (benchmark run and raw estimates), comparing 85a4ddeb333c (#4500) with 19c5f7f859fe:

Operation RSA bits Base This PR Time reduction
Sign 2048 5.180 ms 4.985 ms 3.8%
Sign 3072 15.338 ms 14.811 ms 3.4%
Sign 4096 33.173 ms 32.076 ms 3.3%
Verify 2048 102.91 µs 100.65 µs 2.2%
Verify 3072 220.73 µs 213.83 µs 3.1%
Verify 4096 382.74 µs 372.62 µs 2.6%

The table uses the fastest Criterion median of three interleaved runs per revision on the same native riscv64gc-unknown-linux-gnu runner (1.848 GHz, Rust 1.99.0). Every paired round improved: signing reductions ranged from 2.8–4.7%, verification from 1.5–4.0%. These are single-thread operation timings. This square schedule uses fewer temporaries than the earlier prototype; the numbers above measure the proved implementation itself.

Reproduce:

gh workflow run bench.yml --ref perf/riscv-rsa-private \
  -f base_commit=85a4ddeb333cc681b014065ed2130ba78d9da2d5 \
  -f arches=riscv64 -f modules=rsa_pkcs1_sig \
  -f 'groups=rsa_pkcs1_(sign|verify)' -f rounds=3

Validation: full lake build passed after rebasing (28,606 jobs); all static checks and 660 CI unit tests passed; import analysis passed; complete RISC-V emission passed the standard axiom/compiler/spec-origin audits and byte-for-byte --check. Native RISC-V Rust tests and both Rust lint jobs passed on the benchmarked revision; CI is rerunning after the rebase.

#4500 has merged. This PR now targets main and contains only the Montgomery/private-RSA changes. Rebased head 40ab9d865482 has byte-identical generated RISC-V assembly to the benchmarked 19c5f7f859fe; the complete emitter audit and byte-for-byte check passed again after the rebase.

@github-actions github-actions Bot added the untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. label Nov 7, 2026
Base automatically changed from perf/riscv-rsa-public-square to main November 7, 2026 18:18
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

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

@reaperhulk
reaperhulk force-pushed the perf/riscv-rsa-private branch from 19c5f7f to 40ab9d8 Compare November 7, 2026 18:39
@reaperhulk
reaperhulk enabled auto-merge November 7, 2026 18:41
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit 24a878b Nov 7, 2026
23 checks passed
@reaperhulk
reaperhulk deleted the perf/riscv-rsa-private branch November 7, 2026 19:10
alex pushed a commit that referenced this pull request Nov 7, 2026
Resolves the conflict with #4508: src/dsa/parameters/search.rs and its
tests take main's dispatchers, and riscv64 joins search.rs's module
gate. The RISC-V crate is regenerated with the full emitter, which picks
up #4514's Montgomery code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGhkN8REQLPW8yxN5A
alex pushed a commit that referenced this pull request Nov 7, 2026
Main removed declarations only this branch still used (#4336, #4367,
#4514): `fin64_exec_li_lit`, the RISC-V Miller–Rabin search pieces the
sieved parameter candidate calls, and three of `ModExpWin`'s WP lemmas.
They return, unchanged, as FFDH-only modules (`Proof/Ffdh/RISCV64/Li.lean`,
`Impl/Ffdh/RISCV64/Search.lean`, `Proof/Ffdh/RISCV64/Search.lean`) and
local lemmas in `Cached/Rows.lean`. The registrations drop `consts :=`
(#4380). The generated code takes #4514's Montgomery schedule and #4468's
dispatcher.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHEKNw8eCDhgbwoFBL
alex pushed a commit that referenced this pull request Nov 15, 2026
Main removed declarations only this branch still used (#4336, #4367,
#4514): `fin64_exec_li_lit`, the RISC-V Miller–Rabin search pieces the
sieved parameter candidate calls, and three of `ModExpWin`'s WP lemmas.
They return, unchanged, as FFDH-only modules (`Proof/Ffdh/RISCV64/Li.lean`,
`Impl/Ffdh/RISCV64/Search.lean`, `Proof/Ffdh/RISCV64/Search.lean`) and
local lemmas in `Cached/Rows.lean`. The registrations drop `consts :=`
(#4380). The generated code takes #4514's Montgomery schedule and #4468's
dispatcher.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHEKNw8eCDhgbwoFBL
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.

1 participant