use bench.hpp for bls12

dev
MITSUNARI Shigeo 7 years ago
parent 6e366d2454
commit 30ec0e0038
  1. 19
      test/bench.hpp
  2. 78
      test/bls12_test.cpp
  3. 2
      test/bn_test.cpp

@ -1,9 +1,10 @@
template<class CT>
void testBench(const G1& P, const G2& Q)
{
G1 Pa;
G2 Qa;
Fp12 e1, e2;
BN::pairing(e1, P, Q);
CT::pairing(e1, P, Q);
Fp12::pow(e2, e1, 12345);
const int C = 500;
const int C2 = 1000;
@ -25,7 +26,7 @@ void testBench(const G1& P, const G2& Q)
CYBOZU_BENCH_C("G2::add ", C, G2::add, Qa, Qa, Q);
CYBOZU_BENCH_C("G2::dbl ", C, G2::dbl, Qa, Qa);
CYBOZU_BENCH_C("GT::pow ", C, GT::pow, e1, e1, a);
// CYBOZU_BENCH_C("GT::powGLV ", C, BN::param.glv2.pow, e1, e1, a);
// CYBOZU_BENCH_C("GT::powGLV ", C, CT::param.glv2.pow, e1, e1, a);
G1 PP;
G2 QQ;
std::string s;
@ -33,8 +34,8 @@ void testBench(const G1& P, const G2& Q)
CYBOZU_BENCH_C("G1::setStr ", C, PP.setStr, s);
s = Q.getStr();
CYBOZU_BENCH_C("G2::setStr ", C, QQ.setStr, s);
CYBOZU_BENCH_C("hashAndMapToG1", C, BN::hashAndMapToG1, PP, "abc", 3);
CYBOZU_BENCH_C("hashAndMapToG2", C, BN::hashAndMapToG2, QQ, "abc", 3);
CYBOZU_BENCH_C("hashAndMapToG1", C, CT::hashAndMapToG1, PP, "abc", 3);
CYBOZU_BENCH_C("hashAndMapToG2", C, CT::hashAndMapToG2, QQ, "abc", 3);
CYBOZU_BENCH_C("Fp::add ", C2, Fp::add, x, x, y);
CYBOZU_BENCH_C("Fp::mul ", C2, Fp::mul, x, x, y);
CYBOZU_BENCH_C("Fp::sqr ", C2, Fp::sqr, x, x);
@ -44,10 +45,10 @@ void testBench(const G1& P, const G2& Q)
CYBOZU_BENCH_C("GT::mul ", C2, GT::mul, e1, e1, e2);
CYBOZU_BENCH_C("GT::sqr ", C2, GT::sqr, e1, e1);
CYBOZU_BENCH_C("GT::inv ", C2, GT::inv, e1, e1);
CYBOZU_BENCH_C("pairing ", C, BN::pairing, e1, P, Q);
CYBOZU_BENCH_C("millerLoop ", C, BN::millerLoop, e1, P, Q);
CYBOZU_BENCH_C("finalExp ", C, BN::finalExp, e1, e1);
CYBOZU_BENCH_C("pairing ", C, CT::pairing, e1, P, Q);
CYBOZU_BENCH_C("millerLoop ", C, CT::millerLoop, e1, P, Q);
CYBOZU_BENCH_C("finalExp ", C, CT::finalExp, e1, e1);
std::vector<Fp6> Qcoeff;
BN::precomputeG2(Qcoeff, Q);
CYBOZU_BENCH_C("precomputedML ", C, BN::precomputedMillerLoop, e2, P, Qcoeff);
CT::precomputeG2(Qcoeff, Q);
CYBOZU_BENCH_C("precomputedML ", C, CT::precomputedMillerLoop, e2, P, Qcoeff);
}

@ -242,59 +242,31 @@ void testPairing(const G1& P, const G2& Q, const char *eStr)
ss >> e2;
}
CYBOZU_TEST_EQUAL(e1, e2);
#ifdef ONLY_BENCH
for (int i = 0; i < 1000; i++) BLS12::pairing(e1, P, Q);
// CYBOZU_BENCH_C("pairing", 1000, BLS12::pairing, e1, P, Q); // 2.4Mclk
#else
{
Fp12 e = e1, ea;
G1 Pa;
G2 Qa;
Fp12 e = e1, ea;
G1 Pa;
G2 Qa;
#if defined(__EMSCRIPTEN__) || MCL_SIZEOF_UNIT == 4
const int count = 100;
const int count = 100;
#else
const int count = 1000;
const int count = 1000;
#endif
mpz_class a;
cybozu::XorShift rg;
for (int i = 0; i < count; i++) {
Fr r;
r.setRand(rg);
a = r.getMpz();
Fp12::pow(ea, e, a);
G1::mul(Pa, P, a);
G2::mul(Qa, Q, a);
G1 T;
G1::mulCT(T, P, a);
CYBOZU_TEST_EQUAL(Pa, T);
BLS12::pairing(e1, Pa, Q);
BLS12::pairing(e2, P, Qa);
CYBOZU_TEST_EQUAL(ea, e1);
CYBOZU_TEST_EQUAL(ea, e2);
}
mpz_class z = 3;
CYBOZU_BENCH_C("G1::mulCT ", 500, G1::mulCT, Pa, P, a);
CYBOZU_BENCH_C("G1::mulCT z", 500, G1::mulCT, Pa, P, z);
CYBOZU_BENCH_C("G1::mul ", 500, G1::mul, Pa, Pa, a);
CYBOZU_BENCH_C("G1::mul z", 500, G1::mul, Pa, Pa, z);
CYBOZU_BENCH_C("G1::add", 500, G1::add, Pa, Pa, P);
CYBOZU_BENCH_C("G1::dbl", 500, G1::dbl, Pa, Pa);
CYBOZU_BENCH_C("G2::mulCT ", 500, G2::mulCT, Qa, Q, a);
CYBOZU_BENCH_C("G2::mulCT z", 500, G2::mulCT, Qa, Q, z);
CYBOZU_BENCH_C("G2::mul ", 500, G2::mul, Qa, Qa, a);
CYBOZU_BENCH_C("G2::mul z", 500, G2::mul, Qa, Qa, z);
CYBOZU_BENCH_C("G2::add", 500, G2::add, Qa, Qa, Q);
CYBOZU_BENCH_C("G2::dbl", 500, G2::dbl, Qa, Qa);
CYBOZU_BENCH_C("GT::pow", 500, GT::pow, e1, e1, a);
// CYBOZU_BENCH_C("GT::powGLV", 500, BLS12::param.glv2.pow, e1, e1, a);
G1 PP;
G2 QQ;
// CYBOZU_BENCH_C("hashAndMapToG1", 500, BLS12::hashAndMapToG1, PP, "abc", 3);
// CYBOZU_BENCH_C("hashAndMapToG2", 500, BLS12::hashAndMapToG2, QQ, "abc", 3);
mpz_class a;
cybozu::XorShift rg;
for (int i = 0; i < count; i++) {
Fr r;
r.setRand(rg);
a = r.getMpz();
Fp12::pow(ea, e, a);
G1::mul(Pa, P, a);
G2::mul(Qa, Q, a);
G1 T;
G1::mulCT(T, P, a);
CYBOZU_TEST_EQUAL(Pa, T);
BLS12::pairing(e1, Pa, Q);
BLS12::pairing(e2, P, Qa);
CYBOZU_TEST_EQUAL(ea, e1);
CYBOZU_TEST_EQUAL(ea, e2);
}
CYBOZU_BENCH("pairing", BLS12::pairing, e1, P, Q); // 2.4Mclk
CYBOZU_BENCH("finalExp", BLS12::finalExp, e1, e1); // 1.3Mclk
#endif
}
void testTrivial(const G1& P, const G2& Q)
@ -321,6 +293,8 @@ void testTrivial(const G1& P, const G2& Q)
CYBOZU_TEST_EQUAL(e, 1);
}
#include "bench.hpp"
CYBOZU_TEST_AUTO(naive)
{
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(g_testSetTbl); i++) {
@ -330,7 +304,10 @@ CYBOZU_TEST_AUTO(naive)
const G1 P(Fp(ts.g1.a), Fp(ts.g1.b));
const G2 Q(Fp2(ts.g2.aa, ts.g2.ab), Fp2(ts.g2.ba, ts.g2.bb));
#ifdef ONLY_BENCH
testPairing(P, Q, ts.e);
{
Fp12 e;
for (int i = 0; i < 1000; i++) BLS12::pairing(e, P, Q);
}
clk.put();
return;
#endif
@ -344,6 +321,7 @@ CYBOZU_TEST_AUTO(naive)
testPairing(P, Q, ts.e);
testPrecomputed(P, Q);
testMillerLoop2(P, Q);
testBench<BLS12>(P, Q);
}
int count = (int)clk.getCount();
if (count) {

@ -360,7 +360,7 @@ CYBOZU_TEST_AUTO(naive)
testPairing(P, Q, ts.e);
testPrecomputed(P, Q);
testMillerLoop2(P, Q);
testBench(P, Q);
testBench<BN>(P, Q);
}
int count = (int)clk.getCount();
if (count) {

Loading…
Cancel
Save