diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2015-06-11 09:24:17 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2015-06-11 09:24:17 +0800 |
commit | 96c6d37dc13c666f3f3bbd309549bc086ecc54c8 (patch) | |
tree | 0fa79f470a9467477126ea78a8ccbe0bcda01c4e | |
parent | 230fd2e21e6ae655e4d9eb544dae7bef63395015 (diff) | |
download | dexon-mcl-96c6d37dc13c666f3f3bbd309549bc086ecc54c8.tar.gz dexon-mcl-96c6d37dc13c666f3f3bbd309549bc086ecc54c8.tar.zst dexon-mcl-96c6d37dc13c666f3f3bbd309549bc086ecc54c8.zip |
rename Ec::power to Ec::mul
-rw-r--r-- | include/mcl/ec.hpp | 22 | ||||
-rw-r--r-- | include/mcl/util.hpp | 13 | ||||
-rw-r--r-- | include/mcl/window_method.hpp | 44 | ||||
-rw-r--r-- | sample/ecdh_smpl.cpp | 8 | ||||
-rw-r--r-- | test/Makefile | 2 | ||||
-rw-r--r-- | test/window_method_test.cpp | 24 |
6 files changed, 57 insertions, 56 deletions
diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index f8d649f..085676d 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -412,11 +412,11 @@ public: } static inline int compare(const EcT& P, const EcT& Q) { - return compareFunc(P, Q, _Fp::compare); + return compareFunc(P, Q, Fp::compare); } static inline int compareRaw(const EcT& P, const EcT& Q) { - return compareFunc(P, Q, _Fp::compareRaw); + return compareFunc(P, Q, Fp::compareRaw); } bool isZero() const { @@ -500,10 +500,10 @@ public: bool operator!=(const EcT& rhs) const { return !operator==(rhs); } }; -template<class _Fp> _Fp EcT<_Fp>::a_; -template<class _Fp> _Fp EcT<_Fp>::b_; -template<class _Fp> int EcT<_Fp>::specialA_; -template<class _Fp> bool EcT<_Fp>::compressedExpression_; +template<class Fp> Fp EcT<Fp>::a_; +template<class Fp> Fp EcT<Fp>::b_; +template<class Fp> int EcT<Fp>::specialA_; +template<class Fp> bool EcT<Fp>::compressedExpression_; struct EcParam { const char *name; @@ -521,14 +521,14 @@ struct EcParam { namespace std { CYBOZU_NAMESPACE_TR1_BEGIN template<class T> struct hash; -template<class _Fp> -struct hash<mcl::EcT<_Fp> > { - size_t operator()(const mcl::EcT<_Fp>& P) const +template<class Fp> +struct hash<mcl::EcT<Fp> > { + size_t operator()(const mcl::EcT<Fp>& P) const { if (P.isZero()) return 0; P.normalize(); - uint64_t v = hash<_Fp>()(P.x); - v = hash<_Fp>()(P.y, v); + uint64_t v = hash<Fp>()(P.x); + v = hash<Fp>()(P.y, v); return static_cast<size_t>(v); } }; diff --git a/include/mcl/util.hpp b/include/mcl/util.hpp index 4ac3dca..aa3fb12 100644 --- a/include/mcl/util.hpp +++ b/include/mcl/util.hpp @@ -113,17 +113,16 @@ 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&)){ G t(x); + while (n > 0) { + if (y[n - 1]) break; + n--; + } for (size_t i = 0; i < n; i++) { T v = y[i]; int m = (int)sizeof(T) * 8; if (i == n - 1) { -#if 1 - m = v ? cybozu::bsr<T>(v) + 1 : 0; -#else - while (m > 0 && (v & (Unit(1) << (m - 1))) == 0) { - m--; - } -#endif + assert(v); + m = cybozu::bsr<T>(v) + 1; } for (int j = 0; j < m; j++) { if (v & (T(1) << j)) { diff --git a/include/mcl/window_method.hpp b/include/mcl/window_method.hpp index a55bb9f..5e58470 100644 --- a/include/mcl/window_method.hpp +++ b/include/mcl/window_method.hpp @@ -17,24 +17,24 @@ namespace mcl { namespace fp { */ template<class T> struct ArrayIterator { - static const size_t TBitN = sizeof(T) * 8; + static const size_t TbitSize = sizeof(T) * 8; ArrayIterator(const T *x, size_t bitSize, size_t w) : x(x) , bitSize(bitSize) , w(w) , pos(0) - , mask((w == TBitN ? 0 : (T(1) << w)) - 1) + , mask((w == TbitSize ? 0 : (T(1) << w)) - 1) { - assert(w <= TBitN); + assert(w <= TbitSize); } bool hasNext() const { return bitSize > 0; } T getNext() { - if (w == TBitN) { + if (w == TbitSize) { bitSize -= w; return *x++; } - if (pos + w < TBitN) { + if (pos + w < TbitSize) { T v = (*x >> pos) & mask; pos += w; if (bitSize < w) { @@ -44,7 +44,7 @@ struct ArrayIterator { } return v; } - if (pos + bitSize <= TBitN) { + if (pos + bitSize <= TbitSize) { assert(bitSize <= w); T v = *x >> pos; assert((v >> bitSize) == 0); @@ -52,9 +52,9 @@ struct ArrayIterator { return v & mask; } assert(pos > 0); - T v = (x[0] >> pos) | (x[1] << (TBitN - pos)); + T v = (x[0] >> pos) | (x[1] << (TbitSize - pos)); v &= mask; - pos = (pos + w) - TBitN; + pos = (pos + w) - TbitSize; bitSize -= w; x++; return v; @@ -111,30 +111,32 @@ public: @param y [in] exponent */ template<class tag2, size_t maxBitSize2> - void power(Ec& z, const FpT<tag2, maxBitSize2>& y) const + void mul(Ec& z, const FpT<tag2, maxBitSize2>& y) const { fp::Block b; y.getBlock(b); - powerArray(z, b.p, b.n * UnitBitSize, false); + powerArray(z, b.p, b.n, false); } - void power(Ec& z, int y) const + void mul(Ec& z, int y) const { - if (y == 0) { - z.clear(); - return; - } Unit u = std::abs(y); - powerArray(z, &u, cybozu::bsr<Unit>(y) + 1, y < 0); + powerArray(z, &u, 1, y < 0); } - void power(Ec& z, const mpz_class& y) const + void mul(Ec& z, const mpz_class& y) const { - powerArray(z, Gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size) * UnitBitSize, y < 0); + powerArray(z, Gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0); } - void powerArray(Ec& z, const Unit* y, size_t bitSize, bool isNegative) const + void powerArray(Ec& z, const Unit* y, size_t n, bool isNegative) const { - if ((bitSize + winSize_ - 1) / winSize_ > tbl_.size()) throw cybozu::Exception("mcl:WindowMethod:powerArray:bad value") << bitSize << bitSize_ << winSize_; z.clear(); - if (bitSize == 0) return; + while (n > 0) { + if (y[n - 1]) break; + n--; + } + if (n == 0) return; + if (n > tbl_.size()) throw cybozu::Exception("mcl:WindowMethod:powerArray:bad n") << n << tbl_.size(); + assert(y[n - 1]); + const size_t bitSize = (n - 1) * UnitBitSize + cybozu::bsr<Unit>(y[n - 1]) + 1; size_t i = 0; ArrayIterator<Unit> ai(y, bitSize, winSize_); do { diff --git a/sample/ecdh_smpl.cpp b/sample/ecdh_smpl.cpp index 91bb9f6..22b9734 100644 --- a/sample/ecdh_smpl.cpp +++ b/sample/ecdh_smpl.cpp @@ -37,7 +37,7 @@ int main() Ec aP; a.setRand(rg); - Ec::power(aP, P, a); // aP = a * P; + Ec::mul(aP, P, a); // aP = a * P; std::cout << "aP=" << aP << std::endl; @@ -48,17 +48,17 @@ int main() Ec bP; b.setRand(rg); - Ec::power(bP, P, b); // bP = b * P; + Ec::mul(bP, P, b); // bP = b * P; std::cout << "bP=" << bP << std::endl; Ec abP, baP; // Alice uses bP(B's public key) and a(A's priavte key) - Ec::power(abP, bP, a); // abP = a * (bP) + Ec::mul(abP, bP, a); // abP = a * (bP) // Bob uses aP(A's public key) and b(B's private key) - Ec::power(baP, aP, b); // baP = b * (aP) + Ec::mul(baP, aP, b); // baP = b * (aP) if (abP == baP) { std::cout << "key sharing succeed:" << abP << std::endl; diff --git a/test/Makefile b/test/Makefile index 2b9551e..e6e3f0a 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,7 +2,7 @@ include ../common.mk TARGET=$(TEST_FILE) -SRC=fp_test.cpp ec_test.cpp fp_util_test.cpp +SRC=fp_test.cpp ec_test.cpp fp_util_test.cpp window_method_test.cpp ifeq ($(CPU),x64) SRC+=fp_generator_test.cpp mont_fp_test.cpp endif diff --git a/test/window_method_test.cpp b/test/window_method_test.cpp index fde6064..2d651be 100644 --- a/test/window_method_test.cpp +++ b/test/window_method_test.cpp @@ -42,29 +42,29 @@ CYBOZU_TEST_AUTO(int) for (size_t winSize = 2; winSize <= bitSize; winSize += 3) { PW pw(P, bitSize, winSize); for (int i = 0; i < (1 << bitSize); i++) { - pw.power(Q, i); - Ec::power(R, P, i); + pw.mul(Q, i); + Ec::mul(R, P, i); CYBOZU_TEST_EQUAL(Q, R); } } PW pw(P, para.bitSize, 10); - pw.power(Q, -12345); - Ec::power(R, P, -12345); + pw.mul(Q, -12345); + Ec::mul(R, P, -12345); CYBOZU_TEST_EQUAL(Q, R); mpz_class t(para.gx); - pw.power(Q, t); - Ec::power(R, P, t); + pw.mul(Q, t); + Ec::mul(R, P, t); CYBOZU_TEST_EQUAL(Q, R); t = -t; - pw.power(Q, t); - Ec::power(R, P, t); + pw.mul(Q, t); + Ec::mul(R, P, t); CYBOZU_TEST_EQUAL(Q, R); - pw.power(Q, x); - Ec::power(R, P, x); + pw.mul(Q, x); + Ec::mul(R, P, x); CYBOZU_TEST_EQUAL(Q, R); - pw.power(Q, y); - Ec::power(R, P, y); + pw.mul(Q, y); + Ec::mul(R, P, y); CYBOZU_TEST_EQUAL(Q, R); } |