aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2018-01-31 09:22:08 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2018-01-31 09:22:08 +0800
commit57532d0464788b111b57f7555cdbdafaee476d08 (patch)
treeb610999ade3478bc888814d33e6099001db6d8b0
parent8bff270e42981332dc5236dc1324d9fde305f883 (diff)
downloaddexon-mcl-57532d0464788b111b57f7555cdbdafaee476d08.tar.gz
dexon-mcl-57532d0464788b111b57f7555cdbdafaee476d08.tar.zst
dexon-mcl-57532d0464788b111b57f7555cdbdafaee476d08.zip
unify bench for bn
-rw-r--r--test/bench.hpp48
-rw-r--r--test/bn384_test.cpp11
-rw-r--r--test/bn512_test.cpp11
-rw-r--r--test/bn_test.cpp79
4 files changed, 83 insertions, 66 deletions
diff --git a/test/bench.hpp b/test/bench.hpp
new file mode 100644
index 0000000..d2813a5
--- /dev/null
+++ b/test/bench.hpp
@@ -0,0 +1,48 @@
+void testBench(const G1& P, const G2& Q)
+{
+ G1 Pa;
+ G2 Qa;
+ Fp12 e1, e2;
+ BN::pairing(e1, P, Q);
+ Fp12::pow(e2, e1, 12345);
+ const int C = 500;
+ const int C2 = 1000;
+ Fp x, y;
+ x.setHashOf("abc");
+ y.setHashOf("xyz");
+ mpz_class z = 3;
+ mpz_class a = x.getMpz();
+ CYBOZU_BENCH_C("G1::mulCT ", C, G1::mulCT, Pa, P, a);
+ CYBOZU_BENCH_C("G1::mulCTsmall", C, G1::mulCT, Pa, P, z);
+ CYBOZU_BENCH_C("G1::mul ", C, G1::mul, Pa, Pa, a);
+ CYBOZU_BENCH_C("G1::mulsmall ", C, G1::mul, Pa, Pa, z);
+ CYBOZU_BENCH_C("G1::add ", C, G1::add, Pa, Pa, P);
+ CYBOZU_BENCH_C("G1::dbl ", C, G1::dbl, Pa, Pa);
+ CYBOZU_BENCH_C("G2::mulCT ", C, G2::mulCT, Qa, Q, a);
+ CYBOZU_BENCH_C("G2::mulCTsmall", C, G2::mulCT, Qa, Q, z);
+ CYBOZU_BENCH_C("G2::mul ", C, G2::mul, Qa, Qa, a);
+ CYBOZU_BENCH_C("G2::mulsmall ", C, G2::mul, Qa, Qa, z);
+ 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);
+ G1 PP;
+ G2 QQ;
+ CYBOZU_BENCH_C("hashAndMapToG1", C, BN::hashAndMapToG1, PP, "abc", 3);
+ CYBOZU_BENCH_C("hashAndMapToG2", C, BN::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);
+ CYBOZU_BENCH_C("Fp::inv ", C2, Fp::inv, x, x);
+
+ CYBOZU_BENCH_C("GT::add ", C2, GT::add, e1, e1, e2);
+ 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);
+ std::vector<Fp6> Qcoeff;
+ BN::precomputeG2(Qcoeff, Q);
+ CYBOZU_BENCH_C("precomputedML ", C, BN::precomputedMillerLoop, e2, P, Qcoeff);
+}
diff --git a/test/bn384_test.cpp b/test/bn384_test.cpp
index 39cbbb1..af44bee 100644
--- a/test/bn384_test.cpp
+++ b/test/bn384_test.cpp
@@ -10,6 +10,8 @@ using namespace mcl::bn384;
mcl::fp::Mode g_mode;
+#include "bench.hpp"
+
void testCurve(const mcl::bn::CurveParam& cp)
{
initPairing(cp, g_mode);
@@ -31,14 +33,7 @@ void testCurve(const mcl::bn::CurveParam& cp)
BN::pairing(e2, aP, bQ);
GT::pow(e1, e1, a * b);
CYBOZU_TEST_EQUAL(e1, e2);
- CYBOZU_BENCH_C("G1::mulCT", 500, G1::mul, aP, aP, a);
- CYBOZU_BENCH_C("G1::add", 500, G1::add, aP, aP, P);
- CYBOZU_BENCH_C("G1::dbl", 500, G1::dbl, aP, aP);
- CYBOZU_BENCH_C("G2::mulCT", 500, G2::mul, bQ, bQ, b);
- CYBOZU_BENCH_C("G2::add", 500, G2::add, bQ, bQ, Q);
- CYBOZU_BENCH_C("G2::dbl", 500, G2::dbl, bQ, bQ);
- CYBOZU_BENCH("pairing", BN::pairing, e1, P, Q);
- CYBOZU_BENCH("finalExp", BN::finalExp, e1, e1);
+ testBench(P, Q);
}
CYBOZU_TEST_AUTO(pairing)
diff --git a/test/bn512_test.cpp b/test/bn512_test.cpp
index 67b8e94..2b59023 100644
--- a/test/bn512_test.cpp
+++ b/test/bn512_test.cpp
@@ -10,6 +10,8 @@ using namespace mcl::bn512;
mcl::fp::Mode g_mode;
+#include "bench.hpp"
+
void testHashAndMapto(const mcl::bn::CurveParam& cp)
{
G1 P;
@@ -63,15 +65,8 @@ void testCurve(const mcl::bn::CurveParam& cp)
BN::pairing(e2, aP, bQ);
GT::pow(e1, e1, a * b);
CYBOZU_TEST_EQUAL(e1, e2);
- CYBOZU_BENCH_C("G1::mulCT", 500, G1::mul, aP, aP, a);
- CYBOZU_BENCH_C("G1::add", 500, G1::add, aP, aP, P);
- CYBOZU_BENCH_C("G1::dbl", 500, G1::dbl, aP, aP);
- CYBOZU_BENCH_C("G2::mulCT", 500, G2::mul, bQ, bQ, b);
- CYBOZU_BENCH_C("G2::add", 500, G2::add, bQ, bQ, Q);
- CYBOZU_BENCH_C("G2::dbl", 500, G2::dbl, bQ, bQ);
- CYBOZU_BENCH("pairing", BN::pairing, e1, P, Q);
- CYBOZU_BENCH("finalExp", BN::finalExp, e1, e1);
testHashAndMapto(cp);
+ testBench(P, Q);
}
CYBOZU_TEST_AUTO(pairing)
diff --git a/test/bn_test.cpp b/test/bn_test.cpp
index 7368e2f..fe35c86 100644
--- a/test/bn_test.cpp
+++ b/test/bn_test.cpp
@@ -242,59 +242,32 @@ 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++) BN::pairing(e1, P, Q);
-// CYBOZU_BENCH_C("pairing", 1000, BN::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);
- BN::pairing(e1, Pa, Q);
- BN::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, BN::param.glv2.pow, e1, e1, a);
- G1 PP;
- G2 QQ;
- CYBOZU_BENCH_C("hashAndMapToG1", 500, BN::hashAndMapToG1, PP, "abc", 3);
- CYBOZU_BENCH_C("hashAndMapToG2", 500, BN::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);
+ BN::pairing(e1, Pa, Q);
+ BN::pairing(e2, P, Qa);
+ CYBOZU_TEST_EQUAL(ea, e1);
+ CYBOZU_TEST_EQUAL(ea, e2);
}
- CYBOZU_BENCH("pairing", BN::pairing, e1, P, Q); // 2.4Mclk
- CYBOZU_BENCH("finalExp", BN::finalExp, e1, e1); // 1.3Mclk
-#endif
}
void testTrivial(const G1& P, const G2& Q)
@@ -358,6 +331,8 @@ void testIo(const G1& P, const G2& Q)
testIoAll(Z1, Z2);
}
+#include "bench.hpp"
+
CYBOZU_TEST_AUTO(naive)
{
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(g_testSetTbl); i++) {
@@ -367,7 +342,10 @@ CYBOZU_TEST_AUTO(naive)
const G1 P(ts.g1.a, 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++) BN::pairing(e, P, Q);
+ }
clk.put();
return;
#endif
@@ -382,6 +360,7 @@ CYBOZU_TEST_AUTO(naive)
testPairing(P, Q, ts.e);
testPrecomputed(P, Q);
testMillerLoop2(P, Q);
+ testBench(P, Q);
}
int count = (int)clk.getCount();
if (count) {