|
| 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