diff --git a/include/mcl/she.h b/include/mcl/she.h index 093c7a0..20b88c4 100644 --- a/include/mcl/she.h +++ b/include/mcl/she.h @@ -119,6 +119,13 @@ MCLSHE_DLL_API int sheDecG1(int64_t *m, const sheSecretKey *sec, const sheCipher MCLSHE_DLL_API int sheDecG2(int64_t *m, const sheSecretKey *sec, const sheCipherTextG2 *c); MCLSHE_DLL_API int sheDecGT(int64_t *m, const sheSecretKey *sec, const sheCipherTextGT *c); +/* + return 1 if dec(c) == 0 +*/ +MCLSHE_DLL_API int sheIsZeroG1(const sheSecretKey *sec, const sheCipherTextG1 *c); +MCLSHE_DLL_API int sheIsZeroG2(const sheSecretKey *sec, const sheCipherTextG2 *c); +MCLSHE_DLL_API int sheIsZeroGT(const sheSecretKey *sec, const sheCipherTextGT *c); + // return 0 if success // z = x + y MCLSHE_DLL_API int sheAddG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const sheCipherTextG1 *y); diff --git a/src/she_c_impl.hpp b/src/she_c_impl.hpp index f7ea627..1eb676f 100644 --- a/src/she_c_impl.hpp +++ b/src/she_c_impl.hpp @@ -274,6 +274,30 @@ int sheDecGT(int64_t *m, const sheSecretKey *sec, const sheCipherTextGT *c) return decT(m, sec, c); } +template +int isZeroT(const sheSecretKey *sec, const CT *c) + try +{ + return cast(sec)->isZero(*cast(c)); +} catch (std::exception& e) { + fprintf(stderr, "err %s\n", e.what()); + return 0; +} + +int sheIsZeroG1(const sheSecretKey *sec, const sheCipherTextG1 *c) +{ + return isZeroT(sec, c); +} +int sheIsZeroG2(const sheSecretKey *sec, const sheCipherTextG2 *c) +{ + return isZeroT(sec, c); +} +int sheIsZeroGT(const sheSecretKey *sec, const sheCipherTextGT *c) +{ + return isZeroT(sec, c); +} + + template int addT(CT& z, const CT& x, const CT& y) try diff --git a/test/she_c_test.hpp b/test/she_c_test.hpp index 84b171e..8a8a2a0 100644 --- a/test/she_c_test.hpp +++ b/test/she_c_test.hpp @@ -30,15 +30,30 @@ CYBOZU_TEST_AUTO(encDec) int64_t m = 123; sheCipherTextG1 c1; + sheCipherTextG2 c2; sheCipherTextGT ct; sheEncG1(&c1, &pub, m); + sheEncG2(&c2, &pub, m); sheEncGT(&ct, &pub, m); int64_t dec; CYBOZU_TEST_EQUAL(sheDecG1(&dec, &sec, &c1), 0); CYBOZU_TEST_EQUAL(dec, m); + dec = 0; + CYBOZU_TEST_EQUAL(sheDecG2(&dec, &sec, &c2), 0); + CYBOZU_TEST_EQUAL(dec, m); + dec = 0; CYBOZU_TEST_EQUAL(sheDecGT(&dec, &sec, &ct), 0); CYBOZU_TEST_EQUAL(dec, m); + + for (int m = -3; m < 3; m++) { + sheEncG1(&c1, &pub, m); + CYBOZU_TEST_EQUAL(sheIsZeroG1(&sec, &c1), m == 0); + sheEncG2(&c2, &pub, m); + CYBOZU_TEST_EQUAL(sheIsZeroG2(&sec, &c2), m == 0); + sheEncGT(&ct, &pub, m); + CYBOZU_TEST_EQUAL(sheIsZeroGT(&sec, &ct), m == 0); + } } CYBOZU_TEST_AUTO(addMul)