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);
#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);

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

@ -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/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;

Loading…
Cancel
Save