Repository navigation
RISC-V RSA key import: key checks by products and word-level division for balanced keys - #4982
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
alex
marked this pull request as ready for review
November 12, 2026 01:51
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
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 #4971 (x86-64), #4974 (AArch64), #4976 (PPC64LE) and #4979 (i686).
vg_rsa_check_keyandvg_rsa_crt_valueson RISC-V already checked p·q = n and q·qInv by products, but reducedd mod (p − 1)andd mod (q − 1)bit by bit (divL, 64w steps over ⌈p_len/8⌉ + 1 words, about 550k instructions each at 2048 bits), ande·dP,e·dQby a 33-stepmulMod. Those were 75% ofcheck_keyand 55% ofcrt_valuesat 2048 bits, and 92% ofcheck_keyat 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 bymulLowcompared with n, with no Montgomery n mod p (once the product equals n exactly, p ∣ n); q·qInv bymulLow, thendivWby p, compared with 1, which measured faster than the two Montgomery products it replaces; e·dP and e·dQ bymulLow, thendivWby p − 1 and q − 1, compared with 1; d mod (p − 1) and d mod (q − 1) bydivW.crt_values: dP and dQ bydivW.divWis[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;mulSubRestfactored 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.leanandWordStepNorm.lean, aremain'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
divWrestores from memory would make the later stores' pointers secret:crt_values'validis regrouped (same instructions) asseqs [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/orSpec/. Onlyvg_rsa_check_keyandvg_rsa_crt_valuesdiffer frommainin the generated crate.Measurements
qemu-riscv64 instructions per call, main → this branch:
check_keycrt_valuesWhat remains of
crt_valuesis 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, groupsrsa_from_.*, base 57d3b56 = main, with OpenSSL and aws-lc-rs):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/512from_crtis 76–77% faster andfrom_primes55–57% faster, now 1.1–1.3× faster than OpenSSL's. OpenSSL'sfrom_crtdoes not check the key asvg_rsa_check_keydoes, 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 onefrom_componentscall on the bench's 4096-bit key, base → head:vg_rsa_recover_primes_detvg_rsa_crt_valuesvg_rsa_check_keyvg_rsa_private_precomputeThe 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) becausecheck_keyandcrt_valuesgrew. 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,
main57d3b56 merged)Artifacts.Rsa.RISCV64with its constant-time proofs; the fullEmit.leanrewrote nothing after the merge;Emit.lean --checkexits 0.check_lean_public_imports.pyon the 7 edited modules.cargo test rsawith Wycheproof on x86_64, i686, aarch64, armv7, riscv64 and ppc64le (qemu): 0 failed;cargo fmt,clippy.main82dcd68 (after RSA PKCS#1 v1.5 decrypt: vg_rsa_pkcs1_decrypt_precomputed on every architecture #4959, AArch64: word-by-word reductions for RSA key import #4974 and x86-64 vg_ecdsa_p256_verify_ifma: Q's odd multiples kept as limbs #4983, none of which touches RISC-V key import).🤖 Generated with Claude Code
https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh