diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-07-02 10:23:43 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-07-02 10:23:43 +0800 |
commit | 75c77060fb73757cbdeaea7ad08fb063fb1b868b (patch) | |
tree | e1caaf1e0124a5a49489b3e154c29ae5d6519256 | |
parent | 122485f2c6026ac815310cd327e3f195b3dabf6a (diff) | |
download | dexon-mcl-75c77060fb73757cbdeaea7ad08fb063fb1b868b.tar.gz dexon-mcl-75c77060fb73757cbdeaea7ad08fb063fb1b868b.tar.zst dexon-mcl-75c77060fb73757cbdeaea7ad08fb063fb1b868b.zip |
add default random_generator for bgn
-rw-r--r-- | include/mcl/bgn.hpp | 35 | ||||
-rw-r--r-- | sample/bgn_smpl.cpp | 53 | ||||
-rw-r--r-- | test/bgn_test.cpp | 50 |
3 files changed, 72 insertions, 66 deletions
diff --git a/include/mcl/bgn.hpp b/include/mcl/bgn.hpp index 1d10756..f15c37e 100644 --- a/include/mcl/bgn.hpp +++ b/include/mcl/bgn.hpp @@ -22,10 +22,23 @@ #define MCL_USE_BN256 #endif +#if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11 +#include <random> +#else +#include <cybozu/random_generator.hpp> +#endif + namespace mcl { namespace bgn { namespace local { +#if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11 +typedef std::random_device RandomDevice; +static thread_local std::random_device g_rg; +#else +static cybozu::RandomGenerator g_rg; +#endif + struct KeyCount { uint32_t key; int32_t count; // power @@ -444,6 +457,7 @@ public: z2.setRand(rg); initInner(); } + void setByCSPRNG() { setByCSPRNG(local::g_rg); } /* set range for G1-DLP */ @@ -641,6 +655,10 @@ public: c.isMultiplied_ = false; enc(c.a, m, rg); } + void enc(CipherTextG1& c, int m) const { return enc(c, m, local::g_rg); } + void enc(CipherTextG2& c, int m) const { return enc(c, m, local::g_rg); } + void enc(CipherTextA& c, int m) const { return enc(c, m, local::g_rg); } + void enc(CipherText& c, int m) const { return enc(c, m, local::g_rg); } /* convert from CipherTextG1 to CipherTextM */ @@ -716,6 +734,10 @@ public: rerandomize(c.a, rg); } } + void rerandomize(CipherTextA& c) const { rerandomize(c, local::g_rg); } + void rerandomize(CipherTextM& c) const { rerandomize(c, local::g_rg); } + void rerandomize(CipherText& c) const { rerandomize(c, local::g_rg); } + std::istream& readStream(std::istream& is, int ioMode) { xP.readStream(is, ioMode); @@ -1043,5 +1065,18 @@ typename BN::G1 BGNT<BN, Fr>::P; template<class BN, class Fr> typename BN::G2 BGNT<BN, Fr>::Q; +#ifdef MCL_USE_BN384 +typedef mcl::bgn::BGNT<mcl::bn384::BN, mcl::bn256::Fr> BGN; +#else +typedef mcl::bgn::BGNT<mcl::bn256::BN, mcl::bn256::Fr> BGN; +#endif +typedef BGN::SecretKey SecretKey; +typedef BGN::PublicKey PublicKey; +typedef BGN::CipherTextG1 CipherTextG1; +typedef BGN::CipherTextG2 CipherTextG2; +typedef BGN::CipherTextA CipherTextA; +typedef BGN::CipherTextM CipherTextM; +typedef BGN::CipherText CipherText; + } } // mcl::bgn diff --git a/sample/bgn_smpl.cpp b/sample/bgn_smpl.cpp index eaf772e..ea182e4 100644 --- a/sample/bgn_smpl.cpp +++ b/sample/bgn_smpl.cpp @@ -3,19 +3,8 @@ */ #define PUT(x) std::cout << #x << "=" << (x) << std::endl; #include <cybozu/benchmark.hpp> -#include <mcl/bn256.hpp> #include <mcl/bgn.hpp> -#if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11 -#include <random> -std::random_device g_rg; -#else -#include <cybozu/random_generator.hpp> -cybozu::RandomGenerator g_rg; -#endif - -typedef mcl::bgn::BGNT<mcl::bn256::BN, mcl::bn256::Fr> BGN; - using namespace mcl::bgn; void miniSample() @@ -23,15 +12,15 @@ void miniSample() // init library BGN::init(); - BGN::SecretKey sec; + SecretKey sec; // init secret key by random_device - sec.setByCSPRNG(g_rg); + sec.setByCSPRNG(); // set range to decode GT DLP sec.setRangeForGTDLP(1000); - BGN::PublicKey pub; + PublicKey pub; // get public key sec.getPublicKey(pub); @@ -44,19 +33,19 @@ void miniSample() sum += a[i] * b[i]; } - std::vector<BGN::CipherText> ca(N), cb(N); + std::vector<CipherText> ca(N), cb(N); // encrypt each a[] and b[] for (size_t i = 0; i < N; i++) { - pub.enc(ca[i], a[i], g_rg); - pub.enc(cb[i], b[i], g_rg); + pub.enc(ca[i], a[i]); + pub.enc(cb[i], b[i]); } - BGN::CipherText c; + CipherText c; c.clearAsMultiplied(); // clear as multiplied before using c.add() // inner product of encrypted vector for (size_t i = 0; i < N; i++) { - BGN::CipherText t; - BGN::CipherText::mul(t, ca[i], cb[i]); // t = ca[i] * cb[i] + CipherText t; + CipherText::mul(t, ca[i], cb[i]); // t = ca[i] * cb[i] c.add(t); // c += t } // decode it @@ -74,30 +63,30 @@ void usePrimitiveCipherText() // init library BGN::init(); - BGN::SecretKey sec; + SecretKey sec; // init secret key by random_device - sec.setByCSPRNG(g_rg); + sec.setByCSPRNG(); // set range to decode GT DLP sec.setRangeForGTDLP(1000); - BGN::PublicKey pub; + PublicKey pub; // get public key sec.getPublicKey(pub); int a1 = 1, a2 = 2; int b1 = 5, b2 = -4; - BGN::CipherTextG1 c1, c2; // size of CipherTextG1 = N * 2 ; N = 256-bit for CurveFp254BNb - BGN::CipherTextG2 d1, d2; // size of CipherTextG2 = N * 4 - pub.enc(c1, a1, g_rg); - pub.enc(c2, a2, g_rg); - pub.enc(d1, b1, g_rg); - pub.enc(d2, b2, g_rg); + CipherTextG1 c1, c2; // size of CipherTextG1 = N * 2 ; N = 256-bit for CurveFp254BNb + CipherTextG2 d1, d2; // size of CipherTextG2 = N * 4 + pub.enc(c1, a1); + pub.enc(c2, a2); + pub.enc(d1, b1); + pub.enc(d2, b2); c1.add(c2); // CipherTextG1 is additive HE d1.add(d2); // CipherTextG2 is additive HE - BGN::CipherTextM cm; // size of CipherTextM = N * 12 * 4 - BGN::CipherTextM::mul(cm, c1, d1); // cm = c1 * d1 + CipherTextM cm; // size of CipherTextM = N * 12 * 4 + CipherTextM::mul(cm, c1, d1); // cm = c1 * d1 cm.add(cm); // 2cm int m = sec.dec(cm); int ok = (a1 + a2) * (b1 + b2) * 2; @@ -120,7 +109,7 @@ void usePrimitiveCipherText() s = cm.getStr(mcl::IoFixedSizeByteSeq); // serialize printf("cm data size %d byte\n", (int)s.size()); - BGN::CipherTextM cm2; + CipherTextM cm2; cm2.setStr(s, mcl::IoFixedSizeByteSeq); printf("deserialize %s\n", cm == cm2 ? "ok" : "ng"); } diff --git a/test/bgn_test.cpp b/test/bgn_test.cpp index 32192aa..9efe588 100644 --- a/test/bgn_test.cpp +++ b/test/bgn_test.cpp @@ -1,26 +1,8 @@ #define PUT(x) std::cout << #x << "=" << (x) << std::endl; #include <cybozu/test.hpp> #include <cybozu/benchmark.hpp> -#include <cybozu/random_generator.hpp> -#include <mcl/bn256.hpp> #include <mcl/bgn.hpp> -#if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11 -#include <random> -std::random_device g_rg; -#else -cybozu::RandomGenerator g_rg; -#endif - -typedef mcl::bgn::BGNT<mcl::bn256::BN, mcl::bn256::Fr> BGN; -typedef BGN::SecretKey SecretKey; -typedef BGN::PublicKey PublicKey; -typedef BGN::CipherTextG1 CipherTextG1; -typedef BGN::CipherTextG2 CipherTextG2; -typedef BGN::CipherTextA CipherTextA; -typedef BGN::CipherTextM CipherTextM; -typedef BGN::CipherText CipherText; - using namespace mcl::bgn; using namespace mcl::bn256; @@ -87,15 +69,15 @@ CYBOZU_TEST_AUTO(GTHashTable) CYBOZU_TEST_AUTO(enc_dec) { SecretKey& sec = g_sec; - sec.setByCSPRNG(g_rg); + sec.setByCSPRNG(); sec.setRangeForDLP(1024); PublicKey pub; sec.getPublicKey(pub); CipherText c; for (int i = -5; i < 5; i++) { - pub.enc(c, i, g_rg); + pub.enc(c, i); CYBOZU_TEST_EQUAL(sec.dec(c), i); - pub.rerandomize(c, g_rg); + pub.rerandomize(c); CYBOZU_TEST_EQUAL(sec.dec(c), i); } } @@ -108,12 +90,12 @@ CYBOZU_TEST_AUTO(add_sub_mul) for (int m1 = -5; m1 < 5; m1++) { for (int m2 = -5; m2 < 5; m2++) { CipherText c1, c2, c3; - pub.enc(c1, m1, g_rg); - pub.enc(c2, m2, g_rg); + pub.enc(c1, m1); + pub.enc(c2, m2); CipherText::add(c3, c1, c2); CYBOZU_TEST_EQUAL(m1 + m2, sec.dec(c3)); - pub.rerandomize(c3, g_rg); + pub.rerandomize(c3); CYBOZU_TEST_EQUAL(m1 + m2, sec.dec(c3)); CipherText::sub(c3, c1, c2); @@ -122,7 +104,7 @@ CYBOZU_TEST_AUTO(add_sub_mul) CipherText::mul(c3, c1, c2); CYBOZU_TEST_EQUAL(m1 * m2, sec.dec(c3)); - pub.rerandomize(c3, g_rg); + pub.rerandomize(c3); CYBOZU_TEST_EQUAL(m1 * m2, sec.dec(c3)); } } @@ -136,7 +118,7 @@ CYBOZU_TEST_AUTO(add_mul_add_sub) int m[8] = { 1, -2, 3, 4, -5, 6, -7, 8 }; CipherText c[8]; for (int i = 0; i < 8; i++) { - pub.enc(c[i], m[i], g_rg); + pub.enc(c[i], m[i]); CYBOZU_TEST_EQUAL(sec.dec(c[i]), m[i]); CYBOZU_TEST_ASSERT(!c[i].isMultiplied()); CipherText mc; @@ -181,21 +163,21 @@ CYBOZU_TEST_AUTO(io) G1::setIoMode(mcl::IoFixedSizeByteSeq); } SecretKey sec; - sec.setByCSPRNG(g_rg); + sec.setByCSPRNG(); sec.setRangeForDLP(100, 2); testIo(sec); PublicKey pub; sec.getPublicKey(pub); testIo(pub); CipherTextG1 g1; - pub.enc(g1, 3, g_rg); + pub.enc(g1, 3); m = sec.dec(testIo(g1)); CYBOZU_TEST_EQUAL(m, 3); CipherTextG2 g2; - pub.enc(g2, 5, g_rg); + pub.enc(g2, 5); testIo(g2); CipherTextA ca; - pub.enc(ca, -4, g_rg); + pub.enc(ca, -4); m = sec.dec(testIo(ca)); CYBOZU_TEST_EQUAL(m, -4); CipherTextM cm; @@ -211,12 +193,12 @@ CYBOZU_TEST_AUTO(bench) PublicKey pub; sec.getPublicKey(pub); CipherText c1, c2, c3; - CYBOZU_BENCH("enc", pub.enc, c1, 5, g_rg); - pub.enc(c2, 4, g_rg); + CYBOZU_BENCH("enc", pub.enc, c1, 5); + pub.enc(c2, 4); CYBOZU_BENCH("add", c1.add, c2); CYBOZU_BENCH("mul", CipherText::mul, c3, c1, c2); - pub.enc(c1, 5, g_rg); - pub.enc(c2, 4, g_rg); + pub.enc(c1, 5); + pub.enc(c2, 4); c1.mul(c2); CYBOZU_BENCH("dec", sec.dec, c1); c2 = c1; |