Skip to content

Commit 329ae29

Browse files
authored
Merge pull request #29 from icon-project/update-for-api-0.9.5
Update for syncing with javaee-api 0.9.5 APIs
2 parents e7f20b5 + 8deccf7 commit 329ae29

File tree

4 files changed

+362
-17
lines changed

4 files changed

+362
-17
lines changed

unittest/src/main/java/score/Context.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,41 @@ public static byte[] aggregate(String type, byte[] prevAgg, byte[] values) {
175175
return Crypto.aggregate(type, prevAgg, values);
176176
}
177177

178+
public static byte[] ecAdd(String curve, byte[] data, boolean compressed) {
179+
require(null != curve, "Elliptic curve can't be NULL");
180+
require(null != data, "Data can't be NULL");
181+
switch (curve) {
182+
case "bls12-381-g1":
183+
return Crypto.bls12381G1Add(data, compressed);
184+
case "bls12-381-g2":
185+
return Crypto.bls12381G2Add(data, compressed);
186+
}
187+
throw new IllegalArgumentException("Unsupported curve " + curve);
188+
}
189+
190+
public static byte[] ecScalarMul(String curve, byte[] scalar, byte[] data, boolean compressed) {
191+
require(null != curve, "Elliptic curve can't be NULL");
192+
require(null != scalar, "Scalar can't be NULL");
193+
require(null != data, "Data can't be NULL");
194+
switch (curve) {
195+
case "bls12-381-g1":
196+
return Crypto.bls12381G1ScalarMul(scalar, data, compressed);
197+
case "bls12-381-g2":
198+
return Crypto.bls12381G2ScalarMul(scalar, data, compressed);
199+
}
200+
throw new IllegalArgumentException("Unsupported curve " + curve);
201+
}
202+
203+
public static boolean ecPairingCheck(String curve, byte[] data, boolean compressed) {
204+
require(null != curve, "Elliptic curve can't be NULL");
205+
require(null != data, "Data can't be NULL");
206+
switch (curve) {
207+
case "bls12-381":
208+
return Crypto.bls12381PairingCheck(data, compressed);
209+
}
210+
throw new IllegalArgumentException("Unsupported curve " + curve);
211+
}
212+
178213
public static Address getAddressFromKey(byte[] pubKey) {
179214
require(null != pubKey, "pubKey can't be NULL");
180215
return new Address(Crypto.getAddressBytesFromKey(pubKey));

unittest/src/main/java/score/impl/Crypto.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,54 @@ private static void checkArgument(boolean expression, String message) {
193193
throw new IllegalArgumentException(message);
194194
}
195195
}
196+
197+
public static byte[] bls12381G1Add(byte[] data, boolean compressed) {
198+
try {
199+
return BLS12381.g1Add(data, compressed);
200+
} catch (IllegalArgumentException e) {
201+
throw e;
202+
} catch (RuntimeException e) {
203+
throw new IllegalArgumentException(e.getMessage());
204+
}
205+
}
206+
207+
public static byte[] bls12381G2Add(byte[] data, boolean compressed) {
208+
try {
209+
return BLS12381.g2Add(data, compressed);
210+
} catch (IllegalArgumentException e) {
211+
throw e;
212+
} catch (RuntimeException e) {
213+
throw new IllegalArgumentException(e.getMessage());
214+
}
215+
}
216+
217+
public static byte[] bls12381G1ScalarMul(byte[] scalar, byte[] data, boolean compressed) {
218+
try {
219+
return BLS12381.g1ScalarMul(scalar, data, compressed);
220+
} catch (IllegalArgumentException e) {
221+
throw e;
222+
} catch (RuntimeException e) {
223+
throw new IllegalArgumentException(e.getMessage());
224+
}
225+
}
226+
227+
public static byte[] bls12381G2ScalarMul(byte[] scalar, byte[] data, boolean compressed) {
228+
try {
229+
return BLS12381.g2ScalarMul(scalar, data, compressed);
230+
} catch (IllegalArgumentException e) {
231+
throw e;
232+
} catch (RuntimeException e) {
233+
throw new IllegalArgumentException(e.getMessage());
234+
}
235+
}
236+
237+
public static boolean bls12381PairingCheck(byte[] data, boolean compressed) {
238+
try {
239+
return BLS12381.pairingCheck(data, compressed);
240+
} catch (IllegalArgumentException e) {
241+
throw e;
242+
} catch (RuntimeException e) {
243+
throw new IllegalArgumentException(e.getMessage());
244+
}
245+
}
196246
}

unittest/src/main/java/score/impl/bls12381/BLS12381.java

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import supranational.blst.BLST_ERROR;
2020
import supranational.blst.P1;
2121
import supranational.blst.P1_Affine;
22+
import supranational.blst.P2;
2223
import supranational.blst.P2_Affine;
24+
import supranational.blst.PT;
25+
import supranational.blst.Scalar;
2326

2427
public class BLS12381 {
2528
private static final String dst = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_";
@@ -33,15 +36,16 @@ public static P1 identity() {
3336

3437
/**
3538
* Returns aggregation of prevAgg and values.
39+
*
3640
* @param prevAgg previous aggregation. null if there is no previous
3741
* aggregation.
38-
* @param values values to be aggregated.
42+
* @param values values to be aggregated.
3943
* @return aggregated value.
4044
*/
4145
public static byte[] aggregateG1Values(byte[] prevAgg, byte[] values) {
4246
try {
4347
P1 res;
44-
if (prevAgg!=null) {
48+
if (prevAgg != null) {
4549
res = new P1(prevAgg);
4650
if (!res.in_group()) {
4751
throw new IllegalArgumentException("prevAgg is not in group");
@@ -51,8 +55,8 @@ public static byte[] aggregateG1Values(byte[] prevAgg, byte[] values) {
5155
}
5256
var nValues = values.length / G1_LEN;
5357
byte[] pk = new byte[G1_LEN];
54-
for (int i=0; i<nValues; i++) {
55-
System.arraycopy(values, i*G1_LEN, pk, 0, G1_LEN);
58+
for (int i = 0; i < nValues; i++) {
59+
System.arraycopy(values, i * G1_LEN, pk, 0, G1_LEN);
5660
var p1a = new P1_Affine(pk);
5761
if (!p1a.in_group()) {
5862
throw new IllegalArgumentException("a value is not in group");
@@ -79,4 +83,88 @@ public static boolean verifyG2Signature(byte[] pubKey, byte[] sig, byte[] msg) {
7983
throw new IllegalArgumentException(e);
8084
}
8185
}
86+
87+
public static byte[] g1Add(byte[] data, boolean compressed) {
88+
P1 acc = new P1();
89+
int size = compressed ? G1_LEN : 2 * G1_LEN;
90+
if (data.length == 0 || data.length % size != 0) {
91+
throw new IllegalArgumentException("BLS12-381: g1Add: invalid data layout: expected a multiple of " + size
92+
+ " bytes, got " + data.length);
93+
}
94+
byte[] buf = new byte[size];
95+
for (int i = 0; i < data.length; i += size) {
96+
System.arraycopy(data, i, buf, 0, size);
97+
acc = acc.add(new P1(buf));
98+
}
99+
return compressed ? acc.compress() : acc.serialize();
100+
}
101+
102+
public static byte[] g2Add(byte[] data, boolean compressed) {
103+
P2 acc = new P2();
104+
int size = compressed ? G2_LEN : 2 * G2_LEN;
105+
if (data.length == 0 || data.length % size != 0) {
106+
throw new IllegalArgumentException("BLS12-381: g2Add: invalid data layout: expected a multiple of " + size
107+
+ " bytes, got " + data.length);
108+
}
109+
byte[] buf = new byte[size];
110+
for (int i = 0; i < data.length; i += size) {
111+
System.arraycopy(data, i, buf, 0, size);
112+
acc = acc.add(new P2(buf));
113+
}
114+
return compressed ? acc.compress() : acc.serialize();
115+
}
116+
117+
public static byte[] g1ScalarMul(byte[] scalarBytes, byte[] data, boolean compressed) {
118+
int size = compressed ? G1_LEN : 2 * G1_LEN;
119+
Scalar scalar = new Scalar().from_bendian(scalarBytes);
120+
if (data.length != size) {
121+
throw new IllegalArgumentException(
122+
"BLS12-381: g1ScalarMul: invalid data layout: expected " + size + " bytes, got " + data.length);
123+
}
124+
P1 p = new P1(data);
125+
p = p.mult(scalar);
126+
return compressed ? p.compress() : p.serialize();
127+
}
128+
129+
public static byte[] g2ScalarMul(byte[] scalarBytes, byte[] data, boolean compressed) {
130+
int size = compressed ? G2_LEN : 2 * G2_LEN;
131+
Scalar scalar = new Scalar().from_bendian(scalarBytes);
132+
if (data.length != size) {
133+
throw new IllegalArgumentException(
134+
"BLS12-381: g2ScalarMul: invalid data layout: expected " + size + " bytes, got " + data.length);
135+
}
136+
P2 p = new P2(data);
137+
p = p.mult(scalar);
138+
return compressed ? p.compress() : p.serialize();
139+
}
140+
141+
public static boolean pairingCheck(byte[] data, boolean compressed) {
142+
int g1Size = compressed ? G1_LEN : 2 * G1_LEN;
143+
int g2Size = compressed ? G2_LEN : 2 * G2_LEN;
144+
int size = g1Size + g2Size;
145+
146+
if (data.length == 0 || data.length % size != 0) {
147+
throw new IllegalArgumentException("BLS12-381: pairingCheck: invalid data layout: expected a multiple of "
148+
+ size + " bytes, got " + data.length);
149+
}
150+
151+
PT acc = PT.one();
152+
byte[] p1buf = new byte[g1Size];
153+
byte[] p2buf = new byte[g2Size];
154+
for (int i = 0; i < data.length; i += size) {
155+
System.arraycopy(data, i, p1buf, 0, g1Size);
156+
System.arraycopy(data, i + g1Size, p2buf, 0, g2Size);
157+
P1 p1 = new P1(p1buf);
158+
P2 p2 = new P2(p2buf);
159+
if (!p1.in_group() || !p2.in_group()) {
160+
throw new IllegalArgumentException("G1 or G2 point not in subgroup!");
161+
}
162+
if (p1.is_inf() || p2.is_inf()) {
163+
continue;
164+
}
165+
acc = acc.mul(new PT(p1, p2));
166+
}
167+
168+
return acc.final_exp().is_one();
169+
}
82170
}

0 commit comments

Comments
 (0)