Skip to content

Commit 17629c6

Browse files
committed
Revert "remove unsused classes"
This reverts commit 272b307.
1 parent 272b307 commit 17629c6

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.arkecosystem.crypto.signature.bls;
2+
3+
// import org.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
4+
5+
import com.herumi.mcl.Fr;
6+
import com.herumi.mcl.G1;
7+
import com.herumi.mcl.G2;
8+
import com.herumi.mcl.Mcl;
9+
10+
public class PrivateKey implements java.security.PrivateKey {
11+
private int curveType;
12+
private byte[] secKey;
13+
14+
@Override
15+
public String getAlgorithm() {
16+
if (curveType == Bls.BLS12_381) {
17+
return "BLS12-381";
18+
} else {
19+
return "NOT SUPPORTED";
20+
}
21+
}
22+
23+
@Override
24+
public String getFormat() {
25+
return null;
26+
}
27+
28+
@Override
29+
public byte[] getEncoded() {
30+
return secKey;
31+
}
32+
33+
public PrivateKey(int curveType) {
34+
this.curveType = curveType;
35+
Fr fr = new Fr();
36+
fr.setByCSPRNG();
37+
secKey = fr.serialize();
38+
}
39+
40+
public PublicKey getPublicKey() {
41+
G1 Q = new G1();
42+
Q.setStr(Bls.BaseG1);
43+
G1 pub = new G1();
44+
Fr fr = new Fr();
45+
fr.deserialize(secKey);
46+
Mcl.mul(pub, Q, fr);
47+
48+
PublicKey publicKey = new PublicKey(curveType, pub.serialize());
49+
return publicKey;
50+
}
51+
52+
/*
53+
* Sign a message
54+
*/
55+
public byte[] sign(byte[] msg) {
56+
G2 H = new G2();
57+
Mcl.hashAndMapToG2(H, msg); // H = Hash(m)
58+
G2 sig = new G2();
59+
Fr fr = new Fr();
60+
fr.deserialize(secKey);
61+
Mcl.mul(sig, H, fr); // signature of m = s H
62+
return sig.serialize();
63+
}
64+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.arkecosystem.crypto.signature.bls;
2+
3+
import com.herumi.mcl.G1;
4+
import com.herumi.mcl.G2;
5+
import com.herumi.mcl.GT;
6+
import com.herumi.mcl.Mcl;
7+
8+
public class PublicKey implements java.security.PublicKey {
9+
private int curveType;
10+
private byte[] pubKey;
11+
12+
@Override
13+
public String getAlgorithm() {
14+
if (curveType == Bls.BLS12_381) {
15+
return "BLS12-381";
16+
} else {
17+
return "NOT SUPPORTED";
18+
}
19+
}
20+
21+
@Override
22+
public String getFormat() {
23+
return null;
24+
}
25+
26+
@Override
27+
public byte[] getEncoded() {
28+
return pubKey;
29+
}
30+
31+
public PublicKey(int curveType, byte[] pubKey) {
32+
this.curveType = curveType;
33+
this.pubKey = pubKey;
34+
}
35+
36+
/*
37+
* Veryfi signature
38+
* Case 1: single message and single signature
39+
* Case 2: single message and multiple signatures
40+
*/
41+
public boolean verify(byte[] msg, byte[] sig) {
42+
G2 H = new G2();
43+
Mcl.hashAndMapToG2(H, msg); // H = Hash(m)
44+
G1 pub = new G1();
45+
pub.deserialize(pubKey);
46+
G1 Q = new G1();
47+
Q.setStr(Bls.BaseG1);
48+
G2 g2 = new G2();
49+
g2.deserialize(sig);
50+
GT e1 = new GT();
51+
GT e2 = new GT();
52+
Mcl.pairing(e1, pub, H); // e1 = e(H, s Q)
53+
Mcl.pairing(e2, Q, g2); // e2 = e(s H, Q);
54+
return e1.equals(e2);
55+
}
56+
57+
/*
58+
* Verify mutiple signatures
59+
* Case: Single private key and multiple messages with multiple signatures
60+
* h: Not the message itself, it's result of hashAndMapToG2
61+
*/
62+
public boolean verifyAggregate(byte[] h, byte[] sig) {
63+
G2 H = new G2();
64+
H.deserialize(h); // H = Hash(m)
65+
G1 pub = new G1();
66+
pub.deserialize(pubKey);
67+
G1 Q = new G1();
68+
Q.setStr(Bls.BaseG1);
69+
70+
G2 g2 = new G2();
71+
g2.deserialize(sig);
72+
GT e1 = new GT();
73+
GT e2 = new GT();
74+
Mcl.pairing(e1, pub, H); // e1 = e(H, s Q)
75+
Mcl.pairing(e2, Q, g2); // e2 = e(s H, Q);
76+
return e1.equals(e2);
77+
}
78+
}

0 commit comments

Comments
 (0)