under refactoring

dev
MITSUNARI Shigeo 10 years ago
parent 151d9bd5a1
commit a0f4c41f03
  1. 18
      include/mcl/fp.hpp
  2. 2
      include/mcl/op.hpp
  3. 88
      src/fp.cpp

@ -23,7 +23,7 @@
#include <cybozu/bit_operation.hpp>
#include <mcl/op.hpp>
#include <mcl/util.hpp>
#include <mcl/power.hpp>
#include <mcl/power.hpp> // QQQ : removed laster
namespace mcl {
@ -60,22 +60,13 @@ public:
static inline void setModulo(const std::string& mstr, int base = 0)
{
assert(maxBitN <= MCL_MAX_OP_BIT_N);
bool isMinus;
fp::strToGmp(op_.mp, &isMinus, mstr, base);
if (isMinus) throw cybozu::Exception("mcl:FpT:setModulo:mstr is not minus") << mstr;
const size_t bitLen = Gmp::getBitLen(op_.mp);
if (bitLen > maxBitN) throw cybozu::Exception("mcl:FpT:setModulo:too large bitLen") << bitLen << maxBitN;
const size_t n = Gmp::getRaw(op_.p, maxN, op_.mp);
if (n == 0) throw cybozu::Exception("mcl:FpT:setModulo:bad mstr") << mstr;
// default
assert(sizeof(mp_limb_t) == sizeof(Unit));
// set default wrapper function
op_.neg = negW;
op_.add = addW;
op_.sub = subW;
op_.mul = mulW;
const Unit *p = op_.p;
op_.bitLen = bitLen;
op_.init(p, bitLen);
op_.sq.set(op_.mp);
op_.init(mstr, base, maxBitN);
}
static inline void getModulo(std::string& pstr)
{
@ -322,7 +313,6 @@ public:
/*
QQQ : should be removed
*/
bool operator<(const FpT&) const { return false; }
static inline int compare(const FpT& x, const FpT& y)
{
fp::Block xb, yb;

@ -95,7 +95,7 @@ struct Op {
{
mul(y, x, RR);
}
void init(const Unit *p, size_t bitLen);
void init(const std::string& mstr, int base, size_t maxBitN);
static FpGenerator* createFpGenerator();
static void destroyFpGenerator(FpGenerator *fg);
private:

@ -27,6 +27,44 @@ void Op::destroyFpGenerator(FpGenerator *)
}
#endif
inline const char *verifyStr(bool *isMinus, int *base, const std::string& str)
{
const char *p = str.c_str();
if (*p == '-') {
*isMinus = true;
p++;
} else {
*isMinus = false;
}
if (p[0] == '0') {
if (p[1] == 'x') {
if (*base != 0 && *base != 16) {
throw cybozu::Exception("fp:verifyStr:bad base") << *base << str;
}
*base = 16;
p += 2;
} else if (p[1] == 'b') {
if (*base != 0 && *base != 2) {
throw cybozu::Exception("fp:verifyStr:bad base") << *base << str;
}
*base = 2;
p += 2;
}
}
if (*base == 0) *base = 10;
if (*p == '\0') throw cybozu::Exception("fp:verifyStr:str is empty");
return p;
}
void strToGmp(mpz_class& x, bool *isMinus, const std::string& str, int base)
{
const char *p = fp::verifyStr(isMinus, &base, str);
if (!Gmp::fromStr(x, p, base)) {
throw cybozu::Exception("fp:FpT:inFromStr") << str;
}
}
template<size_t bitN>
struct OpeFunc {
static const size_t N = (bitN + UnitBitN - 1) / UnitBitN;
@ -204,10 +242,16 @@ static void initForMont(Op& op, const Unit *p)
#endif
void Op::init(const Unit* p, size_t bitLen)
void Op::init(const std::string& mstr, int base, size_t maxBitN)
{
assert(sizeof(mp_limb_t) == sizeof(Unit));
const size_t UnitBitN = sizeof(Unit) * 8;
static const size_t maxN = (maxBitN + UnitBitN - 1) / UnitBitN;
bool isMinus;
strToGmp(mp, &isMinus, mstr, base);
if (isMinus) throw cybozu::Exception("Op:init:mstr is minus") << mstr;
bitLen = Gmp::getBitLen(mp);
if (bitLen > maxBitN) throw cybozu::Exception("Op:init:too large bitLen") << mstr << bitLen << maxBitN;
const size_t n = Gmp::getRaw(p, maxN, mp);
if (n == 0) throw cybozu::Exception("Op:init:bad mstr") << mstr;
if (bitLen <= 128) {
SET_OP(128)
@ -249,6 +293,7 @@ void Op::init(const Unit* p, size_t bitLen)
#ifdef USE_MONT_FP
fp::initForMont(*this, p);
#endif
sq.set(mp);
}
void arrayToStr(std::string& str, const Unit *x, size_t n, int base, bool withPrefix)
@ -272,42 +317,5 @@ void arrayToStr(std::string& str, const Unit *x, size_t n, int base, bool withPr
}
}
inline const char *verifyStr(bool *isMinus, int *base, const std::string& str)
{
const char *p = str.c_str();
if (*p == '-') {
*isMinus = true;
p++;
} else {
*isMinus = false;
}
if (p[0] == '0') {
if (p[1] == 'x') {
if (*base != 0 && *base != 16) {
throw cybozu::Exception("fp:verifyStr:bad base") << *base << str;
}
*base = 16;
p += 2;
} else if (p[1] == 'b') {
if (*base != 0 && *base != 2) {
throw cybozu::Exception("fp:verifyStr:bad base") << *base << str;
}
*base = 2;
p += 2;
}
}
if (*base == 0) *base = 10;
if (*p == '\0') throw cybozu::Exception("fp:verifyStr:str is empty");
return p;
}
void strToGmp(mpz_class& x, bool *isMinus, const std::string& str, int base)
{
const char *p = fp::verifyStr(isMinus, &base, str);
if (!Gmp::fromStr(x, p, base)) {
throw cybozu::Exception("fp:FpT:inFromStr") << str;
}
}
} } // mcl::fp

Loading…
Cancel
Save