diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2018-04-17 10:07:23 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2018-04-17 10:07:23 +0800 |
commit | 09ee2e1ab22d30790e5c8bc87e7f63c63650c0a7 (patch) | |
tree | 7815058b33734089991e3f5352fa8171b867cb6d | |
parent | cfc6586a58b8af0770fa011fbbda453875f0a444 (diff) | |
download | tangerine-mcl-09ee2e1ab22d30790e5c8bc87e7f63c63650c0a7.tar.gz tangerine-mcl-09ee2e1ab22d30790e5c8bc87e7f63c63650c0a7.tar.zst tangerine-mcl-09ee2e1ab22d30790e5c8bc87e7f63c63650c0a7.zip |
add CipherText::neg
-rw-r--r-- | include/mcl/she.h | 6 | ||||
-rw-r--r-- | include/mcl/she.hpp | 6 | ||||
-rw-r--r-- | src/she_c_impl.hpp | 27 | ||||
-rw-r--r-- | test/she_c_test.hpp | 15 |
4 files changed, 53 insertions, 1 deletions
diff --git a/include/mcl/she.h b/include/mcl/she.h index 6c11050..9d6a2f7 100644 --- a/include/mcl/she.h +++ b/include/mcl/she.h @@ -180,6 +180,12 @@ 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 +// y = -x +MCLSHE_DLL_API int sheNegG1(sheCipherTextG1 *y, const sheCipherTextG1 *x); +MCLSHE_DLL_API int sheNegG2(sheCipherTextG2 *y, const sheCipherTextG2 *x); +MCLSHE_DLL_API int sheNegGT(sheCipherTextGT *y, const sheCipherTextGT *x); + +// return 0 if success // z = x + y MCLSHE_DLL_API int sheAddG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const sheCipherTextG1 *y); MCLSHE_DLL_API int sheAddG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, const sheCipherTextG2 *y); diff --git a/include/mcl/she.hpp b/include/mcl/she.hpp index 485412f..d9ba5d4 100644 --- a/include/mcl/she.hpp +++ b/include/mcl/she.hpp @@ -1253,6 +1253,12 @@ public: g_[i].setOne(); } } + static void neg(CipherTextGT& y, const CipherTextGT& x) + { + for (int i = 0; i < 4; i++) { + GT::unitaryInv(y.g_[i], x.g_[i]); + } + } static void add(CipherTextGT& z, const CipherTextGT& x, const CipherTextGT& y) { /* diff --git a/src/she_c_impl.hpp b/src/she_c_impl.hpp index 49e5447..cdb8932 100644 --- a/src/she_c_impl.hpp +++ b/src/she_c_impl.hpp @@ -421,6 +421,33 @@ int sheIsZeroGT(const sheSecretKey *sec, const sheCipherTextGT *c) return isZeroT(sec, c); } +template<class CT> +int negT(CT& y, const CT& x) + try +{ + CT::neg(y, x); + return 0; +} catch (std::exception& e) { +#ifndef NDEBUG + fprintf(stderr, "err %s\n", e.what()); +#endif + return -1; +} + +int sheNegG1(sheCipherTextG1 *y, const sheCipherTextG1 *x) +{ + return negT(*cast(y), *cast(x)); +} + +int sheNegG2(sheCipherTextG2 *y, const sheCipherTextG2 *x) +{ + return negT(*cast(y), *cast(x)); +} + +int sheNegGT(sheCipherTextGT *y, const sheCipherTextGT *x) +{ + return negT(*cast(y), *cast(x)); +} template<class CT> int addT(CT& z, const CT& x, const CT& y) diff --git a/test/she_c_test.hpp b/test/she_c_test.hpp index e9dd71a..91d6775 100644 --- a/test/she_c_test.hpp +++ b/test/she_c_test.hpp @@ -97,15 +97,24 @@ CYBOZU_TEST_AUTO(allOp) int64_t m2 = -9; int64_t m3 = 12; int64_t m4 = -9; + int64_t dec; sheCipherTextG1 c11, c12; sheCipherTextG2 c21, c22; sheCipherTextGT ct; + sheEncG1(&c11, &pub, m1); + sheNegG1(&c12, &c11); + CYBOZU_TEST_EQUAL(sheDecG1(&dec, &sec, &c12), 0); + CYBOZU_TEST_EQUAL(dec, -m1); + sheEncG1(&c12, &pub, m2); sheSubG1(&c11, &c11, &c12); // m1 - m2 sheMulG1(&c11, &c11, 4); // 4 * (m1 - m2) sheEncG2(&c21, &pub, m3); + sheNegG2(&c22, &c21); + CYBOZU_TEST_EQUAL(sheDecG2(&dec, &sec, &c22), 0); + CYBOZU_TEST_EQUAL(dec, -m3); sheEncG2(&c22, &pub, m4); sheSubG2(&c21, &c21, &c22); // m3 - m4 sheMulG2(&c21, &c21, -5); // -5 * (m3 - m4) @@ -114,9 +123,13 @@ CYBOZU_TEST_AUTO(allOp) sheMulGT(&ct, &ct, -4); // 160 * (m1 - m2) * (m3 - m4) int64_t t = 160 * (m1 - m2) * (m3 - m4); - int64_t dec; CYBOZU_TEST_EQUAL(sheDecGT(&dec, &sec, &ct), 0); CYBOZU_TEST_EQUAL(dec, t); + + sheCipherTextGT ct2; + sheNegGT(&ct2, &ct); + CYBOZU_TEST_EQUAL(sheDecGT(&dec, &sec, &ct2), 0); + CYBOZU_TEST_EQUAL(dec, -t); } CYBOZU_TEST_AUTO(rerand) |