diff options
-rw-r--r-- | include/mcl/ec.hpp | 14 | ||||
-rw-r--r-- | include/mcl/fp.hpp | 14 | ||||
-rw-r--r-- | include/mcl/util.hpp | 29 | ||||
-rw-r--r-- | sample/Makefile | 2 |
4 files changed, 48 insertions, 11 deletions
diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index 30cc433..f1e2de7 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -368,12 +368,16 @@ public: } static inline void mulArray(EcT& z, const EcT& x, const fp::Unit *y, size_t yn, bool isNegative) { - EcT out; - fp::powerGeneric(out, x, y, yn, EcT::add, EcT::dbl); + EcT tmp; + const EcT *px = &x; + if (&z == &x) { + tmp = x; + px = &tmp; + } + z.clear(); + fp::powerGeneric(z, *px, y, yn, EcT::add, EcT::dbl); if (isNegative) { - neg(z, out); - } else { - z = out; + neg(z, z); } } template<class tag, size_t maxBitSize, template<class _tag, size_t _maxBitSize>class FpT> diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index b159e2b..b6f4df8 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -270,12 +270,16 @@ public: } static inline void powerArray(FpT& z, const FpT& x, const Unit *y, size_t yn, bool isNegative) { - FpT out(1); - fp::powerGeneric(out, x, y, yn, FpT::mul, FpT::square); + FpT tmp; + const FpT *px = &x; + if (&z == &x) { + tmp = x; + px = &tmp; + } + z = 1; + fp::powerGeneric(z, *px, y, yn, FpT::mul, FpT::square); if (isNegative) { - FpT::inv(z, out); - } else { - z = out; + FpT::inv(z, z); } } template<class tag2, size_t maxBitSize2> diff --git a/include/mcl/util.hpp b/include/mcl/util.hpp index 4722a07..74d7bda 100644 --- a/include/mcl/util.hpp +++ b/include/mcl/util.hpp @@ -142,6 +142,34 @@ void getRandVal(T *out, RG& rg, const T *in, size_t bitSize) */ template<class G, class T> void powerGeneric(G& out, const G& x, const T *y, size_t n, void mul(G&, const G&, const G&) , void square(G&, const G&)){ +#if 1 + assert(&out != &x); + G t(x); + while (n > 0) { + if (y[n - 1]) break; + n--; + } + if (n == 0) return; + out = x; + int m = cybozu::bsr<T>(y[n - 1]); + if (m == 0) { + if (n == 1) return; + n--; + m = (int)sizeof(T) * 8; + } + for (int i = (int)n - 1; i >= 0; i--) { + T v = y[i]; + if (i < n - 1) { + m = (int)sizeof(T) * 8; + } + for (int j = m - 1; j >= 0; j--) { + square(out, out); + if (v & (T(1) << j)) { + mul(out, out, t); + } + } + } +#else G t(x); while (n > 0) { if (y[n - 1]) break; @@ -161,6 +189,7 @@ void powerGeneric(G& out, const G& x, const T *y, size_t n, void mul(G&, const G square(t, t); } } +#endif } } } // mcl::fp diff --git a/sample/Makefile b/sample/Makefile index a5c41dd..d68f746 100644 --- a/sample/Makefile +++ b/sample/Makefile @@ -3,7 +3,7 @@ include ../common.mk TARGET=$(TEST_FILE) LIBS= -SRC=$(wildcard *.cpp) +SRC=bench.cpp ecdh.cpp random.cpp vote.cpp all: $(TARGET) |