add getUint64

dev
MITSUNARI Shigeo 9 years ago
parent 5a1e0f32a4
commit 714f979a1e
  1. 27
      include/mcl/fp.hpp
  2. 30
      test/fp_test.cpp

@ -299,6 +299,33 @@ public:
{
return fp::compareArray(v_, op_.p, op_.N) < 0;
}
uint64_t getUint64(bool *pb = 0) const
{
fp::Block b;
getBlock(b);
#if CYBOZU_OS_BIT == 32
const size_t start = 2;
#else
const size_t start = 1;
#endif
for (size_t i = start; i < b.n; i++) {
if (b.p[i]) {
if (pb) {
*pb = false;
return 0;
}
throw cybozu::Exception("fpT::getUint64:large value") << *this;
}
}
if (pb) {
*pb = true;
}
#if CYBOZU_OS_BIT == 32
return b.p[0] | (uint64_t(b.p[1]) << 32);
#else
return b.p[0];
#endif
}
static inline size_t getModBitLen() { return op_.bitSize; }
bool operator==(const FpT& rhs) const { return fp::isEqualArray(v_, rhs.v_, op_.N); }
bool operator!=(const FpT& rhs) const { return !operator==(rhs); }

@ -377,6 +377,36 @@ CYBOZU_TEST_AUTO(set64bit)
}
}
CYBOZU_TEST_AUTO(getUint64)
{
Fp::setModulo("0x1000000000000000000f");
const uint64_t tbl[] = {
0, 1, 123, 0xffffffff, int64_t(0x7fffffffffffffffull)
};
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
uint64_t a = tbl[i];
Fp x(a);
uint64_t b = x.getUint64();
CYBOZU_TEST_EQUAL(a, b);
}
{
Fp x("0xffffffffffffffff");
CYBOZU_TEST_EQUAL(x.getUint64(), uint64_t(0xffffffffffffffffull));
}
{
Fp x("0x10000000000000000");
CYBOZU_TEST_EXCEPTION(x.getUint64(), cybozu::Exception);
x = -1;
CYBOZU_TEST_EXCEPTION(x.getUint64(), cybozu::Exception);
}
{
Fp x("0x10000000000000000");
bool b = true;
CYBOZU_TEST_EQUAL(x.getUint64(&b), 0);
CYBOZU_TEST_ASSERT(!b);
}
}
CYBOZU_TEST_AUTO(getArray)
{
const struct {

Loading…
Cancel
Save