diff --git a/include/mcl/she.h b/include/mcl/she.h index 8a17150..c15fb0b 100644 --- a/include/mcl/she.h +++ b/include/mcl/she.h @@ -78,6 +78,14 @@ typedef struct { typedef struct { mclBnFr d[2]; } sheZkpDec; + +typedef struct { + mclBnGT d[4]; +} sheAuxiliaryForZkpDecGT; + +typedef struct { + mclBnFr d[4]; +} sheZkpDecGT; /* initialize this library call this once before using the other functions @@ -101,6 +109,7 @@ MCLSHE_DLL_API mclSize sheZkpBinSerialize(void *buf, mclSize maxBufSize, const s MCLSHE_DLL_API mclSize sheZkpEqSerialize(void *buf, mclSize maxBufSize, const sheZkpEq *zkp); MCLSHE_DLL_API mclSize sheZkpBinEqSerialize(void *buf, mclSize maxBufSize, const sheZkpBinEq *zkp); MCLSHE_DLL_API mclSize sheZkpDecSerialize(void *buf, mclSize maxBufSize, const sheZkpDec *zkp); +MCLSHE_DLL_API mclSize sheZkpDecGTSerialize(void *buf, mclSize maxBufSize, const sheZkpDecGT *zkp); // return read byte size if sucess else 0 MCLSHE_DLL_API mclSize sheSecretKeyDeserialize(sheSecretKey* sec, const void *buf, mclSize bufSize); @@ -112,6 +121,7 @@ MCLSHE_DLL_API mclSize sheZkpBinDeserialize(sheZkpBin* zkp, const void *buf, mcl MCLSHE_DLL_API mclSize sheZkpEqDeserialize(sheZkpEq* zkp, const void *buf, mclSize bufSize); MCLSHE_DLL_API mclSize sheZkpBinEqDeserialize(sheZkpBinEq* zkp, const void *buf, mclSize bufSize); MCLSHE_DLL_API mclSize sheZkpDecDeserialize(sheZkpDec* zkp, const void *buf, mclSize bufSize); +MCLSHE_DLL_API mclSize sheZkpDecGTDeserialize(sheZkpDecGT* zkp, const void *buf, mclSize bufSize); /* set secretKey if system has /dev/urandom or CryptGenRandom @@ -121,6 +131,8 @@ MCLSHE_DLL_API int sheSecretKeySetByCSPRNG(sheSecretKey *sec); MCLSHE_DLL_API void sheGetPublicKey(shePublicKey *pub, const sheSecretKey *sec); +MCLSHE_DLL_API void sheGetAuxiliaryForZkpDecGT(sheAuxiliaryForZkpDecGT *aux, const shePublicKey *pub); + /* make table to decode DLP return 0 if success @@ -203,6 +215,7 @@ MCLSHE_DLL_API int shePrecomputedPublicKeyEncWithZkpEq(sheCipherTextG1 *c1, sheC return 0 if success */ MCLSHE_DLL_API int sheDecWithZkpDecG1(mclInt *m, sheZkpDec *zkp, const sheSecretKey *sec, const sheCipherTextG1 *c, const shePublicKey *pub); +MCLSHE_DLL_API int sheDecWithZkpDecGT(mclInt *m, sheZkpDecGT *zkp, const sheSecretKey *sec, const sheCipherTextGT *c, const sheAuxiliaryForZkpDecGT *aux); /* decode c and set m @@ -224,6 +237,7 @@ MCLSHE_DLL_API int shePrecomputedPublicKeyVerifyZkpBinG2(const shePrecomputedPub MCLSHE_DLL_API int shePrecomputedPublicKeyVerifyZkpEq(const shePrecomputedPublicKey *ppub, const sheCipherTextG1 *c1, const sheCipherTextG2 *c2, const sheZkpEq *zkp); MCLSHE_DLL_API int shePrecomputedPublicKeyVerifyZkpBinEq(const shePrecomputedPublicKey *ppub, const sheCipherTextG1 *c1, const sheCipherTextG2 *c2, const sheZkpBinEq *zkp); MCLSHE_DLL_API int sheVerifyZkpDecG1(const shePublicKey *pub, const sheCipherTextG1 *c1, mclInt m, const sheZkpDec *zkp); +MCLSHE_DLL_API int sheVerifyZkpDecGT(const sheAuxiliaryForZkpDecGT *aux, const sheCipherTextGT *ct, mclInt m, const sheZkpDecGT *zkp); /* decode c via GT and set m return 0 if success diff --git a/src/she_c_impl.hpp b/src/she_c_impl.hpp index 6fcb2d3..2878163 100644 --- a/src/she_c_impl.hpp +++ b/src/she_c_impl.hpp @@ -44,6 +44,12 @@ static const ZkpBinEq *cast(const sheZkpBinEq *p) { return reinterpret_cast(p); } static const ZkpDec *cast(const sheZkpDec *p) { return reinterpret_cast(p); } +static AuxiliaryForZkpDecGT *cast(sheAuxiliaryForZkpDecGT *p) { return reinterpret_cast(p); } +static const AuxiliaryForZkpDecGT *cast(const sheAuxiliaryForZkpDecGT *p) { return reinterpret_cast(p); } + +static ZkpDecGT *cast(sheZkpDecGT *p) { return reinterpret_cast(p); } +static const ZkpDecGT *cast(const sheZkpDecGT *p) { return reinterpret_cast(p); } + int sheInit(int curve, int compiledTimeVar) try { @@ -124,6 +130,11 @@ mclSize sheZkpDecSerialize(void *buf, mclSize maxBufSize, const sheZkpDec *zkp) return (mclSize)cast(zkp)->serialize(buf, maxBufSize); } +mclSize sheZkpGTDecSerialize(void *buf, mclSize maxBufSize, const sheZkpDecGT *zkp) +{ + return (mclSize)cast(zkp)->serialize(buf, maxBufSize); +} + mclSize sheSecretKeyDeserialize(sheSecretKey* sec, const void *buf, mclSize bufSize) { return (mclSize)cast(sec)->deserialize(buf, bufSize); @@ -169,6 +180,11 @@ mclSize sheZkpDecDeserialize(sheZkpDec* zkp, const void *buf, mclSize bufSize) return (mclSize)cast(zkp)->deserialize(buf, bufSize); } +mclSize sheZkpDecGTDeserialize(sheZkpDecGT* zkp, const void *buf, mclSize bufSize) +{ + return (mclSize)cast(zkp)->deserialize(buf, bufSize); +} + int sheSecretKeySetByCSPRNG(sheSecretKey *sec) { cast(sec)->setByCSPRNG(); @@ -180,6 +196,11 @@ void sheGetPublicKey(shePublicKey *pub, const sheSecretKey *sec) cast(sec)->getPublicKey(*cast(pub)); } +void sheGetAuxiliaryForZkpDecGT(sheAuxiliaryForZkpDecGT *aux, const shePublicKey *pub) +{ + cast(pub)->getAuxiliaryForZkpDecGT(*cast(aux)); +} + static int wrapSetRangeForDLP(void f(size_t), mclSize hashSize) try { @@ -788,8 +809,20 @@ int sheDecWithZkpDecG1(mclInt *m, sheZkpDec *zkp, const sheSecretKey *sec, const return b ? 0 : -1; } +int sheDecWithZkpDecGT(mclInt *m, sheZkpDecGT *zkp, const sheSecretKey *sec, const sheCipherTextGT *c, const sheAuxiliaryForZkpDecGT *aux) +{ + bool b; + *m = cast(sec)->decWithZkpDec(&b, *cast(zkp), *cast(c), *cast(aux)); + return b ? 0 : -1; +} + int sheVerifyZkpDecG1(const shePublicKey *pub, const sheCipherTextG1 *c1, mclInt m, const sheZkpDec *zkp) { return cast(pub)->verify(*cast(c1), m, *cast(zkp)); } +int sheVerifyZkpDecGT(const sheAuxiliaryForZkpDecGT *aux, const sheCipherTextGT *ct, mclInt m, const sheZkpDecGT *zkp) +{ + return cast(aux)->verify(*cast(ct), m, *cast(zkp)); +} + diff --git a/test/she_c_test.hpp b/test/she_c_test.hpp index b00b6a2..9f48942 100644 --- a/test/she_c_test.hpp +++ b/test/she_c_test.hpp @@ -442,7 +442,7 @@ void ZkpEqTest(const sheSecretKey *sec, const PK *pub, encWithZkpFunc encWithZkp } } -CYBOZU_TEST_AUTO(ZkpEq) +CYBOZU_TEST_AUTO(ZkpDecG1) { sheSecretKey sec; sheSecretKeySetByCSPRNG(&sec); @@ -464,7 +464,31 @@ CYBOZU_TEST_AUTO(ZkpEq) CYBOZU_TEST_EQUAL(sheVerifyZkpDecG1(&pub, &c1, m, &zkp), 0); } -CYBOZU_TEST_AUTO(ZkpDec) +CYBOZU_TEST_AUTO(ZkpDecGT) +{ + sheSecretKey sec; + sheSecretKeySetByCSPRNG(&sec); + shePublicKey pub; + sheGetPublicKey(&pub, &sec); + sheAuxiliaryForZkpDecGT aux; + sheGetAuxiliaryForZkpDecGT(&aux, &pub); + int m = 123; + sheCipherTextGT c1; + sheEncGT(&c1, &pub, m); + sheZkpDecGT zkp; + int64_t dec; + CYBOZU_TEST_EQUAL(sheDecWithZkpDecGT(&dec, &zkp, &sec, &c1, &aux), 0); + CYBOZU_TEST_EQUAL(m, dec); + CYBOZU_TEST_EQUAL(sheVerifyZkpDecGT(&aux, &c1, m, &zkp), 1); + CYBOZU_TEST_EQUAL(sheVerifyZkpDecGT(&aux, &c1, m + 1, &zkp), 0); + sheCipherTextGT c2; + sheEncGT(&c2, &pub, m); + CYBOZU_TEST_EQUAL(sheVerifyZkpDecGT(&aux, &c2, m, &zkp), 0); + zkp.d[0].d[0]++; + CYBOZU_TEST_EQUAL(sheVerifyZkpDecGT(&aux, &c1, m, &zkp), 0); +} + +CYBOZU_TEST_AUTO(ZkpEq) { sheSecretKey sec; sheSecretKeySetByCSPRNG(&sec);