perf: optimize precomputation in fixed-argument pairings#797
perf: optimize precomputation in fixed-argument pairings#797
Conversation
There was a problem hiding this comment.
Pull request overview
This PR optimizes the doubleAndAddStep function in fixed-argument pairing by using Montgomery's batch inversion technique to compute two slopes with a single field inversion instead of two. The optimization is based on the Eisenträger-Lauter-Montgomery formula from https://eprint.iacr.org/2003/257 and achieves performance improvements ranging from 0.52% to 21.07% depending on the curve.
Changes:
- Optimized
doubleAndAddStepin four curves (bn254, bls12-381, bls12-377, bw6-761) to use batch inversion - Added
BenchmarkPrecomputeLinesto the pairing test template and all curve implementations
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| internal/generator/pairing/template/tests/pairing.go.tmpl | Added benchmark template for PrecomputeLines function |
| ecc/bn254/pairing.go | Optimized doubleAndAddStep using Montgomery batch inversion (21.07% improvement) |
| ecc/bn254/pairing_test.go | Added PrecomputeLines benchmark (generated from template) |
| ecc/bls12-381/pairing.go | Optimized doubleAndAddStep using Montgomery batch inversion (3.83% improvement) |
| ecc/bls12-381/pairing_test.go | Added PrecomputeLines benchmark (generated from template) |
| ecc/bls12-377/pairing.go | Optimized doubleAndAddStep using Montgomery batch inversion (4.24% improvement) |
| ecc/bls12-377/pairing_test.go | Added PrecomputeLines benchmark (generated from template) |
| ecc/bw6-761/pairing.go | Optimized doubleAndAddStep using Montgomery batch inversion (0.52% improvement) |
| ecc/bw6-761/pairing_test.go | Added PrecomputeLines benchmark (generated from template) |
| ecc/bw6-633/pairing_test.go | Added PrecomputeLines benchmark for consistency (no optimization - uses separate double/add steps) |
| ecc/bls24-317/pairing_test.go | Added PrecomputeLines benchmark for consistency (no optimization - uses separate double/add steps) |
| ecc/bls24-315/pairing_test.go | Added PrecomputeLines benchmark for consistency (no optimization - uses separate double/add steps) |
ivokub
left a comment
There was a problem hiding this comment.
Looks good to me! I also added compatibility test to ensure the new implementation is equivalent to the old one. See if it makes sense to you. Good to merge!
Yeah that makes sense. Good to merge. |
Description
This PR pptimizes
doubleAndAddStepin fixed-argument pairing by computing2P+Qwith a single field inversion instead of two. Based on https://eprint.iacr.org/2003/257, this uses Montgomery's batch inversion to compute both slopesλ1andλ2together:1/A = U/(A·U) and 1/U = A/(A·U)with a single inversion ofA·UwhereA = x1 - x2andU = B² - (2x1 + x2)·A².This speeds up the hints computation in gnark.
Changes:
Type of change
How has this been tested?
MillerLoopandMillerLoopFixedQoutput the same result.How has this been benchmarked?
Benchmarking
PrecomputeLineson a AWS c7a.8xlarge:Checklist:
golangci-lintdoes not output errors locallyNote
Optimizes fixed-argument pairing precomputation by computing
2P+Qwith a single inversion via the Eisenträger–Lauter–Montgomery formula and Montgomery batch inversion.doubleAndAddStepinbn254,bls12-381,bls12-377,bw6-761to use batch-inversion forλ1/λ2(one field inversion instead of two)doubleAndAddStepRefand property-based equivalence tests to verify optimized vs. reference outputsBenchmarkPrecomputeLinesacross pairing tests and updates the test template to generate the new test/benchmarkNo API changes; behavior is validated to match previous outputs while improving precompute performance.
Written by Cursor Bugbot for commit b7ee69d. This will update automatically on new commits. Configure here.