From 572fa8d81688d7e335ec55352ea382f736f22a30 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Sat, 17 Aug 2019 18:21:16 +0900 Subject: [PATCH] add millerLoopVec --- include/mcl/bn.hpp | 19 +++++++++++++++++++ test/bn_test.cpp | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/include/mcl/bn.hpp b/include/mcl/bn.hpp index 147f8bb..53a456f 100644 --- a/include/mcl/bn.hpp +++ b/include/mcl/bn.hpp @@ -1900,6 +1900,25 @@ inline void precomputedMillerLoop2mixed(Fp12& f, const G1& P1, const G2& Q1, con precomputedMillerLoop2mixed(f, P1, Q1, P2, Q2coeff.data()); } #endif + +/* + f = prod_{i=0}^{n-1} millerLoop(Pvec[i], Qvec[i]) +*/ +inline void millerLoopVec(Fp12& f, const G1* Pvec, const G2* Qvec, size_t n) +{ + if (n == 0) { + f = 1; + return; + } + millerLoop(f, Pvec[0], Qvec[0]); + for (size_t i = 1; i < n; i++) { + Fp12 g; + millerLoop(g, Pvec[i], Qvec[i]); + f *= g; + } +} + + inline void mapToG1(bool *pb, G1& P, const Fp& x) { *pb = BN::param.mapTo.calcG1(P, x); } inline void mapToG2(bool *pb, G2& P, const Fp2& x) { *pb = BN::param.mapTo.calcG2(P, x); } #ifndef CYBOZU_DONT_USE_EXCEPTION diff --git a/test/bn_test.cpp b/test/bn_test.cpp index 071ec70..b66cad8 100644 --- a/test/bn_test.cpp +++ b/test/bn_test.cpp @@ -249,6 +249,28 @@ void testMillerLoop2(const G1& P1, const G2& Q1) CYBOZU_TEST_EQUAL(e2, e3); } +void testMillerLoopVec() +{ + const size_t n = 8; + G1 Pvec[n]; + G2 Qvec[n]; + char c = 'a'; + for (size_t i = 0; i < n; i++) { + hashAndMapToG1(Pvec[i], &c, 1); + hashAndMapToG2(Qvec[i], &c, 1); + c++; + } + Fp12 f1, f2; + f1 = 1; + for (size_t i = 0; i < n; i++) { + Fp12 e; + millerLoop(e, Pvec[i], Qvec[i]); + f1 *= e; + } + millerLoopVec(f2, Pvec, Qvec, n); + CYBOZU_TEST_EQUAL(f1, f2); +} + void testPairing(const G1& P, const G2& Q, const char *eStr) { Fp12 e1; @@ -378,6 +400,7 @@ CYBOZU_TEST_AUTO(naive) testPairing(P, Q, ts.e); testPrecomputed(P, Q); testMillerLoop2(P, Q); + testMillerLoopVec(); testBench(P, Q); benchAddDblG1(); benchAddDblG2();