[she] add dec without exception

pull/2/head
MITSUNARI Shigeo 6 years ago
parent 696330be5d
commit d9ea36e00c
  1. 5
      include/cybozu/random_generator.hpp
  2. 47
      include/mcl/she.hpp
  3. 8
      test/she_test.cpp

@ -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<class T>
void read(T *buf, size_t bufNum)
{
@ -137,8 +140,6 @@ private:
return get32();
}
#endif
private:
FILE *fp_;
};
template<class T, class RG>

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

@ -564,7 +564,7 @@ CYBOZU_TEST_AUTO(saveHash)
static inline void putK(double t) { printf("%.2e\n", t * 1e-3); }
template<class CT>
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);
}
}

Loading…
Cancel
Save