Skip to content

PPC64LE DSA: R² mod p by r2Hi (r2Prime when p's top word is zero) - #4678

Merged
alex merged 2 commits into
mainfrom
claude/ppc64le-dsa-r2
Nov 8, 2026
Merged

alex merged 2 commits into
mainfrom
claude/ppc64le-dsa-r2

Conversation

@alex

@alex alex commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Part of #4141 (finding 7 of the PPC64LE audit). #4667 has merged, and this branch is restacked onto main (cd0c332). The full build and emitter leave the generated crate current. This PR changes no Spec/ or TCB/ file and no Rust code outside the generated PPC64LE crate.

What was slow

setupP computed R² mod p = 2^(128 w) mod p with Precompute.r2: 128·w modular doublings, each over every digit of p. For a 2048-bit p that is about as much work as fifty Montgomery multiplications. It runs once for each check of a key or a domain, and for each signature and verification. The bignum session found the same cost in FFDH (#4658).

What changed

Two commits:

  1. r2Prime (Impl/Dsa/PPC64LE/Common.lean). setupP computes -p⁻¹ mod 2³² first (PublicChecked.minv), then Precompute.r2Prime, which is already verified for any odd modulus: 65·w doublings of 64-bit words and six Montgomery squares.
  2. r2Hi when p's top word is nonzero. Precompute.r2Hi (64 + w doublings from 2^(64(w−1)), then six squares) needs p's top word to be nonzero. The checks of a key don't guarantee that: they check p's bit length, not that p fills its p_len octets. p is public data, though, and the key checks already branch on it. So setupP now branches on p's top word:
    • setupPre loads p, computes -p⁻¹ and the bases of arrays 1–3, and loads p's top word into r3 (topWord).
    • Then ite r3 = 0 (r2Prime; copy) (r2Hi; copy).
    • Every real key, whose p fills p_len octets, takes the r2Hi path.

Proofs:

  • setupP_ok (Proof/Dsa/PPC64LE/Elem.lean) is built from setupPre_ok, which includes topWord_ok, and setupTail_ok, which covers either arm.
    • The r2Hi arm gets its precondition from top_gt: an odd number of 2w digits whose top word is nonzero is above 2^(64(w−1)). It is proved from val32_top, which splits a number at its top word.
    • setupP_ok takes that p is odd. Its callers pass the oddness check they already made.
  • Constant time: setupP_rel (CT/Key.lean) replaces the taint analysis of all of setupP (the old t4) at its four call sites (checkParams and checkKey in CT/Key, DeriveChecked/CT, Prevalidated/CT, PrevalidatedSign/CT).
    • The taint analysis still checks setupPre (t4) and each arm (t4a, t4b, from the P space's registers and the bases).
    • Both runs take the same arm: r3 is zero exactly when the public p's top word is (w_pre0, top_div, zero_iff_agree, rel_ite_zero). The other key checks already argue their branches the same way.

Numbers

Each run below is one run of each side on POWER9, dsa module, with OpenSSL. Both were measured before the restack.

r2Prime against #4667's branch: run 42513407754. Signing, verification, cold verification and key generation were 30–48% faster. A few rows read 64–69%, which overstates the effect, because the base column was noisy: the same code measured 4.54 ms in one run and 8.97 ms in another.

r2Hi against r2Prime (f8299ff against ee64e5b): run 42515529342.

Benchmark Base (r2Prime) Head (r2Hi) Change OpenSSL Head vs OpenSSL
dsa_sign_q160/1024 320.63 µs 244.94 µs 23.6% faster 160.83 µs 1.5×
dsa_sign_q160/2048 1.06 ms 727.48 µs 31.3% faster 384.49 µs 1.9×
dsa_sign_q224/2048 987.86 µs 839.12 µs 15.1% faster 457.49 µs 1.8×
dsa_sign_q256/2048 1.25 ms 976.69 µs 21.6% faster 600.27 µs 1.6×
dsa_sign_q256/3072 2.37 ms 1.95 ms 17.8% faster 1.29 ms 1.5×
dsa_verify_q160/1024 530.23 µs 281.01 µs 47.0% faster 108.70 µs 2.6×
dsa_verify_q160/2048 884.42 µs 757.37 µs 14.4% faster 293.33 µs 2.6×
dsa_verify_q224/2048 1.31 ms 998.38 µs 23.7% faster 401.62 µs 2.5×
dsa_verify_q256/2048 1.44 ms 1.19 ms 17.4% faster 501.14 µs 2.4×
dsa_verify_q256/3072 3.11 ms 2.31 ms 25.7% faster 1.46 ms 1.6×
dsa_verify_cold_q160/1024 1.15 ms 651.86 µs 43.4% faster 126.40 µs 5.2×
dsa_verify_cold_q160/2048 2.23 ms 2.18 ms 2.1% faster 314.33 µs 6.9×
dsa_verify_cold_q224/2048 3.09 ms 2.54 ms 18.0% faster 431.80 µs 5.9×
dsa_verify_cold_q256/2048 3.12 ms 2.76 ms 11.5% faster 538.03 µs 5.1×
dsa_verify_cold_q256/3072 7.76 ms 5.91 ms 23.9% faster 955.53 µs 6.2×
dsa_generate_key_q160/1024 645.11 µs 409.19 µs 36.6% faster 179.89 µs 2.3×
dsa_generate_key_q160/2048 1.83 ms 1.59 ms 13.1% faster 538.07 µs 3.0×
dsa_generate_key_q224/2048 2.87 ms 2.54 ms 11.5% faster 704.18 µs 3.6×
dsa_generate_key_q256/2048 2.93 ms 2.48 ms 15.4% faster 719.15 µs 3.4×
dsa_generate_key_q256/3072 7.47 ms 5.74 ms 23.2% faster 1.49 ms 3.8×

Every benchmark is faster, typically by 12–25%. The spread (2–47%) shows how noisy one run on these shared VMs is. I did not rerun anything.

Together with #4662 and #4667, against the audit (main at 0bb4dbe), DSA on POWER9 goes:

Operation Audit, vs OpenSSL Now, vs OpenSSL
Verification 7.7–8.4× 1.6–2.6×
Signing 2.3–4.9× 1.5–1.9×
Key generation 7.0–7.3× 2.3–3.8×
Cold verification 10–15× 5.1–6.9×

Checks

  • After the restack: lake build (every module, on main's CI cache) and the full emitter pass, and the emitter leaves the tree clean. The Static checks pass.
  • check_lean_public_imports.py is OK on Elem and CT/Key.
  • cargo build --release --target powerpc64le-unknown-linux-gnu --lib builds the regenerated crate. CI's ppc64le jobs run the tests.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHEf743DNZVAyToasZ6

@github-actions github-actions Bot added the untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. label Nov 8, 2026
@alex alex changed the title PPC64LE DSA: R² mod p by r2Prime (half the doublings) PPC64LE DSA: R² mod p by r2Hi (r2Prime when p's top word is zero) Nov 8, 2026
Base automatically changed from claude/ppc64le-dsa-powwin to main November 8, 2026 20:06
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

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

`setupP` computed R² mod p = 2^(128 w) mod p by `Precompute.r2`: 128 w
modular doublings, each over all of p's digits, about as much work as fifty
Montgomery multiplications for a 2048-bit p, once per check of a key or a
domain and per signature or verification. It now computes `-p⁻¹ mod 2³²`
first and uses `Precompute.r2Prime`, proven for any odd p: 65 w doublings
of 64-bit words and six Montgomery squares.

`Precompute.r2Hi` (64 + w doublings) needs p's top word not zero, which
the checks of a key do not establish (they check p's bit length, not that
p fills `p_len` octets), so it is not used here.

`setupP_ok` takes that p is odd, which every caller has checked. No change
to the TCB, the specs or the Rust code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHEf743DNZVAyToasZ6
p is public (the checks of a key already branch on it), so `setupP` now
branches on p's top word: when it is not zero, which every key with p of
`p_len` octets has, `Precompute.r2Hi` (64 + w doublings from
2^(64 (w - 1)), then six Montgomery squares); else `r2Prime`, as before.

`setupPre` loads p, computes `-p⁻¹ mod 2³²`, the bases of arrays 1-3 and
p's top word into r3. `setupP_ok` proves both branches (`top_gt`: an odd
number whose top word is not zero is above 2^(64 (w - 1)), as `r2Hi_ok`
needs). For constant time, `setupP_rel` replaces the taint analysis of all
of `setupP` (t4): the taint analysis checks `setupPre` and each branch,
and both runs take the same branch because r3 is zero exactly when the
public p's top word is (`w_pre0`, `zero_iff_agree`). No change to the TCB,
the specs or the Rust code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHEf743DNZVAyToasZ6
@alex
alex force-pushed the claude/ppc64le-dsa-r2 branch from f8299ff to c14404e Compare November 8, 2026 20:41
@alex
alex marked this pull request as ready for review November 8, 2026 20:41
@alex
alex enabled auto-merge November 8, 2026 20:47
@alex
alex added this pull request to the merge queue Nov 8, 2026
Merged via the queue into main with commit fbc66f8 Nov 8, 2026
23 checks passed
@alex
alex deleted the claude/ppc64le-dsa-r2 branch November 8, 2026 21:19
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