Repository navigation
perf(crypto): optimize BN128 pairing arithmetic - #68
Open
Federico2014 wants to merge 1 commit into
Open
Federico2014 wants to merge 1 commit into
Federico2014 wants to merge 1 commit into
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
There was a problem hiding this comment.
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
Federico2014
force-pushed
the
perf/bn128-arithmetic
branch
from
November 18, 2026 10:53
c80cf21 to
8cc12de
Compare
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.
What does this PR do?
Reduce BN128 pairing work without changing the intended point-acceptance rule or pairing result.
i^2 = -1, and multiplication by the Fp6 non-residue9+i.BN128G2. Preserve infinity acceptance and compare points by subtraction.BN128G2Testregressions comparing subgroup acceptance with the full-order[r]P = Ooracle, 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.
FpTest,Fp12Test,BN128G2Test, andIstanbulTest.altBn128PairingEnergyChangeTest. All 18 tests passed../gradlew clean build -x test, the CI Checkstyle tasks, the Math usage check, andgit diff --checkall 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
c80cf21d5fBenchmarked against
release_v4.8.3on macOS with JDK 8. Each worker used a fixed 512 MiB heap, Parallel GC and-Xbatch.A new driver ran the five workloads through the precompiles’
executeentry 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
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. Atc80cf21d5f, comparisons againstrelease_v4.8.3and 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_TIMEafter 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
release_v4.8.3. The benchmark measures execution time and allocations.Fp2.toString()changes fromai + btoa + bi. Tests or tools that compare this text literally must update their expected output.