aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2018-02-05 16:44:45 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2018-02-05 16:44:45 +0800
commit241563308358cf8df7b1c91b661cae329d7aacfe (patch)
treeec9d47d6cb9db4346abcc5b478d7d8791e010155
parentafce65fbba2f895fdb5668d8c4bbb4f050ad4c99 (diff)
downloadtangerine-mcl-241563308358cf8df7b1c91b661cae329d7aacfe.tar.gz
tangerine-mcl-241563308358cf8df7b1c91b661cae329d7aacfe.tar.zst
tangerine-mcl-241563308358cf8df7b1c91b661cae329d7aacfe.zip
new RandGen
-rw-r--r--include/mcl/ahe.hpp5
-rw-r--r--include/mcl/elgamal.hpp13
-rw-r--r--include/mcl/fp.hpp14
-rw-r--r--include/mcl/op.hpp17
-rw-r--r--include/mcl/random.hpp34
-rw-r--r--src/fp.cpp10
-rw-r--r--test/fp_util_test.cpp2
7 files changed, 27 insertions, 68 deletions
diff --git a/include/mcl/ahe.hpp b/include/mcl/ahe.hpp
index 89e2771..239319d 100644
--- a/include/mcl/ahe.hpp
+++ b/include/mcl/ahe.hpp
@@ -8,7 +8,6 @@
*/
#include <mcl/elgamal.hpp>
#include <mcl/ecparam.hpp>
-#include <mcl/random.hpp>
namespace mcl {
@@ -37,7 +36,7 @@ static inline void initAhe()
static inline void initSecretKey(SecretKey& sec)
{
const Ec P(Fp(para.gx), Fp(para.gy));
- sec.init(P, Zn::getBitSize(), mcl::getRandomGenerator());
+ sec.init(P, Zn::getBitSize());
}
} //mcl::ahe192
@@ -68,7 +67,7 @@ static inline void initAhe()
static inline void initSecretKey(SecretKey& sec)
{
const Ec P(Fp(para.gx), Fp(para.gy));
- sec.init(P, Zn::getBitSize(), mcl::getRandomGenerator());
+ sec.init(P, Zn::getBitSize());
}
} //mcl::ahe256
diff --git a/include/mcl/elgamal.hpp b/include/mcl/elgamal.hpp
index 56ade5e..886e44a 100644
--- a/include/mcl/elgamal.hpp
+++ b/include/mcl/elgamal.hpp
@@ -228,8 +228,7 @@ struct ElgamalT {
input : m
output : c = (c1, c2) = (g^u, h^u f^m)
*/
- template<class RG>
- void enc(CipherText& c, const Zn& m, RG& rg) const
+ void enc(CipherText& c, const Zn& m, fp::RandGen rg = fp::RandGen()) const
{
Zn u;
u.setRand(rg);
@@ -244,8 +243,8 @@ struct ElgamalT {
input : m = 0 or 1
output : c (c1, c2), zkp
*/
- template<class RG, class Hash>
- void encWithZkp(CipherText& c, Zkp& zkp, int m, Hash& hash, RG& rg) const
+ template<class Hash>
+ void encWithZkp(CipherText& c, Zkp& zkp, int m, Hash& hash, fp::RandGen rg = fp::RandGen()) const
{
if (m != 0 && m != 1) {
throw cybozu::Exception("elgamal:PublicKey:encWithZkp") << m;
@@ -338,8 +337,7 @@ struct ElgamalT {
input : c = (c1, c2)
output : c = (c1 g^v, c2 h^v)
*/
- template<class RG>
- void rerandomize(CipherText& c, RG& rg) const
+ void rerandomize(CipherText& c, fp::RandGen rg = fp::RandGen()) const
{
Zn v;
v.setRand(rg);
@@ -479,8 +477,7 @@ struct ElgamalT {
g in Ec
h = g^z
*/
- template<class RG>
- void init(const Ec& f, size_t bitSize, RG& rg)
+ void init(const Ec& f, size_t bitSize, fp::RandGen rg = fp::RandGen())
{
Ec g, h;
z.setRand(rg);
diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp
index d500086..26f0e3c 100644
--- a/include/mcl/fp.hpp
+++ b/include/mcl/fp.hpp
@@ -57,7 +57,10 @@ std::string littleEndianToHexStr(const void *buf, size_t bufSize);
bool isEnableJIT(); // 1st call is not threadsafe
-void getRandVal(Unit *out, WrapperRG& rg, const Unit *in, size_t bitSize);
+void getRandVal(Unit *out, RandGen& rg, const Unit *in, size_t bitSize);
+
+// rg must be thread safe
+void setRandGen(RandGen& rg);
namespace local {
@@ -307,13 +310,12 @@ public:
b.p = &v_[0];
}
}
- void setByCSPRNG(mcl::fp::WrapperRG rg = mcl::fp::WrapperRG())
+ void setByCSPRNG(fp::RandGen rg = fp::RandGen())
{
- if (rg.isZero()) rg = op_.wrapperRg;
fp::getRandVal(v_, rg, op_.p, op_.bitSize);
toMont();
}
- void setRand(mcl::fp::WrapperRG rg = mcl::fp::WrapperRG())
+ void setRand(fp::RandGen rg = fp::RandGen()) // old api
{
setByCSPRNG(rg);
}
@@ -472,10 +474,6 @@ public:
{
op_.hash = hash;
}
- static inline void setWrapperRG(void *self, void (*readFunc)(void *self, void *buf, uint32_t bufSize))
- {
- op_.wrapperRg.set(self, readFunc);
- }
};
template<class tag, size_t maxBitSize> fp::Op FpT<tag, maxBitSize>::op_;
diff --git a/include/mcl/op.hpp b/include/mcl/op.hpp
index a5913ca..6c61f6e 100644
--- a/include/mcl/op.hpp
+++ b/include/mcl/op.hpp
@@ -173,15 +173,16 @@ void readWrapper<std::random_device>(void *self, void *buf, uint32_t bufSize)
/*
wrapper of cryptographically secure pseudo random number generator
*/
-class WrapperRG {
+class RandGen {
typedef void (*readFuncType)(void *self, void *buf, uint32_t bufSize);
void *self_;
readFuncType readFunc_;
public:
- WrapperRG() : self_(0), readFunc_(0) {}
- WrapperRG(void *self, readFuncType readFunc) : self_(self) , readFunc_(readFunc) {}
+ RandGen() : self_(0), readFunc_(0) {}
+ RandGen(void *self, readFuncType readFunc) : self_(self) , readFunc_(readFunc) {}
+ RandGen(const RandGen& rhs) : self_(rhs.self_), readFunc_(rhs.readFunc_) {}
template<class RG>
- WrapperRG(RG& rg)
+ RandGen(RG& rg)
: self_(reinterpret_cast<void*>(&rg))
, readFunc_(local::readWrapper<RG>)
{
@@ -191,12 +192,6 @@ public:
readFunc_(self_, out, byteSize);
}
bool isZero() const { return self_ == 0 && readFunc_ == 0; }
- void clear() { self_ = 0; readFunc_ = 0; }
- void set(void *self, void (*readFunc)(void *self,void *buf, uint32_t bufSize))
- {
- self_ = self;
- readFunc_ = readFunc;
- }
};
struct Op {
@@ -265,7 +260,6 @@ struct Op {
void2u fp2_sqr;
void2u fp2_mul_xi;
uint32_t (*hash)(void *out, uint32_t maxOutSize, const void *msg, uint32_t msgSize);
- WrapperRG wrapperRg;
PrimeMode primeMode;
bool isFullBit; // true if bitSize % uniSize == 0
@@ -339,7 +333,6 @@ struct Op {
isMont = false;
isFastMod = false;
hash = 0;
- wrapperRg.clear();
}
void fromMont(Unit* y, const Unit *x) const
{
diff --git a/include/mcl/random.hpp b/include/mcl/random.hpp
deleted file mode 100644
index 504b3f7..0000000
--- a/include/mcl/random.hpp
+++ /dev/null
@@ -1,34 +0,0 @@
-#pragma once
-/**
- @file
- @brief random generator
- @author MITSUNARI Shigeo(@herumi)
- @license modified new BSD license
- http://opensource.org/licenses/BSD-3-Clause
-*/
-
-#if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11
-#include <random>
-#else
-#include <cybozu/random_generator.hpp>
-#endif
-
-namespace mcl {
-
-#if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11
-inline std::random_device& getRandomGenerator()
-{
- static std::random_device rd;
- return rd;
-}
-#else
-
-inline cybozu::RandomGenerator& getRandomGenerator()
-{
- static cybozu::RandomGenerator rg;
- return rg;
-}
-
-#endif
-
-} // mcl
diff --git a/src/fp.cpp b/src/fp.cpp
index 8579860..9b63039 100644
--- a/src/fp.cpp
+++ b/src/fp.cpp
@@ -24,6 +24,7 @@
#endif
cybozu::RandomGenerator s_cybozuRandomGenerator;
+mcl::fp::RandGen s_rg(s_cybozuRandomGenerator);
namespace mcl {
@@ -197,8 +198,9 @@ bool isEnableJIT()
#endif
}
-void getRandVal(Unit *out, WrapperRG& rg, const Unit *in, size_t bitSize)
+void getRandVal(Unit *out, RandGen& rg, const Unit *in, size_t bitSize)
{
+ if (rg.isZero()) rg = s_rg;
const size_t n = (bitSize + UnitBitSize - 1) / UnitBitSize;
const size_t rem = bitSize & (UnitBitSize - 1);
for (;;) {
@@ -208,6 +210,11 @@ void getRandVal(Unit *out, WrapperRG& rg, const Unit *in, size_t bitSize)
}
}
+void setRandGen(RandGen& rg)
+{
+ s_rg = rg;
+}
+
static uint32_t sha256(void *out, uint32_t maxOutSize, const void *msg, uint32_t msgSize)
{
const uint32_t hashSize = 256 / 8;
@@ -555,7 +562,6 @@ void Op::init(const std::string& mstr, size_t maxBitSize, Mode mode, size_t mclM
} else {
hash = sha512;
}
- wrapperRg = mcl::fp::WrapperRG(s_cybozuRandomGenerator);
}
void arrayToStr(std::string& str, const Unit *x, size_t n, int ioMode)
diff --git a/test/fp_util_test.cpp b/test/fp_util_test.cpp
index 51bd529..24d7fa1 100644
--- a/test/fp_util_test.cpp
+++ b/test/fp_util_test.cpp
@@ -186,7 +186,7 @@ CYBOZU_TEST_AUTO(getRandVal)
const uint32_t *mod = tbl[i].mod;
const uint32_t *expect = tbl[i].expect;
#endif
- mcl::fp::WrapperRG wrg(rg);
+ mcl::fp::RandGen wrg(rg);
mcl::fp::getRandVal(out, wrg, mod, tbl[i].bitSize);
CYBOZU_TEST_EQUAL(out[0], expect[0]);
#if CYBOZU_OS_BIT == 32