Repository navigation
RSA key import: the p·q = n product over the primes' words on every architecture but ARMv7 - #4990
Merged
Merged
Conversation
mulHalf runs mulWide's rows over the low halves of p and q, from 2w words zero, where the guard (WordModRt.balanced) holds; mulWide otherwise. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
start computes the guard (WordModRt.balanced) before the product and runs mulHalf where it holds, mulWide otherwise. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
The product runs ⌈p_len/8⌉ rows of ⌈q_len/8⌉ words, as AArch64's crt_values does: p < 256^p_len and q < 256^q_len, so the rest of the 2w+2 cleared words stay zero. check_key gets its own mulPQ; mulXR, which the q·qInv products use, is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
As AArch64's crt_values already does: mulPQ runs ⌈p_len/8⌉ rows of ⌈q_len/8⌉ words; mulXR, which the q·qInv product uses, is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
…lanced key RecoverDet.mulHalf: w/2 rows of w/2 words of [x], each row's carry stored to the word above it, so [o] holds the whole product, from [o] = 0. check_key's word path (under shape) and crt_values' product (pqProduct, branching on shape) use it; mulLow stays for the other keys. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
…balanced key MontKey.mulHalf: rows of ⌈len/4⌉ digits of [y], each row's carry stored to the digit above it, so [o] holds the whole product. check_key's nModP and crt_values' arith branch on shape before the product. crt_values also leaves out Montgomery's n mod (p | 1) for a balanced key, as check_key does: there p q is exact in w digits, so p q mod 2^(32 w) = n already gives n mod p = 0 (MontCheckMath.maskM_iff now takes M's iff under L = n). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFdT7ugYMSPA3JMuhh
alex
marked this pull request as ready for review
November 12, 2026 06:13
alex
pushed a commit
that referenced
this pull request
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
alex
pushed a commit
that referenced
this pull request
Nov 12, 2026
The full Emit.lean on the merge of main (with #4990) and this branch; Emit.lean --check exits 0. 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
Merges origin/main (c7f68f6), including #4990 (the p·q = n product over the primes' words), into the batched PPC64LE inverse branch. Conflicts: only the generated crates/verified-garbage-asm-powerpc64le/ {assembly/rsa.s,src/rsa.rs}; took the branch's side and regenerated every crate with the full emitter (Emit.lean; --check exits 0). The Lean files auto-merged, but #4990 gave partA_ok three more hypotheses (h22, hqw, hQ). This branch's new arithAB_ok in Proof/Rsa/PPC64LE/CvArith.lean still called it with the old ones. It now passes them, the same change #4990 made to arith_ok's call; it already had those hypotheses. No statement changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGgaB288H4oFvukrsY
alex
pushed a commit
that referenced
this pull request
Nov 12, 2026
Merges origin/main (c7f68f6), which contains #4990 (squash-merged as f36e35a; this branch was stacked on it) and #4995 (x86-64 fused inverse). Squash double-apply: for each file below, main's version is byte-identical to #4990's head (6c4ad41), so the branch's version is taken: - lean/VerifiedGarbage/Proof/Rsa/X86/KcWord.lean (conflicted) - Impl/Rsa/{X86_64,AArch64}/CheckKeyCore.lean, Proof/Rsa/AArch64/{CkCT,CkPieces,CkSteps}.lean, Proof/Rsa/X86_64/{KeyCTPieces,KeyChecks,KeySteps}.lean (auto-merged without conflict, but with #4990's definitions duplicated, e.g. mulPQ) Impl/Rsa/X86_64/Keys.lean auto-merged, keeping both this branch's changes and #4995's. The conflicting generated files (rsa.s and rsa.rs of the x86, x86_64 and powerpc64le crates) and every other crate were regenerated with the full emitter (Emit.lean); Emit.lean --check passes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGGAywWt7REP7CHZ6u
alex
pushed a commit
that referenced
this pull request
Nov 12, 2026
…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
alex
pushed a commit
that referenced
this pull request
Nov 12, 2026
Merges origin/main (c7f68f6), including #4990 (the p·q = n product over the primes' words, which changed the AArch64 check_key and the generated aarch64 rsa crate), into the AArch64 batched inverse branch. No conflicts. The full lake build passes, and the full emitter (Emit.lean) regenerated every crate with no change from the merge: --check exits 0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHGgaB288H4oFvukrsY
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.
vg_rsa_check_keyandvg_rsa_crt_valuescheckp·q = nwith a product. On several architectures that product ran over all w words of each factor, though p and q have only w/2 words in a balanced key. This PR sizes it to the primes on i686, x86-64, AArch64 (check_key), RISC-V and PPC64LE.On PPC64LE,
crt_valuesalso stops computingn mod (p|1)by Montgomery multiplication for balanced keys, ascheck_keyalready does. It was ~50k instructions per call.ARMv7 is covered by #4986, which replaces its bit-serial
n / pwith a product compare and word division.By architecture
crt_values(WordModRt.mulHalf): the rows ofmulWideover the primes' low w/2 words.zeroWide, thentopZerofor the last word of array o.mulRowgains ahalfflag; withhalf = falseits code is unchanged.firstDivbranches on the guard it has just computed,ifBalanced sG mulHalf mulWide.check_key:startcomputesbalancedbefore the product and takes the same branch.crtCheckscomputes the guard again (~40 instructions).crt_valuespqCheckalready has. It needs no guard, so unbalanced keys gain too.check_keygets its ownmulPQ.mulXR, used by the q·qInv products, is unchanged.check_keygets the same length-sizedmulPQ.crt_valuesalready had it and is untouched.RecoverDet.mulHalf: w/2 rows of w/2 words, each row storing its carry word.check_key's word path, which already runs undershape, uses it.crt_values,mulChecksnow callspqProduct, which branches onshapebetweenmulHalfandmulLow.shape's definition moved abovemulChecksinImpl/Rsa/RISCV64/CrtValues.lean; its text is unchanged.MontKey.mulHalf, storing its carry the same way.check_key'snModPholds the product:mulHalfundershape,mulLootherwise.crt_values'arithnow doesshape → (mulHalf | mulLo), and latershape → (aDT := 0 | p|1 and montMul). So a balanced key skips Montgomery'sn mod (p|1), as incheck_key. In the proof,MontCheckMath.maskM_iff'shMnow assumesL = n, andpartA'ssE2iff holds underp q mod 2^(32w) = n. Its only user isCvArith.Proofs
mulHalf_ok(Mul.lean). The key fact is p < 2^(8·ap) = 2^(32·w/2), so p is its own low w/2 words.firstDiv_okgainspB/qB, whichcore_okandTimeget fromInvGate.rowsFrom_relCTgeneralizesmulRows_relCT, andmulHalf_relCTis new.firstDiv_relCTruns block → guard →RelCT.iteonzf, which is equal in both runs because n is.CvCkgains two taint checks.check_key:KcStart,KcWord.Stg.mulHalf_ok,KcTime.start_relCT, and two new taint checks inKcCk.Trust
No change to
TCB/orSpec/. The generatedrsa.{rs,s}change for x86, x86_64, aarch64, riscv64 and powerpc64le.Measurements
Instructions per call. x86 and x86-64 are callgrind, inclusive. AArch64, RISC-V and PPC64LE are qemu, counting instructions inside each function's own symbol range, callees excluded. Base f3525be → this branch:
crt_valuescheck_keycrt_valuescheck_keycheck_keycheck_keycrt_valuescheck_keycrt_valuesAArch64's
crt_valuescount is identical to main's (225,003 at 2048), as it should be: its code is unchanged.Benchmarks
Run 43163569742 (x86, x86_64, aarch64, riscv64 and powerpc64; module
rsa, groupsrsa_from_.*; base f3525be; with OpenSSL and aws-lc-rs). The tables below are each architecture's default configuration. The run's otherVG_CPU_FEATURESjobs are linked from it. In the tables,from_crtrunscheck_keyandfrom_primesrunscrt_values.rsa_from_crtrsa_from_primesrsa_from_componentsPPC64LE
rsa_from_crt/384shows 27.2% faster, but that is the base's outlier, not this change. Its base is 82.26 µs against 33.79 µs at 256 and 94.45 µs at 512, while the head's 59.89 µs fits the curve.x86 (i686):
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/512x86_64:
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/512aarch64:
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/512riscv64:
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/512powerpc64:
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/512Checks
On 6c4ad41 (8 commits on main f3525be):
lake buildof every default target exits 0.Emit.lean, run with every crate reset to main first, changes only the five architectures'rsa.{rs,s}.Emit.lean --checkexits 0.run_unit_testspass, and so doescheck_lean_public_imports.py --base origin/main.rsatests (lib, the tests binary and CAVP) pass:VG_CPU_FEATURES=none;Against the other open RSA PRs, only generated files conflict. With #4989 (i686) it is the x86
rsa.{rs,s}, with #4987 (RISC-V) the riscv64 ones, and with #4992 (AArch64) the aarch64 ones. Whichever merges second is regenerated with the fullEmit.leanafter merging main.🤖 Generated with Claude Code
https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh