From d9ea36e00c441e0d50a6019f976a792cdc58f9ef Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Wed, 15 May 2019 17:03:04 +0900 Subject: [PATCH] [she] add dec without exception --- include/cybozu/random_generator.hpp | 5 +-- include/mcl/she.hpp | 47 +++++++++++++++++------------ test/she_test.cpp | 8 ++--- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/include/cybozu/random_generator.hpp b/include/cybozu/random_generator.hpp index 2e097e3..375db06 100644 --- a/include/cybozu/random_generator.hpp +++ b/include/cybozu/random_generator.hpp @@ -111,8 +111,11 @@ private: const size_t byteSize = sizeof(T) * bufNum; *pb = ::fread(buf, 1, (int)byteSize, fp_) == byteSize; } +private: + FILE *fp_; #endif #ifndef CYBOZU_DONT_USE_EXCEPTION +public: template void read(T *buf, size_t bufNum) { @@ -137,8 +140,6 @@ private: return get32(); } #endif -private: - FILE *fp_; }; template diff --git a/include/mcl/she.hpp b/include/mcl/she.hpp index 73dc4f8..282f2fe 100644 --- a/include/mcl/she.hpp +++ b/include/mcl/she.hpp @@ -170,9 +170,9 @@ public: find range which has same hash of xP in kcv_, and detect it */ - int basicLog(G xP, bool *ok = 0) const + int basicLog(G xP, bool *pok = 0) const { - if (ok) *ok = true; + if (pok) *pok = true; if (I::isZero(xP)) return 0; typedef KeyCountVec::const_iterator Iter; KeyCount kc; @@ -205,8 +205,8 @@ public: prev = abs_c; ++p.first; } - if (ok) { - *ok = false; + if (pok) { + *pok = false; return 0; } throw cybozu::Exception("HashTable:basicLog:not found"); @@ -215,11 +215,12 @@ public: compute log_P(xP) call basicLog at most 2 * tryNum */ - int64_t log(const G& xP) const + int64_t log(const G& xP, bool *pok = 0) const { bool ok; int c = basicLog(xP, &ok); if (ok) { + if (pok) *pok = true; return c; } G posP = xP, negP = xP; @@ -231,15 +232,21 @@ public: posCenter += next; c = basicLog(posP, &ok); if (ok) { + if (pok) *pok = true; return posCenter + c; } I::add(negP, negP, nextP_); negCenter -= next; c = basicLog(negP, &ok); if (ok) { + if (pok) *pok = true; return negCenter + c; } } + if (pok) { + *pok = false; + return 0; + } throw cybozu::Exception("HashTable:log:not found"); } /* @@ -683,7 +690,7 @@ public: throw cybozu::Exception("she:dec:log:not found"); } #endif - int64_t dec(const CipherTextG1& c) const + int64_t dec(const CipherTextG1& c, bool *pok = 0) const { if (useDecG1ViaGT_) return decViaGT(c); /* @@ -694,51 +701,51 @@ public: G1 R; G1::mul(R, c.T_, x_); G1::sub(R, c.S_, R); - return PhashTbl_.log(R); + return PhashTbl_.log(R, pok); } - int64_t dec(const CipherTextG2& c) const + int64_t dec(const CipherTextG2& c, bool *pok = 0) const { if (useDecG2ViaGT_) return decViaGT(c); G2 R; G2::mul(R, c.T_, y_); G2::sub(R, c.S_, R); - return QhashTbl_.log(R); + return QhashTbl_.log(R, pok); } - int64_t dec(const CipherTextA& c) const + int64_t dec(const CipherTextA& c, bool *pok = 0) const { - return dec(c.c1_); + return dec(c.c1_, pok); } - int64_t dec(const CipherTextGT& c) const + int64_t dec(const CipherTextGT& c, bool *pok = 0) const { GT v; getPowOfePQ(v, c); - return ePQhashTbl_.log(v); + return ePQhashTbl_.log(v, pok); // return log(g, v); } - int64_t decViaGT(const CipherTextG1& c) const + int64_t decViaGT(const CipherTextG1& c, bool *pok = 0) const { G1 R; G1::mul(R, c.T_, x_); G1::sub(R, c.S_, R); GT v; pairing(v, R, Q_); - return ePQhashTbl_.log(v); + return ePQhashTbl_.log(v, pok); } - int64_t decViaGT(const CipherTextG2& c) const + int64_t decViaGT(const CipherTextG2& c, bool *pok = 0) const { G2 R; G2::mul(R, c.T_, y_); G2::sub(R, c.S_, R); GT v; pairing(v, P_, R); - return ePQhashTbl_.log(v); + return ePQhashTbl_.log(v, pok); } - int64_t dec(const CipherText& c) const + int64_t dec(const CipherText& c, bool *pok = 0) const { if (c.isMultiplied()) { - return dec(c.m_); + return dec(c.m_, pok); } else { - return dec(c.a_); + return dec(c.a_, pok); } } bool isZero(const CipherTextG1& c) const diff --git a/test/she_test.cpp b/test/she_test.cpp index 9292c35..cb64478 100644 --- a/test/she_test.cpp +++ b/test/she_test.cpp @@ -564,7 +564,7 @@ CYBOZU_TEST_AUTO(saveHash) static inline void putK(double t) { printf("%.2e\n", t * 1e-3); } template -void decBench(const char *msg, int C, const SecretKey& sec, const PublicKey& pub, int64_t (SecretKey::*dec)(const CT& c) const = &SecretKey::dec) +void decBench(const char *msg, int C, const SecretKey& sec, const PublicKey& pub, int64_t (SecretKey::*dec)(const CT& c, bool *pok) const = &SecretKey::dec) { int64_t begin = 1 << 20; int64_t end = 1LL << 32; @@ -573,8 +573,8 @@ void decBench(const char *msg, int C, const SecretKey& sec, const PublicKey& pub int64_t x = begin - 1; pub.enc(c, x); printf("m=%08x ", (uint32_t)x); - CYBOZU_BENCH_C(msg, C, (sec.*dec), c); - CYBOZU_TEST_EQUAL((sec.*dec)(c), x); + CYBOZU_BENCH_C(msg, C, (sec.*dec), c, 0); + CYBOZU_TEST_EQUAL((sec.*dec)(c, 0), x); begin *= 2; } int64_t mTbl[] = { -0x80000003ll, 0x80000000ll, 0x80000005ll }; @@ -582,7 +582,7 @@ void decBench(const char *msg, int C, const SecretKey& sec, const PublicKey& pub int64_t m = mTbl[i]; CT c; pub.enc(c, m); - CYBOZU_TEST_EQUAL((sec.*dec)(c), m); + CYBOZU_TEST_EQUAL((sec.*dec)(c, 0), m); } }