From 2053b495a7b90be7268ef74e3a5ce0e84910e202 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Thu, 25 Jul 2019 22:10:49 +0900 Subject: [PATCH] simplify GLV1T class --- include/mcl/bn.hpp | 2 +- include/mcl/ec.hpp | 43 ++++++++++++++++++++++--------------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/include/mcl/bn.hpp b/include/mcl/bn.hpp index d1cb9f7..147f8bb 100644 --- a/include/mcl/bn.hpp +++ b/include/mcl/bn.hpp @@ -567,7 +567,7 @@ typedef mcl::FixedArray NafArray; GLV for G1 on BN/BLS12 */ -struct GLV1 : mcl::GLV1T { +struct GLV1 : mcl::GLV1T { static bool usePrecomputedTable(int curveType) { if (curveType < 0) return false; diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index dd77193..1d0ad49 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -1070,8 +1070,8 @@ template int EcT::mode_; namespace local { -template -void addTbl(G& Q, const G *tbl, const Vec& naf, size_t i) +template +void addTbl(Ec& Q, const Ec *tbl, const Vec& naf, size_t i) { if (i >= naf.size()) return; int n = naf[i]; @@ -1084,9 +1084,10 @@ void addTbl(G& Q, const G *tbl, const Vec& naf, size_t i) } // mcl::local -template +template struct GLV1T { - static F rw; // rw = 1 / w = (-1 - sqrt(-3)) / 2 + typedef typename Ec::Fp Fp; + static Fp rw; // rw = 1 / w = (-1 - sqrt(-3)) / 2 static size_t rBitSize; static mpz_class v0, v1; static mpz_class B[2][2]; @@ -1110,9 +1111,9 @@ public: /* L (x, y) = (rw x, y) */ - static void mulLambda(G& Q, const G& P) + static void mulLambda(Ec& Q, const Ec& P) { - F::mul(Q.x, P.x, rw); + Fp::mul(Q.x, P.x, rw); Q.y = P.y; Q.z = P.z; } @@ -1127,14 +1128,14 @@ public: a = x - (t * B[0][0] + b * B[1][0]); b = - (t * B[0][1] + b * B[1][1]); } - static void mul(G& Q, const G& P, mpz_class x, bool constTime = false) + static void mul(Ec& Q, const Ec& P, mpz_class x, bool constTime = false) { const int w = 5; const size_t tblSize = 1 << (w - 2); - typedef mcl::FixedArray NafArray; + typedef mcl::FixedArray NafArray; NafArray naf[2]; mpz_class u[2]; - G tbl[2][tblSize]; + Ec tbl[2][tblSize]; bool b; x %= r; @@ -1154,22 +1155,22 @@ public: tbl[0][0] = P; mulLambda(tbl[1][0], tbl[0][0]); { - G P2; - G::dbl(P2, P); + Ec P2; + Ec::dbl(P2, P); for (size_t i = 1; i < tblSize; i++) { - G::add(tbl[0][i], tbl[0][i - 1], P2); + Ec::add(tbl[0][i], tbl[0][i - 1], P2); mulLambda(tbl[1][i], tbl[0][i]); } } const size_t maxBit = fp::max_(naf[0].size(), naf[1].size()); Q.clear(); for (size_t i = 0; i < maxBit; i++) { - G::dbl(Q, Q); + Ec::dbl(Q, Q); local::addTbl(Q, tbl[0], naf[0], maxBit - 1 - i); local::addTbl(Q, tbl[1], naf[1], maxBit - 1 - i); } } - static void mulArray(G& z, const G& x, const mcl::fp::Unit *y, size_t yn, bool isNegative, bool constTime) + static void mulArray(Ec& z, const Ec& x, const mcl::fp::Unit *y, size_t yn, bool isNegative, bool constTime) { mpz_class s; bool b; @@ -1183,7 +1184,7 @@ public: */ static void initForSecp256k1(const mpz_class& _r) { - bool b = F::squareRoot(rw, -3); + bool b = Fp::squareRoot(rw, -3); assert(b); (void)b; rw = -(rw + 1) / 2; @@ -1203,12 +1204,12 @@ public: }; // rw = 1 / w = (-1 - sqrt(-3)) / 2 -template F GLV1T::rw; -template size_t GLV1T::rBitSize; -template mpz_class GLV1T::v0; -template mpz_class GLV1T::v1; -template mpz_class GLV1T::B[2][2]; -template mpz_class GLV1T::r; +template typename Ec::Fp GLV1T::rw; +template size_t GLV1T::rBitSize; +template mpz_class GLV1T::v0; +template mpz_class GLV1T::v1; +template mpz_class GLV1T::B[2][2]; +template mpz_class GLV1T::r; struct EcParam { const char *name;