diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-12-06 15:58:49 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-12-06 15:58:49 +0800 |
commit | 3e5adb7c331bb41cc022ffd4bf7e34fb4a9184eb (patch) | |
tree | 855246d574886ef8d1dbc472dab1aec802e968df | |
parent | 506a4790e1d4c0054d437ae4d9919bf8c4192bf2 (diff) | |
download | tangerine-mcl-3e5adb7c331bb41cc022ffd4bf7e34fb4a9184eb.tar.gz tangerine-mcl-3e5adb7c331bb41cc022ffd4bf7e34fb4a9184eb.tar.zst tangerine-mcl-3e5adb7c331bb41cc022ffd4bf7e34fb4a9184eb.zip |
[she] add serialize method
-rw-r--r-- | include/mcl/she.hpp | 54 | ||||
-rw-r--r-- | src/she_c_impl.hpp | 39 |
2 files changed, 69 insertions, 24 deletions
diff --git a/include/mcl/she.hpp b/include/mcl/she.hpp index 95cb9f3..cf7416e 100644 --- a/include/mcl/she.hpp +++ b/include/mcl/she.hpp @@ -431,6 +431,18 @@ private: return S_ == rhs.S_ && T_ == rhs.T_; } bool operator!=(const CipherTextAT& rhs) const { return !operator==(rhs); } + size_t serialize(void *buf, size_t maxBufSize) const + { + std::string s1 = S_.getStr(mcl::IoFixedSizeByteSeq); + std::string s2 = T_.getStr(mcl::IoFixedSizeByteSeq); + if (maxBufSize < s1.size() + s2.size()) { + throw cybozu::Exception("she:CipherTextAT:serialize"); + } + char *p = (char *)buf; + memcpy(p, s1.c_str(), s1.size()); + memcpy(p + s1.size(), s2.c_str(), s2.size()); + return s1.size() + s2.size(); + } }; /* g1 = millerLoop(P1, Q) @@ -663,6 +675,18 @@ public: return x_ == rhs.x_ && y_ == rhs.y_; } bool operator!=(const SecretKey& rhs) const { return !operator==(rhs); } + size_t serialize(void *buf, size_t maxBufSize) const + { + std::string s1 = x_.getStr(mcl::IoFixedSizeByteSeq); + std::string s2 = y_.getStr(mcl::IoFixedSizeByteSeq); + if (maxBufSize < s1.size() + s2.size()) { + throw cybozu::Exception("she:SecretKey:serialize"); + } + char *p = (char *)buf; + memcpy(p, s1.c_str(), s1.size()); + memcpy(p + s1.size(), s2.c_str(), s2.size()); + return s1.size() + s2.size(); + } }; class PublicKey { @@ -905,6 +929,18 @@ public: return xP_ == rhs.xP_ && yQ_ == rhs.yQ_; } bool operator!=(const PublicKey& rhs) const { return !operator==(rhs); } + size_t serialize(void *buf, size_t maxBufSize) const + { + std::string s1 = xP_.getStr(mcl::IoFixedSizeByteSeq); + std::string s2 = yQ_.getStr(mcl::IoFixedSizeByteSeq); + if (maxBufSize < s1.size() + s2.size()) { + throw cybozu::Exception("she:PublicKey:serialize"); + } + char *p = (char *)buf; + memcpy(p, s1.c_str(), s1.size()); + memcpy(p + s1.size(), s2.c_str(), s2.size()); + return s1.size() + s2.size(); + } }; class PrecomputedPublicKey { @@ -1166,6 +1202,24 @@ public: return true; } bool operator!=(const CipherTextGT& rhs) const { return !operator==(rhs); } + size_t serialize(void *buf, size_t maxBufSize) const + { + std::string s[4]; + size_t totalSize = 0; + for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(s); i++) { + g_[i].getStr(s[i], mcl::IoFixedSizeByteSeq); + totalSize += s[i].size(); + } + if (maxBufSize < totalSize) { + throw cybozu::Exception("she:CipherTextGT:serialize"); + } + char *p = (char *)buf; + for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(s); i++) { + memcpy(p, s[i].c_str(), s[i].size()); + p += s[i].size(); + } + return totalSize; + } }; class CipherText { diff --git a/src/she_c_impl.hpp b/src/she_c_impl.hpp index d7b6e31..14df1e5 100644 --- a/src/she_c_impl.hpp +++ b/src/she_c_impl.hpp @@ -79,48 +79,39 @@ int sheInit(int curve, int maxUnitSize) return -1; } +template<class T> +size_t serialize(void *buf, size_t maxBufSize, const T *x) + try +{ + return cast(x)->serialize(buf, maxBufSize); +} catch (std::exception& e) { + fprintf(stderr, "err %s\n", e.what()); + return 0; +} + size_t sheSecretKeySerialize(void *buf, size_t maxBufSize, const sheSecretKey *sec) { - char *p = (char *)buf; - size_t n = mclBnFr_serialize(p, maxBufSize, &sec->x); - if (n == 0) return 0; - return n += mclBnFr_serialize(p + n, maxBufSize - n, &sec->y); + return serialize(buf, maxBufSize, sec); } size_t shePublicKeySerialize(void *buf, size_t maxBufSize, const shePublicKey *pub) { - char *p = (char *)buf; - size_t n = mclBnG1_serialize(p, maxBufSize, &pub->xP); - if (n == 0) return 0; - return n += mclBnG2_serialize(p + n, maxBufSize - n, &pub->yQ); + return serialize(buf, maxBufSize, pub); } size_t sheCipherTextG1Serialize(void *buf, size_t maxBufSize, const sheCipherTextG1 *c) { - char *p = (char *)buf; - size_t n = mclBnG1_serialize(p, maxBufSize, &c->S); - if (n == 0) return 0; - return n += mclBnG1_serialize(p + n, maxBufSize - n, &c->T); + return serialize(buf, maxBufSize, c); } size_t sheCipherTextG2Serialize(void *buf, size_t maxBufSize, const sheCipherTextG2 *c) { - char *p = (char *)buf; - size_t n = mclBnG2_serialize(p, maxBufSize, &c->S); - if (n == 0) return 0; - return n += mclBnG2_serialize(p + n, maxBufSize - n, &c->T); + return serialize(buf, maxBufSize, c); } size_t sheCipherTextGTSerialize(void *buf, size_t maxBufSize, const sheCipherTextGT *c) { - char *p = (char *)buf; - size_t n = 0; - for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(c->g); i++) { - size_t r = mclBnGT_serialize(p + n, maxBufSize - n, &c->g[i]); - if (r == 0) return 0; - n += r; - } - return n; + return serialize(buf, maxBufSize, c); } int sheSecretKeyDeserialize(sheSecretKey* sec, const void *buf, size_t bufSize) |