Skip to content

RSA key import: the p·q = n product over the primes' words on every architecture but ARMv7 - #4990

Merged
alex merged 8 commits into
mainfrom
claude/x86-pq-half
Nov 12, 2026
Merged

alex merged 8 commits into
mainfrom
claude/x86-pq-half

Conversation

@alex

@alex alex commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

vg_rsa_check_key and vg_rsa_crt_values check p·q = n with 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_values also stops computing n mod (p|1) by Montgomery multiplication for balanced keys, as check_key already does. It was ~50k instructions per call.

ARMv7 is covered by #4986, which replaces its bit-serial n / p with a product compare and word division.

By architecture

  • i686:
    • crt_values (WordModRt.mulHalf): the rows of mulWide over the primes' low w/2 words.
      • It starts from all 2w words zero: zeroWide, then topZero for the last word of array o.
      • mulRow gains a half flag; with half = false its code is unchanged.
      • firstDiv branches on the guard it has just computed, ifBalanced sG mulHalf mulWide.
    • check_key: start computes balanced before the product and takes the same branch. crtChecks computes the guard again (~40 instructions).
  • x86-64:
    • Both functions now run ⌈p_len/8⌉ rows of ⌈q_len/8⌉ words, the shape AArch64's crt_values pqCheck already has. It needs no guard, so unbalanced keys gain too.
    • check_key gets its own mulPQ. mulXR, used by the q·qInv products, is unchanged.
  • AArch64: check_key gets the same length-sized mulPQ. crt_values already had it and is untouched.
  • RISC-V:
    • A new RecoverDet.mulHalf: w/2 rows of w/2 words, each row storing its carry word.
    • check_key's word path, which already runs under shape, uses it.
    • In crt_values, mulChecks now calls pqProduct, which branches on shape between mulHalf and mulLow. shape's definition moved above mulChecks in Impl/Rsa/RISCV64/CrtValues.lean; its text is unchanged.
  • PPC64LE:
    • A new MontKey.mulHalf, storing its carry the same way. check_key's nModP holds the product: mulHalf under shape, mulLo otherwise.
    • crt_values' arith now does shape → (mulHalf | mulLo), and later shape → (aDT := 0 | p|1 and montMul). So a balanced key skips Montgomery's n mod (p|1), as in check_key. In the proof, MontCheckMath.maskM_iff's hM now assumes L = n, and partA's sE2 iff holds under p q mod 2^(32w) = n. Its only user is CvArith.

Proofs

  • i686:
    • 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_ok gains pB/qB, which core_ok and Time get from InvGate.
    • Constant time: rowsFrom_relCT generalizes mulRows_relCT, and mulHalf_relCT is new. firstDiv_relCT runs block → guard → RelCT.ite on zf, which is equal in both runs because n is. CvCk gains two taint checks.
  • i686 check_key: KcStart, KcWord.Stg.mulHalf_ok, KcTime.start_relCT, and two new taint checks in KcCk.
  • x86-64, AArch64, RISC-V and PPC64LE: the new product routines' correctness and constant-time proofs, and their callers' proofs updated to match.

Trust

No change to TCB/ or Spec/. The generated rsa.{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:

arch function 2048 3072 4096
i686 crt_values 1,117,542 → 1,078,086 (−3.5%) 2,267,382 → 2,180,534 3,815,046 → 3,662,374 (−4.0%)
i686 check_key 445,900 → 406,832 (−8.8%) 916,636 → 830,352 (−9.4%) 1,554,796 → 1,402,864 (−9.8%)
x86-64 crt_values 277,880 → 269,229 (−3.1%) 521,798 → 502,483 838,676 → 804,473 (−4.1%)
x86-64 check_key 153,747 → 145,095 (−5.6%) 275,835 → 256,519 (−7.0%) 429,923 → 395,719 (−8.0%)
AArch64 check_key 94,340 → 84,871 (−10.0%) 169,908 → 148,791 (−12.4%) 266,596 → 229,223 (−14.0%)
RISC-V check_key 154,258 → 149,730 (−2.9%) 311,906 → 302,042 525,618 → 508,370 (−3.3%)
RISC-V crt_values 742,231 → 737,713 1,528,607 → 1,518,753 2,594,791 → 2,577,553
PPC64LE check_key 159,006 → 152,142 (−4.3%) 313,454 → 298,166 521,150 → 494,110 (−5.2%)
PPC64LE crt_values 1,316,818 → 1,235,034 (−6.2%) 2,774,778 → 2,596,474 (−6.4%) 4,769,058 → 4,457,114 (−6.5%)

AArch64's crt_values count 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, groups rsa_from_.*; base f3525be; with OpenSSL and aws-lc-rs). The tables below are each architecture's default configuration. The run's other VG_CPU_FEATURES jobs are linked from it. In the tables, from_crt runs check_key and from_primes runs crt_values.

arch rsa_from_crt rsa_from_primes rsa_from_components
i686 4.7–6.1% faster 4.4–6.6% faster 0.4–1.1% faster
x86-64 2.1–3.5% faster 2.3–4.4% faster ±0.2%
AArch64 3.9–5.3% faster 1.9–2.5% faster ±0.0%
RISC-V 2.6% faster to 1.3% slower 1.3–3.3% faster ±0.5%
PPC64LE 2.2–3.3% faster (and 27% at 384, see below) 3.8–6.0% faster 0.9–1.0% faster

PPC64LE rsa_from_crt/384 shows 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):

Benchmark Base Head Change OpenSSL Head vs OpenSSL aws-lc-rs Head vs aws-lc-rs
rsa_from_components/256 3.36 ms 3.34 ms 0.4% faster 12.95 ms 3.9× faster – –
rsa_from_components/384 7.05 ms 6.98 ms 1.1% faster 42.10 ms 6.0× faster – –
rsa_from_components/512 12.15 ms 12.07 ms 0.7% faster 103.57 ms 8.6× faster – –
rsa_from_crt/256 75.26 µs 71.73 µs 4.7% faster 2.88 µs 24.9× slower 446.40 µs 6.2× faster
rsa_from_crt/384 151.61 µs 143.50 µs 5.4% faster 3.65 µs 39.3× slower 980.83 µs 6.8× faster
rsa_from_crt/512 258.37 µs 242.53 µs 6.1% faster 4.40 µs 55.2× slower 1.72 ms 7.1× faster
rsa_from_primes/256 169.27 µs 161.84 µs 4.4% faster 151.39 µs 1.1× slower – –
rsa_from_primes/384 344.03 µs 327.02 µs 4.9% faster 299.93 µs 1.1× slower – –
rsa_from_primes/512 590.98 µs 551.90 µs 6.6% faster 452.38 µs 1.2× slower – –

x86_64:

Benchmark Base Head Change OpenSSL Head vs OpenSSL aws-lc-rs Head vs aws-lc-rs
rsa_from_components/256 2.10 ms 2.10 ms 0.0% faster 2.05 ms about the same – –
rsa_from_components/384 4.68 ms 4.67 ms 0.2% faster 5.94 ms 1.3× faster – –
rsa_from_components/512 8.11 ms 8.12 ms 0.2% slower 13.11 ms 1.6× faster – –
rsa_from_crt/256 19.52 µs 19.11 µs 2.1% faster 1.78 µs 10.7× slower 141.03 µs 7.4× faster
rsa_from_crt/384 35.62 µs 34.77 µs 2.4% faster 2.46 µs 14.1× slower 283.13 µs 8.1× faster
rsa_from_crt/512 56.18 µs 54.24 µs 3.5% faster 3.10 µs 17.5× slower 481.68 µs 8.9× faster
rsa_from_primes/256 41.86 µs 40.88 µs 2.3% faster 75.22 µs 1.8× faster – –
rsa_from_primes/384 77.50 µs 74.11 µs 4.4% faster 129.15 µs 1.7× faster – –
rsa_from_primes/512 121.27 µs 116.29 µs 4.1% faster 190.77 µs 1.6× faster – –

aarch64:

Benchmark Base Head Change OpenSSL Head vs OpenSSL aws-lc-rs Head vs aws-lc-rs
rsa_from_components/256 2.04 ms 2.04 ms 0.0% faster 2.49 ms 1.2× faster – –
rsa_from_components/384 4.32 ms 4.32 ms 0.0% faster 7.30 ms 1.7× faster – –
rsa_from_components/512 7.44 ms 7.44 ms 0.0% faster 16.02 ms 2.2× faster – –
rsa_from_crt/256 17.31 µs 16.64 µs 3.9% faster 2.18 µs 7.6× slower 130.92 µs 7.9× faster
rsa_from_crt/384 32.72 µs 31.16 µs 4.8% faster 2.98 µs 10.4× slower 265.45 µs 8.5× faster
rsa_from_crt/512 53.06 µs 50.27 µs 5.3% faster 3.74 µs 13.4× slower 452.61 µs 9.0× faster
rsa_from_primes/256 36.70 µs 36.01 µs 1.9% faster 108.75 µs 3.0× faster – –
rsa_from_primes/384 67.95 µs 66.35 µs 2.4% faster 168.53 µs 2.5× faster – –
rsa_from_primes/512 108.59 µs 105.83 µs 2.5% faster 268.77 µs 2.5× faster – –

riscv64:

Benchmark Base Head Change OpenSSL Head vs OpenSSL aws-lc-rs Head vs aws-lc-rs
rsa_from_components/256 2.38 ms 2.37 ms 0.4% faster 30.43 ms 12.8× faster – –
rsa_from_components/384 4.97 ms 4.95 ms 0.5% faster 102.79 ms 20.8× faster – –
rsa_from_components/512 8.41 ms 8.44 ms 0.4% slower 207.96 ms 24.6× faster – –
rsa_from_crt/256 96.48 µs 93.94 µs 2.6% faster 8.71 µs 10.8× slower 894.19 µs 9.5× faster
rsa_from_crt/384 177.36 µs 179.74 µs 1.3% slower 10.44 µs 17.2× slower 1.83 ms 10.2× faster
rsa_from_crt/512 284.54 µs 280.89 µs 1.3% faster 12.39 µs 22.7× slower 3.03 ms 10.8× faster
rsa_from_primes/256 295.92 µs 291.99 µs 1.3% faster 470.10 µs 1.6× faster – –
rsa_from_primes/384 572.85 µs 562.05 µs 1.9% faster 846.06 µs 1.5× faster – –
rsa_from_primes/512 945.91 µs 914.47 µs 3.3% faster 1.29 ms 1.4× faster – –

powerpc64:

Benchmark Base Head Change OpenSSL Head vs OpenSSL aws-lc-rs Head vs aws-lc-rs
rsa_from_components/256 5.36 ms 5.31 ms 0.9% faster 2.98 ms 1.8× slower – –
rsa_from_components/384 10.72 ms 10.60 ms 1.0% faster 8.12 ms 1.3× slower – –
rsa_from_components/512 18.94 ms 18.77 ms 0.9% faster 18.06 ms about the same – –
rsa_from_crt/256 33.79 µs 33.04 µs 2.2% faster 5.22 µs 6.3× slower 511.10 µs 15.5× faster
rsa_from_crt/384 82.26 µs 59.89 µs 27.2% faster 6.83 µs 8.8× slower 1.16 ms 19.3× faster
rsa_from_crt/512 94.45 µs 91.36 µs 3.3% faster 8.52 µs 10.7× slower 2.04 ms 22.3× faster
rsa_from_primes/256 163.19 µs 156.94 µs 3.8% faster 147.64 µs 1.1× slower – –
rsa_from_primes/384 358.50 µs 336.94 µs 6.0% faster 250.93 µs 1.3× slower – –
rsa_from_primes/512 611.40 µs 579.00 µs 5.3% faster 356.17 µs 1.6× slower – –

Checks

On 6c4ad41 (8 commits on main f3525be):

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 full Emit.lean after merging main.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh

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
@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 06:13
@alex
alex added this pull request to the merge queue Nov 12, 2026
Merged via the queue into main with commit f36e35a Nov 12, 2026
61 checks passed
@alex
alex deleted the claude/x86-pq-half branch November 12, 2026 12:11
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
Brings in #4990, #4986, #4988, #4975 and #4966. The generated x86 rsa
crate conflicted with #4990's; it is regenerated in the next commit.

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
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
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