Skip to content

PPC64LE: vg_rsa_recover_primes_det by words - #5001

Merged
alex merged 12 commits into
mainfrom
claude/ppc64le-recover-words-pr
Nov 12, 2026
Merged

alex merged 12 commits into
mainfrom
claude/ppc64le-recover-words-pr

Conversation

@alex

@alex alex commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

It was stacked on #4997 (PPC64LE fused inverse passes), which has merged; this PR now targets main, with main merged in (7aea322).

PPC64LE's vg_rsa_recover_primes_det did the stage after d e - 1 a bit at a time: a bit-serial product d e, halvings, divisions, products by 32-bit digits, the full inverse for a GCD, and a bit-serial square root. This PR moves that stage to 64-bit words throughout, as #4999 (RISC-V) and #5000 (x86-64) do.

  • d e, a = g 2^k x and b² (Recover.mulW). Each is a product by rows of 64-bit words (mulld, mulhdu, addc, adde; InvBatch.macWBody). There is one row per word of the multiplier, and each row's carry out of the top is dropped. d e takes ⌈e_len / 8⌉ rows, a count that depends only on the public length of e (a single row for every e_len ≤ 8). It replaces Keys.mulMod, a double-and-add over the 8 e_len bits of e. a and b² replace MontKey.mulLo's rows of 32-bit digits.
  • Halvings (twos). The trailing zeros of x are counted on words. The shift is done in stages chosen by mask, never by a branch on secret data.
  • GCD. It goes through a gcd-only path of the batched inverse, InvBatch.gcd: init, then batches of 59 divsteps and PPC64LE RSA: the batched inverse on 64-bit words, in one pass per batch #4997's fused (f, g) pass alone (batchG), then |f| (finishG). The (a, b) pass and the doublings are never run.
  • Divisions (divA, divB). m = ⌊a / n⌋, r = a mod n and ⌊(n − r) / (m + 1)⌋ are all computed by word division that keeps the quotient (KeyDiv.divQ). divB first shifts its divisor so that the top word is nonzero.
  • Square root by words (sqrt).
    • The radicand is shifted up by an even number of bits. Its top nonzero word is found by a masked scan of every word (clzX), never by a load at a secret index.
    • The first digit and the reciprocal of the root's top word come from the top two words.
    • Each later digit is estimated from the remainder's top words (a 3-by-2 quotient). Then the estimate times (2 s β + q̂) is subtracted, and two masked corrections follow. The number of passes does not depend on the data.
    • The root is shifted back down, and sOk5 records whether the radicand is a perfect square.

The old bit-serial pieces (mulMod in prod, setTop, the bitwise sqrtBody) are gone from this function.

Proofs

  • Shared, target-independent modules, already on main byte for byte: Proof/Bignum/SqrtRem.lean, Proof/Bignum/ShiftStages.lean, Proof/Bignum/LongDivision.lean, Proof/Rsa/RecoverDetWords.lean and Proof/Rsa/RecoverRoot.lean (root_bridge).
  • PPC64LE:
    • RdMulW: mulW_ok, the product by rows of words (macWLoop_ok, zerosW_ok). RdFinish.prod_ok and RdProd.DS.mulW are its instances.
    • IbInverse: batchG_ok, loopG_ok and gcd_ok, which take [TcSLaw] like PPC64LE RSA: the batched inverse on 64-bit words, in one pass per batch #4997's inverse. InvLaw gains gcd_ok, which RdOps.DS.inv and RdGcd use.
    • RdShift and RdTwos: the halvings by stages. RdDiv: division with a quotient.
    • KeyDiv: one word division, extended rather than duplicated.
      • RSA check_key: word steps sized to the operands on x86, x86-64, AArch64 and PPC64LE #4993 (on main) gave PPC64LE a seeded divW … (top) for check_key and crt_values.
      • This PR adds the quotient as a flag on the same step, stepG (q : Bool); step := stepG false is main's step, so check_key and crt_values emit the same code as on main. divQ := divG true starts from a zero remainder and is what RdDiv uses.
      • There is one loop proof, loopG_ok (q), from any remainder R₀ < M: the remainder is (R₀·2^(64A) + X) mod M, and with q the quotient is (R₀·2^(64A) + X) / M. Main's loop_ok is its corollary (q = false, R₀ = Pf j₀ mod M, by pf_split), and divG_ok is its instance at R₀ = 0.
    • RdSqrtW (new):
      • the estimate (estimate_ok), the multiply-subtract (mulSubD_ok), the corrections (correct_ok), and the step invariant SqInv with step_ok and steps_ok;
      • the first digit (first_ok), the normalization (clzX_ok, counts_ok, sqrtPre_ok), the end (sqrtPost_ok), and sqrtW_ok;
      • sqrtW_ok states that the root is isqrt X and that sOk5 = [isqrt X² = X]. RdSqrt.sqrt_ok restates it on the recovery's arrays.
    • RdCorrect reaches Proof/Rsa/RecoverDetModel through root_bridge:
      • the mask is the model's, by mask_c6 and x_lt (X fits the root's 2 H words wherever m < n and b² ≥ 4 n);
      • det_ok gives the factors only where the mask is set, which is all valid_ok needs.
  • Constant time: RdVerified's taint_decide over the whole valid branch.

Trust

No change to TCB/ or Spec/. In crates/verified-garbage-asm-powerpc64le, only rsa.{rs,s} change.

Measurements

qemu-ppc64le instructions per call of vg_rsa_recover_primes_det (keys of the qemu test set):

key #4997 (4978e3a) this branch
2048 51,759,395 678,912 76× fewer
3072 111,937,018 1,243,871 90× fewer
4096 195,034,314 1,972,946 99× fewer

By piece:

piece 2048 3072 4096
gcd 363,111 647,607 1,004,295
divB 77,648 155,312 261,512
divA 65,268 136,772 234,900
sqrt 50,962 88,266 136,439
twos 36,512 54,192 74,734
square (b, copies, b²) 28,200 59,456 102,232
aProduct 26,388 56,812 98,756
loads 18,594 27,433 36,265
prod (d e) 1,377 2,017 2,657
d e − 1, minus4n, factors, stores 10,852 16,004 21,156

Before the gcd-only path, the inverse's (a, b) pass and its doublings were 47%, 51% and 53% of the function at 2048, 3072 and 4096 bits. Before the word rows, prod, aProduct and square took 356k instructions at 2048 bits; they now take 56k.

Benchmarks

Run 43270848500: powerpc64 (POWER9), default CPU features, module rsa, group rsa_from_components, base 4978e3a (#4997's head then), with OpenSSL and aws-lc-rs. Recover has no CPU-feature variants, so one configuration measures it. Recover's emitted code is unchanged since this run (see Checks).

rsa_from_components runs recover_primes_det and then the rest of the key import. It is 96.9–97.5% faster (32–40×). Before, it was 1.2–1.7× slower than OpenSSL's equivalent at 256 and 384 bytes and about as fast at 512; it is now 19–41× faster.

Benchmark Base Head Change OpenSSL Head vs OpenSSL aws-lc-rs Head vs aws-lc-rs
rsa_from_components/256 4.92 ms 154.54 µs 96.9% faster 2.98 ms 19.3× faster – –
rsa_from_components/384 9.92 ms 294.46 µs 97.0% faster 8.15 ms 27.7× faster – –
rsa_from_components/512 17.40 ms 433.46 µs 97.5% faster 17.87 ms 41.2× faster – –

Checks

On 7aea322 (main merged after #4997, #5004, #5005 and #4996 landed): every PPC64LE Lean file and the generated verified-garbage-asm-powerpc64le crate are byte-identical to 6268dec's, since main's PPC64LE Lean equals #4997's final head. The merge differs from main only in PPC64LE files, and the static checks pass on it.

On 6268dec (#4997's head aca0bc4 merged; IbInverse's divstep algebra is a class there, TcSLaw, and this PR's gcd-only theorems batchG_ok, loopG_ok and gcd_ok take [TcSLaw] too): the full lake build (29,019 jobs) and Emit.lean --check pass, the generated code is unchanged, and check_lean_speed.py passes with Divstep/Alg's importers at 40, also with #5004 and #5005 merged.

On add1aff (#4997's head d1f8fee merged, KeyDiv unified as above, crate regenerated):

  • The full lake build passes (29,016 jobs). The full Emit.lean and Emit.lean --check exit 0.
  • In the PPC64LE rsa.s, against PPC64LE RSA: the batched inverse on 64-bit words, in one pass per batch #4997's head, only vg_rsa_recover_primes_det differs. Its code is byte-identical to this PR's before the merge, so the counts above are unchanged.
  • The shared files are unchanged: ShiftStages 2c11e420, RecoverDetWords dc87246d, SqrtRem fa1648a3, RecoverRoot cd29156b, LongDivision ee9bddf8.
  • Every Static-checks step passes, and so does check_lean_public_imports.py on the edited modules.
  • cargo test on ppc64le (qemu) with Wycheproof passes with default CPU features and with VG_CPU_FEATURES=none.

Before the merge, on a43a954, whose tree was 7fa203c's:

  • The full lake build passes, constant-time proofs included.
  • The full Emit.lean and Emit.lean --check exit 0.
  • Every step of the Static checks passes, and so does check_lean_public_imports.py on the edited modules (against 4978e3a).
  • cargo test on ppc64le (qemu) with Wycheproof passes with default CPU features and with VG_CPU_FEATURES=none.
  • Two sets ran through the final code under qemu. The output was identical to PPC64LE RSA: the batched inverse on 64-bit words, in one pass per batch #4997's head, and the recovered factors were checked against n:
    • 165 keys:
      • valid keys from 1024 to 8192 bits, and every byte length from 64 to 136;
      • p − 1 and q − 1 sharing powers of 2 up to 2^1000, and n − 1 with up to 1000 twos;
      • invalid d (d ± 1, d + λ, 1, 3, n − 1, a flipped bit) and a wrong e;
    • 36 keys aimed at the square root: p = q and q = nextprime(p), from 1024 to 4096 bits.
  • The square root alone ran on 3000 radicands of 2 to 16 words, 40% of them perfect squares, and was checked against Python's math.isqrt.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh

vg_rsa_crt_values, vg_rsa_recover_primes_det and vg_rsa_keygen_key share
the batched divstep inverse (Impl/Rsa/PPC64LE/InvBatch.lean). Its numbers
were 32-bit digits. They are now 64-bit words, ν = ⌈n / 2⌉ of them, with
the carries by CA (addc, adde, addze) and signed products by mulhu less the
word where the coefficient is negative. As on RISC-V and AArch64, each batch
updates (f, g) and (a, b) in one pass over the words of each pair, both
rows of the matrix at once:

* over f and g, the low words shifted by 59 are stored a word down;
* over a and b, k m is added with an unsigned carry and the result divided by
  2^64 as it goes.

The passes borrow r14–r28, saved to the first words of arrays 11–13 for each
batch. For a balanced key, crt_values leaves the inverse's 2^K scaling to
one word division (inverseS and CvTail, as RISC-V's #4984), and its width w
is now even.

The proofs:
* IbLoops: the word loops, fgLoop_ok and abLoop_ok (FGAcc, ABAcc on the
  signed accumulators of lin_term).
* IbOps: the blocks, spill and restore.
* IbInverse: updateG_ok, updateAB_ok and batch_ok, and InvLaw's instance.
  BS, InvLaw and inverse_ok keep their statements.
* Proof/Divstep/WordArith.lean: the top word's and the reduction's
  arithmetic, ISA-independent, to be shared with the other ports.

The taint analysis treats memory as secret, so the registers restore
reloads are secret to it. Each caller's constant-time proof is cut after
the inverse (RelCT.cut over Code.flat, Seqs.lean), and the argument
registers are pinned again from the functional proof there:
* crt_values: after the inverse and its checks (preAB_ok);
* recover: after the GCD (valid_gcd_ok);
* keygen: after lcmPart, dPart and qinvPart (body_cut_ok).

The proofs found two bugs in the first version of the code:
* An odd crt_values width w could overflow the inverse's arrays; w is now
  even.
* zeroW left digit w + 1 unzeroed; it now clears w + 2 digits.

No TCB or Spec change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
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
The stage after d e - 1 now works on 64-bit words throughout, as on RISC-V:

* d e, a = g 2^k x and b² by rows of 64-bit words (Recover.mulW, on
  InvBatch.macWBody), d e over e's ⌈e_len / 8⌉ words, replacing the
  bit-serial mulMod and MontKey.mulLo's 32-bit rows;
* the halvings by stages chosen by mask;
* gcd(u, x) by the batched inverse's gcd-only path (InvBatch.gcd: the
  (f, g) pass alone, then |f|), without the (a, b) pass and doublings;
* the divisions with a quotient (KeyDiv.divQ);
* the square root by words: a masked scan for the top word, the first
  digit from the top two words, then each digit estimated (3-by-2) with
  its multiply-subtract and two masked corrections.

Proofs: RdMulW, RdSqrtW (new), the gcd-only batch in IbInverse with
InvLaw.gcd_ok, and RdCorrect's link to the model by the shared
RecoverRoot.root_bridge. The shared SqrtRem, ShiftStages, LongDivision,
RecoverDetWords and RecoverRoot are copied verbatim.

2048-bit recovery in qemu: 51,759,395 instructions before, 678,912 after.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHEBn13MbzWmGJdqbMB
@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
…fused

# Conflicts:
#	crates/verified-garbage-asm-powerpc64le/assembly/rsa.s
#	crates/verified-garbage-asm-powerpc64le/src/rsa.rs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHu5XUqPT84QNkv9Dw
@github-actions

Copy link
Copy Markdown

This pull request has conflicts with claude/ppc64le-inv64-fused, found after dae9c3c 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.

…ords-pr

KeyDiv: main's seeded divW (#4993) and this branch's quotient (divG) share one
loop proof: loopG_ok starts from any remainder R₀ below the divisor, so main's
loop_ok is its instance with R₀ the seed, and divG_ok's with R₀ = 0 and the
quotient. stepG q is the step, step its instance without the quotient.

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
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHu5XUqPT84QNkv9Dw
IbInverse no longer imports Proof/Divstep/Alg: it states what it uses of
TcS, TcSBounds and Tc as the fields of TcSLaw (Proof/Divstep/TcSLaw.lean),
whose instance is in TcSBounds, which only the registrations import beside
it. The definitions it states (mredRaw, batchS, invRunS) move, unchanged,
to Proof/Divstep/BatchS.lean, without the algebra. Proof-only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHu5XUqPT84QNkv9Dw
Base automatically changed from claude/ppc64le-inv64-fused to main November 12, 2026 23:10
@github-actions github-actions Bot removed the untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. label Nov 12, 2026
@github-actions

Copy link
Copy Markdown

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

…ds-pr

main's PPC64LE Lean equals #4997's final head, on which this branch is built,
so this branch's side is kept; the generated powerpc64le crate is this branch's,
whose inputs the merge leaves unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh
@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 23:28
@alex
alex added this pull request to the merge queue Nov 12, 2026
Merged via the queue into main with commit 779305e Nov 12, 2026
22 checks passed
@alex
alex deleted the claude/ppc64le-recover-words-pr branch November 12, 2026 23:42
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