diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-05-12 04:37:48 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-05-12 04:37:51 +0800 |
commit | d3d4532184586ba3e0470819d47fe9dfbe91a95f (patch) | |
tree | 2a9e9c02c92d6a00c0e8f8b8b3765d6dc5ed2eae /include | |
parent | 52dd40b22a7cf01760662059e6101a070400f107 (diff) | |
download | tangerine-mcl-d3d4532184586ba3e0470819d47fe9dfbe91a95f.tar.gz tangerine-mcl-d3d4532184586ba3e0470819d47fe9dfbe91a95f.tar.zst tangerine-mcl-d3d4532184586ba3e0470819d47fe9dfbe91a95f.zip |
change struct gmp to namespace gmp
Diffstat (limited to 'include')
-rw-r--r-- | include/mcl/bn.hpp | 4 | ||||
-rw-r--r-- | include/mcl/gmp_util.hpp | 524 |
2 files changed, 264 insertions, 264 deletions
diff --git a/include/mcl/bn.hpp b/include/mcl/bn.hpp index a02c562..d178dc1 100644 --- a/include/mcl/bn.hpp +++ b/include/mcl/bn.hpp @@ -349,9 +349,9 @@ struct ParamT { Fp::sqr(Z, tmp.a); const mpz_class largest_c = abs(6 * z + 2); - useNAF = getNAF(siTbl, largest_c); + useNAF = gmp::getNAF(siTbl, largest_c); precomputedQcoeffSize = getPrecomputeQcoeffSize(siTbl); - getNAF(zReplTbl, abs(z)); + gmp::getNAF(zReplTbl, abs(z)); exp_c0 = -2 + z * (-18 + z * (-30 - 36 *z)); exp_c1 = 1 + z * (-12 + z * (-18 - 36 * z)); exp_c2 = 6 * z * z + 1; diff --git a/include/mcl/gmp_util.hpp b/include/mcl/gmp_util.hpp index ff98159..8fbd51e 100644 --- a/include/mcl/gmp_util.hpp +++ b/include/mcl/gmp_util.hpp @@ -28,286 +28,284 @@ #include <cybozu/link_mpir.hpp> #endif -namespace mcl { +namespace mcl { namespace gmp { -struct gmp { - typedef mpz_class ImplType; +typedef mpz_class ImplType; #if CYBOZU_OS_BIT == 64 - typedef uint64_t Unit; +typedef uint64_t Unit; #else - typedef uint32_t Unit; +typedef uint32_t Unit; #endif - // z = [buf[n-1]:..:buf[1]:buf[0]] - // eg. buf[] = {0x12345678, 0xaabbccdd}; => z = 0xaabbccdd12345678; - template<class T> - static void setArray(mpz_class& z, const T *buf, size_t n) - { - mpz_import(z.get_mpz_t(), n, -1, sizeof(*buf), 0, 0, buf); - } - /* - buf[0, size) = x - buf[size, maxSize) with zero - */ - template<class T> - static void getArray(T *buf, size_t maxSize, const mpz_srcptr x) - { - const size_t bufByteSize = sizeof(T) * maxSize; - const int xn = x->_mp_size; - if (xn < 0) throw cybozu::Exception("gmp:getArray:x is negative"); - size_t xByteSize = sizeof(*x->_mp_d) * xn; - if (xByteSize > bufByteSize) throw cybozu::Exception("gmp:getArray:too small") << xn << maxSize; - memcpy(buf, x->_mp_d, xByteSize); - memset((char*)buf + xByteSize, 0, bufByteSize - xByteSize); - } - template<class T> - static void getArray(T *buf, size_t maxSize, const mpz_class& x) - { - getArray(buf, maxSize, x.get_mpz_t()); - } - static inline void set(mpz_class& z, uint64_t x) - { - setArray(z, &x, 1); - } - static inline bool setStr(mpz_class& z, const std::string& str, int base = 0) - { - return z.set_str(str, base) == 0; - } - static inline void getStr(std::string& str, const mpz_class& z, int base = 10) - { - str = z.get_str(base); - } - static inline void add(mpz_class& z, const mpz_class& x, const mpz_class& y) - { - mpz_add(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t()); - } - static inline void add(mpz_class& z, const mpz_class& x, unsigned int y) - { - mpz_add_ui(z.get_mpz_t(), x.get_mpz_t(), y); - } - static inline void sub(mpz_class& z, const mpz_class& x, const mpz_class& y) - { - mpz_sub(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t()); - } - static inline void sub(mpz_class& z, const mpz_class& x, unsigned int y) - { - mpz_sub_ui(z.get_mpz_t(), x.get_mpz_t(), y); - } - static inline void mul(mpz_class& z, const mpz_class& x, const mpz_class& y) - { - mpz_mul(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t()); - } - static inline void sqr(mpz_class& z, const mpz_class& x) - { - mpz_mul(z.get_mpz_t(), x.get_mpz_t(), x.get_mpz_t()); - } - static inline void mul(mpz_class& z, const mpz_class& x, unsigned int y) - { - mpz_mul_ui(z.get_mpz_t(), x.get_mpz_t(), y); - } - static inline void divmod(mpz_class& q, mpz_class& r, const mpz_class& x, const mpz_class& y) - { - mpz_divmod(q.get_mpz_t(), r.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t()); - } - static inline void div(mpz_class& q, const mpz_class& x, const mpz_class& y) - { - mpz_div(q.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t()); - } - static inline void div(mpz_class& q, const mpz_class& x, unsigned int y) - { - mpz_div_ui(q.get_mpz_t(), x.get_mpz_t(), y); - } - static inline void mod(mpz_class& r, const mpz_class& x, const mpz_class& m) - { - mpz_mod(r.get_mpz_t(), x.get_mpz_t(), m.get_mpz_t()); - } - static inline void mod(mpz_class& r, const mpz_class& x, unsigned int m) - { - mpz_mod_ui(r.get_mpz_t(), x.get_mpz_t(), m); - } - static inline void clear(mpz_class& z) - { - mpz_set_ui(z.get_mpz_t(), 0); - } - static inline bool isZero(const mpz_class& z) - { - return mpz_sgn(z.get_mpz_t()) == 0; - } - static inline bool isNegative(const mpz_class& z) - { - return mpz_sgn(z.get_mpz_t()) < 0; - } - static inline void neg(mpz_class& z, const mpz_class& x) - { - mpz_neg(z.get_mpz_t(), x.get_mpz_t()); - } - static inline int compare(const mpz_class& x, const mpz_class & y) - { - return mpz_cmp(x.get_mpz_t(), y.get_mpz_t()); - } - static inline int compare(const mpz_class& x, int y) - { - return mpz_cmp_si(x.get_mpz_t(), y); - } - template<class T> - static inline void addMod(mpz_class& z, const mpz_class& x, const T& y, const mpz_class& m) - { - add(z, x, y); - if (compare(z, m) >= 0) { - sub(z, z, m); - } - } - template<class T> - static inline void subMod(mpz_class& z, const mpz_class& x, const T& y, const mpz_class& m) - { - sub(z, x, y); - if (!isNegative(z)) return; - add(z, z, m); - } - template<class T> - static inline void mulMod(mpz_class& z, const mpz_class& x, const T& y, const mpz_class& m) - { - mul(z, x, y); - mod(z, z, m); - } - static inline void sqrMod(mpz_class& z, const mpz_class& x, const mpz_class& m) - { - sqr(z, x); - mod(z, z, m); - } - // z = x^y (y >= 0) - static inline void pow(mpz_class& z, const mpz_class& x, unsigned int y) - { - mpz_pow_ui(z.get_mpz_t(), x.get_mpz_t(), y); - } - // z = x^y mod m (y >=0) - static inline void powMod(mpz_class& z, const mpz_class& x, const mpz_class& y, const mpz_class& m) - { - mpz_powm(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t(), m.get_mpz_t()); - } - // z = 1/x mod m - static inline void invMod(mpz_class& z, const mpz_class& x, const mpz_class& m) - { - mpz_invert(z.get_mpz_t(), x.get_mpz_t(), m.get_mpz_t()); - } - // z = lcm(x, y) - static inline void lcm(mpz_class& z, const mpz_class& x, const mpz_class& y) - { - mpz_lcm(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t()); - } - static inline mpz_class lcm(const mpz_class& x, const mpz_class& y) - { - mpz_class z; - lcm(z, x, y); - return z; - } - // z = gcd(x, y) - static inline void gcd(mpz_class& z, const mpz_class& x, const mpz_class& y) - { - mpz_gcd(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t()); - } - static inline mpz_class gcd(const mpz_class& x, const mpz_class& y) - { - mpz_class z; - gcd(z, x, y); - return z; - } - /* - assume p : odd prime - return 1 if x^2 = a mod p for some x - return -1 if x^2 != a mod p for any x - */ - static inline int legendre(const mpz_class& a, const mpz_class& p) - { - return mpz_legendre(a.get_mpz_t(), p.get_mpz_t()); - } - static inline bool isPrime(const mpz_class& x) - { - return mpz_probab_prime_p(x.get_mpz_t(), 25) != 0; - } - static inline size_t getBitSize(const mpz_class& x) - { - return mpz_sizeinbase(x.get_mpz_t(), 2); - } - static inline bool testBit(const mpz_class& x, size_t pos) - { - return mpz_tstbit(x.get_mpz_t(), pos) != 0; - } - static inline void setBit(mpz_class& x, size_t pos, bool v = true) - { - if (v) { - mpz_setbit(x.get_mpz_t(), pos); - } else { - resetBit(x, pos); - } - } - static inline void resetBit(mpz_class& x, size_t pos) - { - mpz_clrbit(x.get_mpz_t(), pos); - } - static inline Unit getUnit(const mpz_class& x, size_t i) - { - return x.get_mpz_t()->_mp_d[i]; +// z = [buf[n-1]:..:buf[1]:buf[0]] +// eg. buf[] = {0x12345678, 0xaabbccdd}; => z = 0xaabbccdd12345678; +template<class T> +void setArray(mpz_class& z, const T *buf, size_t n) +{ + mpz_import(z.get_mpz_t(), n, -1, sizeof(*buf), 0, 0, buf); +} +/* + buf[0, size) = x + buf[size, maxSize) with zero +*/ +template<class T> +void getArray(T *buf, size_t maxSize, const mpz_srcptr x) +{ + const size_t bufByteSize = sizeof(T) * maxSize; + const int xn = x->_mp_size; + if (xn < 0) throw cybozu::Exception("gmp:getArray:x is negative"); + size_t xByteSize = sizeof(*x->_mp_d) * xn; + if (xByteSize > bufByteSize) throw cybozu::Exception("gmp:getArray:too small") << xn << maxSize; + memcpy(buf, x->_mp_d, xByteSize); + memset((char*)buf + xByteSize, 0, bufByteSize - xByteSize); +} +template<class T> +void getArray(T *buf, size_t maxSize, const mpz_class& x) +{ + getArray(buf, maxSize, x.get_mpz_t()); +} +inline void set(mpz_class& z, uint64_t x) +{ + setArray(z, &x, 1); +} +inline bool setStr(mpz_class& z, const std::string& str, int base = 0) +{ + return z.set_str(str, base) == 0; +} +inline void getStr(std::string& str, const mpz_class& z, int base = 10) +{ + str = z.get_str(base); +} +inline void add(mpz_class& z, const mpz_class& x, const mpz_class& y) +{ + mpz_add(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t()); +} +inline void add(mpz_class& z, const mpz_class& x, unsigned int y) +{ + mpz_add_ui(z.get_mpz_t(), x.get_mpz_t(), y); +} +inline void sub(mpz_class& z, const mpz_class& x, const mpz_class& y) +{ + mpz_sub(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t()); +} +inline void sub(mpz_class& z, const mpz_class& x, unsigned int y) +{ + mpz_sub_ui(z.get_mpz_t(), x.get_mpz_t(), y); +} +inline void mul(mpz_class& z, const mpz_class& x, const mpz_class& y) +{ + mpz_mul(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t()); +} +inline void sqr(mpz_class& z, const mpz_class& x) +{ + mpz_mul(z.get_mpz_t(), x.get_mpz_t(), x.get_mpz_t()); +} +inline void mul(mpz_class& z, const mpz_class& x, unsigned int y) +{ + mpz_mul_ui(z.get_mpz_t(), x.get_mpz_t(), y); +} +inline void divmod(mpz_class& q, mpz_class& r, const mpz_class& x, const mpz_class& y) +{ + mpz_divmod(q.get_mpz_t(), r.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t()); +} +inline void div(mpz_class& q, const mpz_class& x, const mpz_class& y) +{ + mpz_div(q.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t()); +} +inline void div(mpz_class& q, const mpz_class& x, unsigned int y) +{ + mpz_div_ui(q.get_mpz_t(), x.get_mpz_t(), y); +} +inline void mod(mpz_class& r, const mpz_class& x, const mpz_class& m) +{ + mpz_mod(r.get_mpz_t(), x.get_mpz_t(), m.get_mpz_t()); +} +inline void mod(mpz_class& r, const mpz_class& x, unsigned int m) +{ + mpz_mod_ui(r.get_mpz_t(), x.get_mpz_t(), m); +} +inline void clear(mpz_class& z) +{ + mpz_set_ui(z.get_mpz_t(), 0); +} +inline bool isZero(const mpz_class& z) +{ + return mpz_sgn(z.get_mpz_t()) == 0; +} +inline bool isNegative(const mpz_class& z) +{ + return mpz_sgn(z.get_mpz_t()) < 0; +} +inline void neg(mpz_class& z, const mpz_class& x) +{ + mpz_neg(z.get_mpz_t(), x.get_mpz_t()); +} +inline int compare(const mpz_class& x, const mpz_class & y) +{ + return mpz_cmp(x.get_mpz_t(), y.get_mpz_t()); +} +inline int compare(const mpz_class& x, int y) +{ + return mpz_cmp_si(x.get_mpz_t(), y); +} +template<class T> +void addMod(mpz_class& z, const mpz_class& x, const T& y, const mpz_class& m) +{ + add(z, x, y); + if (compare(z, m) >= 0) { + sub(z, z, m); } - static inline const Unit *getUnit(const mpz_class& x) - { - return reinterpret_cast<const Unit*>(x.get_mpz_t()->_mp_d); +} +template<class T> +void subMod(mpz_class& z, const mpz_class& x, const T& y, const mpz_class& m) +{ + sub(z, x, y); + if (!isNegative(z)) return; + add(z, z, m); +} +template<class T> +void mulMod(mpz_class& z, const mpz_class& x, const T& y, const mpz_class& m) +{ + mul(z, x, y); + mod(z, z, m); +} +inline void sqrMod(mpz_class& z, const mpz_class& x, const mpz_class& m) +{ + sqr(z, x); + mod(z, z, m); +} +// z = x^y (y >= 0) +inline void pow(mpz_class& z, const mpz_class& x, unsigned int y) +{ + mpz_pow_ui(z.get_mpz_t(), x.get_mpz_t(), y); +} +// z = x^y mod m (y >=0) +inline void powMod(mpz_class& z, const mpz_class& x, const mpz_class& y, const mpz_class& m) +{ + mpz_powm(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t(), m.get_mpz_t()); +} +// z = 1/x mod m +inline void invMod(mpz_class& z, const mpz_class& x, const mpz_class& m) +{ + mpz_invert(z.get_mpz_t(), x.get_mpz_t(), m.get_mpz_t()); +} +// z = lcm(x, y) +inline void lcm(mpz_class& z, const mpz_class& x, const mpz_class& y) +{ + mpz_lcm(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t()); +} +inline mpz_class lcm(const mpz_class& x, const mpz_class& y) +{ + mpz_class z; + lcm(z, x, y); + return z; +} +// z = gcd(x, y) +inline void gcd(mpz_class& z, const mpz_class& x, const mpz_class& y) +{ + mpz_gcd(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t()); +} +inline mpz_class gcd(const mpz_class& x, const mpz_class& y) +{ + mpz_class z; + gcd(z, x, y); + return z; +} +/* + assume p : odd prime + return 1 if x^2 = a mod p for some x + return -1 if x^2 != a mod p for any x +*/ +inline int legendre(const mpz_class& a, const mpz_class& p) +{ + return mpz_legendre(a.get_mpz_t(), p.get_mpz_t()); +} +inline bool isPrime(const mpz_class& x) +{ + return mpz_probab_prime_p(x.get_mpz_t(), 25) != 0; +} +inline size_t getBitSize(const mpz_class& x) +{ + return mpz_sizeinbase(x.get_mpz_t(), 2); +} +inline bool testBit(const mpz_class& x, size_t pos) +{ + return mpz_tstbit(x.get_mpz_t(), pos) != 0; +} +inline void resetBit(mpz_class& x, size_t pos) +{ + mpz_clrbit(x.get_mpz_t(), pos); +} +inline void setBit(mpz_class& x, size_t pos, bool v = true) +{ + if (v) { + mpz_setbit(x.get_mpz_t(), pos); + } else { + resetBit(x, pos); } - static inline size_t getUnitSize(const mpz_class& x) - { - assert(x.get_mpz_t()->_mp_size >= 0); - return x.get_mpz_t()->_mp_size; +} +inline Unit getUnit(const mpz_class& x, size_t i) +{ + return x.get_mpz_t()->_mp_d[i]; +} +inline const Unit *getUnit(const mpz_class& x) +{ + return reinterpret_cast<const Unit*>(x.get_mpz_t()->_mp_d); +} +inline size_t getUnitSize(const mpz_class& x) +{ + assert(x.get_mpz_t()->_mp_size >= 0); + return x.get_mpz_t()->_mp_size; +} +template<class RG> +void getRand(mpz_class& z, size_t bitSize, RG& rg) +{ + assert(bitSize > 1); + const size_t rem = bitSize & 31; + const size_t n = (bitSize + 31) / 32; + std::vector<uint32_t> buf(n); + rg.read(buf.data(), n); + uint32_t v = buf[n - 1]; + if (rem == 0) { + v |= 1U << 31; + } else { + v &= (1U << rem) - 1; + v |= 1U << (rem - 1); } - template<class RG> - static inline void getRand(mpz_class& z, size_t bitSize, RG& rg) - { - assert(bitSize > 1); - const size_t rem = bitSize & 31; - const size_t n = (bitSize + 31) / 32; - std::vector<uint32_t> buf(n); - rg.read(buf.data(), n); - uint32_t v = buf[n - 1]; - if (rem == 0) { - v |= 1U << 31; - } else { - v &= (1U << rem) - 1; - v |= 1U << (rem - 1); + buf[n - 1] = v; + setArray(z, &buf[0], n); +} +template<class RG> +void getRandPrime(mpz_class& z, size_t bitSize, RG& rg, bool setSecondBit = false, bool mustBe3mod4 = false) +{ + assert(bitSize > 2); + do { + getRand(z, bitSize, rg); + if (setSecondBit) { + z |= mpz_class(1) << (bitSize - 2); } - buf[n - 1] = v; - gmp::setArray(z, &buf[0], n); - } - template<class RG> - static void getRandPrime(mpz_class& z, size_t bitSize, RG& rg, bool setSecondBit = false, bool mustBe3mod4 = false) - { - assert(bitSize > 2); - do { - getRand(z, bitSize, rg); - if (setSecondBit) { - z |= mpz_class(1) << (bitSize - 2); - } - if (mustBe3mod4) { - z |= 3; - } - } while (!(isPrime(z))); - } - static inline mpz_class getQuadraticNonResidue(const mpz_class& p) - { - mpz_class g = 2; - while (gmp::legendre(g, p) > 0) { - g++; + if (mustBe3mod4) { + z |= 3; } - return g; + } while (!(isPrime(z))); +} +inline mpz_class getQuadraticNonResidue(const mpz_class& p) +{ + mpz_class g = 2; + while (legendre(g, p) > 0) { + g++; } -}; + return g; +} namespace impl { template<class Vec> void convertToBinary(Vec& v, const mpz_class& x) { - const size_t len = mcl::gmp::getBitSize(x); + const size_t len = gmp::getBitSize(x); v.clear(); for (size_t i = 0; i < len; i++) { - v.push_back(mcl::gmp::testBit(x, len - 1 - i) ? 1 : 0); + v.push_back(gmp::testBit(x, len - 1 - i) ? 1 : 0); } } @@ -380,6 +378,8 @@ bool getNAF(Vec& v, const mpz_class& x) } } +} // mcl::gmp + /* Tonelli-Shanks */ |