diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-07-31 11:44:23 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-07-31 11:44:23 +0800 |
commit | 433203a446082e33cc160443e236b6818d5719d8 (patch) | |
tree | c696251542ff80a534dcc8860f4e146c13df9108 | |
parent | 585ac4f329647389e7d2fb7824cb9d1f23121eda (diff) | |
download | tangerine-mcl-433203a446082e33cc160443e236b6818d5719d8.tar.gz tangerine-mcl-433203a446082e33cc160443e236b6818d5719d8.tar.zst tangerine-mcl-433203a446082e33cc160443e236b6818d5719d8.zip |
use MCL_SIZEOF_UNIT macro instead of CYBOZU_OS_BIT
-rw-r--r-- | common.mk | 3 | ||||
-rw-r--r-- | include/mcl/gmp_util.hpp | 29 | ||||
-rw-r--r-- | include/mcl/op.hpp | 7 | ||||
-rw-r--r-- | include/mcl/vint.hpp | 22 | ||||
-rw-r--r-- | src/fp.cpp | 12 | ||||
-rw-r--r-- | src/fp_generator.hpp | 2 | ||||
-rw-r--r-- | src/low_func.hpp | 4 | ||||
-rw-r--r-- | src/low_func_llvm.hpp | 6 | ||||
-rw-r--r-- | test/base_test.cpp | 4 | ||||
-rw-r--r-- | test/bn_test.cpp | 2 | ||||
-rw-r--r-- | test/fp_generator_test.cpp | 2 | ||||
-rw-r--r-- | test/fp_test.cpp | 2 | ||||
-rw-r--r-- | test/mont_fp_test.cpp | 6 |
13 files changed, 61 insertions, 40 deletions
@@ -94,6 +94,9 @@ MCL_USE_OPENSSL?=1 ifeq ($(MCL_USE_GMP),0) CFLAGS+=-DMCL_USE_VINT endif +ifneq ($(MCL_SIZEOF_UNIT),) + CFLAGS+=-DMCL_SIZEOF_UNIT=$(MCL_SIZEOF_UNIT) +endif ifeq ($(MCL_USE_OPENSSL),0) CFLAGS+=-DMCL_DONT_USE_OPENSSL endif diff --git a/include/mcl/gmp_util.hpp b/include/mcl/gmp_util.hpp index 819f183..84b74d5 100644 --- a/include/mcl/gmp_util.hpp +++ b/include/mcl/gmp_util.hpp @@ -33,14 +33,31 @@ typedef mcl::Vint mpz_class; #endif #endif -namespace mcl { namespace gmp { +#ifndef MCL_SIZEOF_UNIT + #if defined(CYBOZU_OS_BIT) && (CYBOZU_OS_BIT == 32) + #define MCL_SIZEOF_UNIT 4 + #else + #define MCL_SIZEOF_UNIT 8 + #endif +#endif -typedef mpz_class ImplType; -#if CYBOZU_OS_BIT == 64 +namespace mcl { + +namespace fp { + +#if MCL_SIZEOF_UNIT == 8 typedef uint64_t Unit; #else typedef uint32_t Unit; #endif +#define MCL_UNIT_BIT_SIZE (MCL_SIZEOF_UNIT * 8) + +} // mcl::fp + +namespace gmp { + +typedef mpz_class ImplType; + // z = [buf[n-1]:..:buf[1]:buf[0]] // eg. buf[] = {0x12345678, 0xaabbccdd}; => z = 0xaabbccdd12345678; template<class T> @@ -367,15 +384,15 @@ inline void setBit(mpz_class& x, size_t pos, bool v = true) } #endif } -inline const Unit *getUnit(const mpz_class& x) +inline const fp::Unit *getUnit(const mpz_class& x) { #ifdef MCL_USE_VINT return x.getUnit(); #else - return reinterpret_cast<const Unit*>(x.get_mpz_t()->_mp_d); + return reinterpret_cast<const fp::Unit*>(x.get_mpz_t()->_mp_d); #endif } -inline Unit getUnit(const mpz_class& x, size_t i) +inline fp::Unit getUnit(const mpz_class& x, size_t i) { return getUnit(x)[i]; } diff --git a/include/mcl/op.hpp b/include/mcl/op.hpp index f485e7c..9e02058 100644 --- a/include/mcl/op.hpp +++ b/include/mcl/op.hpp @@ -89,15 +89,10 @@ enum IoMode { namespace fp { -#if MCL_UNIT_BYTE_SIZE == 4 || (defined(CYBOZU_OS_BIT) && (CYBOZU_OS_BIT == 32)) -typedef uint32_t Unit; -#else -typedef uint64_t Unit; -#endif const size_t UnitBitSize = sizeof(Unit) * 8; const size_t maxUnitSize = (MCL_MAX_BIT_SIZE + UnitBitSize - 1) / UnitBitSize; -#define MCL_MAX_UNIT_SIZE ((MCL_MAX_BIT_SIZE + CYBOZU_OS_BIT - 1) / CYBOZU_OS_BIT) +#define MCL_MAX_UNIT_SIZE ((MCL_MAX_BIT_SIZE + MCL_UNIT_BIT_SIZE - 1) / MCL_UNIT_BIT_SIZE) struct FpGenerator; struct Op; diff --git a/include/mcl/vint.hpp b/include/mcl/vint.hpp index 4f46eb9..5e3d240 100644 --- a/include/mcl/vint.hpp +++ b/include/mcl/vint.hpp @@ -14,20 +14,22 @@ #include <iostream> #include <mcl/util.hpp> -#ifndef MCL_SIZEOF_VINT_UNIT - #define MCL_SIZEOF_VINT_UNIT 4 +#ifndef MCL_SIZEOF_UNIT + #if defined(CYBOZU_OS_BIT) && (CYBOZU_OS_BIT == 32) + #define MCL_SIZEOF_UNIT 4 + #else + #define MCL_SIZEOF_UNIT 8 + #endif #endif namespace mcl { namespace vint { -#if MCL_SIZEOF_VINT_UNIT == 8 +#if MCL_SIZEOF_UNIT == 8 typedef uint64_t Unit; -#elif MCL_SIZEOF_VINT_UNIT == 4 -typedef uint32_t Unit; #else - #error "define MCL_SIZEOF_VINT_UNIT" +typedef uint32_t Unit; #endif inline uint64_t make64(uint32_t H, uint32_t L) @@ -47,7 +49,7 @@ inline void split64(uint32_t *H, uint32_t *L, uint64_t x) */ static inline Unit mulUnit(Unit *H, Unit a, Unit b) { -#if MCL_SIZEOF_VINT_UNIT == 4 +#if MCL_SIZEOF_UNIT == 4 uint64_t t = uint64_t(a) * b; uint32_t L; split64(H, &L, t); @@ -71,7 +73,7 @@ static inline Unit mulUnit(Unit *H, Unit a, Unit b) */ static Unit divUnit(Unit *r, Unit H, Unit L, Unit y) { -#if MCL_SIZEOF_VINT_UNIT == 4 +#if MCL_SIZEOF_UNIT == 4 uint64_t t = make64(H, L); uint32_t q = uint32_t(t / y); *r = Unit(t % y); @@ -449,7 +451,7 @@ static inline double GetApp(const T *x, size_t xn, bool up) union di di; di.f = (double)H; unsigned int len = int(di.i >> 52) - 1023 + 1; -#if MCL_SIZEOF_VINT_UNIT == 4 +#if MCL_SIZEOF_UNIT == 4 uint32_t M = x[xn - 2]; if (len >= 21) { di.i |= M >> (len - 21); @@ -1048,7 +1050,7 @@ public: { printf("size_=%d ", (int)size_); for (size_t i = 0; i < size_; i++) { -#if CYBOZU_OS_BIT == 32 +#if MCL_SIZEOF_UNIT == 4 printf("%08x", (uint32_t)buf_[size_ - 1 - i]); #else printf("%016llx", (unsigned long long)buf_[size_ - 1 - i]); @@ -47,7 +47,7 @@ void Op::destroyFpGenerator(FpGenerator *) inline void setUnitAsLE(void *p, Unit x) { -#if CYBOZU_OS_BIT == 32 +#if MCL_SIZEOF_UNIT == 4 cybozu::Set32bitAsLE(p, x); #else cybozu::Set64bitAsLE(p, x); @@ -55,7 +55,7 @@ inline void setUnitAsLE(void *p, Unit x) } inline Unit getUnitAsLE(const void *p) { -#if CYBOZU_OS_BIT == 32 +#if MCL_SIZEOF_UNIT == 4 return cybozu::Get32bitAsLE(p); #else return cybozu::Get64bitAsLE(p); @@ -128,7 +128,7 @@ Mode StrToMode(const std::string& s) void dumpUnit(Unit x) { -#if CYBOZU_OS_BIT == 32 +#if MCL_SIZEOF_UNIT == 4 printf("%08x", (uint32_t)x); #else printf("%016llx", (unsigned long long)x); @@ -136,7 +136,7 @@ void dumpUnit(Unit x) } void UnitToHex(char *buf, size_t maxBufSize, Unit x) { -#if CYBOZU_OS_BIT == 32 +#if MCL_SIZEOF_UNIT == 4 CYBOZU_SNPRINTF(buf, maxBufSize, "%08x", (uint32_t)x); #else CYBOZU_SNPRINTF(buf, maxBufSize, "%016llx ", (unsigned long long)x); @@ -516,7 +516,7 @@ void Op::init(const std::string& mstr, size_t maxBitSize, Mode mode, size_t mclM case 17: setOp<17>(*this, mode); break; // 521 if 32-bit #endif default: - throw cybozu::Exception("Op::init:not:support") << N << mstr; + throw cybozu::Exception("Op:init:not:support") << N << mstr; } #ifdef MCL_USE_LLVM if (primeMode == PM_NICT_P192) { @@ -655,7 +655,7 @@ static bool isInUint64(uint64_t *pv, const fp::Block& b) for (size_t i = start; i < b.n; i++) { if (b.p[i]) return false; } -#if CYBOZU_OS_BIT == 32 +#if MCL_SIZEOF_UNIT == 4 *pv = b.p[0] | (uint64_t(b.p[1]) << 32); #else *pv = b.p[0]; diff --git a/src/fp_generator.hpp b/src/fp_generator.hpp index e7b1ec9..7011f21 100644 --- a/src/fp_generator.hpp +++ b/src/fp_generator.hpp @@ -10,7 +10,7 @@ #define XBYAK_NO_OP_NAMES #include <xbyak/xbyak_util.h> -#if CYBOZU_OS_BIT == 64 +#if MCL_SIZEOF_UNIT == 8 #include <stdio.h> #include <assert.h> #include <cybozu/exception.hpp> diff --git a/src/low_func.hpp b/src/low_func.hpp index a482bf2..232f9eb 100644 --- a/src/low_func.hpp +++ b/src/low_func.hpp @@ -545,7 +545,7 @@ template<size_t N, bool isFullBit, class Tag = Gtag> struct Mont { static inline void func(Unit *z, const Unit *x, const Unit *y, const Unit *p) { -#if MCL_MAX_BIT_SIZE == 1024 || CYBOZU_OS_BIT == 32 // check speed +#if MCL_MAX_BIT_SIZE == 1024 || MCL_SIZEOF_UNIT == 4 // check speed Unit xy[N * 2]; MulPre<N, Tag>::f(xy, x, y); MontRed<N, Tag>::f(z, xy, p); @@ -629,7 +629,7 @@ template<size_t N, bool isFullBit, class Tag = Gtag> struct SqrMont { static inline void func(Unit *y, const Unit *x, const Unit *p) { -#if MCL_MAX_BIT_SIZE == 1024 || CYBOZU_OS_BIT == 32 // check speed +#if MCL_MAX_BIT_SIZE == 1024 || MCL_SIZEOF_UNIT == 4 // check speed Unit xx[N * 2]; SqrPre<N, Tag>::f(xx, x); MontRed<N, Tag>::f(y, xx, p); diff --git a/src/low_func_llvm.hpp b/src/low_func_llvm.hpp index 1eb61cc..104b7fa 100644 --- a/src/low_func_llvm.hpp +++ b/src/low_func_llvm.hpp @@ -4,7 +4,7 @@ namespace mcl { namespace fp { template<> struct EnableKaratsuba<Ltag> { -#if CYBOZU_OS_BIT == 32 +#if MCL_SIZEOF_UNIT == 4 static const size_t minMulN = 10; static const size_t minSqrN = 10; #else @@ -13,7 +13,7 @@ struct EnableKaratsuba<Ltag> { #endif }; -#if CYBOZU_OS_BIT == 32 +#if MCL_SIZEOF_UNIT == 4 #define MCL_GMP_IS_FASTER_THAN_LLVM // QQQ : check later #endif @@ -78,7 +78,7 @@ MCL_DEF_LLVM_FUNC(14) #endif #if MCL_MAX_UNIT_SIZE >= 16 MCL_DEF_LLVM_FUNC(15) -#if CYBOZU_OS_BIT == 32 +#if MCL_SIZEOF_UNIT == 4 MCL_DEF_LLVM_FUNC(16) #else /// QQQ : check speed diff --git a/test/base_test.cpp b/test/base_test.cpp index 2b97f6c..29a39ee 100644 --- a/test/base_test.cpp +++ b/test/base_test.cpp @@ -9,7 +9,7 @@ #include <mcl/fp.hpp> #include "../src/fp_generator.hpp" -#if (CYBOZU_HOST == CYBOZU_HOST_INTEL) && (CYBOZU_OS_BIT == 64) +#if (CYBOZU_HOST == CYBOZU_HOST_INTEL) && (MCL_SIZEOF_UNIT == 8) #define USE_XBYAK static mcl::FpGenerator fg; #endif @@ -214,7 +214,7 @@ const struct FuncOp { { 384, mcl_fp_add384S, mcl_fp_add384L, mcl_fp_sub384S, mcl_fp_sub384L, mcl_fp_mul384pre, mcl_fp_mont384 }, { 448, mcl_fp_add448S, mcl_fp_add448L, mcl_fp_sub448S, mcl_fp_sub448L, mcl_fp_mul448pre, mcl_fp_mont448 }, { 512, mcl_fp_add512S, mcl_fp_add512L, mcl_fp_sub512S, mcl_fp_sub512L, mcl_fp_mul512pre, mcl_fp_mont512 }, -#if CYBOZU_OS_BIT == 32 +#if MCL_SIZEOF_UNIT == 4 { 160, mcl_fp_add160S, mcl_fp_add160L, mcl_fp_sub160S, mcl_fp_sub160L, mcl_fp_mul160pre, mcl_fp_mont160 }, { 224, mcl_fp_add224S, mcl_fp_add224L, mcl_fp_sub224S, mcl_fp_sub224L, mcl_fp_mul224pre, mcl_fp_mont224 }, { 288, mcl_fp_add288S, mcl_fp_add288L, mcl_fp_sub288S, mcl_fp_sub288L, mcl_fp_mul288pre, mcl_fp_mont288 }, diff --git a/test/bn_test.cpp b/test/bn_test.cpp index facba35..2ce7ba0 100644 --- a/test/bn_test.cpp +++ b/test/bn_test.cpp @@ -252,7 +252,7 @@ void testPairing(const G1& P, const G2& Q, const char *eStr) Fp12 e = e1, ea; G1 Pa; G2 Qa; -#if CYBOZU_OS_BIT == 64 +#if MCL_SIZEOF_UNIT == 8 const int count = 1000; #else const int count = 100; diff --git a/test/fp_generator_test.cpp b/test/fp_generator_test.cpp index a5ab762..6edee91 100644 --- a/test/fp_generator_test.cpp +++ b/test/fp_generator_test.cpp @@ -1,5 +1,5 @@ #include <cybozu/test.hpp> -#if CYBOZU_OS_BIT == 32 +#if MCL_SIZEOF_UNIT == 4 // not support #else #include <mcl/gmp_util.hpp> diff --git a/test/fp_test.cpp b/test/fp_test.cpp index 628e0df..8af9af0 100644 --- a/test/fp_test.cpp +++ b/test/fp_test.cpp @@ -910,7 +910,7 @@ CYBOZU_TEST_AUTO(main) CYBOZU_TEST_AUTO(copyUnitToByteAsLE) { using namespace mcl::fp; -#if CYBOZU_OS_BIT == 32 +#if MCL_SIZEOF_UNIT == 4 const Unit src[] = { 0x12345678, 0xaabbccdd, 0xffeeddcc, 0x87654321 }; #else const Unit src[] = { uint64_t(0xaabbccdd12345678ull), uint64_t(0x87654321ffeeddcc) }; diff --git a/test/mont_fp_test.cpp b/test/mont_fp_test.cpp index 0cb8340..5245510 100644 --- a/test/mont_fp_test.cpp +++ b/test/mont_fp_test.cpp @@ -9,7 +9,7 @@ typedef mcl::FpT<ZnTag> Zn; typedef mcl::FpT<> Fp; struct Montgomery { - typedef mcl::gmp::Unit Unit; + typedef mcl::fp::Unit Unit; mpz_class p_; mpz_class R_; // (1 << (pn_ * 64)) % p mpz_class RR_; // (R * R) % p @@ -53,7 +53,11 @@ struct Montgomery { z = x * y; for (size_t i = 0; i < pn_; i++) { Unit q = mcl::gmp::getUnit(z, 0) * rp_; +#ifdef MCL_USE_VINT + z += p_ * q; +#else z += p_ * (mp_limb_t)q; +#endif z >>= sizeof(Unit) * 8; } if (z >= p_) { |