diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-07-21 10:11:03 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-07-21 10:11:03 +0800 |
commit | 74f8c065dd0a56289bfee308b0d5a05ff9f608ff (patch) | |
tree | 300e65950a2a58e826a47fe058549d998a8ae4fe | |
parent | 54f67cadaa6314ee5b87e757491bf00d2270c893 (diff) | |
download | dexon-mcl-74f8c065dd0a56289bfee308b0d5a05ff9f608ff.tar.gz dexon-mcl-74f8c065dd0a56289bfee308b0d5a05ff9f608ff.tar.zst dexon-mcl-74f8c065dd0a56289bfee308b0d5a05ff9f608ff.zip |
add CipherText::mul by int
-rw-r--r-- | include/mcl/bgn.hpp | 46 | ||||
-rw-r--r-- | test/bgn_test.cpp | 8 |
2 files changed, 43 insertions, 11 deletions
diff --git a/include/mcl/bgn.hpp b/include/mcl/bgn.hpp index 1dc9107..66adf0a 100644 --- a/include/mcl/bgn.hpp +++ b/include/mcl/bgn.hpp @@ -346,6 +346,11 @@ private: G::sub(z.S, x.S, y.S); G::sub(z.T, x.T, y.T); } + static void mul(CipherTextAT& z, const CipherTextAT& x, int y) + { + G::mul(z.S, x.S, y); + G::mul(z.T, x.T, y); + } static void neg(CipherTextAT& y, const CipherTextAT& x) { G::neg(y.S, x.S); @@ -756,17 +761,22 @@ public: c1.clear(); c2.clear(); } - static inline void add(CipherTextA& z, const CipherTextA& x, const CipherTextA& y) + static void add(CipherTextA& z, const CipherTextA& x, const CipherTextA& y) { CipherTextG1::add(z.c1, x.c1, y.c1); CipherTextG2::add(z.c2, x.c2, y.c2); } - static inline void sub(CipherTextA& z, const CipherTextA& x, const CipherTextA& y) + static void sub(CipherTextA& z, const CipherTextA& x, const CipherTextA& y) { CipherTextG1::sub(z.c1, x.c1, y.c1); CipherTextG2::sub(z.c2, x.c2, y.c2); } - static inline void neg(CipherTextA& y, const CipherTextA& x) + static void mul(CipherTextA& z, const CipherTextA& x, int y) + { + CipherTextG1::mul(z.c1, x.c1, y); + CipherTextG2::mul(z.c2, x.c2, y); + } + static void neg(CipherTextA& y, const CipherTextA& x) { CipherTextG1::neg(y.c1, x.c1); CipherTextG2::neg(y.c2, x.c2); @@ -824,7 +834,7 @@ public: g[i].setOne(); } } - static inline void add(CipherTextM& z, const CipherTextM& x, const CipherTextM& y) + static void add(CipherTextM& z, const CipherTextM& x, const CipherTextM& y) { /* (g[i]) + (g'[i]) = (g[i] * g'[i]) @@ -833,7 +843,7 @@ public: GT::mul(z.g[i], x.g[i], y.g[i]); } } - static inline void sub(CipherTextM& z, const CipherTextM& x, const CipherTextM& y) + static void sub(CipherTextM& z, const CipherTextM& x, const CipherTextM& y) { /* (g[i]) - (g'[i]) = (g[i] / g'[i]) @@ -844,7 +854,7 @@ public: GT::mul(z.g[i], x.g[i], t); } } - static inline void mul(CipherTextM& z, const CipherTextG1& x, const CipherTextG2& y) + static void mul(CipherTextM& z, const CipherTextG1& x, const CipherTextG2& y) { /* (S1, T1) * (S2, T2) = (e(S1, S2), e(S1, T2), e(T1, S2), e(T1, T2)) @@ -852,15 +862,21 @@ public: */ tensorProduct(z.g, x.S, x.T, y.S, y.T); } - static inline void mul(CipherTextM& z, const CipherTextA& x, const CipherTextA& y) + static void mul(CipherTextM& z, const CipherTextA& x, const CipherTextA& y) { mul(z, x.c1, y.c2); } + static void mul(CipherTextM& z, const CipherTextM& x, int y) + { + for (int i = 0; i < 4; i++) { + GT::pow(z.g[i], x.g[i], y); + } + } void add(const CipherTextM& c) { add(*this, *this, c); } void sub(const CipherTextM& c) { sub(*this, *this, c); } std::istream& readStream(std::istream& is, int ioMode) { - for (size_t i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { g[i].readStream(is, ioMode); } return is; @@ -922,7 +938,7 @@ public: m.clear(); } bool isMultiplied() const { return isMultiplied_; } - static inline void add(CipherText& z, const CipherText& x, const CipherText& y) + static void add(CipherText& z, const CipherText& x, const CipherText& y) { if (x.isMultiplied() && y.isMultiplied()) { z.isMultiplied_ = true; @@ -936,7 +952,7 @@ public: } throw cybozu::Exception("bgn:CipherText:add:mixed CipherText"); } - static inline void sub(CipherText& z, const CipherText& x, const CipherText& y) + static void sub(CipherText& z, const CipherText& x, const CipherText& y) { if (x.isMultiplied() && y.isMultiplied()) { z.isMultiplied_ = true; @@ -950,7 +966,7 @@ public: } throw cybozu::Exception("bgn:CipherText:sub:mixed CipherText"); } - static inline void mul(CipherText& z, const CipherText& x, const CipherText& y) + static void mul(CipherText& z, const CipherText& x, const CipherText& y) { if (x.isMultiplied() || y.isMultiplied()) { throw cybozu::Exception("bgn:CipherText:mul:mixed CipherText"); @@ -958,6 +974,14 @@ public: z.isMultiplied_ = true; CipherTextM::mul(z.m, x.a, y.a); } + static void mul(CipherText& z, const CipherText& x, int y) + { + if (x.isMultiplied()) { + CipherTextM::mul(z.m, x.m, y); + } else { + CipherTextA::mul(z.a, x.a, y); + } + } void add(const CipherText& c) { add(*this, *this, c); } void sub(const CipherText& c) { sub(*this, *this, c); } void mul(const CipherText& c) { mul(*this, *this, c); } diff --git a/test/bgn_test.cpp b/test/bgn_test.cpp index 58338d7..5c81ec9 100644 --- a/test/bgn_test.cpp +++ b/test/bgn_test.cpp @@ -101,11 +101,19 @@ CYBOZU_TEST_AUTO(add_sub_mul) CipherText::sub(c3, c1, c2); CYBOZU_TEST_EQUAL(m1 - m2, sec.dec(c3)); + CipherText::mul(c3, c1, 5); + CYBOZU_TEST_EQUAL(m1 * 5, sec.dec(c3)); + CipherText::mul(c3, c1, -123); + CYBOZU_TEST_EQUAL(m1 * -123, sec.dec(c3)); + CipherText::mul(c3, c1, c2); CYBOZU_TEST_EQUAL(m1 * m2, sec.dec(c3)); pub.rerandomize(c3); CYBOZU_TEST_EQUAL(m1 * m2, sec.dec(c3)); + + CipherText::mul(c3, c3, -25); + CYBOZU_TEST_EQUAL(m1 * m2 * -25, sec.dec(c3)); } } } |