Repository navigation
PPC64LE: vg_rsa_recover_primes_det by words - #5001
Merged
Merged
Conversation
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
…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
|
This pull request has conflicts with Posted by |
…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
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
|
This pull request has conflicts with Posted by |
…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
alex
marked this pull request as ready for review
November 12, 2026 23:28
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.
It was stacked on #4997 (PPC64LE fused inverse passes), which has merged; this PR now targets
main, withmainmerged in (7aea322).PPC64LE's
vg_rsa_recover_primes_detdid the stage afterd e - 1a bit at a time: a bit-serial productd 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 xandb²(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 etakes⌈e_len / 8⌉rows, a count that depends only on the public length ofe(a single row for everye_len ≤ 8). It replacesKeys.mulMod, a double-and-add over the8 e_lenbits ofe.aandb²replaceMontKey.mulLo's rows of 32-bit digits.twos). The trailing zeros ofxare counted on words. The shift is done in stages chosen by mask, never by a branch on secret data.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.divA,divB).m = ⌊a / n⌋,r = a mod nand⌊(n − r) / (m + 1)⌋are all computed by word division that keeps the quotient (KeyDiv.divQ).divBfirst shifts its divisor so that the top word is nonzero.sqrt).clzX), never by a load at a secret index.(2 s β + q̂)is subtracted, and two masked corrections follow. The number of passes does not depend on the data.sOk5records whether the radicand is a perfect square.The old bit-serial pieces (
mulModinprod,setTop, the bitwisesqrtBody) are gone from this function.Proofs
mainbyte for byte:Proof/Bignum/SqrtRem.lean,Proof/Bignum/ShiftStages.lean,Proof/Bignum/LongDivision.lean,Proof/Rsa/RecoverDetWords.leanandProof/Rsa/RecoverRoot.lean(root_bridge).RdMulW:mulW_ok, the product by rows of words (macWLoop_ok,zerosW_ok).RdFinish.prod_okandRdProd.DS.mulWare its instances.IbInverse:batchG_ok,loopG_okandgcd_ok, which take[TcSLaw]like PPC64LE RSA: the batched inverse on 64-bit words, in one pass per batch #4997's inverse.InvLawgainsgcd_ok, whichRdOps.DS.invandRdGcduse.RdShiftandRdTwos: the halvings by stages.RdDiv: division with a quotient.KeyDiv: one word division, extended rather than duplicated.divW … (top)forcheck_keyandcrt_values.stepG (q : Bool);step := stepG falseis main's step, socheck_keyandcrt_valuesemit the same code as on main.divQ := divG truestarts from a zero remainder and is whatRdDivuses.loopG_ok (q), from any remainderR₀ < M: the remainder is(R₀·2^(64A) + X) mod M, and withqthe quotient is(R₀·2^(64A) + X) / M. Main'sloop_okis its corollary (q = false, R₀ = Pf j₀ mod M, bypf_split), anddivG_okis its instance at R₀ = 0.RdSqrtW(new):estimate_ok), the multiply-subtract (mulSubD_ok), the corrections (correct_ok), and the step invariantSqInvwithstep_okandsteps_ok;first_ok), the normalization (clzX_ok,counts_ok,sqrtPre_ok), the end (sqrtPost_ok), andsqrtW_ok;sqrtW_okstates that the root isisqrt Xand thatsOk5 = [isqrt X² = X].RdSqrt.sqrt_okrestates it on the recovery's arrays.RdCorrectreachesProof/Rsa/RecoverDetModelthroughroot_bridge:mask_c6andx_lt(Xfits the root's2 Hwords whereverm < nandb² ≥ 4 n);det_okgives the factors only where the mask is set, which is allvalid_okneeds.RdVerified'staint_decideover the whole valid branch.Trust
No change to
TCB/orSpec/. Incrates/verified-garbage-asm-powerpc64le, onlyrsa.{rs,s}change.Measurements
qemu-ppc64le instructions per call of
vg_rsa_recover_primes_det(keys of the qemu test set):By piece:
gcddivBdivAsqrttwossquare(b, copies,b²)aProductprod(d e)d e − 1,minus4n, factors, storesBefore 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,aProductandsquaretook 356k instructions at 2048 bits; they now take 56k.Benchmarks
Run 43270848500: powerpc64 (POWER9), default CPU features, module
rsa, grouprsa_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_componentsrunsrecover_primes_detand 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.rsa_from_components/256rsa_from_components/384rsa_from_components/512Checks
On 7aea322 (
mainmerged after #4997, #5004, #5005 and #4996 landed): every PPC64LE Lean file and the generatedverified-garbage-asm-powerpc64lecrate are byte-identical to 6268dec's, sincemain's PPC64LE Lean equals #4997's final head. The merge differs frommainonly 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 theoremsbatchG_ok,loopG_okandgcd_oktake[TcSLaw]too): the fulllake build(29,019 jobs) andEmit.lean --checkpass, the generated code is unchanged, andcheck_lean_speed.pypasses withDivstep/Alg's importers at 40, also with #5004 and #5005 merged.On add1aff (#4997's head d1f8fee merged,
KeyDivunified as above, crate regenerated):lake buildpasses (29,016 jobs). The fullEmit.leanandEmit.lean --checkexit 0.rsa.s, against PPC64LE RSA: the batched inverse on 64-bit words, in one pass per batch #4997's head, onlyvg_rsa_recover_primes_detdiffers. Its code is byte-identical to this PR's before the merge, so the counts above are unchanged.check_lean_public_imports.pyon the edited modules.cargo teston ppc64le (qemu) with Wycheproof passes with default CPU features and withVG_CPU_FEATURES=none.Before the merge, on a43a954, whose tree was 7fa203c's:
lake buildpasses, constant-time proofs included.Emit.leanandEmit.lean --checkexit 0.check_lean_public_imports.pyon the edited modules (against 4978e3a).cargo teston ppc64le (qemu) with Wycheproof passes with default CPU features and withVG_CPU_FEATURES=none.n:p − 1andq − 1sharing powers of 2 up to2^1000, andn − 1with up to 1000 twos;d(d ± 1,d + λ,1,3,n − 1, a flipped bit) and a wronge;p = qandq = nextprime(p), from 1024 to 4096 bits.math.isqrt.🤖 Generated with Claude Code
https://claude.ai/code/session_01TKUFHH1um6BHmQjMB1jTPh