pailler uses RandGen

dev
MITSUNARI Shigeo 7 years ago
parent 63248ea9bb
commit dc677f8b66
  1. 12
      include/mcl/gmp_util.hpp
  2. 8
      include/mcl/paillier.hpp
  3. 8
      test/gmp_test.cpp
  4. 8
      test/paillier_test.cpp

@ -416,14 +416,15 @@ inline mpz_class abs(const mpz_class& x)
return ::abs(x); return ::abs(x);
#endif #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); assert(bitSize > 1);
const size_t rem = bitSize & 31; const size_t rem = bitSize & 31;
const size_t n = (bitSize + 31) / 32; const size_t n = (bitSize + 31) / 32;
std::vector<uint32_t> buf(n); 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]; uint32_t v = buf[n - 1];
if (rem == 0) { if (rem == 0) {
v |= 1U << 31; v |= 1U << 31;
@ -434,9 +435,10 @@ void getRand(mpz_class& z, size_t bitSize, RG& rg)
buf[n - 1] = v; buf[n - 1] = v;
setArray(z, &buf[0], n); 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); assert(bitSize > 2);
do { do {
getRand(z, bitSize, rg); getRand(z, bitSize, rg);

@ -24,9 +24,9 @@ public:
g = 1 + _n; g = 1 + _n;
n2 = _n * _n; n2 = _n * _n;
} }
template<class RG> void enc(mpz_class& c, const mpz_class& m, mcl::fp::RandGen rg = mcl::fp::RandGen()) const
void enc(mpz_class& c, const mpz_class& m, RG& rg) const
{ {
if (rg.isZero()) rg = mcl::fp::RandGen::get();
if (primeBitSize == 0) throw cybozu::Exception("paillier:PublicKey:not init"); if (primeBitSize == 0) throw cybozu::Exception("paillier:PublicKey:not init");
mpz_class r; mpz_class r;
mcl::gmp::getRand(r, primeBitSize, rg); mcl::gmp::getRand(r, primeBitSize, rg);
@ -56,9 +56,9 @@ public:
/* /*
the size of prime is half of bitSize the size of prime is half of bitSize
*/ */
template<class RG> void init(size_t bitSize, mcl::fp::RandGen rg = mcl::fp::RandGen())
void init(size_t bitSize, RG& rg)
{ {
if (rg.isZero()) rg = mcl::fp::RandGen::get();
primeBitSize = bitSize / 2; primeBitSize = bitSize / 2;
mpz_class p, q; mpz_class p, q;
mcl::gmp::getRandPrime(p, primeBitSize, rg); mcl::gmp::getRandPrime(p, primeBitSize, rg);

@ -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));
}
}

@ -1,19 +1,17 @@
#include <cybozu/test.hpp> #include <cybozu/test.hpp>
#include <cybozu/random_generator.hpp>
#include <mcl/paillier.hpp> #include <mcl/paillier.hpp>
CYBOZU_TEST_AUTO(paillier) CYBOZU_TEST_AUTO(paillier)
{ {
cybozu::RandomGenerator rg;
using namespace mcl::paillier; using namespace mcl::paillier;
SecretKey sec; SecretKey sec;
sec.init(2048, rg); sec.init(2048);
PublicKey pub; PublicKey pub;
sec.getPublicKey(pub); sec.getPublicKey(pub);
mpz_class m1("12342340928409"), m2("23049820498204"); mpz_class m1("12342340928409"), m2("23049820498204");
mpz_class c1, c2, c3; mpz_class c1, c2, c3;
pub.enc(c1, m1, rg); pub.enc(c1, m1);
pub.enc(c2, m2, rg); pub.enc(c2, m2);
std::cout << std::hex << "c1=" << c1 << "\nc2=" << c2 << std::endl; std::cout << std::hex << "c1=" << c1 << "\nc2=" << c2 << std::endl;
pub.add(c3, c1, c2); pub.add(c3, c1, c2);
mpz_class d1, d2, d3; mpz_class d1, d2, d3;

Loading…
Cancel
Save