refactoring setArray

2merge^2
MITSUNARI Shigeo 4 years ago
parent 8ff04a209a
commit dd0b16ca3f
  1. 13
      include/mcl/fp.hpp
  2. 2
      include/mcl/impl/bn_c_impl.hpp
  3. 15
      src/fp.cpp
  4. 15
      test/fp_test.cpp

@ -343,11 +343,16 @@ public:
set array x as little endian
*/
template<class S>
void setArray(bool *pb, const S *x, size_t n, mcl::fp::MaskMode mode = fp::NoMask)
void setArray_(bool *pb, const S *x, size_t n, mcl::fp::MaskMode mode = fp::NoMask)
{
*pb = fp::copyAndMask(v_, x, sizeof(S) * n, op_, mode);
toMont();
}
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
*/
@ -364,7 +369,7 @@ public:
template<class S>
void setArrayMod(bool *pb, const S *x, size_t n)
{
setArray(pb, x, n, fp::Mod);
setArray_(pb, x, n, fp::Mod);
}
/*
@ -415,7 +420,7 @@ public:
*/
void setLittleEndianMod(bool *pb, const void *buf, size_t bufSize)
{
setArray(pb, (const uint8_t *)buf, bufSize, mcl::fp::Mod);
setArrayMod(pb, (const uint8_t *)buf, bufSize);
}
/*
set (big endian % p)
@ -432,7 +437,7 @@ public:
for (size_t i = 0; i < bufSize; i++) {
swapBuf[bufSize - 1 - i] = p[i];
}
setArray(pb, swapBuf, bufSize, mcl::fp::Mod);
setArray_(pb, swapBuf, bufSize, mcl::fp::Mod);
}
void setByCSPRNG(bool *pb, fp::RandGen rg = fp::RandGen())
{

@ -176,7 +176,7 @@ mclSize mclBnFr_getLittleEndian(void *buf, mclSize maxBufSize, const mclBnFr *x)
int mclBnFr_setLittleEndianMod(mclBnFr *x, const void *buf, mclSize bufSize)
{
bool b;
cast(x)->setArray(&b, (const char *)buf, bufSize, mcl::fp::Mod);
cast(x)->setArrayMod(&b, (const char *)buf, bufSize);
return b ? 0 : -1;
}
mclSize mclBnFr_deserialize(mclBnFr *x, const void *buf, mclSize bufSize)

@ -676,25 +676,24 @@ bool copyAndMask(Unit *y, const void *x, size_t xByteSize, const Op& op, MaskMod
#else
mx %= op.mp;
#endif
const Unit *pmx = gmp::getUnit(mx);
size_t i = 0;
for (const size_t n = gmp::getUnitSize(mx); i < n; i++) {
y[i] = pmx[i];
}
for (; i < op.N; i++) {
y[i] = 0;
}
mcl::fp::convertArrayAsLE(y, op.N, gmp::getUnit(mx), gmp::getUnitSize(mx));
return true;
}
if (xByteSize > fpByteSize) {
if (maskMode == NoMask) return false;
xByteSize = fpByteSize;
}
#if 0
if (!mcl::fp::convertArrayAsLE(y, op.N, x, xByteSize)) {
return false;
}
#else
// QQQ : fixed later for big endian
copyByteToUnitAsLE(y, (const uint8_t*)x, xByteSize);
for (size_t i = (xByteSize + sizeof(Unit) - 1) / sizeof(Unit); i < op.N; i++) {
y[i] = 0;
}
#endif
if (maskMode == mcl::fp::SmallMask || maskMode == mcl::fp::MaskAndMod) {
maskArray(y, op.N, op.bitSize);
}

@ -609,7 +609,7 @@ void setArrayModTest()
const size_t fpByteSize = unitByteSize * Fp::getOp().N;
Fp y;
bool b;
y.setArray(&b, px, xn, mcl::fp::Mod);
y.setArrayMod(&b, px, xn);
bool expected = xByteSize <= fpByteSize * 2;
CYBOZU_TEST_EQUAL(b, expected);
if (!b) continue;
@ -1041,7 +1041,7 @@ CYBOZU_TEST_AUTO(main)
#endif
}
CYBOZU_TEST_AUTO(copyByteToUnitAsLE)
CYBOZU_TEST_AUTO(convertArrayAsLE)
{
using namespace mcl::fp;
#if MCL_SIZEOF_UNIT == 4
@ -1050,18 +1050,19 @@ CYBOZU_TEST_AUTO(copyByteToUnitAsLE)
const Unit src[] = { uint64_t(0xaabbccdd12345678ull), uint64_t(0x87654321ffeeddcc) };
#endif
const uint8_t ok[] = { 0x78, 0x56, 0x34, 0x12, 0xdd, 0xcc, 0xbb, 0xaa, 0xcc, 0xdd, 0xee, 0xff, 0x21, 0x43, 0x65, 0x87 };
mcl::fp::Unit dst[2];
const size_t dstN = 2;
mcl::fp::Unit dst[dstN];
for (size_t i = 1; i <= sizeof(dst); i++) {
memset(dst, 0xff, sizeof(dst));
mcl::fp::copyByteToUnitAsLE(dst, ok, i);
mcl::fp::convertArrayAsLE(dst, dstN, ok, i);
if (i < sizeof(Unit)) {
CYBOZU_TEST_EQUAL(src[0] & ((uint64_t(1) << (i * 8)) - 1), dst[0]);
CYBOZU_TEST_EQUAL(dst[1], Unit(-1));
CYBOZU_TEST_EQUAL(dst[1], 0);
continue;
}
CYBOZU_TEST_EQUAL(dst[0], src[0]);
if (i == sizeof(Unit)) {
CYBOZU_TEST_EQUAL(dst[1], Unit(-1));
CYBOZU_TEST_EQUAL(dst[1], 0);
continue;
}
if (i < sizeof(dst)) {
@ -1071,7 +1072,7 @@ CYBOZU_TEST_AUTO(copyByteToUnitAsLE)
CYBOZU_TEST_EQUAL(src[1], dst[1]);
}
dst[0] = 1;
copyByteToUnitAsLE(dst, ok, 0);
mcl::fp::convertArrayAsLE(dst, 0, ok, 1);
CYBOZU_TEST_EQUAL(dst[0], 1u);
}

Loading…
Cancel
Save