aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2018-02-05 18:41:13 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2018-02-05 18:41:13 +0800
commitdc677f8b660ceb56ad0de48e74a7cb5b0c094b78 (patch)
tree9383b30b661938c0f25d582239eb168be043fbcf
parent63248ea9bb2ce832bac46411b84e08535a4248cc (diff)
downloaddexon-mcl-dc677f8b660ceb56ad0de48e74a7cb5b0c094b78.tar.gz
dexon-mcl-dc677f8b660ceb56ad0de48e74a7cb5b0c094b78.tar.zst
dexon-mcl-dc677f8b660ceb56ad0de48e74a7cb5b0c094b78.zip
pailler uses RandGen
-rw-r--r--include/mcl/gmp_util.hpp12
-rw-r--r--include/mcl/paillier.hpp8
-rw-r--r--test/gmp_test.cpp8
-rw-r--r--test/paillier_test.cpp8
4 files changed, 22 insertions, 14 deletions
diff --git a/include/mcl/gmp_util.hpp b/include/mcl/gmp_util.hpp
index 2b581a3..78cafc1 100644
--- a/include/mcl/gmp_util.hpp
+++ b/include/mcl/gmp_util.hpp
@@ -416,14 +416,15 @@ inline mpz_class abs(const mpz_class& x)
return ::abs(x);
#endif
}
-template<class RG>
-void getRand(mpz_class& z, size_t bitSize, RG& rg)
+
+inline void getRand(mpz_class& z, size_t bitSize, fp::RandGen rg = fp::RandGen())
{
+ if (rg.isZero()) rg = fp::RandGen::get();
assert(bitSize > 1);
const size_t rem = bitSize & 31;
const size_t n = (bitSize + 31) / 32;
std::vector<uint32_t> buf(n);
- rg.read(buf.data(), n);
+ rg.read(buf.data(), n * sizeof(buf[0]));
uint32_t v = buf[n - 1];
if (rem == 0) {
v |= 1U << 31;
@@ -434,9 +435,10 @@ void getRand(mpz_class& z, size_t bitSize, RG& rg)
buf[n - 1] = v;
setArray(z, &buf[0], n);
}
-template<class RG>
-void getRandPrime(mpz_class& z, size_t bitSize, RG& rg, bool setSecondBit = false, bool mustBe3mod4 = false)
+
+inline void getRandPrime(mpz_class& z, size_t bitSize, fp::RandGen rg = fp::RandGen(), bool setSecondBit = false, bool mustBe3mod4 = false)
{
+ if (rg.isZero()) rg = fp::RandGen::get();
assert(bitSize > 2);
do {
getRand(z, bitSize, rg);
diff --git a/include/mcl/paillier.hpp b/include/mcl/paillier.hpp
index faab203..03e44cb 100644
--- a/include/mcl/paillier.hpp
+++ b/include/mcl/paillier.hpp
@@ -24,9 +24,9 @@ public:
g = 1 + _n;
n2 = _n * _n;
}
- template<class RG>
- void enc(mpz_class& c, const mpz_class& m, RG& rg) const
+ void enc(mpz_class& c, const mpz_class& m, mcl::fp::RandGen rg = mcl::fp::RandGen()) const
{
+ if (rg.isZero()) rg = mcl::fp::RandGen::get();
if (primeBitSize == 0) throw cybozu::Exception("paillier:PublicKey:not init");
mpz_class r;
mcl::gmp::getRand(r, primeBitSize, rg);
@@ -56,9 +56,9 @@ public:
/*
the size of prime is half of bitSize
*/
- template<class RG>
- void init(size_t bitSize, RG& rg)
+ void init(size_t bitSize, mcl::fp::RandGen rg = mcl::fp::RandGen())
{
+ if (rg.isZero()) rg = mcl::fp::RandGen::get();
primeBitSize = bitSize / 2;
mpz_class p, q;
mcl::gmp::getRandPrime(p, primeBitSize, rg);
diff --git a/test/gmp_test.cpp b/test/gmp_test.cpp
index 2d3e563..22c80dd 100644
--- a/test/gmp_test.cpp
+++ b/test/gmp_test.cpp
@@ -21,3 +21,11 @@ CYBOZU_TEST_AUTO(testBit)
}
}
+CYBOZU_TEST_AUTO(getRandPrime)
+{
+ for (int i = 0; i < 10; i++) {
+ mpz_class z;
+ mcl::gmp::getRandPrime(z, i * 10 + 3);
+ CYBOZU_TEST_ASSERT(mcl::gmp::isPrime(z));
+ }
+}
diff --git a/test/paillier_test.cpp b/test/paillier_test.cpp
index 3791452..31d2b26 100644
--- a/test/paillier_test.cpp
+++ b/test/paillier_test.cpp
@@ -1,19 +1,17 @@
#include <cybozu/test.hpp>
-#include <cybozu/random_generator.hpp>
#include <mcl/paillier.hpp>
CYBOZU_TEST_AUTO(paillier)
{
- cybozu::RandomGenerator rg;
using namespace mcl::paillier;
SecretKey sec;
- sec.init(2048, rg);
+ sec.init(2048);
PublicKey pub;
sec.getPublicKey(pub);
mpz_class m1("12342340928409"), m2("23049820498204");
mpz_class c1, c2, c3;
- pub.enc(c1, m1, rg);
- pub.enc(c2, m2, rg);
+ pub.enc(c1, m1);
+ pub.enc(c2, m2);
std::cout << std::hex << "c1=" << c1 << "\nc2=" << c2 << std::endl;
pub.add(c3, c1, c2);
mpz_class d1, d2, d3;