diff --git a/include/mcl/she.h b/include/mcl/she.h index d474216..84a25a7 100644 --- a/include/mcl/she.h +++ b/include/mcl/she.h @@ -74,6 +74,10 @@ typedef struct { typedef struct { mclBnFr d[7]; } sheZkpBinEq; + +typedef struct { + mclBnFr d[7]; +} sheZkpDec; /* initialize this library call this once before using the other functions @@ -96,6 +100,7 @@ MCLSHE_DLL_API mclSize sheCipherTextGTSerialize(void *buf, mclSize maxBufSize, c MCLSHE_DLL_API mclSize sheZkpBinSerialize(void *buf, mclSize maxBufSize, const sheZkpBin *zkp); 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); // return read byte size if sucess else 0 MCLSHE_DLL_API mclSize sheSecretKeyDeserialize(sheSecretKey* sec, const void *buf, mclSize bufSize); @@ -106,6 +111,7 @@ MCLSHE_DLL_API mclSize sheCipherTextGTDeserialize(sheCipherTextGT* c, const void MCLSHE_DLL_API mclSize sheZkpBinDeserialize(sheZkpBin* zkp, const void *buf, mclSize bufSize); 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); /* set secretKey if system has /dev/urandom or CryptGenRandom @@ -192,6 +198,12 @@ MCLSHE_DLL_API int shePrecomputedPublicKeyEncWithZkpBinEq(sheCipherTextG1 *c1, s MCLSHE_DLL_API int sheEncWithZkpEq(sheCipherTextG1 *c1, sheCipherTextG2 *c2, sheZkpEq *zkp, const shePublicKey *pub, mclInt m); MCLSHE_DLL_API int shePrecomputedPublicKeyEncWithZkpEq(sheCipherTextG1 *c1, sheCipherTextG2 *c2, sheZkpEq *zkp, const shePrecomputedPublicKey *ppub, mclInt m); +/* + Zkp s.t. Dec(c) = m + return 0 if success +*/ +MCLSHE_DLL_API int sheDecWithZkpDecG1(mclInt *m, sheZkpDec *zkp, const sheSecretKey *sec, const sheCipherTextG1 *c, const shePublicKey *pub); + /* decode c and set m return 0 if success @@ -211,6 +223,7 @@ MCLSHE_DLL_API int shePrecomputedPublicKeyVerifyZkpBinG1(const shePrecomputedPub MCLSHE_DLL_API int shePrecomputedPublicKeyVerifyZkpBinG2(const shePrecomputedPublicKey *ppub, const sheCipherTextG2 *c, const sheZkpBin *zkp); 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); /* 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 8dd0a54..6fcb2d3 100644 --- a/src/she_c_impl.hpp +++ b/src/she_c_impl.hpp @@ -41,6 +41,9 @@ static const ZkpEq *cast(const sheZkpEq *p) { return reinterpret_cast(p); } static const ZkpBinEq *cast(const sheZkpBinEq *p) { return reinterpret_cast(p); } +static ZkpDec *cast(sheZkpDec *p) { return reinterpret_cast(p); } +static const ZkpDec *cast(const sheZkpDec *p) { return reinterpret_cast(p); } + int sheInit(int curve, int compiledTimeVar) try { @@ -116,6 +119,11 @@ mclSize sheZkpBinEqSerialize(void *buf, mclSize maxBufSize, const sheZkpBinEq *z return (mclSize)cast(zkp)->serialize(buf, maxBufSize); } +mclSize sheZkpDecSerialize(void *buf, mclSize maxBufSize, const sheZkpDec *zkp) +{ + return (mclSize)cast(zkp)->serialize(buf, maxBufSize); +} + mclSize sheSecretKeyDeserialize(sheSecretKey* sec, const void *buf, mclSize bufSize) { return (mclSize)cast(sec)->deserialize(buf, bufSize); @@ -156,6 +164,11 @@ mclSize sheZkpBinEqDeserialize(sheZkpBinEq* zkp, const void *buf, mclSize bufSiz return (mclSize)cast(zkp)->deserialize(buf, bufSize); } +mclSize sheZkpDecDeserialize(sheZkpDec* zkp, const void *buf, mclSize bufSize) +{ + return (mclSize)cast(zkp)->deserialize(buf, bufSize); +} + int sheSecretKeySetByCSPRNG(sheSecretKey *sec) { cast(sec)->setByCSPRNG(); @@ -768,3 +781,15 @@ int shePrecomputedPublicKeyVerifyZkpBinEq(const shePrecomputedPublicKey *ppub, c return verifyT(*cast(ppub), *cast(c1), *cast(c2), *cast(zkp)); } +int sheDecWithZkpDecG1(mclInt *m, sheZkpDec *zkp, const sheSecretKey *sec, const sheCipherTextG1 *c, const shePublicKey *pub) +{ + bool b; + *m = cast(sec)->decWithZkpDec(&b, *cast(zkp), *cast(c), *cast(pub)); + 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)); +} + diff --git a/test/she_c_test.hpp b/test/she_c_test.hpp index 58139f0..b00b6a2 100644 --- a/test/she_c_test.hpp +++ b/test/she_c_test.hpp @@ -443,6 +443,28 @@ void ZkpEqTest(const sheSecretKey *sec, const PK *pub, encWithZkpFunc encWithZkp } CYBOZU_TEST_AUTO(ZkpEq) +{ + sheSecretKey sec; + sheSecretKeySetByCSPRNG(&sec); + shePublicKey pub; + sheGetPublicKey(&pub, &sec); + int m = 123; + sheCipherTextG1 c1; + sheEncG1(&c1, &pub, m); + sheZkpDec zkp; + int64_t dec; + CYBOZU_TEST_EQUAL(sheDecWithZkpDecG1(&dec, &zkp, &sec, &c1, &pub), 0); + CYBOZU_TEST_EQUAL(m, dec); + CYBOZU_TEST_EQUAL(sheVerifyZkpDecG1(&pub, &c1, m, &zkp), 1); + CYBOZU_TEST_EQUAL(sheVerifyZkpDecG1(&pub, &c1, m + 1, &zkp), 0); + sheCipherTextG1 c2; + sheEncG1(&c2, &pub, m); + CYBOZU_TEST_EQUAL(sheVerifyZkpDecG1(&pub, &c2, m, &zkp), 0); + zkp.d[0].d[0]++; + CYBOZU_TEST_EQUAL(sheVerifyZkpDecG1(&pub, &c1, m, &zkp), 0); +} + +CYBOZU_TEST_AUTO(ZkpDec) { sheSecretKey sec; sheSecretKeySetByCSPRNG(&sec);