diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index c4cf3c2..72534f5 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -35,7 +35,7 @@ namespace mcl { namespace fp { -void setOp(mcl::fp::Op& op, const Unit* p, size_t pBitLen); +void setOp(mcl::fp::Op& op, const Unit* p, size_t bitLen); struct Block { @@ -55,8 +55,6 @@ class FpT { static const size_t UnitByteN = sizeof(Unit); static const size_t maxUnitN = (maxBitN + UnitByteN * 8 - 1) / (UnitByteN * 8); static fp::Op op_; - static mcl::SquareRoot sq_; - static size_t pBitLen_; template friend class FpT; Unit v_[maxUnitN]; public: @@ -78,53 +76,54 @@ public: mpz_class mp; inFromStr(mp, &isMinus, mstr, base); if (isMinus) throw cybozu::Exception("mcl:FpT:setModulo:mstr is not minus") << mstr; - pBitLen_ = Gmp::getBitLen(mp); - if (pBitLen_ > maxBitN) throw cybozu::Exception("mcl:FpT:setModulo:too large bitLen") << pBitLen_ << maxBitN; + const size_t bitLen = Gmp::getBitLen(mp); + if (bitLen > maxBitN) throw cybozu::Exception("mcl:FpT:setModulo:too large bitLen") << bitLen << maxBitN; Unit p[maxUnitN] = {}; const size_t n = Gmp::getRaw(p, maxUnitN, mp); if (n == 0) throw cybozu::Exception("mcl:FpT:setModulo:bad mstr") << mstr; - mcl::fp::setOp(op_, p, pBitLen_); + mcl::fp::setOp(op_, p, bitLen); #if 1 #ifdef USE_MONT_FP - if (pBitLen_ <= 128) { op_ = fp::MontFp::init(p); } + if (bitLen <= 128) { op_ = fp::MontFp::init(p); } #if CYBOZU_OS_BIT == 32 - else if (pBitLen_ <= 160) { static fp::MontFp f; op_ = f.init(p); } + else if (bitLen <= 160) { static fp::MontFp f; op_ = f.init(p); } #endif - else if (pBitLen_ <= 192) { static fp::MontFp f; op_ = f.init(p); } + else if (bitLen <= 192) { static fp::MontFp f; op_ = f.init(p); } #if CYBOZU_OS_BIT == 32 - else if (pBitLen_ <= 224) { static fp::MontFp f; op_ = f.init(p); } + else if (bitLen <= 224) { static fp::MontFp f; op_ = f.init(p); } #endif - else if (pBitLen_ <= 256) { static fp::MontFp f; op_ = f.init(p); } - else if (pBitLen_ <= 384) { static fp::MontFp f; op_ = f.init(p); } - else if (pBitLen_ <= 448) { static fp::MontFp f; op_ = f.init(p); } + else if (bitLen <= 256) { static fp::MontFp f; op_ = f.init(p); } + else if (bitLen <= 384) { static fp::MontFp f; op_ = f.init(p); } + else if (bitLen <= 448) { static fp::MontFp f; op_ = f.init(p); } #if CYBOZU_OS_BIT == 32 - else if (pBitLen_ <= 544) { static fp::MontFp f; op_ = f.init(p); } + else if (bitLen <= 544) { static fp::MontFp f; op_ = f.init(p); } #else - else if (pBitLen_ <= 576) { static fp::MontFp f; op_ = f.init(p); } + else if (bitLen <= 576) { static fp::MontFp f; op_ = f.init(p); } #endif else { static fp::MontFp f; op_ = f.init(p); } #else - if (pBitLen_ <= 128) { op_ = fp::FixedFp::init(p); } + if (bitLen <= 128) { op_ = fp::FixedFp::init(p); } #if CYBOZU_OS_BIT == 32 - else if (pBitLen_ <= 160) { static fp::FixedFp f; op_ = f.init(p); } + else if (bitLen <= 160) { static fp::FixedFp f; op_ = f.init(p); } #endif - else if (pBitLen_ <= 192) { static fp::FixedFp f; op_ = f.init(p); } + else if (bitLen <= 192) { static fp::FixedFp f; op_ = f.init(p); } #if CYBOZU_OS_BIT == 32 - else if (pBitLen_ <= 224) { static fp::FixedFp f; op_ = f.init(p); } + else if (bitLen <= 224) { static fp::FixedFp f; op_ = f.init(p); } #endif - else if (pBitLen_ <= 256) { static fp::FixedFp f; op_ = f.init(p); } - else if (pBitLen_ <= 384) { static fp::FixedFp f; op_ = f.init(p); } - else if (pBitLen_ <= 448) { static fp::FixedFp f; op_ = f.init(p); } + else if (bitLen <= 256) { static fp::FixedFp f; op_ = f.init(p); } + else if (bitLen <= 384) { static fp::FixedFp f; op_ = f.init(p); } + else if (bitLen <= 448) { static fp::FixedFp f; op_ = f.init(p); } #if CYBOZU_OS_BIT == 32 - else if (pBitLen_ <= 544) { static fp::FixedFp f; op_ = f.init(p); } + else if (bitLen <= 544) { static fp::FixedFp f; op_ = f.init(p); } #else - else if (pBitLen_ <= 576) { static fp::FixedFp f; op_ = f.init(p); } + else if (bitLen <= 576) { static fp::FixedFp f; op_ = f.init(p); } #endif else { static fp::FixedFp f; op_ = f.init(p); } #endif assert(op_.N <= maxUnitN); #endif - sq_.set(mp); + op_.bitLen = bitLen; + op_.sq.set(mp); } static inline void getModulo(std::string& pstr) { @@ -140,7 +139,7 @@ public: { mpz_class mx, my; x.toGmp(mx); - bool b = sq_.get(my, mx); + bool b = op_.sq.get(my, mx); if (!b) return false; y.fromGmp(my); return true; @@ -242,7 +241,7 @@ public: template void setRand(RG& rg) { - fp::getRandVal(v_, rg, op_.p, pBitLen_); + fp::getRandVal(v_, rg, op_.p, op_.bitLen); fromMont(*this, *this); } static inline void toStr(std::string& str, const Unit *x, size_t n, int base = 10, bool withPrefix = false) @@ -352,7 +351,7 @@ public: { fp::Block b; getBlock(b); - bv.append(b.p, pBitLen_); + bv.append(b.p, op_.bitLen); } bool isValid() const { @@ -360,11 +359,11 @@ public: } void fromBitVec(const cybozu::BitVector& bv) { - if (bv.size() != pBitLen_) throw cybozu::Exception("FpT:fromBitVec:bad size") << bv.size() << pBitLen_; + if (bv.size() != op_.bitLen) throw cybozu::Exception("FpT:fromBitVec:bad size") << bv.size() << op_.bitLen; setRaw(bv.getBlock(), bv.getBlockSize()); } - static inline size_t getModBitLen() { return pBitLen_; } - static inline size_t getBitVecSize() { return pBitLen_; } + static inline size_t getModBitLen() { return op_.bitLen; } + static inline size_t getBitVecSize() { return op_.bitLen; } bool operator==(const FpT& rhs) const { return fp::local::isEqualArray(v_, rhs.v_, op_.N); } bool operator!=(const FpT& rhs) const { return !operator==(rhs); } inline friend FpT operator+(const FpT& x, const FpT& y) { FpT z; add(z, x, y); return z; } @@ -422,8 +421,6 @@ private: }; template fp::Op FpT::op_; -template mcl::SquareRoot FpT::sq_; -template size_t FpT::pBitLen_; namespace power_impl { diff --git a/include/mcl/fp_base.hpp b/include/mcl/fp_base.hpp index 0fb174f..10fed99 100644 --- a/include/mcl/fp_base.hpp +++ b/include/mcl/fp_base.hpp @@ -136,6 +136,7 @@ struct Op { mpz_class mp; const Unit* p; size_t N; + size_t bitLen; bool (*isZero)(const Unit*); void1op clear; void2op neg; @@ -148,6 +149,7 @@ struct Op { // for Montgomery void2op toMont; void2op fromMont; + mcl::SquareRoot sq; Op() : p(0), N(0), isZero(0), clear(0), neg(0), inv(0) , square(0), copy(0),add(0), sub(0), mul(0), toMont(0), fromMont(0)