-
Notifications
You must be signed in to change notification settings - Fork 224
perf: optimize pre-computation in fixed-argument pairings by batching double steps #798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR optimizes fixed-argument pairing operations by implementing a batch doubling algorithm (manyDoubleSteps) that reduces the number of field inversions. The optimization applies to consecutive zero bits in the loop counter by computing k doublings with a single batch inversion instead of k individual inversions.
Changes:
- Implements
manyDoubleStepsfunction for bn254, bls12-381, bls12-377, bw6-761, bw6-633 curves - Adds
doubleAndAddStepfunction for bls24-315 and bls24-317 curves - Refactors test structure into separate test functions (TestPairing, TestFixedPairing, TestMillerLoop, TestExponentiation, TestTorusCompression)
- Moves reference implementations from separate compatibility test files into main test files
- Unrolls PrecomputeLines loops for better performance
Reviewed changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| internal/generator/pairing/template/tests/pairing.go.tmpl | Test refactoring and reference implementations |
| ecc/*/pairing.go | Implements manyDoubleSteps and unrolled PrecomputeLines |
| ecc/*/pairing_test.go | Reorganized tests with new structure |
| ecc/*/pairing_compatibility_test.go | Deleted (moved to main test file) |
|
|
||
| // manyDoubleSteps performs k consecutive doublings on p and returns the line evaluations. | ||
| // It uses a recurrence to compute 2^k*P with a single batch inversion. | ||
| func (p *G2Affine) manyDoubleSteps(k int, evaluations []LineEvaluationAff) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this unused? Additionally, PrecomputeLines is missing manyDoublesAndAdd, is the batching double not applied to all curves? This seems like an incomplete optimization.
Description
In fixed-argument pairing we use affine coordinates to pre-compute all the lines. The pre-computation loop iterates over a fixed constant with low-Hamming weight. For consecutive zeros, this PR computes
kdoubleStep more efficiently asmanyDoubleSteps(k)following "Efficient Scalar Multiplications on Elliptic Curves without Repeated Doublings and Their Practical Performance" (Alg. 1) by Sakai and Sakurai. Then we mergemanyDoubleSteps(k)withdoubleAndAddStep(from #797) inmanyDoubleStepsAndAdd(k)by batching their respective inverse computations.Following Table 1 in the paper, and benchmarking the cost ratio of inverse and multiplication in
Fp2for each curve, we choosek=3as the crossover point to usemanyDoubleSteps. For BLS24, it is faster to use a loop-based version withdoubleStepanddoubleAndAddStep.Type of change
How has this been tested?
Refactored test into
TestFixedPairing.How has this been benchmarked?
manyDoubleStepsbut onlydoubleAndAddStep(not implemented before).Checklist:
golangci-lintdoes not output errors locallyNote
High Risk
High risk because it changes core pairing precomputation and line evaluation logic across multiple curves using new batch-inversion recurrences; any mistake can silently break cryptographic correctness despite added tests.
Overview
Speeds up fixed-argument pairings by introducing
manyDoubleSteps(batch k doublings with one inversion) and, where beneficial,manyDoublesAndAdd(fuse k doublings +doubleAndAddStepinto one batch inversion) and wiring these intoPrecomputeLinesforbls12-377,bls12-381,bn254,bw6-633, andbw6-761.For
bls24-315/bls24-317, updatesPrecomputeLinesto usedoubleAndAddStep(ELM-based, single-inversion) for non-zero loop counter steps, and addsmanyDoubleStepswhere applicable.Testing is reorganized and strengthened: removes
pairing_compatibility_test.gofiles, moves reference implementations intopairing_test.go, adds property tests comparing optimized vs referencePrecomputeLines/manyDoubleSteps, and updates the test generator template to match the new test structure.Written by Cursor Bugbot for commit 30030d0. This will update automatically on new commits. Configure here.