Skip to content

RISC-V RSA key import: key checks by products and word-level division for balanced keys - #4982

Merged
alex merged 11 commits into
mainfrom
claude/riscv64-rsa-key-import-words
Nov 12, 2026
Merged

alex merged 11 commits into
mainfrom
claude/riscv64-rsa-key-import-words

Conversation

@alex

@alex alex commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

The RISC-V counterpart of #4971 (x86-64), #4974 (AArch64), #4976 (PPC64LE) and #4979 (i686). vg_rsa_check_key and vg_rsa_crt_values on RISC-V already checked p·q = n and q·qInv by products, but reduced d mod (p − 1) and d mod (q − 1) bit by bit (divL, 64w steps over ⌈p_len/8⌉ + 1 words, about 550k instructions each at 2048 bits), and e·dP, e·dQ by a 33-step mulMod. Those were 75% of check_key and 55% of crt_values at 2048 bits, and 92% of check_key at 4096.

Guard (public): p_len % 8 = 0, p_len = q_len = 4w, p_len ≥ 32. Every other key keeps the bit-serial code; the guard and branch cost 11 instructions there. Rust trims p and q (primes() → trim()), so ordinary keys take the word path.

Balanced path:

  • check_key: p·q = n by mulLow compared with n, with no Montgomery n mod p (once the product equals n exactly, p ∣ n); q·qInv by mulLow, then divW by p, compared with 1, which measured faster than the two Montgomery products it replaces; e·dP and e·dQ by mulLow, then divW by p − 1 and q − 1, compared with 1; d mod (p − 1) and d mod (q − 1) by divW.
  • crt_values: dP and dQ by divW.

divW is [r] := [a] mod [m] from RISC-V's proven 3-by-2 word step (Proof/Bignum/RISCV64/PreR2Words.lean: normTail, recip, quot, quot3, mulSub, addBack), generalized to an incoming word u (x := (x·2^64 + u) mod m, wordStepU_ok; mulSubRest factored out). The public precompute passes u = 0, and its emitted code is byte-identical. The divisor is copied to a temporary with bit 63 of its top word ORed in (fixTop), so the divisor array is never modified and the time is the same whether p·q = n or not; when p·q ≠ n, the mask already fails. The shared fold and lemmas, Proof/Bignum/WordDiv.lean and WordStepNorm.lean, are main's (from #4979), unchanged.

Constant time

The guard reads only the public lengths. d, p, q, dP, dQ, qInv and everything derived from them are secret on both paths. RISC-V's taint analysis treats loaded words as secret, so the nine registers divW restores from memory would make the later stores' pointers secret: crt_values' valid is regrouped (same instructions) as seqs [front, dPartsAll, stores], and the constant-time proof cuts after the divisions and re-pins those registers as public from the functional proof.

Trust

No change to TCB/ or Spec/. Only vg_rsa_check_key and vg_rsa_crt_values differ from main in the generated crate.

Measurements

qemu-riscv64 instructions per call, main → this branch:

key check_key crt_values
2048 1,460,291 → 154,258 (9.5×) 2,009,714 → 952,168 (2.1×)
3072 2,878,931 → 311,906 (9.2×) 4,191,474 → 1,988,728 (2.1×)
4096 4,760,291 → 525,618 (9.1×) 7,159,666 → 3,401,608 (2.1×)
2048, unbalanced (1088/960-bit primes, fallback) 1,458,291 → 1,458,302 2,110,288 → 2,110,299

What remains of crt_values is mostly the batch inverse; its doubling tail is the next step (as #4980 on x86-64 and #4981 on AArch64).

Benchmarks

Run 43134124541 (riscv64, module rsa, groups rsa_from_.*, base 57d3b56 = main, with OpenSSL and aws-lc-rs):

Benchmark Base Head Change OpenSSL Head vs OpenSSL aws-lc-rs Head vs aws-lc-rs
rsa_from_components/256 3.06 ms 2.53 ms 17.4% faster 30.30 ms 12.0× faster – –
rsa_from_components/384 6.41 ms 5.26 ms 17.9% faster 103.76 ms 19.7× faster – –
rsa_from_components/512 10.72 ms 11.90 ms 11.0% slower 209.98 ms 17.6× faster – –
rsa_from_crt/256 412.23 µs 98.51 µs 76.1% faster 9.86 µs 10.0× slower 908.84 µs 9.2× faster
rsa_from_crt/384 777.65 µs 182.07 µs 76.6% faster 11.41 µs 16.0× slower 1.86 ms 10.2× faster
rsa_from_crt/512 1.28 ms 291.09 µs 77.3% faster 13.68 µs 21.3× slower 3.09 ms 10.6× faster
rsa_from_primes/256 919.08 µs 410.04 µs 55.4% faster 551.18 µs 1.3× faster – –
rsa_from_primes/384 1.90 ms 803.49 µs 57.7% faster 898.90 µs 1.1× faster – –
rsa_from_primes/512 3.17 ms 1.35 ms 57.4% faster 1.50 ms 1.1× faster – –

from_crt is 76–77% faster and from_primes 55–57% faster, now 1.1–1.3× faster than OpenSSL's. OpenSSL's from_crt does not check the key as vg_rsa_check_key does, so that row is not like for like; aws-lc-rs, which does, is 9–11× slower.

from_components/512 (+11%) is not more work. qemu instruction counts for one from_components call on the bench's 4096-bit key, base → head:

function 57d3b56 307f394
vg_rsa_recover_primes_det 31,476,324 31,476,324 (identical)
vg_rsa_crt_values 7,159,666 3,401,608
vg_rsa_check_key 4,760,291 525,618
vg_rsa_private_precompute 382,306 382,306
whole call 44,410,176 36,417,428 (−18.0%)

The key takes the balanced path, vg_rsa_recover_primes_det's emitted code is byte-identical between the two revisions, and it calls no other verified function. The only difference is its placement: it moved from 0x59224 to 0x5aee0 (cache-line offset 36 → 32) because check_key and crt_values grew. It is 86% of the call at 4096 bits, so code alignment on the runner's core, or runner variance, is the likely cause; the 2048- and 3072-bit rows improved 17–18%, in line with their counts. The benchmark was not rerun.

Checks (on 307f394, main 57d3b56 merged)

🤖 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
@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 01:51
@alex
alex added this pull request to the merge queue Nov 12, 2026
Merged via the queue into main with commit 2ae2495 Nov 12, 2026
25 checks passed
@alex
alex deleted the claude/riscv64-rsa-key-import-words branch November 12, 2026 02:10
alex pushed a commit that referenced this pull request Nov 12, 2026
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
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