From dc677f8b660ceb56ad0de48e74a7cb5b0c094b78 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 5 Feb 2018 19:41:13 +0900 Subject: [PATCH] pailler uses RandGen --- include/mcl/gmp_util.hpp | 12 +++++++----- include/mcl/paillier.hpp | 8 ++++---- test/gmp_test.cpp | 8 ++++++++ test/paillier_test.cpp | 8 +++----- 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 -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 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 -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 - 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 - 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 -#include #include 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;