Repository navigation
x86-64 vg_ecdsa_p256_verify_ifma: the scalars' digits without branches on their bits - #4988
Merged
Merged
Conversation
…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
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFEUkKJRQvsEBN9oBp
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.
vg_ecdsa_p256_verify_ifmamade the digits ofuandv(Joint.prep:FastNaf.prepN16for the generator's, of width nine, andFastNaf.prepNfor 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.prepBmakes the same digits with no branch but the loop's. A step (FastNaf.stepB,stepB16):digitB w: the lowwbits of the residual as a signed number (and,xorandsubof2^(w-1)), and zero if the residual is even (andwith the mask0 - (r8 & 1));Naf.subtractDigitNand 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,1or1 + 2^e(the width), from the digit's low bit, inrdx, and 64 less it inrcx;shiftV n: the residual shifted right byrdx(shrx,shlxandorfor each pair of words: BMI2, which the variant already needs);add rbx, rdxand the loop'scmp.No change to
TCB/orSpec/. The x86-64 crate changes only inecdsa_p256, and there only invg_ecdsa_p256_verify_ifma(23,863 instructions for 23,907): the other joint verifiers keepJoint.prep, whose code the variants without BMI2 run too.This is independent of #4985, which changes the same function: whichever merges second needs
mainmerged 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_p256verification on x86-64, againstmain(f3525be), with five rounds each (set before the first: #4985's run of the same benchmarks, with one round, spread over ±2%):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.cpu_features), so that it measures the variant or nothing. On an EPYC 9V45:ecdsa_p256_sha256_verify/64ecdsa_p256_sha256_verify/1024ecdsa_p256_sha256_verify/16384ecdsa_p256_sha384_verify/64ecdsa_p256_sha384_verify/1024ecdsa_p256_sha384_verify/16384The 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:
main's (seven runs: 1.43% to 1.49%).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
stepNdoes, 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, whatdigitBleaves, 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_okandfastPrepStepB16_ok, with the statements offastPrepStep_okandfastPrepStep16_ok.FastNafPrep.lean,FastNaf16.lean:FastStepOk,FastStepOk16(a step's statement); the loops for any such step (fastPrepOf_ok,fastPrepDigitsOf_ok, and the16ones), of whichfastPrep_ok,fastPrepDigits_okandfastPrepDigitsB_okare instances.FastNafTiming.lean,FastNaf16Timing.lean: the same for the timing (fastPrepOf_relCT,fastPrep16Of_relCT), with the checks of the new steps (FastPrepChecksB,FastPrepChecksB16) andfastPrepB_relCT,fastPrepB16_relCT.JointPrep.lean,JointPrepFields.lean,JointMul.lean,JointPrepTiming.lean,JointMulTiming.lean: what the two recoders leave is a definition (JointPrepPost), the lemmas aboutJoint.prepare instances of lemmas about any code that leaves it (jointPrepOf_ok,jointPrepFieldsOf_ok,jointMulPrepOf_ok,jointPrepOf_relCT,jointMulPrepOf_relCT), andJoint.prepBhas 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 withJoint.prepB.Checks
lake build --log-level=warningandlake env lean --run Emit.lean. Every step of theStatic checksjob,ci/check_lean_public_imports.pyon the modules changed,cargo fmt --check,cargo clippy --all-targets -- -D warnings, andcargo testwith Wycheproof on a CPU with AVX512_IFMA.🤖 Generated with Claude Code
https://claude.ai/code/session_01TKUFHFEUkKJRQvsEBN9oBp