This library provides functionality to compute the optimal ate pairing over Barreto-Naehrig (BN) or BLS12-381 curves.
Load the library mcljava
.
import com.herumi.mcl.*;
System.loadLibrary(System.mapLibraryName("mcljava"));
Mcl.SystemInit(curveType); // curveType = Mcl.BN254 or Mcl.BLS12_381
G1
; The cyclic group instantiated as E(Fp)[r] where where r = p + 1 - t.G2
; The cyclic group instantiated as the inverse image of E'(Fp^2)[r].GT
; The cyclic group in the image of the optimal ate pairing.e : G1 x G2 -> GT
Fr
; The finite field with characteristic r.
Fr::setInt(int x)
; set by xFr::setStr(String str)
; set by str such as "123", "0xfff", etc.Fr::setByCSPRNG()
; randomly setMcl.neg(Fr y, Fr x)
;y = -x
Mcl.add(Fr z, Fr x, Fr y)
;z = x + y
Mcl.sub(Fr z, Fr x, Fr y)
;z = x - y
Mcl.mul(Fr z, Fr x, Fr y)
;z = x * y
Mcl.div(Fr z, Fr x, Fr y)
;z = x / y
Mcl.neg(G1 y, G1 x)
;y = -x
Mcl.dbl(G1 y, G1 x)
;y = 2x
Mcl.add(G1 z, G1 x, G1 y)
;z = x + y
Mcl.sub(G1 z, G1 x, G1 y)
;z = x - y
Mcl.mul(G1 z, G1 x, Fr y)
;z = x * y
Mcl.verifyOrderG1(boolean doVerify)
; If set totrue
,setStr
ofG1
will automatically check subgroup membership. Set tofalse
by default, meaning only a on curve check is done by default.
Mcl.neg(G2 y, G2 x)
;y = -x
Mcl.dbl(G2 y, G2 x)
;y = 2x
Mcl.add(G2 z, G2 x, G2 y)
;z = x + y
Mcl.sub(G2 z, G2 x, G2 y)
;z = x - y
Mcl.mul(G2 z, G2 x, Fr y)
;z = x * y
Mcl.verifyOrderG2(boolean doVerify)
; If set totrue
,setStr
ofG2
will automatically check subgroup membership. Set tofalse
by default, meaning only a on curve check is done by default.
GT::setStr(String str)
; set by the result oftoString()
methodMcl.mul(GT z, GT x, GT y)
;z = x * y
Mcl.pow(GT z, GT x, Fr y)
;z = x ^ y
Mcl.inv(GT y, GT x)
;y = x^{-1}
Mcl.pairing(GT e, G1 P, G2 Q)
; e = e(P, Q)
G2 Q = new G2();
Mcl.hashAndMapToG2(Q, "abc".getBytes());
Fr s = new Fr();
s.setByCSPRNG(); // secret key
G2 pub = new G2();
Mcl.mul(pub, Q, s); // public key = sQ
String m = "signature test";
G1 H = new G1();
Mcl.hashAndMapToG1(H, m.getBytes()); // H = Hash(m)
G1 sign = new G1();
Mcl.mul(sign, H, s); // signature of m = s H
GT e1 = new GT();
GT e2 = new GT();
Mcl.pairing(e1, H, pub); // e1 = e(H, s Q)
Mcl.pairing(e2, sign, Q); // e2 = e(s H, Q);
assertBool("verify signature", e1.equals(e2));
cd ffi/java
make test
Install swig and jre at first.
cd ffi/java
make_wrap
run-mcl