diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-12-18 10:36:17 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-12-18 10:36:17 +0800 |
commit | e97429b685a8d0d5bae16a247f503811d3725996 (patch) | |
tree | a4ddc0fa9682ca3e1b1c53f093a24c7830b336b2 | |
parent | 1aeba95effd7b1d174c8bc9b677e2b26be3b79c2 (diff) | |
download | tangerine-mcl-e97429b685a8d0d5bae16a247f503811d3725996.tar.gz tangerine-mcl-e97429b685a8d0d5bae16a247f503811d3725996.tar.zst tangerine-mcl-e97429b685a8d0d5bae16a247f503811d3725996.zip |
[she] change int64_t to INT template
-rw-r--r-- | include/mcl/she.hpp | 53 | ||||
-rw-r--r-- | test/she_test.cpp | 17 |
2 files changed, 50 insertions, 20 deletions
diff --git a/include/mcl/she.hpp b/include/mcl/she.hpp index 917838c..98314cb 100644 --- a/include/mcl/she.hpp +++ b/include/mcl/she.hpp @@ -90,7 +90,8 @@ struct InterfaceForHashTable : G { static void dbl(G& Q, const G& P) { G::dbl(Q, P); } static void neg(G& Q, const G& P) { G::neg(Q, P); } static void add(G& R, const G& P, const G& Q) { G::add(R, P, Q); } - static void mul(G& Q, const G& P, int64_t x) { G::mul(Q, P, x); } + template<class INT> + static void mul(G& Q, const G& P, const INT& x) { G::mul(Q, P, x); } }; /* @@ -113,7 +114,8 @@ struct InterfaceForHashTable<G, false> : G { static void dbl(G& y, const G& x) { G::sqr(y, x); } static void neg(G& Q, const G& P) { G::unitaryInv(Q, P); } static void add(G& z, const G& x, const G& y) { G::mul(z, x, y); } - static void mul(G& z, const G& x, int64_t y) { G::pow(z, x, y); } + template<class INT> + static void mul(G& z, const G& x, const INT& y) { G::pow(z, x, y); } }; /* @@ -382,7 +384,9 @@ 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, int64_t y) + // INT = int64_t or Fr + template<class INT> + static void mul(CipherTextAT& z, const CipherTextAT& x, const INT& y) { G::mul(z.S_, x.S_, y); G::mul(z.T_, x.T_, y); @@ -701,8 +705,8 @@ public: /* (S, T) = (m P + r xP, rP) */ - template<class G, class RG, class I> - static void enc1(G& S, G& T, const G& /*P*/, const G& xP, int64_t m, RG& rg, const mcl::fp::WindowMethod<I>& wm) + template<class G, class INT, class RG, class I> + static void enc1(G& S, G& T, const G& /*P*/, const G& xP, const INT& m, RG& rg, const mcl::fp::WindowMethod<I>& wm) { Fr r; r.setRand(rg); @@ -721,24 +725,28 @@ public: G2::mul(yQ_, Q_, y); } public: - template<class RG> - void enc(CipherTextG1& c, int64_t m, RG& rg) const + /* + you can use INT as int64_t and Fr, + but the return type of dec() is int64_t. + */ + template<class INT, class RG> + void enc(CipherTextG1& c, const INT& m, RG& rg) const { enc1(c.S_, c.T_, P_, xP_, m, rg, PhashTbl_.getWM()); } - template<class RG> - void enc(CipherTextG2& c, int64_t m, RG& rg) const + template<class INT, class RG> + void enc(CipherTextG2& c, const INT& m, RG& rg) const { enc1(c.S_, c.T_, Q_, yQ_, m, rg, QhashTbl_.getWM()); } - template<class RG> - void enc(CipherTextA& c, int64_t m, RG& rg) const + template<class INT, class RG> + void enc(CipherTextA& c, const INT& m, RG& rg) const { enc(c.c1_, m, rg); enc(c.c2_, m, rg); } - template<class RG> - void enc(CipherTextGT& c, int64_t m, RG& rg) const + template<class INT, class RG> + void enc(CipherTextGT& c, const INT& m, RG& rg) const { /* (s, t, u, v) = ((e^x)^a (e^y)^b (e^-xy)^c e^m, e^b, e^a, e^c) @@ -776,8 +784,8 @@ public: GT::pow(c.g_[3], ePQ_, rc); #endif } - template<class RG> - void enc(CipherText& c, int64_t m, RG& rg, bool multiplied = false) const + template<class INT, class RG> + void enc(CipherText& c, const INT& m, RG& rg, bool multiplied = false) const { c.isMultiplied_ = multiplied; if (multiplied) { @@ -786,11 +794,16 @@ public: enc(c.a_, m, rg); } } - void enc(CipherTextG1& c, int64_t m) const { return enc(c, m, local::g_rg); } - void enc(CipherTextG2& c, int64_t m) const { return enc(c, m, local::g_rg); } - void enc(CipherTextA& c, int64_t m) const { return enc(c, m, local::g_rg); } - void enc(CipherTextGT& c, int64_t m) const { return enc(c, m, local::g_rg); } - void enc(CipherText& c, int64_t m, bool multiplied = false) const { return enc(c, m, local::g_rg, multiplied); } + template<class INT> + void enc(CipherTextG1& c, const INT& m) const { return enc(c, m, local::g_rg); } + template<class INT> + void enc(CipherTextG2& c, const INT& m) const { return enc(c, m, local::g_rg); } + template<class INT> + void enc(CipherTextA& c, const INT& m) const { return enc(c, m, local::g_rg); } + template<class INT> + void enc(CipherTextGT& c, const INT& m) const { return enc(c, m, local::g_rg); } + template<class INT> + void enc(CipherText& c, const INT& m, bool multiplied = false) const { return enc(c, m, local::g_rg, multiplied); } /* convert from CipherTextG1 to CipherTextGT */ diff --git a/test/she_test.cpp b/test/she_test.cpp index a96010b..94bb8f4 100644 --- a/test/she_test.cpp +++ b/test/she_test.cpp @@ -156,6 +156,23 @@ CYBOZU_TEST_AUTO(add_sub_mul) } } +CYBOZU_TEST_AUTO(largeEnc) +{ + const SecretKey& sec = g_sec; + PublicKey pub; + sec.getPublicKey(pub); + + cybozu::XorShift rg; + Fr x; + x.setRand(rg); + CipherTextG1 c1, c2; + pub.enc(c1, x); + const int64_t m = 123; + pub.enc(c2, x + m); + CipherTextG1::sub(c1, c1, c2); + CYBOZU_TEST_EQUAL(sec.dec(c1), -m); +} + CYBOZU_TEST_AUTO(add_mul_add_sub) { const SecretKey& sec = g_sec; |