Skip to content

Commit e9afd84

Browse files
authored
fix(crypto): avoid BN128 class initialization deadlock (#7099)
1 parent 03b83bf commit e9afd84

4 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎crypto/src/main/java/org/tron/common/crypto/zksnark/Fp.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ public class Fp implements Field<Fp> {
3535
static final Fp NON_RESIDUE = new Fp(new BigInteger(
3636
"21888242871839275222246405745257275088696311157297823662689037894645226208582"));
3737

38-
static final Fp _2_INV = new Fp(BigInteger.valueOf(2).modInverse(P));
39-
4038
private final BigInteger v;
4139

4240
Fp(BigInteger v) {

‎crypto/src/main/java/org/tron/common/crypto/zksnark/PairingCheck.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.tron.common.crypto.zksnark.Params.B_Fp2;
2121
import static org.tron.common.crypto.zksnark.Params.PAIRING_FINAL_EXPONENT_Z;
2222
import static org.tron.common.crypto.zksnark.Params.TWIST;
23+
import static org.tron.common.crypto.zksnark.Params._2_INV;
2324

2425
import java.math.BigInteger;
2526
import java.util.ArrayList;
@@ -162,13 +163,13 @@ private static Precomputed flippedMillerLoopDoubling(BN128G2 g2) {
162163

163164
Fp2 x = g2.x, y = g2.y, z = g2.z;
164165

165-
Fp2 a = Fp._2_INV.mul(x.mul(y)); // a = x * y / 2
166+
Fp2 a = _2_INV.mul(x.mul(y)); // a = x * y / 2
166167
Fp2 b = y.squared(); // b = y^2
167168
Fp2 c = z.squared(); // c = z^2
168169
Fp2 d = c.add(c).add(c); // d = 3 * c
169170
Fp2 e = B_Fp2.mul(d); // e = twist_b * d
170171
Fp2 f = e.add(e).add(e); // f = 3 * e
171-
Fp2 g = Fp._2_INV.mul(b.add(f)); // g = (b + f) / 2
172+
Fp2 g = _2_INV.mul(b.add(f)); // g = (b + f) / 2
172173
Fp2 h = y.add(z).squared().sub(b.add(c)); // h = (y + z)^2 - (b + c)
173174
Fp2 i = e.sub(b); // i = e - b
174175
Fp2 j = x.squared(); // j = x^2

‎crypto/src/main/java/org/tron/common/crypto/zksnark/Params.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class Params {
3939
static final BigInteger R = new BigInteger(
4040
"21888242871839275222246405745257275088548364400416034343698204186575808495617");
4141

42+
// Field classes must not reference Params in static initializers.
43+
static final Fp _2_INV = new Fp(BigInteger.valueOf(2).modInverse(P));
44+
4245
/**
4346
* "b" curve parameter for {@link BN128Fp}
4447
*/

‎framework/src/test/java/org/tron/common/crypto/zksnark/FpTest.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
package org.tron.common.crypto.zksnark;
22

33
import java.math.BigInteger;
4+
import java.net.URL;
5+
import java.net.URLClassLoader;
46
import java.util.Arrays;
57
import org.junit.Assert;
68
import org.junit.Test;
79

810
public class FpTest {
911

12+
@Test
13+
public void testFieldInitializationWithoutCurveParameters() throws Exception {
14+
for (Class<?> field : new Class<?>[]{Fp.class, Fp2.class, Fp6.class, Fp12.class}) {
15+
assertInitializesWithoutCurveParameters(field.getName());
16+
}
17+
}
18+
1019
@Test(expected = NullPointerException.class)
1120
public void testConstructorRejectsNull() {
1221
new Fp((BigInteger) null);
@@ -77,4 +86,22 @@ public void testRejectsOutOfRangeCoordinates() {
7786
Assert.assertNull(BN128Fp.create(outOfRange, two));
7887
Assert.assertNull(BN128Fp.create(one, outOfRange));
7988
}
89+
90+
private void assertInitializesWithoutCurveParameters(String name) throws Exception {
91+
URL location = Fp.class.getProtectionDomain().getCodeSource().getLocation();
92+
// A fresh loader exercises first use even if another test already initialized these classes.
93+
// Reject Params to detect the dependency that caused the initialization cycle.
94+
try (URLClassLoader loader = new URLClassLoader(new URL[]{location}, null) {
95+
@Override
96+
protected Class<?> loadClass(String className, boolean resolve)
97+
throws ClassNotFoundException {
98+
if (className.equals(Params.class.getName())) {
99+
throw new ClassNotFoundException("Field initialization must not load Params");
100+
}
101+
return super.loadClass(className, resolve);
102+
}
103+
}) {
104+
Assert.assertNotNull(Class.forName(name, true, loader));
105+
}
106+
}
80107
}

0 commit comments

Comments
 (0)