Skip to content

perf(precompile): use fused multi-miller loop for BLS12-381 pairing - #3823

Merged
rakita merged 2 commits into
bluealloy:mainfrom
4waan:perf/bls12-381-fused-pairing
Jul 24, 2026
Merged

rakita merged 2 commits into
bluealloy:mainfrom
4waan:perf/bls12-381-fused-pairing

Conversation

@4waan

@4waan 4waan commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

What

Replace the per-pair miller loop and fp12 multiply accumulation in the blst pairing_check with a single blst_miller_loop_n call followed by one final exponentiation, sharing the per-bit fp12 squaring across all pairs.

This brings the blst backend to parity with the arkworks backend, which already fuses via multi_pairing.

Result

Bit-identical output (the product of the individual miller loops equals the fused product) — only faster. Benchmark via the existing eip2537 pairing bench, end-to-end through the precompile:

pairs change
1 ~0% (nothing to fuse)
2 ~6%
8 ~21%
16 ~30%

Notes

  • Uses the raw FFI blst_miller_loop_n, not blst's miller_loop_n Rust wrapper, which fans out to a thread pool — undesirable inside a precompile and unavailable on no_std targets. This keeps the computation single-threaded and deterministic.
  • Pairing correctness is covered by the existing EIP-2537 execution-spec-tests.

Replace the per-pair miller loop and fp12 multiply accumulation in the blst `pairing_check` with a single `blst_miller_loop_n` call, sharing the per-bit fp12 squaring across all pairs. This mirrors the arkworks backend, which already fuses via `multi_pairing`.

Uses the raw FFI rather than blst's `miller_loop_n` Rust wrapper, which fans out to a thread pool (undesirable inside a precompile and unavailable on no_std targets), keeping the computation single-threaded and deterministic. The result is bit-identical; only faster.

Bench (eip2537, end-to-end precompile): ~6% at 2 pairs, ~21% at 8, ~30% at 16.
@codspeed

codspeed Bot commented Jul 23, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 21.46%

⚡ 3 improved benchmarks
✅ 178 untouched benchmarks
⏩ 1 skipped benchmark1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ Simulation pairing (16 pairs) 36.5 ms 28 ms +30.25%
⚡ Simulation pairing (8 pairs) 19.4 ms 15.4 ms +25.67%
⚡ Simulation pairing (2 pairs) 6.5 ms 5.9 ms +9.47%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing 4waan:perf/bls12-381-fused-pairing (d9b5e8b) with main (46c64ad)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩

blst_miller_loop_n does not special-case points at infinity, so filter
such pairs out before the fused pairing check (e.g. the proof of a
constant polynomial is the point at infinity). Add tests for the valid
constant-polynomial proof and the invalid mixed-infinity case, and
simplify pair collection with unzip/filter.
@rakita

rakita commented Jul 24, 2026

Copy link
Copy Markdown
Member

Nice one!

Have pushed commit that would skip infinity points in kzg as blst_miller_loop had it. For bls precompile, we are already skipping those.

@rakita
rakita merged commit 3cd8b85 into bluealloy:main Jul 24, 2026
34 checks passed
This was referenced Jul 24, 2026
4waan added a commit to 4waan/revm that referenced this pull request Aug 7, 2026
Move z from G2 to G1 in the EIP-4844 point-evaluation pairing check via
bilinearity `e(proof, -[z]G2) = e([z]·proof, -G2)`, so both G2 pairing
inputs collapse to the constants -G2 and [τ]G2:

  e(D, -G2) · e(proof, [τ]G2) == 1,  D = commitment - [y]G1 + [z]·proof

This replaces the per-call G2 scalar multiplication (plus a G2 subtraction
and its Fp2 inversion) with a single cheaper G1 scalar multiplication, in
both the arkworks and blst backends. c-kzg is unchanged (external).

- arkworks: cache the two now-constant G2 points as G2Prepared (Miller-loop
  lines computed once) and run multi_miller_loop + one final exponentiation,
  which still fuses the per-bit squaring across pairs. Fewer G2 ops and cached
  line prep with no loss of fusion.
- blst: keep the fused pairing_check (blst_miller_loop_n from bluealloy#3823); only the
  reformulation applies. Precomputed lines are deliberately not used: blst has
  no fused-multi-loop-with-precomputed-lines primitive, so precomputing would
  forfeit the bluealloy#3823 fusion. That trade is left for a follow-up benchmark.

Tests: add an all-finite tampered-y rejection (pins the pairing relation to
the correct evaluation using the c-kzg vector as oracle) and a D=∞ /
finite-proof rejection (exercises the mirror infinity-filter path introduced
by moving z to G1).
@github-actions github-actions Bot mentioned this pull request Aug 18, 2026
RealiCZ added a commit to megaeth-labs/revm that referenced this pull request Sep 18, 2026
…luealloy#3784 bluealloy#3823 bluealloy#3840 (#57)

Co-authored-by: Saylor Mike <[email protected]>
Co-authored-by: Matthias Seitz <[email protected]>
Co-authored-by: alpharush <[email protected]>
Co-authored-by: a <[email protected]>
Co-authored-by: rakita <[email protected]>
@RealiCZ RealiCZ mentioned this pull request Sep 18, 2026
30 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants