From b7148f7f3b5ddcc6f8a3aa72a3cf84f856479454 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Sun, 7 Jun 2015 05:38:00 +0900 Subject: [PATCH] split Unit --- include/mcl/fp.hpp | 3 +- include/mcl/fp_generator.hpp | 2 +- include/mcl/fp_proto.hpp | 2 +- include/mcl/{fp_base.hpp => op.hpp} | 93 ++--------------------------- include/mcl/unit.hpp | 88 +++++++++++++++++++++++++++ src/fp.cpp | 2 +- test/base_test.cpp | 2 +- 7 files changed, 98 insertions(+), 94 deletions(-) rename include/mcl/{fp_base.hpp => op.hpp} (52%) create mode 100644 include/mcl/unit.hpp diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 24ea9d8..1be0c4b 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -23,9 +23,8 @@ #include #include #include -#include +#include #include -#include #include namespace mcl { diff --git a/include/mcl/fp_generator.hpp b/include/mcl/fp_generator.hpp index 02bdbb9..961c4ca 100644 --- a/include/mcl/fp_generator.hpp +++ b/include/mcl/fp_generator.hpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #if (CYBOZU_HOST == CYBOZU_HOST_INTEL) && (CYBOZU_OS_BIT == 64) diff --git a/include/mcl/fp_proto.hpp b/include/mcl/fp_proto.hpp index b9e6f8b..2f45e55 100644 --- a/include/mcl/fp_proto.hpp +++ b/include/mcl/fp_proto.hpp @@ -6,7 +6,7 @@ @license modified new BSD license http://opensource.org/licenses/BSD-3-Clause */ -#include +#include #ifdef MCL_USE_LLVM diff --git a/include/mcl/fp_base.hpp b/include/mcl/op.hpp similarity index 52% rename from include/mcl/fp_base.hpp rename to include/mcl/op.hpp index afd1357..6f2aadf 100644 --- a/include/mcl/fp_base.hpp +++ b/include/mcl/op.hpp @@ -1,26 +1,14 @@ #pragma once /** @file - @brief basic operation + @brief definition of Op @author MITSUNARI Shigeo(@herumi) @license modified new BSD license http://opensource.org/licenses/BSD-3-Clause */ #include #include -#ifdef _MSC_VER - #pragma warning(push) - #pragma warning(disable : 4616) - #pragma warning(disable : 4800) - #pragma warning(disable : 4244) - #pragma warning(disable : 4127) - #pragma warning(disable : 4512) - #pragma warning(disable : 4146) -#endif -#include -#ifdef _MSC_VER - #pragma warning(pop) -#endif +#include #ifndef MCL_MAX_OP_BIT_N #define MCL_MAX_OP_BIT_N 521 @@ -32,12 +20,6 @@ struct FpGenerator; namespace fp { -#if defined(CYBOZU_OS_BIT) && (CYBOZU_OS_BIT == 32) -typedef uint32_t Unit; -#else -typedef uint64_t Unit; -#endif -const size_t UnitBitN = sizeof(Unit) * 8; const size_t maxOpUnitN = (MCL_MAX_OP_BIT_N + UnitBitN - 1) / UnitBitN; struct Op; @@ -114,74 +96,9 @@ struct Op { void init(const Unit *p, size_t bitLen); static FpGenerator* createFpGenerator(); static void destroyFpGenerator(FpGenerator *fg); +private: + Op(const Op&); + void operator==(const Op&); }; -/* - get pp such that p * pp = -1 mod M, - where p is prime and M = 1 << 64(or 32). - @param pLow [in] p mod M - T is uint32_t or uint64_t -*/ -template -T getMontgomeryCoeff(T pLow) -{ - T ret = 0; - T t = 0; - T x = 1; - for (size_t i = 0; i < sizeof(T) * 8; i++) { - if ((t & 1) == 0) { - t += pLow; - ret += x; - } - t >>= 1; - x <<= 1; - } - return ret; -} - -inline int compareArray(const Unit* x, const Unit* y, size_t n) -{ - for (size_t i = n - 1; i != size_t(-1); i--) { - if (x[i] < y[i]) return -1; - if (x[i] > y[i]) return 1; - } - return 0; -} - -inline bool isEqualArray(const Unit* x, const Unit* y, size_t n) -{ - for (size_t i = 0; i < n; i++) { - if (x[i] != y[i]) return false; - } - return true; -} - -inline bool isZeroArray(const Unit *x, size_t n) -{ - for (size_t i = 0; i < n; i++) { - if (x[i]) return false; - } - return true; -} - -inline void clearArray(Unit *x, size_t begin, size_t end) -{ - for (size_t i = begin; i < end; i++) x[i] = 0; -} - -inline void copyArray(Unit *y, const Unit *x, size_t n) -{ - for (size_t i = 0; i < n; i++) y[i] = x[i]; -} - -inline void toArray(Unit *y, size_t yn, const mpz_srcptr x) -{ - const int xn = x->_mp_size; - assert(xn >= 0); - const Unit* xp = (const Unit*)x->_mp_d; - assert(xn <= (int)yn); - copyArray(y, xp, xn); - clearArray(y, xn, yn); -} - } } // mcl::fp diff --git a/include/mcl/unit.hpp b/include/mcl/unit.hpp new file mode 100644 index 0000000..19c4b1b --- /dev/null +++ b/include/mcl/unit.hpp @@ -0,0 +1,88 @@ +#pragma once +/** + @file + @brief definition of Unit and some functions + @author MITSUNARI Shigeo(@herumi) + @license modified new BSD license + http://opensource.org/licenses/BSD-3-Clause +*/ +#include +#include + +namespace mcl { namespace fp { + +#if defined(CYBOZU_OS_BIT) && (CYBOZU_OS_BIT == 32) +typedef uint32_t Unit; +#else +typedef uint64_t Unit; +#endif +const size_t UnitBitN = sizeof(Unit) * 8; + +/* + get pp such that p * pp = -1 mod M, + where p is prime and M = 1 << 64(or 32). + @param pLow [in] p mod M +*/ +inline Unit getMontgomeryCoeff(Unit pLow) +{ + Unit ret = 0; + Unit t = 0; + Unit x = 1; + for (size_t i = 0; i < UnitBitN; i++) { + if ((t & 1) == 0) { + t += pLow; + ret += x; + } + t >>= 1; + x <<= 1; + } + return ret; +} + +inline int compareArray(const Unit* x, const Unit* y, size_t n) +{ + for (size_t i = n - 1; i != size_t(-1); i--) { + if (x[i] < y[i]) return -1; + if (x[i] > y[i]) return 1; + } + return 0; +} + +inline bool isEqualArray(const Unit* x, const Unit* y, size_t n) +{ + for (size_t i = 0; i < n; i++) { + if (x[i] != y[i]) return false; + } + return true; +} + +inline bool isZeroArray(const Unit *x, size_t n) +{ + for (size_t i = 0; i < n; i++) { + if (x[i]) return false; + } + return true; +} + +inline void clearArray(Unit *x, size_t begin, size_t end) +{ + for (size_t i = begin; i < end; i++) x[i] = 0; +} + +inline void copyArray(Unit *y, const Unit *x, size_t n) +{ + for (size_t i = 0; i < n; i++) y[i] = x[i]; +} + +inline void toArray(Unit *y, size_t yn, const mpz_srcptr x) +{ + const int xn = x->_mp_size; + assert(xn >= 0); + const Unit* xp = (const Unit*)x->_mp_d; + assert(xn <= (int)yn); + copyArray(y, xp, xn); + clearArray(y, xn, yn); +} + +} } // mcl::fp + diff --git a/src/fp.cpp b/src/fp.cpp index 9a58940..9900aa1 100644 --- a/src/fp.cpp +++ b/src/fp.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef USE_MONT_FP #include #endif diff --git a/test/base_test.cpp b/test/base_test.cpp index 3b677f5..d78364a 100644 --- a/test/base_test.cpp +++ b/test/base_test.cpp @@ -1,6 +1,6 @@ #include #define MCL_USE_LLVM -#include +#include #include #include #include