From 8cc12de444a16a9d9d451ba3ebbfef0517bb64c7 Mon Sep 17 00:00:00 2001 From: federico Date: Wed, 18 Nov 2026 18:04:38 +0800 Subject: [PATCH] perf(crypto): optimize BN128 pairing arithmetic --- .../tron/common/crypto/zksnark/BN128G2.java | 22 ++- .../org/tron/common/crypto/zksnark/Fp.java | 1 + .../org/tron/common/crypto/zksnark/Fp12.java | 16 +- .../org/tron/common/crypto/zksnark/Fp2.java | 19 +- .../org/tron/common/crypto/zksnark/Fp6.java | 4 +- .../common/crypto/zksnark/BN128G2Test.java | 173 ++++++++++++++++++ 6 files changed, 209 insertions(+), 26 deletions(-) create mode 100644 framework/src/test/java/org/tron/common/crypto/zksnark/BN128G2Test.java diff --git a/crypto/src/main/java/org/tron/common/crypto/zksnark/BN128G2.java b/crypto/src/main/java/org/tron/common/crypto/zksnark/BN128G2.java index cfa514bde9b..36247145737 100644 --- a/crypto/src/main/java/org/tron/common/crypto/zksnark/BN128G2.java +++ b/crypto/src/main/java/org/tron/common/crypto/zksnark/BN128G2.java @@ -17,12 +17,10 @@ */ package org.tron.common.crypto.zksnark; -import static org.tron.common.crypto.zksnark.Params.R; +import static org.tron.common.crypto.zksnark.Params.PAIRING_FINAL_EXPONENT_Z; import static org.tron.common.crypto.zksnark.Params.TWIST_MUL_BY_P_X; import static org.tron.common.crypto.zksnark.Params.TWIST_MUL_BY_P_Y; -import java.math.BigInteger; - /** * Implementation of specific cyclic subgroup of points belonging to {@link BN128Fp2}
Members * of this subgroup are passed as a second param to pairing input {@link @@ -39,8 +37,6 @@ */ public class BN128G2 extends BN128Fp2 { - static final BigInteger FR_NEG_ONE = BigInteger.ONE.negate().mod(R); - BN128G2(BN128 p) { super(p.x, p.y, p.z); } @@ -71,8 +67,20 @@ public static BN128G2 create(byte[] a, byte[] b, byte[] c, byte[] d) { } private static boolean isGroupMember(BN128 p) { - BN128 left = p.mul(FR_NEG_ONE).add(p); - return left.isZero(); // should satisfy condition: -1 * p + p == 0, where -1 belongs to F_r + if (p.isZero()) { + return true; + } + // For this BN curve, [r]P = O iff [z+1]P + psi([z]P) + psi^2([z]P) + // equals psi^3([2z]P), where psi is mulByP and z is the 63-bit BN seed. + // See https://eprint.iacr.org/2022/348, Sections 3 and 5.1. + BN128G2 zp = new BN128G2(p.mul(PAIRING_FINAL_EXPONENT_Z)); + BN128G2 a = zp.mulByP(); + BN128G2 b = a.mulByP(); + BN128G2 c = b.mulByP(); + BN128 left = zp.add(p).add(a).add(b); + BN128 right = c.add(c); + // equals() compares raw Jacobian coordinates, so compare points by subtraction. + return left.add(new BN128G2(right.x, right.y.negate(), right.z)).isZero(); } @Override diff --git a/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp.java b/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp.java index 768a23e4fda..203491a1094 100644 --- a/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp.java +++ b/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp.java @@ -32,6 +32,7 @@ public class Fp implements Field { static final Fp ZERO = new Fp(BigInteger.ZERO); static final Fp _1 = new Fp(BigInteger.ONE); + // beta = -1 defines the next extension Fp2, where i^2 = beta. static final Fp NON_RESIDUE = new Fp(new BigInteger( "21888242871839275222246405745257275088696311157297823662689037894645226208582")); diff --git a/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp12.java b/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp12.java index 8733d2297fb..1f4c4d8fd60 100644 --- a/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp12.java +++ b/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp12.java @@ -136,14 +136,14 @@ Fp12 mulBy024(Fp2 ell0, Fp2 ellVW, Fp2 ellVV) { // For z.a_.a_ = z0. s1 = z1.mul(x2); t3 = s1.add(d4); - t4 = Fp6.NON_RESIDUE.mul(t3).add(d0); + t4 = t3.mulByNonResidue().add(d0); z0 = t4; // For z.a_.b_ = z1 t3 = z5.mul(x4); s1 = s1.add(t3); t3 = t3.add(d2); - t4 = Fp6.NON_RESIDUE.mul(t3); + t4 = t3.mulByNonResidue(); t3 = z1.mul(x0); s1 = s1.add(t3); t4 = t4.add(t3); @@ -161,7 +161,7 @@ Fp12 mulBy024(Fp2 ell0, Fp2 ellVW, Fp2 ellVV) { z2 = t3; t1 = x2.add(x4); t3 = t0.mul(t1).sub(d2).sub(d4); - t4 = Fp6.NON_RESIDUE.mul(t3); + t4 = t3.mulByNonResidue(); t3 = z3.mul(x0); s1 = s1.add(t3); t4 = t4.add(t3); @@ -170,7 +170,7 @@ Fp12 mulBy024(Fp2 ell0, Fp2 ellVW, Fp2 ellVV) { // For z.b_.b_ = z4 t3 = z5.mul(x2); s1 = s1.add(t3); - t4 = Fp6.NON_RESIDUE.mul(t3); + t4 = t3.mulByNonResidue(); t0 = x0.add(x4); t3 = t2.mul(t0).sub(d0).sub(d4); t4 = t4.add(t3); @@ -259,15 +259,15 @@ Fp12 cyclotomicSquared() { // t0 + t1*y = (z0 + z1*y)^2 = a^2 tmp = z0.mul(z1); - t0 = z0.add(z1).mul(z0.add(Fp6.NON_RESIDUE.mul(z1))).sub(tmp).sub(Fp6.NON_RESIDUE.mul(tmp)); + t0 = z0.add(z1).mul(z0.add(z1.mulByNonResidue())).sub(tmp).sub(tmp.mulByNonResidue()); t1 = tmp.add(tmp); // t2 + t3*y = (z2 + z3*y)^2 = b^2 tmp = z2.mul(z3); - t2 = z2.add(z3).mul(z2.add(Fp6.NON_RESIDUE.mul(z3))).sub(tmp).sub(Fp6.NON_RESIDUE.mul(tmp)); + t2 = z2.add(z3).mul(z2.add(z3.mulByNonResidue())).sub(tmp).sub(tmp.mulByNonResidue()); t3 = tmp.add(tmp); // t4 + t5*y = (z4 + z5*y)^2 = c^2 tmp = z4.mul(z5); - t4 = z4.add(z5).mul(z4.add(Fp6.NON_RESIDUE.mul(z5))).sub(tmp).sub(Fp6.NON_RESIDUE.mul(tmp)); + t4 = z4.add(z5).mul(z4.add(z5.mulByNonResidue())).sub(tmp).sub(tmp.mulByNonResidue()); t5 = tmp.add(tmp); // for A @@ -284,7 +284,7 @@ Fp12 cyclotomicSquared() { // for B // z2 = 3 * (xi * t5) + 2 * z2 - tmp = Fp6.NON_RESIDUE.mul(t5); + tmp = t5.mulByNonResidue(); z2 = tmp.add(z2); z2 = z2.add(z2); z2 = z2.add(tmp); diff --git a/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp2.java b/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp2.java index 162a5b13b30..b30e7432c6b 100644 --- a/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp2.java +++ b/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp2.java @@ -23,7 +23,7 @@ * Arithmetic in F_p2

* * "p" equals 21888242871839275222246405745257275088696311157297823662689037894645226208583, - * elements of F_p2 are represented as a polynomials "a * i + b" modulo "i^2 + 1" from the ring + * elements of F_p2 are represented as polynomials "a + b * i" modulo "i^2 + 1" from the ring * F_p[i]

* * Field arithmetic is ported from libff @@ -36,7 +36,6 @@ class Fp2 implements Field { static final Fp2 ZERO = new Fp2(Fp.ZERO, Fp.ZERO); static final Fp2 _1 = new Fp2(Fp._1, Fp.ZERO); - static final Fp2 NON_RESIDUE = new Fp2(BigInteger.valueOf(9), BigInteger.ONE); static final Fp[] FROBENIUS_COEFFS_B = new Fp[]{ new Fp(BigInteger.ONE), @@ -79,9 +78,7 @@ public Fp2 squared() { Fp ab = a.mul(b); - Fp ra = a.add(b).mul(b.mul(Fp.NON_RESIDUE).add(a)) - .sub(ab) - .sub(ab.mul(Fp.NON_RESIDUE)); // ra = (a + b)(a + NON_RESIDUE * b) - ab - NON_RESIDUE * b + Fp ra = a.add(b).mul(a.sub(b)); // i^2 = -1, so ra = a^2 - b^2 Fp rb = ab.dbl(); return new Fp2(ra, rb); @@ -93,7 +90,7 @@ public Fp2 mul(Fp2 o) { Fp aa = a.mul(o.a); Fp bb = b.mul(o.b); - Fp ra = bb.mul(Fp.NON_RESIDUE).add(aa); // ra = a1 * a2 + NON_RESIDUE * b1 * b2 + Fp ra = aa.sub(bb); // ra = a1 * a2 - b1 * b2 Fp rb = a.add(b).mul(o.a.add(o.b)).sub(aa) .sub(bb); // rb = (a1 + b1)(a2 + b2) - a1 * a2 - b1 * b2 @@ -120,7 +117,7 @@ public Fp2 inverse() { Fp t0 = a.squared(); Fp t1 = b.squared(); - Fp t2 = t0.sub(Fp.NON_RESIDUE.mul(t1)); + Fp t2 = t0.add(t1); Fp t3 = t2.inverse(); Fp ra = a.mul(t3); // ra = a * t3 @@ -175,12 +172,16 @@ Fp2 frobeniusMap(int power) { return new Fp2(ra, rb); } + /** Multiplies this Fp2 element by xi = 9 + i, the defining parameter of Fp6. */ Fp2 mulByNonResidue() { - return NON_RESIDUE.mul(this); + // (a + bi)(9 + i) = (9a - b) + (a + 9b)i + Fp a9 = a.dbl().dbl().dbl().add(a); + Fp b9 = b.dbl().dbl().dbl().add(b); + return new Fp2(a9.sub(b), a.add(b9)); } @Override public String toString() { - return String.format("%si + %s", a.toString(), b.toString()); + return String.format("%s + %si", a.toString(), b.toString()); } } diff --git a/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp6.java b/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp6.java index fb863bb6b34..4a344c0c93d 100644 --- a/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp6.java +++ b/crypto/src/main/java/org/tron/common/crypto/zksnark/Fp6.java @@ -34,7 +34,6 @@ class Fp6 implements Field { static final Fp6 ZERO = new Fp6(Fp2.ZERO, Fp2.ZERO, Fp2.ZERO); static final Fp6 _1 = new Fp6(Fp2._1, Fp2.ZERO, Fp2.ZERO); - static final Fp2 NON_RESIDUE = new Fp2(BigInteger.valueOf(9), BigInteger.ONE); static final Fp2[] FROBENIUS_COEFFS_B = { new Fp2(BigInteger.ONE, @@ -148,9 +147,10 @@ Fp6 mul(Fp2 o) { return new Fp6(ra, rb, rc); } + /** Multiplies by v, where v^3 = 9 + i and Fp12 is defined by w^2 = v. */ Fp6 mulByNonResidue() { - Fp2 ra = NON_RESIDUE.mul(c); + Fp2 ra = c.mulByNonResidue(); Fp2 rb = a; Fp2 rc = b; diff --git a/framework/src/test/java/org/tron/common/crypto/zksnark/BN128G2Test.java b/framework/src/test/java/org/tron/common/crypto/zksnark/BN128G2Test.java new file mode 100644 index 00000000000..0d2d19abc77 --- /dev/null +++ b/framework/src/test/java/org/tron/common/crypto/zksnark/BN128G2Test.java @@ -0,0 +1,173 @@ +package org.tron.common.crypto.zksnark; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Method; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; + +/** + * Verifies the G2 subgroup acceptance rule used when decoding pairing inputs. The private + * predicate is compared against the full-order oracle {@code [r]P = O} for affine, projective, + * infinity, and non-subgroup points. + */ +public class BN128G2Test { + + private static final BigInteger TWO = BigInteger.valueOf(2); + private static final BigInteger P = Params.P; + private static final BigInteger SQRT_EXPONENT = P.add(BigInteger.ONE).shiftRight(2); + + // Standard alt_bn128 G2 generator, with Fp2 elements represented as a + b * i. + private static final BigInteger G2_X_A = new BigInteger( + "10857046999023057135944570762232829481370756359578518086990519993285655852781"); + private static final BigInteger G2_X_B = new BigInteger( + "11559732032986387107991004021392285783925812861821192530917403151452391805634"); + private static final BigInteger G2_Y_A = new BigInteger( + "8495653923123431417604973247489272438418190587263600148770280649306958101930"); + private static final BigInteger G2_Y_B = new BigInteger( + "4082367875863433681332203403145435568316851327593401208105741076214120093531"); + + @Test + public void testInfinityIsGroupMember() { + assertNotNull(BN128G2.create(bytes(0), bytes(0), bytes(0), bytes(0))); + } + + @Test + public void testGeneratorIsGroupMember() { + assertNotNull(createG2(G2_X_A, G2_X_B, G2_Y_A, G2_Y_B)); + } + + @Test + public void testPredicateMatchesFullOrderMultiplication() throws Exception { + List> points = new ArrayList<>(); + BN128 generator = createAffine(G2_X_A, G2_X_B, G2_Y_A, G2_Y_B); + assertNotNull(generator); + points.add(generator); + points.addAll(curvePoints(6)); + + boolean sawNonSubgroup = false; + for (BN128 point : points) { + boolean inSubgroup = point.mul(Params.R).isZero(); + assertEquals("predicate disagrees with [r]P = O for " + point, + inSubgroup, isGroupMember(point)); + + BN128G2 accepted = createG2( + new BigInteger(1, point.x.a.bytes()), new BigInteger(1, point.x.b.bytes()), + new BigInteger(1, point.y.a.bytes()), new BigInteger(1, point.y.b.bytes())); + assertEquals("[r]P = O disagrees with create() for " + point, inSubgroup, accepted != null); + + if (!inSubgroup) { + sawNonSubgroup = true; + } + } + assertTrue("expected at least one non-subgroup point", sawNonSubgroup); + } + + @Test + public void testPredicateMatchesFullOrderForProjectiveInputs() throws Exception { + BN128 generator = createAffine(G2_X_A, G2_X_B, G2_Y_A, G2_Y_B); + assertNotNull(generator); + for (int k = 1; k <= 4; k++) { + BN128 point = generator.mul(BigInteger.valueOf(k)); + assertEquals("projective input " + k, + point.mul(Params.R).isZero(), isGroupMember(point)); + } + + boolean sawProjectiveNonMember = false; + for (BN128 base : curvePoints(2)) { + BN128 point = base.mul(TWO); + boolean inSubgroup = point.mul(Params.R).isZero(); + assertEquals("doubled twist point disagrees with [r]P = O", inSubgroup, isGroupMember(point)); + if (!inSubgroup && !Fp2._1.equals(point.z)) { + sawProjectiveNonMember = true; + } + } + assertTrue("expected a non-member with non-unit Z", sawProjectiveNonMember); + } + + private static BN128G2 createG2(BigInteger xa, BigInteger xb, BigInteger ya, BigInteger yb) { + return BN128G2.create(xa.toByteArray(), xb.toByteArray(), ya.toByteArray(), yb.toByteArray()); + } + + private static BN128 createAffine(BigInteger xa, BigInteger xb, BigInteger ya, + BigInteger yb) { + return BN128Fp2.create(xa.toByteArray(), xb.toByteArray(), ya.toByteArray(), yb.toByteArray()); + } + + private static boolean isGroupMember(BN128 point) throws Exception { + Method method = BN128G2.class.getDeclaredMethod("isGroupMember", BN128.class); + method.setAccessible(true); + return (Boolean) method.invoke(null, point); + } + + /** + * Returns ordinary twist points that are on the curve but, with overwhelming probability, + * outside the order-r subgroup. + */ + private static List> curvePoints(int limit) { + List> points = new ArrayList<>(); + for (int i = 1; i <= 500 && points.size() < limit; i++) { + Fp2 x = new Fp2(BigInteger.valueOf(i), BigInteger.ZERO); + Fp2 rhs = x.squared().mul(x).add(Params.B_Fp2); + Fp2 y = sqrt(rhs); + if (y == null || !y.squared().equals(rhs)) { + continue; + } + BN128 point = BN128Fp2.create(x.a.bytes(), x.b.bytes(), y.a.bytes(), y.b.bytes()); + if (point != null && !point.isZero()) { + points.add(point); + } + } + return points; + } + + /** Computes a square root in Fp2, or null when the value is not a square. */ + private static Fp2 sqrt(Fp2 value) { + BigInteger a = new BigInteger(1, value.a.bytes()); + BigInteger b = new BigInteger(1, value.b.bytes()); + BigInteger norm = a.multiply(a).add(b.multiply(b)).mod(P); + BigInteger c = sqrtFp(norm); + if (c == null) { + return null; + } + Fp2 result = sqrtWith(value, c); + return result != null ? result : sqrtWith(value, c.negate().mod(P)); + } + + private static Fp2 sqrtWith(Fp2 value, BigInteger c) { + BigInteger a = new BigInteger(1, value.a.bytes()); + BigInteger b = new BigInteger(1, value.b.bytes()); + BigInteger twoInv = TWO.modInverse(P); + BigInteger realRoot = sqrtFp(a.add(c).multiply(twoInv).mod(P)); + BigInteger imagRoot = sqrtFp(c.subtract(a).multiply(twoInv).mod(P)); + if (realRoot == null || imagRoot == null) { + return null; + } + BigInteger imaginary = realRoot.multiply(imagRoot).shiftLeft(1).mod(P); + if (imaginary.equals(b.mod(P))) { + return new Fp2(new Fp(realRoot), new Fp(imagRoot)); + } + if (imaginary.negate().mod(P).equals(b.mod(P))) { + return new Fp2(new Fp(realRoot), new Fp(imagRoot.negate().mod(P))); + } + return null; + } + + /** Square root in Fp for p = 3 (mod 4), or null when the value is not a square. */ + private static BigInteger sqrtFp(BigInteger value) { + BigInteger v = value.mod(P); + if (v.signum() == 0) { + return BigInteger.ZERO; + } + BigInteger root = v.modPow(SQRT_EXPONENT, P); + return root.multiply(root).mod(P).equals(v) ? root : null; + } + + private static byte[] bytes(long value) { + return BigInteger.valueOf(value).toByteArray(); + } +}