-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
pairing.cpp
68 lines (61 loc) · 1.37 KB
/
pairing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <mcl/bls12_381.hpp>
using namespace mcl::bn;
void minimum_sample(const G1& P, const G2& Q)
{
Fr a;
const Fr b = 456;
Fp12 e1, e2;
pairing(e1, P, Q);
G2 aQ;
G1 bP;
a.setHashOf("abc", 3);
printf("a = %s\n", a.getStr(16).c_str());
printf("a - b = %s\n", (a - b).getStr(16).c_str());
G2::mul(aQ, Q, a);
G1::mul(bP, P, b);
pairing(e2, bP, aQ);
Fp12::pow(e1, e1, a * b);
printf("pairing = %s\n", e1.getStr(16).c_str());
printf("%s\n", e1 == e2 ? "ok" : "ng");
}
void miller_and_finel_exp(const G1& P, const G2& Q)
{
Fp12 e1, e2;
pairing(e1, P, Q);
millerLoop(e2, P, Q);
finalExp(e2, e2);
printf("%s\n", e1 == e2 ? "ok" : "ng");
}
void precomputed(const G1& P, const G2& Q)
{
Fp12 e1, e2;
pairing(e1, P, Q);
std::vector<Fp6> Qcoeff;
precomputeG2(Qcoeff, Q);
precomputedMillerLoop(e2, P, Qcoeff);
finalExp(e2, e2);
printf("%s\n", e1 == e2 ? "ok" : "ng");
}
int main(int argc, char *[])
try
{
if (argc == 1) {
puts("BLS12_381");
initPairing(mcl::BLS12_381);
} else {
puts("BN254");
initPairing(mcl::BN254);//, mcl::fp::FP_GMP);
}
G1 P;
G2 Q;
hashAndMapToG1(P, "abc", 3);
hashAndMapToG2(Q, "abc", 3);
printf("P = %s\n", P.serializeToHexStr().c_str());
printf("Q = %s\n", Q.serializeToHexStr().c_str());
minimum_sample(P, Q);
miller_and_finel_exp(P, Q);
precomputed(P, Q);
} catch (std::exception& e) {
printf("ERR %s\n", e.what());
return 1;
}