perf: optimize emulated multi-miller loops via sparse×sparse line multiplications for 0-bits#1701
Merged
perf: optimize emulated multi-miller loops via sparse×sparse line multiplications for 0-bits#1701
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the Miller loop in emulated pairing circuits by batching sparse line evaluations 2-by-2 across pairs when processing single lines per pair (0-bit iterations). Instead of multiplying each sparse line individually with the dense accumulator, pairs of sparse lines are first multiplied together using sparse×sparse multiplication to produce a semi-sparse result, which is then more efficiently multiplied with the accumulator. This optimization applies to BLS12-381, BN254, and BW6-761 curves.
Changes:
- Added
Mul02368By02368ThenMulandMulBySemiSparse1_7methods for BLS12-381 to support batched sparse×sparse line multiplication - Updated Miller loop implementations in BLS12-381, BN254, and BW6-761 to batch lines 2-by-2 in 0-bit cases and initial iterations
- Refactored BLS12-381
FinalExponentiationto extract hard part into separatefinalExpHardPartmethod
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
std/algebra/emulated/fields_bls12381/e12_pairing.go |
Adds Mul02368By02368ThenMul for sparse×sparse line multiplication and MulBySemiSparse1_7 for multiplying by semi-sparse elements with zeros at positions 1 and 7 |
std/algebra/emulated/sw_bls12381/pairing.go |
Applies 2-by-2 batching optimization to first iteration and 0-bit cases in Miller loop; refactors final exponentiation hard part into separate method |
std/algebra/emulated/sw_bn254/pairing.go |
Applies 2-by-2 batching to case 0 in main loop and k≥2 in first iteration |
std/algebra/emulated/sw_bw6761/pairing.go |
Applies 2-by-2 batching to k≥2 in first iteration (main loop already had optimization) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ThomasPiellard
approved these changes
Feb 14, 2026
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.
Description
This PR optimizes the Miller loop in emulated pairing circuits by batching sparse line multiplications across pairs. When processing single lines per pair (0-bit iterations), instead of multiplying each line individually with the accumulator, we batch lines 2-by-2 using sparse×sparse multiplication, then multiply the semi-sparse result with the accumulator.
Optimization Pattern
For BLS12-381 and BN254, the optimization applies to:
The key insight is that multiplying two sparse lines together produces a semi-sparse result (with fewer non-zero coefficients than a dense element), which can then be multiplied more efficiently with the dense accumulator.
Changes by Curve
BLS12-381 (
sw_bls12381/pairing.go,fields_bls12381/e12_pairing.go):Mul02368By02368ThenMul: combines sparse×sparse product with dense multiplicationMulBySemiSparse1_7: specialized multiplication where positions 1 and 7 are zeromillerLoopLinesto batch lines 2-by-2 across pairsBN254 (
sw_bn254/pairing.go):Mul01379By01379+MulBy012346789for 2-by-2 batchingBW6-761 (
sw_bw6761/pairing.go):Mul023By023+MulBy02345Type of change
Benchmarks
PairingCheck SCS Constraint Counts in a BN254 circuit
Applications
ECPairBLS precompile:
MulBy02368to 1×Mul02368By02368ThenMulMul01379By01379+MulBy012346789for non-zero bitsHow has this been tested?
TestPairTestSolve,TestPairFixedTestSolve,TestPairingCheckTestSolvepassTestPairingMuxeswith varying pair counts (0-5) passChecklist:
golangci-lintdoes not output errors locallyNote
Medium Risk
Touches core pairing arithmetic (Miller loop multiplication paths) across multiple curves, so any formula/indexing mistake could silently break correctness despite being a performance-focused change.
Overview
Performance optimization for emulated pairing circuits by batching sparse line evaluations during multi-Miller loops, replacing repeated accumulator×line multiplications with sparse×sparse line products followed by a cheaper semi-sparse multiply.
For BLS12-381, adds new
Ext12helpers (Mul02368By02368ThenMulandMulBySemiSparse1_7) and updatesmillerLoopLinesto batch 0-bit iterations across pairs and to combine the two within-pair line multiplications into a single fused operation; it also factors final exponentiation’s hard part intofinalExpHardPart(logic preserved).For BN254 and BW6-761, updates the first-iteration accumulation and 0-bit handling to batch independent lines 2-by-2 using existing sparse×sparse helpers (
Mul01379By01379/MulBy012346789,Mul023By023/MulBy02345). Benchmark stats ininternal/stats/latest_stats.csvare updated accordingly.Written by Cursor Bugbot for commit 4178589. This will update automatically on new commits. Configure here.