From 73b683a288ce52d6cba2254f408d6c3688cd26e9 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Fri, 23 Nov 2018 17:50:54 +0900 Subject: [PATCH] setByCSPRNG uses setArrayMask to be compatible to setHashOf --- include/mcl/fp.hpp | 16 +++------------- src/fp.cpp | 15 --------------- test/fp_util_test.cpp | 35 ----------------------------------- 3 files changed, 3 insertions(+), 63 deletions(-) diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index eb70d50..a8e9ac8 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -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()) diff --git a/src/fp.cpp b/src/fp.cpp index 7296353..75f2932 100644 --- a/src/fp.cpp +++ b/src/fp.cpp @@ -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(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; diff --git a/test/fp_util_test.cpp b/test/fp_util_test.cpp index b6d5efc..e8a9f9a 100644 --- a/test/fp_util_test.cpp +++ b/test/fp_util_test.cpp @@ -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