Skip to content

perf(crypto): optimize BN128 pairing arithmetic - #68

Open
Federico2014 wants to merge 1 commit into
release_v4.8.3from
perf/bn128-arithmetic
Open

Federico2014 wants to merge 1 commit into
release_v4.8.3from
perf/bn128-arithmetic

Conversation

@Federico2014

@Federico2014 Federico2014 commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

What does this PR do?

Reduce BN128 pairing work without changing the intended point-acceptance rule or pairing result.

  • Specialize Fp2 multiplication, squaring, and inversion for i^2 = -1, and multiplication by the Fp6 non-residue 9+i.
  • Route Fp6/Fp12 non-residue operations through the coefficient-field helper, remove the duplicate Fp6 constant, and clarify the Fp2 representation and diagnostic output.
  • Replace the full-order G2 subgroup multiplication with the BN-seed/Frobenius criterion documented in BN128G2. Preserve infinity acceptance and compare points by subtraction.
  • Add four BN128G2Test regressions comparing subgroup acceptance with the full-order [r]P = O oracle, including non-subgroup and projective inputs.

Why are these changes required?

The base implementation performs general finite-field operations for fixed non-residues and a full-order multiplication for each decoded G2 point. These optimizations reduce that work.

This PR has been tested by:

Local validation uses macOS with an x86_64/JDK 8 runtime.

  • Focused Gradle tests: FpTest, Fp12Test, BN128G2Test, and IstanbulTest.altBn128PairingEnergyChangeTest. All 18 tests passed.
  • Outputs from this revision and the base branch matched for 128 Fp2 arithmetic cases, 32 arbitrary twist points with full-order subgroup/cofactor checks, and 18 pairing cases.
  • ./gradlew clean build -x test, the CI Checkstyle tasks, the Math usage check, and git diff --check all passed. The build and tests ran on an identical tree in a standalone temporary clone because the existing Git-properties plugin could not resolve linked-worktree metadata.

The full suite and ARM64/JDK 17 validation have not been run locally.

BN128 precompile benchmark at c80cf21d5f

Benchmarked against release_v4.8.3 on macOS with JDK 8. Each worker used a fixed 512 MiB heap, Parallel GC and -Xbatch.

TVM precompile Base (ms/op) PR head (ms/op) Paired latency reduction Paired allocation reduction
BN128Addition 0.013418 0.012695 1.95% 0.00%
BN128Multiplication 1.262982 1.297352 -1.81% 0.00%
BN128Pairing, 1 pair 18.796760 11.894521 37.80% 26.15%
BN128Pairing, 2 pairs 27.592948 15.616063 42.84% 33.73%
BN128Pairing, 4 pairs 50.261787 27.272365 45.78% 36.90%

A new driver ran the five workloads through the precompiles’ execute entry points. Each revision loaded its own compiled zksnark package ahead of the same FullNode runtime. Each workload rotated through eight deterministic valid inputs. Pairing used a fixed G2 generator and varied G1 points. Both revisions matched the expected output on every call, including both true and false products for the two- and four-pair cases.

Three independent pairs of worker JVMs each ran three warmup rounds and five measured rounds. Base and head ran sequentially in alternating order. Each round timed 256 additions, 16 multiplications and eight calls per pairing workload. Allocation is bytes allocated by the calling thread. Latencies are medians of the three per-worker medians. Each reduction is the median of the three within-worker-pair median percentage reductions, calculated from matched rounds. The measurements cover warmed precompile execution with a nonbinding deadline. Mixed-version timeout behavior requires separate testing.

Follow up

  • Complete multi-platform CI, historical replay, and mixed-version producer/validator testing near CPU deadlines.

Extra details

Consensus impact

The TVM BN128 pairing precompile uses the revised field operations and G2 subgroup check when executing contracts. The intended acceptance criterion is still [r]P = O, including the point at infinity. At c80cf21d5f, comparisons against release_v4.8.3 and the full-order subgroup tests found matching acceptance and pairing outputs for the cases listed above.

Mixed-version validation remains open. TVM enforces a wall-clock CPU deadline. An upgraded producer could finish a pairing call that an older validator times out on. If the validator still returns OUT_OF_TIME after its verification allowance and retry, the receipt mismatch can cause it to reject the block. The PR introduces no activation height. Historical replay and near-deadline tests with both producer/validator version combinations are still pending.

Compatibility and upgrade impact

  • Contracts use the same precompile addresses, input and output encodings, and Energy pricing as on release_v4.8.3. The benchmark measures execution time and allocations.
  • The change requires no chain database migration, configuration update, or P2P, protobuf or RPC schema change.
  • The Java diagnostic format of Fp2.toString() changes from ai + b to a + bi. Tests or tools that compare this text literally must update their expected output.

@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d9e95b3e-be9c-4b08-bc8f-ab654722170d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 6 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

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.

1 participant