diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2018-05-23 11:14:13 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2018-05-23 11:14:13 +0800 |
commit | 9a99be69ea538c6b88cd25cffd3a5cd396062331 (patch) | |
tree | 922975fe57159b8f0170bda912e68b39352a7d6c | |
parent | bfe5f063c0f0c33eacf9057ca36944ba7dc0417a (diff) | |
download | tangerine-mcl-9a99be69ea538c6b88cd25cffd3a5cd396062331.tar.gz tangerine-mcl-9a99be69ea538c6b88cd25cffd3a5cd396062331.tar.zst tangerine-mcl-9a99be69ea538c6b88cd25cffd3a5cd396062331.zip |
serialize/deserialize use save/load wo exception
-rw-r--r-- | include/mcl/ecdsa.hpp | 29 | ||||
-rw-r--r-- | include/mcl/operator.hpp | 14 | ||||
-rw-r--r-- | include/mcl/she.hpp | 211 |
3 files changed, 198 insertions, 56 deletions
diff --git a/include/mcl/ecdsa.hpp b/include/mcl/ecdsa.hpp index 7c3bbc0..69adfc5 100644 --- a/include/mcl/ecdsa.hpp +++ b/include/mcl/ecdsa.hpp @@ -112,18 +112,35 @@ inline void getPublicKey(PublicKey& pub, const SecretKey& sec) struct Signature : public mcl::fp::Serializable<Signature> { Zn r, s; template<class InputStream> + void load(bool *pb, InputStream& is, int ioMode = IoSerialize) + { + r.load(pb, is, ioMode); if (!*pb) return; + s.load(pb, is, ioMode); + } + template<class OutputStream> + void save(bool *pb, OutputStream& os, int ioMode = IoSerialize) const + { + const char sep = *fp::getIoSeparator(ioMode); + r.save(pb, os, ioMode); if (!*pb) return; + if (sep) { + cybozu::writeChar(pb, os, sep); + if (!*pb) return; + } + s.save(pb, os, ioMode); + } + template<class InputStream> void load(InputStream& is, int ioMode = IoSerialize) { - r.load(is, ioMode); - s.load(is, ioMode); + bool b; + load(&b, is, ioMode); + if (!b) throw cybozu::Exception("ecdsa:Signature:load"); } template<class OutputStream> void save(OutputStream& os, int ioMode = IoSerialize) const { - const char sep = *fp::getIoSeparator(ioMode); - r.save(os, ioMode); - if (sep) cybozu::writeChar(os, sep); - s.save(os, ioMode); + bool b; + save(&b, os, ioMode); + if (!b) throw cybozu::Exception("ecdsa:Signature:save"); } friend std::istream& operator>>(std::istream& is, Signature& self) { diff --git a/include/mcl/operator.hpp b/include/mcl/operator.hpp index 19b7956..227d94e 100644 --- a/include/mcl/operator.hpp +++ b/include/mcl/operator.hpp @@ -140,18 +140,20 @@ struct Serializable : public E { return str; } // return written bytes - size_t serialize(void *buf, size_t maxBufSize) const + size_t serialize(void *buf, size_t maxBufSize, int ioMode = IoSerialize) const { cybozu::MemoryOutputStream os(buf, maxBufSize); - static_cast<const T&>(*this).save(os); - return os.getPos(); + bool b; + static_cast<const T&>(*this).save(&b, os, ioMode); + return b ? os.getPos() : 0; } // return read bytes - size_t deserialize(const void *buf, size_t bufSize) + size_t deserialize(const void *buf, size_t bufSize, int ioMode = IoSerialize) { cybozu::MemoryInputStream is(buf, bufSize); - static_cast<T&>(*this).load(is); - return is.getPos(); + bool b; + static_cast<T&>(*this).load(&b, is, ioMode); + return b ? is.getPos() : 0; } }; diff --git a/include/mcl/she.hpp b/include/mcl/she.hpp index 99ea9db..e97059b 100644 --- a/include/mcl/she.hpp +++ b/include/mcl/she.hpp @@ -403,18 +403,35 @@ private: void add(const CipherTextAT& c) { add(*this, *this, c); } void sub(const CipherTextAT& c) { sub(*this, *this, c); } template<class InputStream> + void load(bool *pb, InputStream& is, int ioMode = IoSerialize) + { + S_.load(pb, is, ioMode); if (!*pb) return; + T_.load(pb, is, ioMode); + } + template<class OutputStream> + void save(bool *pb, OutputStream& os, int ioMode = IoSerialize) const + { + const char sep = *fp::getIoSeparator(ioMode); + S_.save(pb, os, ioMode); if (!*pb) return; + if (sep) { + cybozu::writeChar(pb, os, sep); + if (!*pb) return; + } + T_.save(pb, os, ioMode); + } + template<class InputStream> void load(InputStream& is, int ioMode = IoSerialize) { - S_.load(is, ioMode); - T_.load(is, ioMode); + bool b; + load(&b, is, ioMode); + if (!b) throw cybozu::Exception("she:CipherTextA:load"); } template<class OutputStream> void save(OutputStream& os, int ioMode = IoSerialize) const { - const char sep = *fp::getIoSeparator(ioMode); - S_.save(os, ioMode); - if (sep) cybozu::writeChar(os, sep); - T_.save(os, ioMode); + bool b; + save(&b, os, ioMode); + if (!b) throw cybozu::Exception("she:CipherTextA:save"); } friend std::istream& operator>>(std::istream& is, CipherTextAT& self) { @@ -474,22 +491,39 @@ private: struct ZkpT : public fp::Serializable<ZkpT<Tag, n> > { Fr d_[n]; template<class InputStream> - void load(InputStream& is, int ioMode = IoSerialize) + void load(bool *pb, InputStream& is, int ioMode = IoSerialize) { for (size_t i = 0; i < n; i++) { - d_[i].load(is, ioMode); + d_[i].load(pb, is, ioMode); if (!*pb) return; } } template<class OutputStream> - void save(OutputStream& os, int ioMode = IoSerialize) const + void save(bool *pb, OutputStream& os, int ioMode = IoSerialize) const { const char sep = *fp::getIoSeparator(ioMode); - d_[0].save(os, ioMode); + d_[0].save(pb, os, ioMode); if (!*pb) return; for (size_t i = 1; i < n; i++) { - if (sep) cybozu::writeChar(os, sep); - d_[i].save(os, ioMode); + if (sep) { + cybozu::writeChar(pb, os, sep); + if (!*pb) return; + } + d_[i].save(pb, os, ioMode); } } + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) + { + bool b; + load(&b, is, ioMode); + if (!b) throw cybozu::Exception("she:ZkpT:load"); + } + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const + { + bool b; + save(&b, os, ioMode); + if (!b) throw cybozu::Exception("she:ZkpT:save"); + } friend std::istream& operator>>(std::istream& is, ZkpT& self) { self.load(is, fp::detectIoMode(Fr::getIoMode(), is)); @@ -721,19 +755,36 @@ public: } } template<class InputStream> - void load(InputStream& is, int ioMode = IoSerialize) + void load(bool *pb, InputStream& is, int ioMode = IoSerialize) { - x_.load(is, ioMode); - y_.load(is, ioMode); + x_.load(pb, is, ioMode); if (!*pb) return; + y_.load(pb, is, ioMode); } template<class OutputStream> - void save(OutputStream& os, int ioMode = IoSerialize) const + void save(bool *pb, OutputStream& os, int ioMode = IoSerialize) const { const char sep = *fp::getIoSeparator(ioMode); - x_.save(os, ioMode); - if (sep) cybozu::writeChar(os, sep); + x_.save(pb, os, ioMode); if (!*pb) return; + if (sep) { + cybozu::writeChar(pb, os, sep); + if (!*pb) return; + } y_.save(os, ioMode); } + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) + { + bool b; + load(&b, is, ioMode); + if (!b) throw cybozu::Exception("she:SecretKey:load"); + } + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const + { + bool b; + save(&b, os, ioMode); + if (!b) throw cybozu::Exception("she:SecretKey:save"); + } friend std::istream& operator>>(std::istream& is, SecretKey& self) { self.load(is, fp::detectIoMode(Fr::getIoMode(), is)); @@ -1284,18 +1335,35 @@ public: } public: template<class InputStream> + void load(bool *pb, InputStream& is, int ioMode = IoSerialize) + { + xP_.load(pb, is, ioMode); if (!*pb) return; + yQ_.load(pb, is, ioMode); + } + template<class OutputStream> + void save(bool *pb, OutputStream& os, int ioMode = IoSerialize) const + { + const char sep = *fp::getIoSeparator(ioMode); + xP_.save(pb, os, ioMode); if (!*pb) return; + if (sep) { + cybozu::writeChar(pb, os, sep); + if (!*pb) return; + } + yQ_.save(pb, os, ioMode); + } + template<class InputStream> void load(InputStream& is, int ioMode = IoSerialize) { - xP_.load(is, ioMode); - yQ_.load(is, ioMode); + bool b; + load(&b, is, ioMode); + if (!b) throw cybozu::Exception("she:PublicKey:load"); } template<class OutputStream> void save(OutputStream& os, int ioMode = IoSerialize) const { - const char sep = *fp::getIoSeparator(ioMode); - xP_.save(os, ioMode); - if (sep) cybozu::writeChar(os, sep); - yQ_.save(os, ioMode); + bool b; + save(&b, os, ioMode); + if (!b) throw cybozu::Exception("she:PublicKey:save"); } friend std::istream& operator>>(std::istream& is, PublicKey& self) { @@ -1453,18 +1521,35 @@ public: void add(const CipherTextA& c) { add(*this, *this, c); } void sub(const CipherTextA& c) { sub(*this, *this, c); } template<class InputStream> + void load(bool *pb, InputStream& is, int ioMode = IoSerialize) + { + c1_.load(pb, is, ioMode); if (!*pb) return; + c2_.load(pb, is, ioMode); + } + template<class OutputStream> + void save(bool *pb, OutputStream& os, int ioMode = IoSerialize) const + { + const char sep = *fp::getIoSeparator(ioMode); + c1_.save(pb, os, ioMode); if (!*pb) return; + if (sep) { + cybozu::writeChar(pb, os, sep); + if (!*pb) return; + } + c2_.save(pb, os, ioMode); + } + template<class InputStream> void load(InputStream& is, int ioMode = IoSerialize) { - c1_.load(is, ioMode); - c2_.load(is, ioMode); + bool b; + load(&b, is, ioMode); + if (!b) throw cybozu::Exception("she:CipherTextA:load"); } template<class OutputStream> void save(OutputStream& os, int ioMode = IoSerialize) const { - const char sep = *fp::getIoSeparator(ioMode); - c1_.save(os, ioMode); - if (sep) cybozu::writeChar(os, sep); - c2_.save(os, ioMode); + bool b; + save(&b, os, ioMode); + if (!b) throw cybozu::Exception("she:CipherTextA:save"); } friend std::istream& operator>>(std::istream& is, CipherTextA& self) { @@ -1562,22 +1647,39 @@ public: void add(const CipherTextGT& c) { add(*this, *this, c); } void sub(const CipherTextGT& c) { sub(*this, *this, c); } template<class InputStream> - void load(InputStream& is, int ioMode = IoSerialize) + void load(bool *pb, InputStream& is, int ioMode = IoSerialize) { for (int i = 0; i < 4; i++) { - g_[i].load(is, ioMode); + g_[i].load(pb, is, ioMode); if (!*pb) return; } } template<class OutputStream> - void save(OutputStream& os, int ioMode = IoSerialize) const + void save(bool *pb, OutputStream& os, int ioMode = IoSerialize) const { const char sep = *fp::getIoSeparator(ioMode); - g_[0].save(os, ioMode); + g_[0].save(pb, os, ioMode); if (!*pb) return; for (int i = 1; i < 4; i++) { - if (sep) cybozu::writeChar(os, sep); - g_[i].save(os, ioMode); + if (sep) { + cybozu::writeChar(pb, os, sep); + if (!*pb) return; + } + g_[i].save(pb, os, ioMode); if (!*pb) return; } } + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) + { + bool b; + load(&b, is, ioMode); + if (!b) throw cybozu::Exception("she:CipherTextGT:load"); + } + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const + { + bool b; + save(&b, os, ioMode); + if (!b) throw cybozu::Exception("she:CipherTextGT:save"); + } friend std::istream& operator>>(std::istream& is, CipherTextGT& self) { self.load(is, fp::detectIoMode(G1::getIoMode(), is)); @@ -1667,25 +1769,46 @@ public: void sub(const CipherText& c) { sub(*this, *this, c); } void mul(const CipherText& c) { mul(*this, *this, c); } template<class InputStream> - void load(InputStream& is, int ioMode = IoSerialize) + void load(bool *pb, InputStream& is, int ioMode = IoSerialize) { - cybozu::load(isMultiplied_, is); + cybozu::writeChar(pb, isMultiplied_ ? '0' : '1', is); if (!*pb) return; if (isMultiplied()) { - m_.load(is, ioMode); + m_.load(pb, is, ioMode); } else { - a_.load(is, ioMode); + a_.load(pb, is, ioMode); } } template<class OutputStream> - void save(OutputStream& os, int ioMode = IoSerialize) const + void save(bool *pb, OutputStream& os, int ioMode = IoSerialize) const { - cybozu::save(os, isMultiplied_); + char c; + if (!cybozu::readChar(&c, os)) return; + if (c == '0' || c == '1') { + isMultiplied_ = c == '0'; + } else { + *pb = false; + return; + } if (isMultiplied()) { - m_.save(os, ioMode); + m_.save(pb, os, ioMode); } else { - a_.save(os, ioMode); + a_.save(pb, os, ioMode); } } + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) + { + bool b; + load(&b, is, ioMode); + if (!b) throw cybozu::Exception("she:CipherText:load"); + } + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const + { + bool b; + save(&b, os, ioMode); + if (!b) throw cybozu::Exception("she:CipherText:save"); + } friend std::istream& operator>>(std::istream& is, CipherText& self) { self.load(is, fp::detectIoMode(G1::getIoMode(), is)); |