setByCSPRNG uses setArrayMask to be compatible to setHashOf

dev
MITSUNARI Shigeo 6 years ago
parent 4c26c9edfb
commit 73b683a288
  1. 16
      include/mcl/fp.hpp
  2. 15
      src/fp.cpp
  3. 35
      test/fp_util_test.cpp

@ -70,16 +70,6 @@ inline void dumpUnit(Unit x)
bool isEnableJIT(); // 1st call is not threadsafe
void getRandVal(bool *pb, void *p, RandGen& rg, const Unit *in, size_t bitSize);
#ifndef CYBOZU_DONT_USE_EXCEPTION
inline void getRandVal(void *p, RandGen& rg, const Unit *in, size_t bitSize)
{
bool b;
getRandVal(&b, p, rg, in, bitSize);
if (!b) throw cybozu::Exception("getRandVal") << bitSize;
}
#endif
uint32_t sha256(void *out, uint32_t maxOutSize, const void *msg, uint32_t msgSize);
uint32_t sha512(void *out, uint32_t maxOutSize, const void *msg, uint32_t msgSize);
@ -350,9 +340,9 @@ public:
void setByCSPRNG(bool *pb, fp::RandGen rg = fp::RandGen())
{
if (rg.isZero()) rg = fp::RandGen::get();
fp::getRandVal(pb, v_, rg, op_.p, op_.bitSize);
if (!*pb) return;
toMont();
rg.read(pb, v_, op_.N * sizeof(Unit)); // byte size
if (!pb) return;
setArrayMask(v_, op_.N);
}
#ifndef CYBOZU_DONT_USE_EXCEPTION
void setByCSPRNG(fp::RandGen rg = fp::RandGen())

@ -120,21 +120,6 @@ bool isEnableJIT()
#endif
}
void getRandVal(bool *pb, void *p, RandGen& rg, const Unit *in, size_t bitSize)
{
if (rg.isZero()) rg = RandGen::get();
Unit *out = reinterpret_cast<Unit*>(p);
const size_t n = (bitSize + UnitBitSize - 1) / UnitBitSize;
const size_t rem = bitSize & (UnitBitSize - 1);
assert(n > 0);
for (;;) {
rg.read(pb, out, n * sizeof(Unit)); // byte size
if (!*pb) return;
if (rem > 0) out[n - 1] &= (Unit(1) << rem) - 1;
if (isLessArray(out, in, n)) return;
}
}
uint32_t sha256(void *out, uint32_t maxOutSize, const void *msg, uint32_t msgSize)
{
const uint32_t hashSize = 256 / 8;

@ -195,41 +195,6 @@ struct Rand {
}
};
CYBOZU_TEST_AUTO(getRandVal)
{
const size_t rn = 8;
const struct {
uint32_t r[rn];
uint32_t mod[2];
size_t bitSize;
uint32_t expect[2];
} tbl[] = {
{ { 1, 2, 3, 4, 5, 6, 7, 8 }, { 5, 6 }, 64, { 1, 2 } },
{ { 0xfffffffc, 0x7, 3, 4, 5, 6, 7, 8 }, { 0xfffffffe, 0x3 }, 34, { 0xfffffffc, 0x3 } },
{ { 0xfffffffc, 0x7, 3, 4, 5, 6, 7, 8 }, { 0xfffffffb, 0x3 }, 34, { 3, 0 } },
{ { 2, 3, 5, 7, 4, 3, 0, 3 }, { 1, 0x3 }, 34, { 0, 3 } },
};
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
Rand rg(tbl[i].r, rn);
#if CYBOZU_OS_BIT == 64
uint64_t out[1];
#else
uint32_t out[2];
#endif
mcl::fp::RandGen wrg(rg);
#if CYBOZU_OS_BIT == 64
uint64_t mod = tbl[i].mod[0] | (uint64_t(tbl[i].mod[1]) << 32);
mcl::fp::getRandVal(out, wrg, &mod, tbl[i].bitSize);
uint64_t expect = tbl[i].expect[0] | (uint64_t(tbl[i].expect[1]) << 32);
CYBOZU_TEST_EQUAL(out[0], expect);
#else
mcl::fp::getRandVal(out, wrg, tbl[i].mod, tbl[i].bitSize);
CYBOZU_TEST_EQUAL(out[0], tbl[i].expect[0]);
CYBOZU_TEST_EQUAL(out[1], tbl[i].expect[1]);
#endif
}
}
CYBOZU_TEST_AUTO(maskArray)
{
#if 1

Loading…
Cancel
Save