Skip to content

x86-64 vg_ecdsa_p256_verify_ifma: the scalars' digits without branches on their bits - #4988

Merged
alex merged 2 commits into
mainfrom
claude/blissful-faraday-ofi3p6-prepb
Nov 12, 2026
Merged

alex merged 2 commits into
mainfrom
claude/blissful-faraday-ofi3p6-prepb

Conversation

@alex

@alex alex commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

vg_ecdsa_p256_verify_ifma made the digits of u and v (Joint.prep: FastNaf.prepN16 for the generator's, of width nine, and FastNaf.prepN for the peer's, of width five) with a branch on each bit of the scalar that is not skipped (is the residual odd?) and one on each digit's sign (FastNaf.choose). The scalars are public, so the branches are allowed, but for a scalar the processor has not seen it cannot predict them.

Joint.prepB makes the same digits with no branch but the loop's. A step (FastNaf.stepB, stepB16):

  • digitB w: the low w bits of the residual as a signed number (and, xor and sub of 2^(w-1)), and zero if the residual is even (and with the mask 0 - (r8 & 1));
  • Naf.subtractDigitN and the store of the digit, as before, now at every step (a zero digit subtracts nothing and stores zero where zero is);
  • countB e: the shift, 1 or 1 + 2^e (the width), from the digit's low bit, in rdx, and 64 less it in rcx;
  • shiftV n: the residual shifted right by rdx (shrx, shlx and or for each pair of words: BMI2, which the variant already needs);
  • add rbx, rdx and the loop's cmp.

No change to TCB/ or Spec/. The x86-64 crate changes only in ecdsa_p256, and there only in vg_ecdsa_p256_verify_ifma (23,863 instructions for 23,907): the other joint verifiers keep Joint.prep, whose code the variants without BMI2 run too.

This is independent of #4985, which changes the same function: whichever merges second needs main merged in and the crate regenerated.

Measurements

The repository's benchmark shows this change slightly slower; a measurement over different signatures shows it faster. Which one counts is the reviewer's call.

Two Benchmarks runs, scoped to ecdsa_p256 verification on x86-64, against main (f3525be), with five rounds each (set before the first: #4985's run of the same benchmarks, with one round, spread over ±2%):

  • The first got an EPYC 7763, which has no AVX512_IFMA, so it measured vg_ecdsa_p256_verify_adx, which this PR does not change: from 0.1% slower to 0.1% faster. It says nothing about the change.
  • The second names the variant's features (cpu_features), so that it measures the variant or nothing. On an EPYC 9V45:
Benchmark Base Head Change
ecdsa_p256_sha256_verify/64 24.39 µs 24.44 µs 0.2% slower
ecdsa_p256_sha256_verify/1024 26.72 µs 26.84 µs 0.5% slower
ecdsa_p256_sha256_verify/16384 57.43 µs 57.61 µs 0.3% slower
ecdsa_p256_sha384_verify/64 24.50 µs 24.60 µs 0.4% slower
ecdsa_p256_sha384_verify/1024 25.46 µs 25.62 µs 0.7% slower
ecdsa_p256_sha384_verify/16384 43.84 µs 43.61 µs 0.5% faster

The benchmark verifies the same signature in every iteration, so the recoders' branches go the same way every time. On the machine the proofs were built on (an Emerald Rapids Xeon VM), with a harness that times the function alone and takes the least time of each signature:

  • 300 different signatures in turn, 60 times over: the new code takes 1.4% to 1.5% less time than main's (seven runs: 1.43% to 1.49%).
  • One signature 30,000 times in a row, for each of five signatures: the new code takes 0.7% to 0.8% more time (0.65%, 0.76%, 0.80%, 0.65%, 0.74%), as in the run above.

So my reading, which I measured only as far as these two cases: where the processor has seen the scalars before, it predicts the old code's branches and the new code's extra work (a subtraction, a store and a shift at every bit) costs 0.2% to 0.8%; where it has not, as when every verification is of a different signature, the old code's mispredicted branches cost more than that. I did not measure a workload with other code between the verifications.

The proofs

The recoders' lemmas are now stated for any step that does what stepN does, and the old and the new recoders are instances:

  • Proof/Weierstrass/X86_64/FastNafShift.lean: fastShiftV_ok, the shift by a count in a register (one, five, seven or nine).
  • FastNafChoose.lean: fastSel, what digitB leaves, is the digit (fastSel_eq, for an odd and for an even residual) and has the residual's low bit (fastSel_low); digitB_ok, countB_ok, fastAdvanceV_ok.
  • FastNafStep.lean, FastNaf16.lean: fastPrepStepB_ok and fastPrepStepB16_ok, with the statements of fastPrepStep_ok and fastPrepStep16_ok.
  • FastNafPrep.lean, FastNaf16.lean: FastStepOk, FastStepOk16 (a step's statement); the loops for any such step (fastPrepOf_ok, fastPrepDigitsOf_ok, and the 16 ones), of which fastPrep_ok, fastPrepDigits_ok and fastPrepDigitsB_ok are instances.
  • FastNafTiming.lean, FastNaf16Timing.lean: the same for the timing (fastPrepOf_relCT, fastPrep16Of_relCT), with the checks of the new steps (FastPrepChecksB, FastPrepChecksB16) and fastPrepB_relCT, fastPrepB16_relCT.
  • JointPrep.lean, JointPrepFields.lean, JointMul.lean, JointPrepTiming.lean, JointMulTiming.lean: what the two recoders leave is a definition (JointPrepPost), the lemmas about Joint.prep are instances of lemmas about any code that leaves it (jointPrepOf_ok, jointPrepFieldsOf_ok, jointMulPrepOf_ok, jointPrepOf_relCT, jointMulPrepOf_relCT), and Joint.prepB has its own (jointPrepB_ok, jointMulPrepB_ok, jointPrepB_relCT, jointMulPrepB_relCT).
  • Proof/P256/X86_64/Ifma/JointChecks.lean: the taint checks of the two new recoders (fastGeneratorIfmaB_checks, fastPeerIfmaB_checks).
  • Proof/Ecdsa/Verify/X86_64/JointIfma.lean: the verifier's proofs with Joint.prepB.

Checks

lake build --log-level=warning and lake env lean --run Emit.lean. Every step of the Static checks job, ci/check_lean_public_imports.py on the modules changed, cargo fmt --check, cargo clippy --all-targets -- -D warnings, and cargo test with Wycheproof on a CPU with AVX512_IFMA.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHFEUkKJRQvsEBN9oBp

…s on their bits

The recoders of u and v (Joint.prep: FastNaf.prepN16 and prepN) branch on
each bit of the scalar that is not skipped and on each digit's sign. The
scalars are public, but the processor mispredicts about half of those
branches. Joint.prepB (FastNaf.prepB16, prepB) makes the same digits with
no branch but the loop's: the digit by a mask of the residual's low bit
(digitB), subtracted and stored at every step, and the shift by one or by
the width from that bit, with BMI2's shrx and shlx (countB, shiftV).

Only vg_ecdsa_p256_verify_ifma uses it: the other joint verifiers keep
Joint.prep, whose code the variants without BMI2 run too.

Proofs: the recoders' lemmas are stated for any step that does what stepN
does (FastStepOk, FastStepOk16; fastPrepOf_ok, fastPrepOf_relCT and the 16
ones), and the lemmas about Joint.prep for any code that leaves what the
recoders leave (JointPrepPost; jointPrepOf_ok, jointMulPrepOf_ok,
jointPrepOf_relCT, jointMulPrepOf_relCT), with the old and the new
recoders as instances.

No change to TCB/ or Spec/.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFEUkKJRQvsEBN9oBp
@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 added this pull request to the merge queue Nov 12, 2026
Merged via the queue into main with commit 8b5bfea Nov 12, 2026
33 checks passed
@alex
alex deleted the claude/blissful-faraday-ofi3p6-prepb branch November 12, 2026 12:11
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
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