Skip to content

Commit e6b7c96

Browse files
committed
Fix field elements validation
1 parent 575fe99 commit e6b7c96

8 files changed

Lines changed: 28 additions & 43 deletions

File tree

crypto/src/main/java/org/tron/common/crypto/zksnark/BN128.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -220,21 +220,6 @@ public boolean isZero() {
220220
return z.isZero();
221221
}
222222

223-
protected boolean isValid() {
224-
225-
// check whether coordinates belongs to the Field
226-
if (!x.isValid() || !y.isValid() || !z.isValid()) {
227-
return false;
228-
}
229-
230-
// check whether point is on the curve
231-
if (!isOnCurve()) {
232-
return false;
233-
}
234-
235-
return true;
236-
}
237-
238223
@Override
239224
public String toString() {
240225
return String.format("(%s; %s; %s)", x.toString(), y.toString(), z.toString());

crypto/src/main/java/org/tron/common/crypto/zksnark/BN128Fp.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public static BN128<Fp> create(byte[] xx, byte[] yy) {
4747
Fp x = Fp.create(xx);
4848
Fp y = Fp.create(yy);
4949

50+
if (x == null || y == null) {
51+
// It means that one or both coordinates are not elements of Fp
52+
return null;
53+
}
54+
5055
// check for point at infinity
5156
if (x.isZero() && y.isZero()) {
5257
return ZERO;
@@ -55,7 +60,7 @@ public static BN128<Fp> create(byte[] xx, byte[] yy) {
5560
BN128<Fp> p = new BN128Fp(x, y, Fp._1);
5661

5762
// check whether point is a valid one
58-
if (p.isValid()) {
63+
if (p.isOnCurve()) {
5964
return p;
6065
} else {
6166
return null;

crypto/src/main/java/org/tron/common/crypto/zksnark/BN128Fp2.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public static BN128<Fp2> create(byte[] aa, byte[] bb, byte[] cc, byte[] dd) {
5252
Fp2 x = Fp2.create(aa, bb);
5353
Fp2 y = Fp2.create(cc, dd);
5454

55+
if (x == null || y == null) {
56+
// It means that one or both coordinates are not elements of Fp
57+
return null;
58+
}
59+
5560
// check for point at infinity
5661
if (x.isZero() && y.isZero()) {
5762
return ZERO;
@@ -60,7 +65,7 @@ public static BN128<Fp2> create(byte[] aa, byte[] bb, byte[] cc, byte[] dd) {
6065
BN128<Fp2> p = new BN128Fp2(x, y, Fp2._1);
6166

6267
// check whether point is a valid one
63-
if (p.isValid()) {
68+
if (p.isOnCurve()) {
6469
return p;
6570
} else {
6671
return null;

crypto/src/main/java/org/tron/common/crypto/zksnark/Field.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,4 @@ interface Field<T> {
4040
T negate();
4141

4242
boolean isZero();
43-
44-
boolean isValid();
4543
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,19 @@ public class Fp implements Field<Fp> {
8484
}
8585

8686
static Fp create(byte[] v) {
87-
return new Fp(toMontgomery(new BigInteger(1, v)));
87+
BigInteger value = new BigInteger(1, v);
88+
if (value.compareTo(P) >= 0) {
89+
// Only the values less than P are valid
90+
return null;
91+
}
92+
return new Fp(toMontgomery(value));
8893
}
8994

9095
static Fp create(BigInteger v) {
96+
if (v.compareTo(P) >= 0) {
97+
// Only the values less than P are valid
98+
return null;
99+
}
91100
return new Fp(toMontgomery(v));
92101
}
93102

@@ -133,14 +142,6 @@ public boolean isZero() {
133142
return v.compareTo(BigInteger.ZERO) == 0;
134143
}
135144

136-
/**
137-
* Checks if provided value is a valid Fp member
138-
*/
139-
@Override
140-
public boolean isValid() {
141-
return v.compareTo(P) < 0;
142-
}
143-
144145
Fp2 mul(Fp2 o) {
145146
return new Fp2(o.a.mul(this), o.b.mul(this));
146147
}

crypto/src/main/java/org/tron/common/crypto/zksnark/Fp12.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,6 @@ public boolean isZero() {
233233
return this.equals(ZERO);
234234
}
235235

236-
@Override
237-
public boolean isValid() {
238-
return a.isValid() && b.isValid();
239-
}
240-
241236
Fp12 frobeniusMap(int power) {
242237

243238
Fp6 ra = a.frobeniusMap(power);

crypto/src/main/java/org/tron/common/crypto/zksnark/Fp2.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ static Fp2 create(BigInteger aa, BigInteger bb) {
6060

6161
Fp a = Fp.create(aa);
6262
Fp b = Fp.create(bb);
63+
if (a == null || b == null) {
64+
return null;
65+
}
6366

6467
return new Fp2(a, b);
6568
}
@@ -68,6 +71,9 @@ static Fp2 create(byte[] aa, byte[] bb) {
6871

6972
Fp a = Fp.create(aa);
7073
Fp b = Fp.create(bb);
74+
if (a == null || b == null) {
75+
return null;
76+
}
7177

7278
return new Fp2(a, b);
7379
}
@@ -139,11 +145,6 @@ public boolean isZero() {
139145
return this.equals(ZERO);
140146
}
141147

142-
@Override
143-
public boolean isValid() {
144-
return a.isValid() && b.isValid();
145-
}
146-
147148
@Override
148149
public boolean equals(Object o) {
149150
if (this == o) {

crypto/src/main/java/org/tron/common/crypto/zksnark/Fp6.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,6 @@ public boolean isZero() {
211211
return this.equals(ZERO);
212212
}
213213

214-
@Override
215-
public boolean isValid() {
216-
return a.isValid() && b.isValid() && c.isValid();
217-
}
218-
219214
Fp6 frobeniusMap(int power) {
220215

221216
Fp2 ra = a.frobeniusMap(power);

0 commit comments

Comments
 (0)