From dd69b609759cc32a3c4cf8bfa02292832db237c7 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Thu, 13 Apr 2017 12:25:21 +0900 Subject: [PATCH] add EcT::normalize(EcT&, const EcT&) --- include/mcl/ec.hpp | 7 ++++++- include/mcl/fp.hpp | 1 - include/mcl/fp_tower.hpp | 3 --- include/mcl/operator.hpp | 2 +- include/mcl/util.hpp | 17 +++++++++++------ 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index 2533d20..8ddd456 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -155,6 +155,11 @@ public: } #endif } + static void normalize(EcT& y, const EcT& x) + { + y = x; + y.normalize(); + } static inline void init(const Fp& a, const Fp& b, int mode = ec::Jacobi) { a_ = a; @@ -835,7 +840,7 @@ public: px = &tmp; } z.clear(); - fp::powGeneric(z, *px, y, yn, EcT::add, EcT::dbl, constTime); + fp::powGeneric(z, *px, y, yn, EcT::add, EcT::dbl, EcT::normalize, constTime); if (isNegative) { neg(z, z); } diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 90a805a..bb76df3 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -455,7 +455,6 @@ public: { return fp::isLessArray(v_, rhs.v_, op_.N); } - void normalize() const {} // dummy method /* set IoMode for operator<<(), or operator>>() */ diff --git a/include/mcl/fp_tower.hpp b/include/mcl/fp_tower.hpp index 445d190..7ca0a41 100644 --- a/include/mcl/fp_tower.hpp +++ b/include/mcl/fp_tower.hpp @@ -180,7 +180,6 @@ public: bool isOne() const { return a.isOne() && b.isZero(); } bool operator==(const Fp2T& rhs) const { return a == rhs.a && b == rhs.b; } bool operator!=(const Fp2T& rhs) const { return !operator==(rhs); } - void normalize() const {} // dummy method /* return true is a is odd (do not consider b) this function is for only compressed reprezentation of EC @@ -782,7 +781,6 @@ struct Fp6T : public fp::Operator > { Fp2::mul(y.b, p.b, q); Fp2::mul(y.c, p.c, q); } - void normalize() const {} // dummy }; /* @@ -926,7 +924,6 @@ struct Fp12T : public fp::Operator > { { return a.getStr(ioMode) + fp::getIoSeparator(ioMode) + b.getStr(ioMode); } - void normalize() const {} // dummy }; } // mcl diff --git a/include/mcl/operator.hpp b/include/mcl/operator.hpp index 477c9bb..c1996c3 100644 --- a/include/mcl/operator.hpp +++ b/include/mcl/operator.hpp @@ -66,7 +66,7 @@ private: px = &tmp; } z = 1; - fp::powGeneric(z, *px, y, yn, T::mul, T::sqr); + fp::powGeneric(z, *px, y, yn, T::mul, T::sqr, (void (*)(T&, const T&))0); if (isNegative) { T::inv(z, z); } diff --git a/include/mcl/util.hpp b/include/mcl/util.hpp index afdeaf9..0d3876d 100644 --- a/include/mcl/util.hpp +++ b/include/mcl/util.hpp @@ -196,7 +196,7 @@ void getRandVal(T *out, RG& rg, const T *in, size_t bitSize) @note &out != x and out = the unit element of G */ template -void powGeneric(G& out, const G& x, const T *y, size_t n, void mul(G&, const G&, const G&) , void sqr(G&, const G&), bool constTime = false) +void powGeneric(G& out, const G& x, const T *y, size_t n, void mul(G&, const G&, const G&) , void sqr(G&, const G&), void normalize(G&, const G&), bool constTime = false) { assert(&out != &x); while (n > 0) { @@ -223,11 +223,16 @@ void powGeneric(G& out, const G& x, const T *y, size_t n, void mul(G&, const G&, } } G tbl[4]; // tbl = { discard, x, x^2, x^3 } - x.normalize(); - tbl[0] = x; - tbl[1] = x; - sqr(tbl[2], tbl[1]); tbl[2].normalize(); - mul(tbl[3], tbl[2], x); tbl[3].normalize(); + if (normalize) { + normalize(tbl[0], x); + } else { + tbl[0] = x; + } + tbl[1] = tbl[0]; + sqr(tbl[2], tbl[1]); + if (normalize) { normalize(tbl[2], tbl[2]); } + mul(tbl[3], tbl[2], x); + if (normalize) { normalize(tbl[3], tbl[3]); } T v = y[n - 1]; int m = cybozu::bsr(v); if (m & 1) {