diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index bd91934..7a64868 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -1196,7 +1196,7 @@ public: @note &z != xVec[i] */ private: - templateclass FpT> + templateclass FpT> static inline size_t addMulVecN(EcT& z, const EcT *xVec, const FpT *yVec, size_t n) { if (n > N) n = N; @@ -1236,7 +1236,7 @@ public: r.clear(); while (n > 0) { EcT t; - size_t done = addMulVecN(t, xVec, yVec, n); + size_t done = addMulVecN<32>(t, xVec, yVec, n); r += t; xVec += done; yVec += done; diff --git a/test/bls12_test.cpp b/test/bls12_test.cpp index c86fed2..6aac204 100644 --- a/test/bls12_test.cpp +++ b/test/bls12_test.cpp @@ -384,7 +384,7 @@ CYBOZU_TEST_AUTO(naive) testPairing(P, Q, ts.e); testPrecomputed(P, Q); testMillerLoop2(P, Q); - testCommon(); + testCommon(P, Q); testBench(P, Q); } int count = (int)clk.getCount(); diff --git a/test/bn384_test.cpp b/test/bn384_test.cpp index a8c7cdb..e248d48 100644 --- a/test/bn384_test.cpp +++ b/test/bn384_test.cpp @@ -40,7 +40,7 @@ void testCurve(const mcl::CurveParam& cp) pairing(e2, aP, bQ); GT::pow(e1, e1, a * b); CYBOZU_TEST_EQUAL(e1, e2); - testCommon(); + testCommon(P, Q); testBench(P, Q); testSquareRoot(); testLagrange(); diff --git a/test/bn512_test.cpp b/test/bn512_test.cpp index db2aff1..ebbc7c0 100644 --- a/test/bn512_test.cpp +++ b/test/bn512_test.cpp @@ -34,7 +34,7 @@ void testCurve(const mcl::CurveParam& cp) pairing(e2, aP, bQ); GT::pow(e1, e1, a * b); CYBOZU_TEST_EQUAL(e1, e2); - testCommon(); + testCommon(P, Q); testBench(P, Q); testSquareRoot(); testLagrange(); diff --git a/test/bn_test.cpp b/test/bn_test.cpp index a2557a3..397f9a1 100644 --- a/test/bn_test.cpp +++ b/test/bn_test.cpp @@ -402,7 +402,7 @@ CYBOZU_TEST_AUTO(naive) testPrecomputed(P, Q); testMillerLoop2(P, Q); testMillerLoopVec(); - testCommon(); + testCommon(P, Q); testBench(P, Q); benchAddDblG1(); benchAddDblG2(); diff --git a/test/common_test.hpp b/test/common_test.hpp index 400b523..e5378e9 100644 --- a/test/common_test.hpp +++ b/test/common_test.hpp @@ -1,27 +1,44 @@ -void testMulVec() +template +void naiveMulVec(G& out, const G *xVec, const F *yVec, size_t n) { - using namespace mcl::bn; - const size_t n = 3; - G1 xVec[n]; - Fr yVec[n]; - G1 ok; - ok.clear(); - char c = 'a'; + G r, t; + r.clear(); for (size_t i = 0; i < n; i++) { - hashAndMapToG1(xVec[i], &c, 1); + G::mul(t, xVec[i], yVec[i]); + r += t; + } + out = r; +} + +template +void testMulVec(const G& P) +{ + using namespace mcl::bn; + const int N = 33; + G xVec[N]; + mcl::bn::Fr yVec[N]; + + for (size_t i = 0; i < N; i++) { + G::mul(xVec[i], P, i + 3); yVec[i].setByCSPRNG(); - G1 t; - G1::mul(t, xVec[i], yVec[i]); - ok += t; } - G1 z; - G1::mulVec(z, xVec, yVec, n); - CYBOZU_TEST_EQUAL(z, ok); - CYBOZU_BENCH_C("mulVec(new)", 1000, G1::mulVec, z, xVec, yVec, n); - CYBOZU_BENCH_C("mulVec(old)", 1000, G1::mulVec, z, xVec, yVec, n, true); + const size_t nTbl[] = { 1, 2, 3, 5, 30, 31, 32, 33 }; + const int C = 400; + for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(nTbl); i++) { + const size_t n = nTbl[i]; + G Q1, Q2; + CYBOZU_TEST_ASSERT(n <= N); + naiveMulVec(Q1, xVec, yVec, n); + G::mulVec(Q2, xVec, yVec, n); + CYBOZU_TEST_EQUAL(Q1, Q2); + printf("n=%zd\n", n); + CYBOZU_BENCH_C("naive ", C, naiveMulVec, Q1, xVec, yVec, n); + CYBOZU_BENCH_C("mulVec", C, G::mulVec, Q1, xVec, yVec, n); + } } -void testCommon() +template +void testCommon(const G1& P, const G2&) { - testMulVec(); + testMulVec(P); }