Skip to content

Commit

Permalink
add encWithZkpBinEq without exception for wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Jul 24, 2023
1 parent f1cd3b3 commit a3937c5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 50 deletions.
27 changes: 22 additions & 5 deletions include/mcl/she.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1307,9 +1307,9 @@ struct SHET {
encRand1, encRand2 are random values use for ElGamalEnc()
*/
template<class G1, class G2, class I1, class I2, class MulG1, class MulG2>
static void makeZkpBinEq(ZkpBinEq& zkp, G1& S1, G1& T1, G2& S2, G2& T2, int m, const mcl::fp::WindowMethod<I1>& Pmul, const MulG1& xPmul, const mcl::fp::WindowMethod<I2>& Qmul, const MulG2& yQmul)
static bool makeZkpBinEq(ZkpBinEq& zkp, G1& S1, G1& T1, G2& S2, G2& T2, int m, const mcl::fp::WindowMethod<I1>& Pmul, const MulG1& xPmul, const mcl::fp::WindowMethod<I2>& Qmul, const MulG2& yQmul)
{
if (m != 0 && m != 1) throw cybozu::Exception("makeZkpBinEq:bad m") << m;
if (m != 0 && m != 1) return false;
Fr *d = &zkp.d_[0];
Fr *spm = &zkp.d_[2];
Fr& ss = zkp.d_[4];
Expand Down Expand Up @@ -1357,6 +1357,7 @@ struct SHET {
ss += rs;
Fr::mul(sm, c, m);
sm += rm;
return true;
}
template<class G1, class G2, class I1, class I2, class MulG1, class MulG2>
static bool verifyZkpBinEq(const ZkpBinEq& zkp, const G1& S1, const G1& T1, const G2& S2, const G2& T2, const mcl::fp::WindowMethod<I1>& Pmul, const MulG1& xPmul, const mcl::fp::WindowMethod<I2>& Qmul, const MulG2& yQmul)
Expand Down Expand Up @@ -1648,11 +1649,19 @@ struct SHET {
const MulG<G2> yQmul(yQ_);
return verifyZkpEq(zkp, c1.S_, c1.T_, c2.S_, c2.T_, PhashTbl_.getWM(), xPmul, QhashTbl_.getWM(), yQmul);
}
void encWithZkpBinEq(CipherTextG1& c1, CipherTextG2& c2, ZkpBinEq& zkp, int m) const
void encWithZkpBinEq(bool *pb, CipherTextG1& c1, CipherTextG2& c2, ZkpBinEq& zkp, int m) const
{
const MulG<G1> xPmul(xP_);
const MulG<G2> yQmul(yQ_);
makeZkpBinEq(zkp, c1.S_, c1.T_, c2.S_, c2.T_, m, PhashTbl_.getWM(), xPmul, QhashTbl_.getWM(), yQmul);
*pb = makeZkpBinEq(zkp, c1.S_, c1.T_, c2.S_, c2.T_, m, PhashTbl_.getWM(), xPmul, QhashTbl_.getWM(), yQmul);
}
void encWithZkpBinEq(CipherTextG1& c1, CipherTextG2& c2, ZkpBinEq& zkp, int m) const
{
bool b;
encWithZkpBinEq(&b, c1, c2, zkp, m);
if (!b) {
throw cybozu::Exception("encWithZkpBinEq:bad m") << m;
}
}
bool verify(const CipherTextG1& c1, const CipherTextG2& c2, const ZkpBinEq& zkp) const
{
Expand Down Expand Up @@ -1878,9 +1887,17 @@ struct SHET {
{
return verifyZkpEq(zkp, c1.S_, c1.T_, c2.S_, c2.T_, PhashTbl_.getWM(), xPwm_, QhashTbl_.getWM(), yQwm_);
}
void encWithZkpBinEq(bool *pb, CipherTextG1& c1, CipherTextG2& c2, ZkpBinEq& zkp, int m) const
{
*pb = makeZkpBinEq(zkp, c1.S_, c1.T_, c2.S_, c2.T_, m, PhashTbl_.getWM(), xPwm_, QhashTbl_.getWM(), yQwm_);
}
void encWithZkpBinEq(CipherTextG1& c1, CipherTextG2& c2, ZkpBinEq& zkp, int m) const
{
makeZkpBinEq(zkp, c1.S_, c1.T_, c2.S_, c2.T_, m, PhashTbl_.getWM(), xPwm_, QhashTbl_.getWM(), yQwm_);
bool b;
encWithZkpBinEq(&b, c1, c2, zkp, m);
if (!b) {
throw cybozu::Exception("encWithZkpBinEq:bad m") << m;
}
}
bool verify(const CipherTextG1& c1, const CipherTextG2& c2, const ZkpBinEq& zkp) const
{
Expand Down
54 changes: 9 additions & 45 deletions src/she_c_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,9 @@ mclSize sheGetTableSizeForGTDLP() { return SHE::ePQhashTbl_.getTableSize(); }

template<class CT>
int encT(CT *c, const shePublicKey *pub, mclInt m)
try
{
cast(pub)->enc(*cast(c), m);
return 0;
} catch (std::exception&) {
return -1;
}

int sheEncG1(sheCipherTextG1 *c, const shePublicKey *pub, mclInt m)
Expand Down Expand Up @@ -343,12 +340,10 @@ int sheEncIntVecGT(sheCipherTextGT *c, const shePublicKey *pub, const void *buf,

template<class CT, class PK>
int encWithZkpBinT(CT *c, sheZkpBin *zkp, const PK *pub, int m)
try
{
cast(pub)->encWithZkpBin(*cast(c), *cast(zkp), m);
return 0;
} catch (std::exception&) {
return -1;
bool b;
cast(pub)->encWithZkpBin(&b, *cast(c), *cast(zkp), m);
return b ? 0 : -1;
}

int sheEncWithZkpBinG1(sheCipherTextG1 *c, sheZkpBin *zkp, const shePublicKey *pub, int m)
Expand All @@ -373,12 +368,10 @@ int shePrecomputedPublicKeyEncWithZkpBinG2(sheCipherTextG2 *c, sheZkpBin *zkp, c

template<class CT, class PK>
int encWithZkpSetT(CT *c, mclBnFr *zkp, const PK *pub, int m, const int *mVec, mclSize mSize)
try
{
cast(pub)->encWithZkpSet(*cast(c), cast2(zkp), m, mVec, mSize);
return 0;
} catch (std::exception&) {
return -1;
bool b;
cast(pub)->encWithZkpSet(&b, *cast(c), cast2(zkp), m, mVec, mSize);
return b ? 0 : -1;
}

int sheEncWithZkpSetG1(sheCipherTextG1 *c, mclBnFr *zkp, const shePublicKey *pub, int m, const int *mVec, mclSize mSize)
Expand Down Expand Up @@ -412,12 +405,9 @@ int shePrecomputedPublicKeyVerifyZkpSetG1(const shePrecomputedPublicKey *ppub, c

template<class PK>
int encWithZkpEqT(sheCipherTextG1 *c1, sheCipherTextG2 *c2, sheZkpEq *zkp, const PK *pub, mclInt m)
try
{
cast(pub)->encWithZkpEq(*cast(c1), *cast(c2), *cast(zkp), m);
return 0;
} catch (std::exception&) {
return -1;
}

int sheEncWithZkpEq(sheCipherTextG1 *c1, sheCipherTextG2 *c2, sheZkpEq *zkp, const shePublicKey *pub, mclInt m)
Expand All @@ -432,12 +422,10 @@ int shePrecomputedPublicKeyEncWithZkpEq(sheCipherTextG1 *c1, sheCipherTextG2 *c2

template<class PK>
int encWithZkpBinEqT(sheCipherTextG1 *c1, sheCipherTextG2 *c2, sheZkpBinEq *zkp, const PK *pub, int m)
try
{
cast(pub)->encWithZkpBinEq(*cast(c1), *cast(c2), *cast(zkp), m);
return 0;
} catch (std::exception&) {
return -1;
bool b;
cast(pub)->encWithZkpBinEq(&b, *cast(c1), *cast(c2), *cast(zkp), m);
return b ? 0 : -1;
}

int sheEncWithZkpBinEq(sheCipherTextG1 *c1, sheCipherTextG2 *c2, sheZkpBinEq *zkp, const shePublicKey *pub, int m)
Expand Down Expand Up @@ -520,12 +508,9 @@ int sheIsZeroGT(const sheSecretKey *sec, const sheCipherTextGT *c)

template<class CT>
int negT(CT& y, const CT& x)
try
{
CT::neg(y, x);
return 0;
} catch (std::exception&) {
return -1;
}

int sheNegG1(sheCipherTextG1 *y, const sheCipherTextG1 *x)
Expand All @@ -545,12 +530,9 @@ int sheNegGT(sheCipherTextGT *y, const sheCipherTextGT *x)

template<class CT>
int addT(CT& z, const CT& x, const CT& y)
try
{
CT::add(z, x, y);
return 0;
} catch (std::exception&) {
return -1;
}

int sheAddG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const sheCipherTextG1 *y)
Expand All @@ -570,12 +552,9 @@ int sheAddGT(sheCipherTextGT *z, const sheCipherTextGT *x, const sheCipherTextGT

template<class CT>
int subT(CT& z, const CT& x, const CT& y)
try
{
CT::sub(z, x, y);
return 0;
} catch (std::exception&) {
return -1;
}

int sheSubG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const sheCipherTextG1 *y)
Expand All @@ -595,12 +574,9 @@ int sheSubGT(sheCipherTextGT *z, const sheCipherTextGT *x, const sheCipherTextGT

template<class CT1, class CT2, class CT3>
int mulT(CT1& z, const CT2& x, const CT3& y)
try
{
CT1::mul(z, x, y);
return 0;
} catch (std::exception&) {
return -1;
}

int sheMulG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, mclInt y)
Expand Down Expand Up @@ -651,31 +627,22 @@ int sheMul(sheCipherTextGT *z, const sheCipherTextG1 *x, const sheCipherTextG2 *
}

int sheMulML(sheCipherTextGT *z, const sheCipherTextG1 *x, const sheCipherTextG2 *y)
try
{
CipherTextGT::mulML(*cast(z), *cast(x), *cast(y));
return 0;
} catch (std::exception&) {
return -1;
}

int sheFinalExpGT(sheCipherTextGT *y, const sheCipherTextGT *x)
try
{
CipherTextGT::finalExp(*cast(y), *cast(x));
return 0;
} catch (std::exception&) {
return -1;
}

template<class CT>
int reRandT(CT& c, const shePublicKey *pub)
try
{
cast(pub)->reRand(c);
return 0;
} catch (std::exception&) {
return -1;
}

int sheReRandG1(sheCipherTextG1 *c, const shePublicKey *pub)
Expand All @@ -695,12 +662,9 @@ int sheReRandGT(sheCipherTextGT *c, const shePublicKey *pub)

template<class CT>
int convert(sheCipherTextGT *y, const shePublicKey *pub, const CT *x)
try
{
cast(pub)->convert(*cast(y), *cast(x));
return 0;
} catch (std::exception&) {
return -1;
}

int sheConvertG1(sheCipherTextGT *y, const shePublicKey *pub, const sheCipherTextG1 *x)
Expand Down

0 comments on commit a3937c5

Please sign in to comment.