perf(crypto): optimize BN128 pairing arithmetic

FE
8d ago
+209-26

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 precompileBase (ms/op)PR head (ms/op)Paired latency reductionPaired allocation reduction
BN128Addition0.0134180.0126951.95%0.00%
BN128Multiplication1.2629821.297352-1.81%0.00%
BN128Pairing, 1 pair18.79676011.89452137.80%26.15%
BN128Pairing, 2 pairs27.59294815.61606342.84%33.73%
BN128Pairing, 4 pairs50.26178727.27236545.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.