Skip to content

RSA PKCS#1 v1.5 decrypt: vg_rsa_pkcs1_decrypt_precomputed on every architecture - #4959

Merged
alex merged 31 commits into
mainfrom
claude/rsa-pkcs1-decrypt-precomputed-on-main
Nov 12, 2026
Merged

alex merged 31 commits into
mainfrom
claude/rsa-pkcs1-decrypt-precomputed-on-main

Conversation

@alex

@alex alex commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Replaces #4945. Follows #4956 (the precompute) and #4958 (its vg_rsa_private_checked_precomputed), both merged.

This adds the PKCS#1 v1.5 decrypt entrypoint that takes the key's precomputed values (pre), vg_rsa_pkcs1_decrypt_precomputed, on every architecture, and switches PrivateKey's PKCS#1 v1.5 decrypt to it everywhere. It is verified against decryptPrecomputedContract; no architecture registers it by weakening the old contract. Each one calls a vg_rsa_private_checked_precomputed that reads pre, and the CRT behind it skips per-call work that pre already holds:

Architecture What the precomputed private path skips per call
PPC64LE R² mod p and q, R² mod n (already on main, #4908)
RISC-V R² mod p and q (65·w doublings + 6 squares each), R² mod n, and the checks' p·q = n product at the modulus' double width (≈87K instructions at 2048 bits)
Arm R² mod p and q (the per-prime doubling loop), R² mod n, the p·q product
x86 (i686) R² mod p and q, R² mod n, the p·q product
x86-64 R² mod n and the p·q product (base and ADX use 2^E mod n, not R_p²); on the IFMA variants also the three R_X² reductions per call
AArch64 R² mod n and the p·q product (vg_rsa_private_crt_precomputed); its CRT never computes R_p²

The p·q product can go because the precondition pre = privatePrecompute n p q implies p q = n.

No changes to TCB/ or Spec/.

Benchmarks

Run 43069479634: rsa_pkcs1_enc, group rsa_pkcs1_decrypt, every architecture, base main at 8564b91, head 0ab2fda. Later merges of main (#4948, #4964, #4965) change the public R² mod n precompute; only on AArch64 does one (#4948's word step for R² mod m) also reach the CRT, in main's vg_rsa_private_crt as in this PR's, so both sides of the AArch64 comparison change alike. "−" is faster.

Architecture (VG_CPU_FEATURES) 2048 bits 3072 bits 4096 bits
x86-64 (runner's) 226.92 → 220.95 µs (−2.6%) 669.68 → 656.58 µs (−2.0%) 1.40 → 1.37 ms (−1.8%)
x86-64 (avx,avx2,bmi1,bmi2,adx) 711.92 → 701.43 µs (−1.5%) 2.11 → 2.10 ms (−0.4%) 4.70 → 4.69 ms (−0.3%)
x86-64 (avx,avx2,bmi2,adx) 711.65 → 708.88 µs (−0.4%) 2.12 → 2.11 ms (−0.3%) 4.71 → 4.69 ms (−0.4%)
x86-64 (avx,avx2,bmi1,bmi2) 750.14 → 744.71 µs (−0.7%) 2.24 → 2.28 ms (+1.8%) 5.25 → 5.26 ms (+0.2%)
x86-64 (sha,ssse3) 767.81 → 767.71 µs (0.0%) 2.55 → 2.54 ms (−0.5%) 6.06 → 6.67 ms (+10.1%, noise: see below)
x86-64 (none) 1.32 → 1.31 ms (−0.5%) 4.00 → 3.98 ms (−0.5%) 8.97 → 8.93 ms (−0.4%)
x86 2.63 → 2.46 ms (−6.4%) 6.88 → 6.60 ms (−4.1%) 14.17 → 13.56 ms (−4.3%)
x86 (none) 2.20 → 2.04 ms (−7.0%) 5.73 → 5.45 ms (−4.8%) 11.72 → 11.26 ms (−3.9%)
AArch64 681.29 → 680.93 µs (−0.1%) 2.09 → 2.09 ms (0.0%) 4.72 → 4.71 ms (−0.1%)
AArch64 (none) 696.16 → 694.54 µs (−0.2%) 2.11 → 2.11 ms (−0.2%) 4.73 → 4.75 ms (+0.4%)
Arm 3.52 → 3.13 ms (−11.2%) 10.70 → 9.84 ms (−8.0%) 23.78 → 22.27 ms (−6.4%)
PPC64LE 1.14 → 1.02 ms (−10.4%) 3.18 → 2.88 ms (−9.5%) 6.94 → 6.33 ms (−8.7%)
RISC-V 5.10 → 4.71 ms (−7.6%) 14.75 → 14.09 ms (−4.5%) 32.03 → 30.84 ms (−3.7%)
  • x86-64 sha,ssse3 at 4096 bits (+10.1%) is runner noise, not this change. That configuration runs vg_rsa_pkcs1_decrypt_precomputed_sha256_shani, which calls the same vg_rsa_private_checked_precomputed as the none configuration's vg_rsa_pkcs1_decrypt_precomputed (they differ only in the SHA-256 of the implicit rejection, which this PR does not change). none at 4096 bits is 8.97 → 8.93 ms (−0.4%), and the sha,ssse3 job's 2048- and 3072-bit rows are flat. That head sample's mean is far from its median.
  • x86-64 with AVX-512 IFMA was not measured. The runner (AMD EPYC 7763) lacks avx512f, avx512ifma and avx512vl, and the CPU crate refuses a VG_CPU_FEATURES that names features the CPU lacks.
  • No OpenSSL or aws-lc-rs comparison. The run compares with main, and the workflow's openssl option was off, as the benchmarking skill says to leave it.
  • The runner's own x86-64 configuration ran on a CPU with IFMA, hence its lower times. x86-64 and AArch64 change little because their CRTs never computed R_p² per call.

Checks (on a0554d7: main 3fc218b, e502ca9 and 1328178 merged)

Checks (on d8643b8, main ade8505 merged)

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5

… and PrivateKey computes pre everywhere

vg_rsa_private_precompute, verified, on x86-64 (base and ADX), x86,
AArch64, Arm and RISC-V (PPC64LE's is #4940, which this is stacked on),
and the shared framework the *_precomputed entrypoints use:
Verified.withSlice and its per-ISA instances, and Proof/Rsa/PreKey.lean.
PrivateKey computes the key's precomputed values on every architecture.

Each factor's R_p² mod p comes from R² mod n, which the precompute
computes anyway: steps of a Montgomery reduction of R² mod n (chunked,
then whole words while more than 2 w_p are left) give 2^(64 e) mod p
when p divides n, then at most a few doublings and squarings
(Proof/Bignum/PrimeR2.lean's counts on RISC-V, PPC64LE, AArch64 and Arm;
a chunked REDC on x86 and x86-64).

Squashes #4917, #4928, #4929, #4930 and #4931, and the per-architecture
fast R_p² work.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
The precompute checked p q = n with vg_rsa_private_crt's Crt.checkPQ, a
Montgomery multiplication modulo 2^(64 W) - 1 at X's width W = 2 ⌈k / 8⌉.
It now multiplies p's ⌈p_len / 8⌉ words by q's ⌈q_len / 8⌉ (schoolbook
rows, PrivPre.checkPQs) into array 6 and compares the product with n as
checkPQ does (Crt.pqEq, factored out of checkPQ). vg_rsa_private_crt's code
is unchanged.

Crt.checks is now checksWith checkPQ, and checksWith_ok takes any check of
p q = n that meets PQOk; checks_ok is its instance for checkPQ. The two
lengths are loaded before the checks (PrivPre.pqLens), so the constant-time
proof pins a3 and t1 at the cuts after it (pins50, pins51) and its cut
indices move by one before the checks and by -5 after them.

Instructions of vg_rsa_private_precompute under qemu-riscv64, balanced
keys: 2048 bits 327,716 -> 241,199; 3072 bits 692,010 -> 499,853; 4096
bits 1,191,470 -> 852,073.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
The precompute checked p q = n with vg_rsa_private_crt's checkPQ, a
Montgomery product modulo 2^(64 W) - 1 at X's width W = 2 ceil(k / 8),
twice the modulus's. It now multiplies p's w_p = ceil(p_len / 8) words by
q's w_q = ceil(q_len / 8) (schoolbook rows, `PrivPre.checkPQs`) into
array 6, zeroed first, and compares the product with n as checkPQ does
(`Crt.pqEq`, factored out of it). The widths are loaded before the
checks (`pqLens`), into r5 and r7, which checkN and checkLt keep.

Proofs: `Crt.checksWith pq` and `Crt.phaseAWith_ok` take the check of
p q = n as a parameter (`Crt.PQOk`), with `checkPQ_pqOk` for the CRT's
own; `PrivPrePQ.lean` proves the rows (`rows_ok`) and `checkPQs_ok`; the
constant-time chain pins r5 and r7 after `pqLens` (cuts 50 and 51), and
the later cuts move by one. No TCB or Spec change.

Instructions (qemu-ppc64le, one call): 2048: 431,212 -> 386,422;
3072: 825,780 -> 726,198; 4096: 1,340,988 -> 1,164,982.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
The precompute checked p q = n with the private CRT's validation product,
CrtRt.wideProduct: all W = B / 4 words of p by all W of q, W rows of W
words, for factors of about half the modulus's length. It now multiplies
p's ceil(p_len / 4) words by q's ceil(q_len / 4) (the lengths are public
arguments), and compares the product with n as before.

`CrtRt.productBy cols rowsEnd b` is wideProduct with its two counts as
parameters (code that sets a register to a count's bytes, `Counts`, and
`EndsAt`); `wideProduct` is `productBy (widthC 0 .wide) []`, the same
code, so the private CRT is unchanged. `productBy_ok` generalizes
`wideProduct_ok`, which is now its instance; `rowSetup_ok` and
`rowTail_ok` take the counts. The precompute's `pqCheck` uses
`productBy (lenW 9) (lenW 7 .eax)`; `pqCheck_ok` takes the factors'
bounds from their lengths (`counts_lenW`, `val32_fit`). The constant-time
proof is unchanged: the taint analysis checks the new code (the lengths
are arguments, public). No TCB or Spec change.

Instructions (qemu-i386, one call): 2048: 501,419 -> 460,203;
3072: 990,347 -> 898,571; 4096: 1,640,043 -> 1,477,739.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
The precompute checks p q = n with the private CRT's validation, whose
product (Crt.factorProduct) multiplies all 2 W words of p by all 2 W of q,
for factors of about half the modulus's length. Its counts are now
ceil(p_len / 4) rows of ceil(q_len / 4) words (`Crt.lengthCount`, from the
CRT frame's lengths of p and q, slots 68 and 76), and the product is
compared with n as before: the output is cleared first, so the words
above the product stay zero.

`productArgsWith`, `factorProductWith`, `validateProductWith` and
`CrtFn.validateWith` take the counts as a parameter; `productArgs`,
`factorProduct`, `validateProduct` and `CrtFn.validate` are their
instances with `productCount`, the same code, so the private CRT is
unchanged. The proofs take the counts as `CountFrom` (the block that sets
them, from any state that agrees outside the workspace) and their
constant time from equal r0 and sp: `crt_productArgsWith_ok`,
`crt_productArgsWith_ready`, `crt_factorProductWith_ok`,
`crt_validateProductWith_ok`, `crt_productStageWith_ok` and
`crt_validateStageWith_ok`, and the CT lemmas `crt_productArgsWith_ct`,
`crt_factorProductWith_ct`, `crt_validateProductWith_ct` and
`crt_validateStageWith_ct`; the CRT's are their instances
(`countFrom_productCount`). The precompute's: `countFrom_lengthCount`,
`pp_lengthCounts` (the factors fit the counts) and `lengthCount_ct` (its
loads at sp, its stores at r0, by `loadStack_ct`, the taint analysis and
`relCT_keep_r0`). No TCB or Spec change.

Instructions (qemu-arm, one call): 2048: 1,244,559 -> 1,185,139;
3072: 2,707,007 -> 2,574,099; 4096: 4,730,607 -> 4,495,027.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
d0743f2 regenerated crates/verified-garbage-asm-powerpc64le/src/rsa.rs
with EmitOne, which writes only one registration's functions and so
dropped the `mod` lines of internal functions other registrations emit.
This is the full emitter's output (`Emit.lean`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
…recomputed private path

vg_rsa_pkcs1_decrypt_precomputed now calls vg_rsa_private_checked_precomputed
on RISC-V, x86-64 (all four variants), i686 and Arm, in place of
vg_rsa_pkcs1_decrypt's code proven against the weaker contract. That
function copies n's values from pre instead of vg_rsa_public_precompute, and
calls vg_rsa_private_crt_precomputed, which copies R_p² and R_q² from pre (on
x86-64 R² mod n, and on IFMA also R_p² and R_q²) and drops the checks'
p·q = n product, which privatePrecompute = some pre implies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
The committed Arm crate predated the Lean of vg_rsa_private_precompute;
these are the two files a full lake build and Emit.lean write on
defc3ca, nothing else.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHgwmxoKvRUNy64bKd
The tree is claude/rsa-private-precompute-v3 (ecaf7ae): this branch's
commits rebased onto 48ea507 with the PPC64LE crate regenerated by the
full emitter. Full lake build, Emit --check (exit 0), six-arch RSA tests,
fmt/clippy and the static checks pass on it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh
The tree is the decrypt commit applied to #4956's head, verified with a
full lake build, Emit and Emit --check (exit 0), and cargo test rsa on all
six architectures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHG7VqNCm5vQWRrXrv5
…ting n's values

The layout gains the further regions the function may read (X), and the
correctness and constant-time proofs are stated for any stage in place of
vg_rsa_public_precompute's call, for vg_rsa_private_checked_precomputed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHu5XUqPT84QNkv9Dw
vg_rsa_private_checked's frames, CRT and check, with n's values copied from
the key's precomputed values in place of calling vg_rsa_public_precompute,
verified against privateCheckedPrecomputedContract. The proofs run on the
call narrowed to the regions they read (vg_rsa_private_checked's, stack
argument 12, and n's values at the start of pre), and widen to the call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHu5XUqPT84QNkv9Dw
…te-key operation

The slots move up 16 bytes (the frame from 208 to 224 bytes), so that the
frame's bottom can hold vg_rsa_private_checked_precomputed's 14 stack
arguments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHu5XUqPT84QNkv9Dw
The layout gains the further regions the function may read and pre's place,
the code takes the block that sets up the call (codeWith), and correctness
takes the setup and the call as a hypothesis, for the precomputed variant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHu5XUqPT84QNkv9Dw
…l (WIP)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHu5XUqPT84QNkv9Dw
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHu5XUqPT84QNkv9Dw
…precomputed

vg_rsa_pkcs1_decrypt_precomputed: vg_rsa_pkcs1_decrypt's code, moving pre
and pre_len to the precomputed private operation's stack arguments, proven
against decryptPrecomputedContract on the call narrowed to the regions the
proofs read, and registered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHu5XUqPT84QNkv9Dw
…ng vg_rsa_private_checked_precomputed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHu5XUqPT84QNkv9Dw
Base automatically changed from claude/rsa-private-precompute-on-main to main November 11, 2026 15:27
@github-actions

Copy link
Copy Markdown

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

…omputed proven

vg_rsa_private_crt's code (both its four-bit and five-bit main computations,
the dispatch between them and the entry) takes its key checks as a
parameter, and its proofs are generic over them: correctness over a stage
that meets checks_ok's statement for keys with a property G of (n, p, q)
(ChecksOk), constant time over one that is constant time (ChecksCTOf), with
G carried through the stages (RG) and the contract's relation (CCRel).

checks is checksGiven, the checks but p q = n, after the product and its
comparison. vg_rsa_private_crt_precomputed, the code with checksGiven, is
proven against privateCrtProductContract, an internal contract:
vg_rsa_private_crt's, for a key whose factors' product is the modulus. The
emitted code of vg_rsa_private_crt does not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHu5XUqPT84QNkv9Dw
…recomputed

vg_rsa_private_checked's proofs take the CRT they call as a CrtCallee: its
code, verified against vg_rsa_private_crt's contract for call states with K.
vg_rsa_private_checked calls vg_rsa_private_crt (K true);
vg_rsa_private_checked_precomputed calls vg_rsa_private_crt_precomputed (K:
the key's factors' product is its modulus), which its pre implies
(privatePrecompute_mul). CrtImpl gains vg_rsa_private_crt_precomputed.

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

Copy link
Copy Markdown

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

Conflicts: Proof/RsaPkcs1Sig/RISCV64/Sign/Call.lean takes main's, #4958's
generic version; Proof/RsaPkcs1Enc/RISCV64/Dec/Calls.lean keeps the
branch's, generic over pc, which main's PreP false adaptation is a case of.

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
…as memberships (#4970)

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
alex marked this pull request as ready for review November 12, 2026 01:20
@alex
alex added this pull request to the merge queue Nov 12, 2026
Merged via the queue into main with commit 0b6d045 Nov 12, 2026
43 checks passed
@alex
alex deleted the claude/rsa-pkcs1-decrypt-precomputed-on-main branch November 12, 2026 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants