refactor setArrayMask and requires unsigned

2merge^2
MITSUNARI Shigeo 4 years ago
parent a4efdaee14
commit 04b13fb039
  1. 48
      include/mcl/fp.hpp
  2. 6
      include/mcl/impl/bn_c_impl.hpp
  3. 6
      test/fp_test.cpp

@ -339,24 +339,44 @@ public:
cybozu::write(pb, os, buf + sizeof(buf) - len, len);
}
/*
set array x as little endian
treat x as little endian
if x >= p then error
*/
template<class S>
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)
{
if (!fp::convertArrayAsLE(v_, op_.N, x, n) || fp::isGreaterOrEqualArray(v_, op_.p, op_.N)) {
if (!fp::convertArrayAsLE(v_, op_.N, x, n)) {
*pb = false;
return;
}
if (fp::isGreaterOrEqualArray(v_, op_.p, op_.N)) {
*pb = false;
return;
}
*pb = true;
toMont();
}
/*
treat x as little endian
x &= (1 << bitLen) = 1
x &= (1 << (bitLen - 1)) - 1 if x >= p
*/
template<class S>
void setArrayMask(const S *x, size_t n)
{
const size_t dstByte = sizeof(fp::Unit) * op_.N;
if (sizeof(S) * n > dstByte) {
n = dstByte / sizeof(S);
}
bool b = fp::convertArrayAsLE(v_, op_.N, x, n);
assert(b);
(void)b;
fp::maskArray(v_, op_.N, op_.bitSize);
if (fp::isGreaterOrEqualArray(v_, op_.p, op_.N)) {
fp::maskArray(v_, op_.N, op_.bitSize - 1);
}
toMont();
}
/*
set (x as little endian) % p
error if size of x >= sizeof(Fp) * 2
@ -380,16 +400,6 @@ public:
if (!*pb) return;
toMont();
}
/*
mask x with (1 << (bitLen - 1)) - 1 if x >= p
*/
template<class S>
void setArrayMask(const S *x, size_t n)
{
fp::copyAndMask(v_, x, sizeof(S) * n, op_, fp::SmallMask);
toMont();
}
void getBlock(fp::Block& b) const
{
b.n = op_.N;
@ -488,7 +498,7 @@ public:
*/
void setHashOf(const void *msg, size_t msgSize)
{
char buf[MCL_MAX_HASH_BIT_SIZE / 8];
uint8_t buf[MCL_MAX_HASH_BIT_SIZE / 8];
uint32_t size = op_.hash(buf, static_cast<uint32_t>(sizeof(buf)), msg, static_cast<uint32_t>(msgSize));
setArrayMask(buf, size);
}

@ -159,7 +159,7 @@ int mclBnFr_setStr(mclBnFr *x, const char *buf, mclSize bufSize, int ioMode)
}
int mclBnFr_setLittleEndian(mclBnFr *x, const void *buf, mclSize bufSize)
{
cast(x)->setArrayMask((const char *)buf, bufSize);
cast(x)->setArrayMask((const uint8_t *)buf, bufSize);
return 0;
}
int mclBnFr_setBigEndianMod(mclBnFr *x, const void *buf, mclSize bufSize)
@ -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)->setArrayMod(&b, (const char *)buf, bufSize);
cast(x)->setArrayMod(&b, (const uint8_t *)buf, bufSize);
return b ? 0 : -1;
}
mclSize mclBnFr_deserialize(mclBnFr *x, const void *buf, mclSize bufSize)
@ -729,7 +729,7 @@ void mclBnFp_clear(mclBnFp *x)
int mclBnFp_setLittleEndian(mclBnFp *x, const void *buf, mclSize bufSize)
{
cast(x)->setArrayMask((const char *)buf, bufSize);
cast(x)->setArrayMask((const uint8_t *)buf, bufSize);
return 0;
}

@ -547,11 +547,11 @@ void setArrayTest2(mcl::fp::Mode mode)
void setArrayMaskTest1()
{
char b1[] = { 0x56, 0x34, 0x12 };
uint8_t b1[] = { 0x56, 0x34, 0x12 };
Fp x;
x.setArrayMask(b1, 3);
CYBOZU_TEST_EQUAL(x, 0x123456);
int b2[] = { 0x12, 0x34 };
uint32_t b2[] = { 0x12, 0x34 };
x.setArrayMask(b2, 2);
CYBOZU_TEST_EQUAL(x, Fp("0x3400000012"));
}
@ -811,7 +811,7 @@ void setHashOfTest()
digest = cybozu::Sha512().digest(msgTbl[i]);
}
Fp x, y;
x.setArrayMask(digest.c_str(), digest.size());
x.setArrayMask((const uint8_t*)digest.c_str(), digest.size());
y.setHashOf(msgTbl[i]);
CYBOZU_TEST_EQUAL(x, y);
}

Loading…
Cancel
Save