move Block to fp

dev
MITSUNARI Shigeo 10 years ago
parent e10752cef8
commit b3619c63b0
  1. 23
      include/mcl/fp.hpp

@ -37,7 +37,6 @@ 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 pBitLen);
} // mcl::fp
struct Block { struct Block {
typedef fp::Unit Unit; typedef fp::Unit Unit;
@ -48,6 +47,8 @@ struct Block {
Unit v_[maxUnitN]; Unit v_[maxUnitN];
}; };
} // mcl::fp
template<class tag = fp::TagDefault, size_t maxBitN = MCL_FP_BLOCK_MAX_BIT_N> template<class tag = fp::TagDefault, size_t maxBitN = MCL_FP_BLOCK_MAX_BIT_N>
class FpT { class FpT {
typedef fp::Unit Unit; typedef fp::Unit Unit;
@ -131,7 +132,7 @@ public:
} }
static inline bool isYodd(const FpT& x) static inline bool isYodd(const FpT& x)
{ {
Block b; fp::Block b;
x.getBlock(b); x.getBlock(b);
return (b.p[0] & 1) == 1; return (b.p[0] & 1) == 1;
} }
@ -220,16 +221,16 @@ public:
const size_t fpByteN = sizeof(Unit) * op_.N; const size_t fpByteN = sizeof(Unit) * op_.N;
if (byteN < fpByteN) throw cybozu::Exception("getRaw:bad n") << n << fpByteN; if (byteN < fpByteN) throw cybozu::Exception("getRaw:bad n") << n << fpByteN;
assert(byteN >= fpByteN); assert(byteN >= fpByteN);
Block b; fp::Block b;
getBlock(b); getBlock(b);
memcpy(outBuf, b.p, fpByteN); memcpy(outBuf, b.p, fpByteN);
const size_t writeN = (fpByteN + sizeof(S) - 1) / sizeof(S); const size_t writeN = (fpByteN + sizeof(S) - 1) / sizeof(S);
memset((char *)outBuf + fpByteN, 0, writeN * sizeof(S) - fpByteN); memset((char *)outBuf + fpByteN, 0, writeN * sizeof(S) - fpByteN);
return writeN; return writeN;
} }
void getBlock(Block& b) const void getBlock(fp::Block& b) const
{ {
assert(maxUnitN <= Block::maxUnitN); assert(maxUnitN <= fp::Block::maxUnitN);
b.n = op_.N; b.n = op_.N;
if (op_.fromMont) { if (op_.fromMont) {
op_.fromMont(b.v_, v_); op_.fromMont(b.v_, v_);
@ -266,7 +267,7 @@ public:
} }
void toStr(std::string& str, int base = 10, bool withPrefix = false) const void toStr(std::string& str, int base = 10, bool withPrefix = false) const
{ {
Block b; fp::Block b;
getBlock(b); getBlock(b);
toStr(str, b.p, b.n, base, withPrefix); toStr(str, b.p, b.n, base, withPrefix);
} }
@ -278,7 +279,7 @@ public:
} }
void toGmp(mpz_class& x) const void toGmp(mpz_class& x) const
{ {
Block b; fp::Block b;
getBlock(b); getBlock(b);
Gmp::setRaw(x, b.p, b.n); Gmp::setRaw(x, b.p, b.n);
} }
@ -328,7 +329,7 @@ public:
template<class tag2, size_t maxBitN2> template<class tag2, size_t maxBitN2>
static inline void power(FpT& z, const FpT& x, const FpT<tag2, maxBitN2>& y) static inline void power(FpT& z, const FpT& x, const FpT<tag2, maxBitN2>& y)
{ {
Block b; fp::Block b;
y.getBlock(b); y.getBlock(b);
powerArray(z, x, b.p, b.n); powerArray(z, x, b.p, b.n);
} }
@ -349,7 +350,7 @@ public:
*/ */
void appendToBitVec(cybozu::BitVector& bv) const void appendToBitVec(cybozu::BitVector& bv) const
{ {
Block b; fp::Block b;
getBlock(b); getBlock(b);
bv.append(b.p, pBitLen_); bv.append(b.p, pBitLen_);
} }
@ -405,7 +406,7 @@ public:
bool operator<(const FpT&) const { return false; } bool operator<(const FpT&) const { return false; }
static inline int compare(const FpT& x, const FpT& y) static inline int compare(const FpT& x, const FpT& y)
{ {
Block xb, yb; fp::Block xb, yb;
x.getBlock(xb); x.getBlock(xb);
y.getBlock(yb); y.getBlock(yb);
return fp::local::compareArray(xb.p, yb.p, xb.n); return fp::local::compareArray(xb.p, yb.p, xb.n);
@ -429,7 +430,7 @@ namespace power_impl {
template<class G, class tag, size_t bitN, template<class _tag, size_t _bitN>class FpT> template<class G, class tag, size_t bitN, template<class _tag, size_t _bitN>class FpT>
void power(G& z, const G& x, const FpT<tag, bitN>& y) void power(G& z, const G& x, const FpT<tag, bitN>& y)
{ {
Block b; fp::Block b;
y.getBlock(b); y.getBlock(b);
mcl::power_impl::powerArray(z, x, b.p, b.n); mcl::power_impl::powerArray(z, x, b.p, b.n);
} }

Loading…
Cancel
Save