refactor setArray and requires unsigned

2merge^2
MITSUNARI Shigeo 4 years ago
parent 852804f15b
commit a4efdaee14
  1. 15
      include/mcl/fp.hpp
  2. 14
      src/fp.cpp
  3. 4
      test/fp_test.cpp

@ -339,7 +339,6 @@ public:
cybozu::write(pb, os, buf + sizeof(buf) - len, len);
}
/*
mode = Mod : set x mod p if sizeof(S) * n <= 64 else error
set array x as little endian
*/
template<class S>
@ -351,15 +350,11 @@ public:
template<class S>
void setArray(bool *pb, const S *x, size_t n)
{
setArray_(pb, x, n, fp::NoMask);
}
/*
mask x with (1 << bitLen) and subtract p if x >= p
*/
template<class S>
void setArrayMaskMod(const S *x, size_t n)
{
fp::copyAndMask(v_, x, sizeof(S) * n, op_, fp::MaskAndMod);
if (!fp::convertArrayAsLE(v_, op_.N, x, n) || fp::isGreaterOrEqualArray(v_, op_.p, op_.N)) {
*pb = false;
return;
}
*pb = true;
toMont();
}
/*

@ -665,20 +665,6 @@ int detectIoMode(int ioMode, const std::ios_base& ios)
bool copyAndMask(Unit *y, const void *x, size_t xByteSize, const Op& op, MaskMode maskMode)
{
const size_t fpByteSize = sizeof(Unit) * op.N;
if (maskMode == Mod) {
if (xByteSize > fpByteSize * 2) return false;
mpz_class mx;
bool b;
gmp::setArray(&b, mx, (const uint8_t*)x, xByteSize);
if (!b) return false;
#ifdef MCL_USE_VINT
op.modp.modp(mx, mx);
#else
mx %= op.mp;
#endif
mcl::fp::convertArrayAsLE(y, op.N, gmp::getUnit(mx), gmp::getUnitSize(mx));
return true;
}
if (xByteSize > fpByteSize) {
if (maskMode == NoMask) return false;
xByteSize = fpByteSize;

@ -512,11 +512,11 @@ void anotherFpTest(mcl::fp::Mode mode)
void setArrayTest1()
{
char b1[] = { 0x56, 0x34, 0x12 };
uint8_t b1[] = { 0x56, 0x34, 0x12 };
Fp x;
x.setArray(b1, 3);
CYBOZU_TEST_EQUAL(x, 0x123456);
int b2[] = { 0x12, 0x34 };
uint32_t b2[] = { 0x12, 0x34 };
x.setArray(b2, 2);
CYBOZU_TEST_EQUAL(x, Fp("0x3400000012"));
}

Loading…
Cancel
Save