aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2018-02-06 15:23:08 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2018-02-06 15:23:08 +0800
commit199ed712d3056c85369a7e330a779e14feca5cc7 (patch)
tree2ea9c4ffbd5e4ab49de9ff8f3fda131d95998c64
parent8c665543ad4115efcb619db7c977137882635cfd (diff)
downloadtangerine-mcl-199ed712d3056c85369a7e330a779e14feca5cc7.tar.gz
tangerine-mcl-199ed712d3056c85369a7e330a779e14feca5cc7.tar.zst
tangerine-mcl-199ed712d3056c85369a7e330a779e14feca5cc7.zip
[she] add ZkpBin api for c
-rw-r--r--include/mcl/she.h26
-rw-r--r--src/she_c_impl.hpp61
-rw-r--r--test/she_c_test.hpp36
3 files changed, 122 insertions, 1 deletions
diff --git a/include/mcl/she.h b/include/mcl/she.h
index 962f990..1c328bc 100644
--- a/include/mcl/she.h
+++ b/include/mcl/she.h
@@ -45,6 +45,8 @@ typedef struct {
mclBnG2 yQ;
} shePublicKey;
+struct shePrecomputedPublicKey;
+
typedef struct {
mclBnG1 S;
mclBnG1 T;
@@ -59,6 +61,9 @@ typedef struct {
mclBnGT g[4];
} sheCipherTextGT;
+typedef struct {
+ mclBnFr d[4];
+} sheZkpBin;
/*
initialize this library
call this once before using the other functions
@@ -138,6 +143,12 @@ MCLSHE_DLL_API int sheEncG2(sheCipherTextG2 *c, const shePublicKey *pub, mclInt
MCLSHE_DLL_API int sheEncGT(sheCipherTextGT *c, const shePublicKey *pub, mclInt m);
/*
+ m must be 0 or 1
+*/
+MCLSHE_DLL_API int sheEncWithZkpBinG1(sheCipherTextG1 *c, sheZkpBin *zkp, const shePublicKey *pub, int m);
+MCLSHE_DLL_API int sheEncWithZkpBinG2(sheCipherTextG2 *c, sheZkpBin *zkp, const shePublicKey *pub, int m);
+
+/*
decode c and set m
return 0 if success
*/
@@ -145,6 +156,14 @@ MCLSHE_DLL_API int sheDecG1(mclInt *m, const sheSecretKey *sec, const sheCipherT
MCLSHE_DLL_API int sheDecG2(mclInt *m, const sheSecretKey *sec, const sheCipherTextG2 *c);
MCLSHE_DLL_API int sheDecGT(mclInt *m, const sheSecretKey *sec, const sheCipherTextGT *c);
/*
+ verify zkp
+ return 1 if valid
+*/
+MCLSHE_DLL_API int sheVerifyZkpBinG1(const shePublicKey *pub, const sheCipherTextG1 *c, const sheZkpBin *zkp);
+MCLSHE_DLL_API int sheVerifyZkpBinG2(const shePublicKey *pub, const sheCipherTextG2 *c, const sheZkpBin *zkp);
+MCLSHE_DLL_API int shePrecomputedPublicKeyVerifyZkpBinG1(const shePrecomputedPublicKey *ppub, const sheCipherTextG1 *c, const sheZkpBin *zkp);
+MCLSHE_DLL_API int shePrecomputedPublicKeyVerifyZkpBinG2(const shePrecomputedPublicKey *ppub, const sheCipherTextG2 *c, const sheZkpBin *zkp);
+/*
decode c via GT and set m
return 0 if success
*/
@@ -198,7 +217,6 @@ MCLSHE_DLL_API int sheReRandGT(sheCipherTextGT *c, const shePublicKey *pub);
MCLSHE_DLL_API int sheConvertG1(sheCipherTextGT *y, const shePublicKey *pub, const sheCipherTextG1 *x);
MCLSHE_DLL_API int sheConvertG2(sheCipherTextGT *y, const shePublicKey *pub, const sheCipherTextG2 *x);
-struct shePrecomputedPublicKey;
MCLSHE_DLL_API shePrecomputedPublicKey *shePrecomputedPublicKeyCreate();
MCLSHE_DLL_API void shePrecomputedPublicKeyDestroy(shePrecomputedPublicKey *ppub);
// return 0 if success
@@ -207,6 +225,12 @@ MCLSHE_DLL_API int shePrecomputedPublicKeyEncG1(sheCipherTextG1 *c, const shePre
MCLSHE_DLL_API int shePrecomputedPublicKeyEncG2(sheCipherTextG2 *c, const shePrecomputedPublicKey *ppub, mclInt m);
MCLSHE_DLL_API int shePrecomputedPublicKeyEncGT(sheCipherTextGT *c, const shePrecomputedPublicKey *ppub, mclInt m);
+/*
+ m must be 0 or 1
+*/
+MCLSHE_DLL_API int shePrecomputedPublicKeyEncWithZkpBinG1(sheCipherTextG1 *c, sheZkpBin *zkp, const shePrecomputedPublicKey *ppub, int m);
+MCLSHE_DLL_API int shePrecomputedPublicKeyEncWithZkpBinG2(sheCipherTextG2 *c, sheZkpBin *zkp, const shePrecomputedPublicKey *ppub, int m);
+
#ifdef __cplusplus
}
#endif
diff --git a/src/she_c_impl.hpp b/src/she_c_impl.hpp
index 9d29c67..d9a6593 100644
--- a/src/she_c_impl.hpp
+++ b/src/she_c_impl.hpp
@@ -39,6 +39,9 @@ static const CipherTextG2 *cast(const sheCipherTextG2 *p) { return reinterpret_c
static CipherTextGT *cast(sheCipherTextGT *p) { return reinterpret_cast<CipherTextGT*>(p); }
static const CipherTextGT *cast(const sheCipherTextGT *p) { return reinterpret_cast<const CipherTextGT*>(p); }
+static ZkpBin *cast(sheZkpBin *p) { return reinterpret_cast<ZkpBin*>(p); }
+static const ZkpBin *cast(const sheZkpBin *p) { return reinterpret_cast<const ZkpBin*>(p); }
+
int sheInit(int curve, int maxUnitSize)
try
{
@@ -278,6 +281,37 @@ int sheEncGT(sheCipherTextGT *c, const shePublicKey *pub, mclInt m)
return encT(c, pub, m);
}
+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& e) {
+ fprintf(stderr, "err %s\n", e.what());
+ return -1;
+}
+
+int sheEncWithZkpBinG1(sheCipherTextG1 *c, sheZkpBin *zkp, const shePublicKey *pub, int m)
+{
+ return encWithZkpBinT(c, zkp, pub, m);
+}
+
+int sheEncWithZkpBinG2(sheCipherTextG2 *c, sheZkpBin *zkp, const shePublicKey *pub, int m)
+{
+ return encWithZkpBinT(c, zkp, pub, m);
+}
+
+int shePrecomputedPublicKeyEncWithZkpBinG1(sheCipherTextG1 *c, sheZkpBin *zkp, const shePrecomputedPublicKey *pub, int m)
+{
+ return encWithZkpBinT(c, zkp, pub, m);
+}
+
+int shePrecomputedPublicKeyEncWithZkpBinG2(sheCipherTextG2 *c, sheZkpBin *zkp, const shePrecomputedPublicKey *pub, int m)
+{
+ return encWithZkpBinT(c, zkp, pub, m);
+}
+
template<class CT>
int decT(mclInt *m, const sheSecretKey *sec, const CT *c)
try
@@ -547,3 +581,30 @@ int shePrecomputedPublicKeyEncGT(sheCipherTextGT *c, const shePrecomputedPublicK
{
return pEncT(c, pub, m);
}
+
+template<class PK, class CT>
+int verifyT(const PK& pub, const CT& c, const ZkpBin& zkp)
+ try
+{
+ return pub.verify(c, zkp);
+} catch (std::exception& e) {
+ fprintf(stderr, "err %s\n", e.what());
+ return 0;
+}
+
+int sheVerifyZkpBinG1(const shePublicKey *pub, const sheCipherTextG1 *c, const sheZkpBin *zkp)
+{
+ return verifyT(*cast(pub), *cast(c), *cast(zkp));
+}
+int sheVerifyZkpBinG2(const shePublicKey *pub, const sheCipherTextG2 *c, const sheZkpBin *zkp)
+{
+ return verifyT(*cast(pub), *cast(c), *cast(zkp));
+}
+int shePrecomputedPublicKeyVerifyZkpBinG1(const shePrecomputedPublicKey *pub, const sheCipherTextG1 *c, const sheZkpBin *zkp)
+{
+ return verifyT(*cast(pub), *cast(c), *cast(zkp));
+}
+int shePrecomputedPublicKeyVerifyZkpBinG2(const shePrecomputedPublicKey *pub, const sheCipherTextG2 *c, const sheZkpBin *zkp)
+{
+ return verifyT(*cast(pub), *cast(c), *cast(zkp));
+}
diff --git a/test/she_c_test.hpp b/test/she_c_test.hpp
index 651fd04..01fafa6 100644
--- a/test/she_c_test.hpp
+++ b/test/she_c_test.hpp
@@ -272,6 +272,42 @@ CYBOZU_TEST_AUTO(precomputed)
shePrecomputedPublicKeyDestroy(ppub);
}
+template<class CT, class PK, class encWithZkpFunc, class decFunc, class verifyFunc>
+void ZkpBinTest(const sheSecretKey *sec, const PK *pub, encWithZkpFunc encWithZkp, decFunc dec, verifyFunc verify)
+{
+ CT c;
+ sheZkpBin zkp;
+ for (int m = 0; m < 2; m++) {
+ CYBOZU_TEST_EQUAL(encWithZkp(&c, &zkp, pub, m), 0);
+ mclInt mDec;
+ CYBOZU_TEST_EQUAL(dec(&mDec, sec, &c), 0);
+ CYBOZU_TEST_EQUAL(mDec, m);
+ CYBOZU_TEST_EQUAL(verify(pub, &c, &zkp), 1);
+ zkp.d[0].d[0]++;
+ CYBOZU_TEST_EQUAL(verify(pub, &c, &zkp), 0);
+ }
+ CYBOZU_TEST_ASSERT(encWithZkp(&c, &zkp, pub, 2) != 0);
+}
+
+CYBOZU_TEST_AUTO(ZkpBin)
+{
+ sheSecretKey sec;
+ sheSecretKeySetByCSPRNG(&sec);
+ shePublicKey pub;
+ sheGetPublicKey(&pub, &sec);
+
+ ZkpBinTest<sheCipherTextG1>(&sec, &pub, sheEncWithZkpBinG1, sheDecG1, sheVerifyZkpBinG1);
+ ZkpBinTest<sheCipherTextG2>(&sec, &pub, sheEncWithZkpBinG2, sheDecG2, sheVerifyZkpBinG2);
+
+ shePrecomputedPublicKey *ppub = shePrecomputedPublicKeyCreate();
+ CYBOZU_TEST_EQUAL(shePrecomputedPublicKeyInit(ppub, &pub), 0);
+
+ ZkpBinTest<sheCipherTextG1>(&sec, ppub, shePrecomputedPublicKeyEncWithZkpBinG1, sheDecG1, shePrecomputedPublicKeyVerifyZkpBinG1);
+ ZkpBinTest<sheCipherTextG2>(&sec, ppub, shePrecomputedPublicKeyEncWithZkpBinG2, sheDecG2, shePrecomputedPublicKeyVerifyZkpBinG2);
+
+ shePrecomputedPublicKeyDestroy(ppub);
+}
+
CYBOZU_TEST_AUTO(finalExp)
{
sheSecretKey sec;