From d3aaf45e0fc9f38191b61609e71df614b2dcfb9a Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Sat, 26 Oct 2019 22:11:23 +0900 Subject: [PATCH] add option to select algorithm for G2::mulByCofactor --- include/mcl/bn.hpp | 18 +++++++++--------- test/bls12_test.cpp | 7 +++---- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/include/mcl/bn.hpp b/include/mcl/bn.hpp index f1188c8..74eace9 100644 --- a/include/mcl/bn.hpp +++ b/include/mcl/bn.hpp @@ -475,10 +475,10 @@ struct MapTo { Frobenius2(T1, T1); G2::add(Q, T0, T1); } - void mulByCofactorBLS12(G2& Q, const G2& P) const + void mulByCofactorBLS12(G2& Q, const G2& P, bool fast = false) const { mulByCofactorBLS12fast(Q, P); - if (useOriginalG2cofactor_) { + if (useOriginalG2cofactor_ && !fast) { Q *= g2cofactorAdj_; return; } @@ -588,14 +588,14 @@ struct MapTo { } assert(P.isValid()); } - void mulByCofactor(G2& P) const + void mulByCofactor(G2& P, bool fast = false) const { switch(type_) { case BNtype: mulByCofactorBN(P, P); break; case BLS12type: - mulByCofactorBLS12(P, P); + mulByCofactorBLS12(P, P, fast); break; } assert(P.isValid()); @@ -606,7 +606,7 @@ struct MapTo { mulByCofactor(P); return true; } - bool calc(G2& P, const Fp2& t) const + bool calc(G2& P, const Fp2& t, bool fast = false) const { if (!mapToEc(P, t)) return false; if (mapToMode_ == MCL_MAP_TO_MODE_ETH2) { @@ -617,7 +617,7 @@ struct MapTo { P.y = negY; } } - mulByCofactor(P); + mulByCofactor(P, fast); return true; } }; @@ -2141,7 +2141,7 @@ inline bool setMapToMode(int mode) return BN::nonConstParam.mapTo.setMapToMode(mode); } inline void mapToG1(bool *pb, G1& P, const Fp& x) { *pb = BN::param.mapTo.calc(P, x); } -inline void mapToG2(bool *pb, G2& P, const Fp2& x) { *pb = BN::param.mapTo.calc(P, x); } +inline void mapToG2(bool *pb, G2& P, const Fp2& x, bool fast = false) { *pb = BN::param.mapTo.calc(P, x, fast); } #ifndef CYBOZU_DONT_USE_EXCEPTION inline void mapToG1(G1& P, const Fp& x) { @@ -2149,10 +2149,10 @@ inline void mapToG1(G1& P, const Fp& x) mapToG1(&b, P, x); if (!b) throw cybozu::Exception("mapToG1:bad value") << x; } -inline void mapToG2(G2& P, const Fp2& x) +inline void mapToG2(G2& P, const Fp2& x, bool fast = false) { bool b; - mapToG2(&b, P, x); + mapToG2(&b, P, x, fast); if (!b) throw cybozu::Exception("mapToG2:bad value") << x; } #endif diff --git a/test/bls12_test.cpp b/test/bls12_test.cpp index 3f056af..c967006 100644 --- a/test/bls12_test.cpp +++ b/test/bls12_test.cpp @@ -730,9 +730,8 @@ CYBOZU_TEST_AUTO(eth2) const char *expectSig = "b9d1bf921b3dd048bdce38c2ceac2a2a8093c864881f2415f22b198de935ffa791707855c1656dc21a7af2d502bb46590151d645f062634c3b2cb79c4ed1c4a4b8b3f19f0f5c76965c651553e83d153ff95353735156eff77692f7a62ae653fb"; CYBOZU_TEST_EQUAL(sig.getStr(mcl::IoSerializeHexStr), expectSig); - CYBOZU_BENCH_C("mapToG2 org-cofactor", 1000, mapToG2, Q, m); - setOriginalG2cofactor(false); - CYBOZU_BENCH_C("mapToG2 fast-cofactor", 1000, mapToG2, Q, m); + CYBOZU_BENCH_C("mapToG2 org-cofactor", 1000, mapToG2, Q, m, false); + CYBOZU_BENCH_C("mapToG2 fast-cofactor", 1000, mapToG2, Q, m, true); Fp2 x; x.a = 5; @@ -743,7 +742,7 @@ CYBOZU_TEST_AUTO(eth2) BN::param.mapTo.mapToEc(Q, x); G2::mulGeneric(Q1, Q, g2c); Q2 = Q; - BN::param.mapTo.mulByCofactor(Q2); + BN::param.mapTo.mulByCofactor(Q2, true); Q2 *= g2ca; CYBOZU_TEST_EQUAL(Q1, Q2); }