diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-12-27 16:43:58 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-12-27 16:43:58 +0800 |
commit | 3f8c889239e2830272010fcc3d7315eec49f17cf (patch) | |
tree | 7c3038b37a1edb73232c540f0696f05b68a278c9 | |
parent | f3ab053e8c747ca2ea226b264e30460e2f35b683 (diff) | |
download | dexon-mcl-3f8c889239e2830272010fcc3d7315eec49f17cf.tar.gz dexon-mcl-3f8c889239e2830272010fcc3d7315eec49f17cf.tar.zst dexon-mcl-3f8c889239e2830272010fcc3d7315eec49f17cf.zip |
all classes use new load/save api
-rw-r--r-- | include/mcl/ec.hpp | 109 | ||||
-rw-r--r-- | include/mcl/elgamal.hpp | 173 | ||||
-rw-r--r-- | include/mcl/fp.hpp | 12 | ||||
-rw-r--r-- | include/mcl/fp_tower.hpp | 149 | ||||
-rw-r--r-- | include/mcl/she.hpp | 291 |
5 files changed, 374 insertions, 360 deletions
diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index 3278e4f..dd8cb84 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -654,66 +654,69 @@ public: { return !b_.isZero() && (Fp::BaseFp::getBitSize() & 7) != 0; } - /* - see mcl/op.hpp for the format of ioMode - */ - void getStr(std::string& str, int ioMode = 0) const + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const { - const char *sep = fp::getIoSeparator(ioMode); + const char sep = *fp::getIoSeparator(ioMode); if (ioMode & IoEcProj) { - str = '4'; - str += sep; - str += x.getStr(ioMode); - str += sep; - str += y.getStr(ioMode); - str += sep; - str += z.getStr(ioMode); + cybozu::writeChar(os, '4'); + if (sep) cybozu::writeChar(os, sep); + x.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); + y.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); + z.save(os, ioMode); return; } EcT P(*this); P.normalize(); if (ioMode & IoSerialize) { - if (!isFixedSizeByteSeq()) throw cybozu::Exception("EcT:getStr:not supported ioMode") << ioMode; + if (!isFixedSizeByteSeq()) throw cybozu::Exception("EcT:save:not supported ioMode") << ioMode; const size_t n = Fp::getByteSize(); + char buf[sizeof(Fp)]; + std::string str; if (isZero()) { - str.clear(); - str.resize(n); - return; - } - P.x.getStr(str, ioMode); - assert(str.size() == n && (str[n - 1] & 0x80) == 0); - if (P.y.isOdd()) { - str[n - 1] |= 0x80; + memset(buf, 0, n); + } else { + cybozu::MemoryOutputStream mos(buf, n); + P.x.save(mos, ioMode); + if (P.y.isOdd()) { + buf[n - 1] |= 0x80; + } } + cybozu::write(os, buf, n); return; } if (isZero()) { - str = '0'; + cybozu::writeChar(os, '0'); return; } if (ioMode & IoEcCompY) { - str = P.y.isOdd() ? '3' : '2'; - str += sep; - str += P.x.getStr(ioMode); + cybozu::writeChar(os, P.y.isOdd() ? '3' : '2'); + if (sep) cybozu::writeChar(os, sep); + P.x.save(os, ioMode); } else { - str = '1'; - str += sep; - str += P.x.getStr(ioMode); - str += sep; - str += P.y.getStr(ioMode); + cybozu::writeChar(os, '1'); + if (sep) cybozu::writeChar(os, sep); + P.x.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); + P.y.save(os, ioMode); } } + /* + see mcl/op.hpp for the format of ioMode + */ + void getStr(std::string& str, int ioMode = 0) const + { + cybozu::StringOutputStream os(str); + save(os, ioMode); + } std::string getStr(int ioMode = 0) const { std::string str; getStr(str, ioMode); return str; } - friend inline std::ostream& operator<<(std::ostream& os, const EcT& self) - { - int ioMode = fp::detectIoMode(getIoMode(), os); - return os << self.getStr(ioMode); - } template<class InputStream> void load(InputStream& is, int ioMode = IoSerialize) { @@ -762,47 +765,29 @@ public: throw cybozu::Exception("EcT:load:bad order") << *this; } } - std::istream& readStream(std::istream& is, int ioMode = 0) + friend inline std::istream& operator>>(std::istream& is, EcT& self) { - load(is, ioMode); + self.load(is, fp::detectIoMode(getIoMode(), is)); return is; } - friend inline std::istream& operator>>(std::istream& is, EcT& self) + friend inline std::ostream& operator<<(std::ostream& os, const EcT& self) { - int ioMode = fp::detectIoMode(getIoMode(), is); - return self.readStream(is, ioMode); + self.save(os, fp::detectIoMode(getIoMode(), os)); + return os; } void setStr(const std::string& str, int ioMode = 0) { - std::istringstream is(str); - readStream(is, ioMode); + cybozu::StringInputStream is(str); + load(is, ioMode); } - // return written bytes if sucess else 0 + // return written bytes if sucess size_t serialize(void *buf, size_t maxBufSize) const { -#if 0 cybozu::MemoryOutputStream os(buf, maxBufSize); save(os, IoSerialize); return os.getPos(); -#else - if (!isFixedSizeByteSeq()) return 0; - const size_t n = Fp::getByteSize(); - if (maxBufSize < n) return 0; - if (isZero()) { - memset(buf, 0, n); - } else { - char *p = reinterpret_cast<char*>(buf); - EcT P; - EcT::normalize(P, *this); - if (P.x.serialize(p, maxBufSize) == 0) return 0; - if (P.y.isOdd()) { - p[n - 1] |= 0x80; - } - } - return n; -#endif } - // return positive read bytes if sucess else 0 + // return positive read bytes if sucess size_t deserialize(const void *buf, size_t bufSize) { cybozu::MemoryInputStream is(buf, bufSize); diff --git a/include/mcl/elgamal.hpp b/include/mcl/elgamal.hpp index 0f8a379..56ade5e 100644 --- a/include/mcl/elgamal.hpp +++ b/include/mcl/elgamal.hpp @@ -73,18 +73,25 @@ struct ElgamalT { Ec::neg(c1, c1); Ec::neg(c2, c2); } - std::istream& readStream(std::istream& is, int ioMode) + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) { - c1.readStream(is, ioMode); - c2.readStream(is, ioMode); - return is; + c1.load(is, ioMode); + c2.load(is, ioMode); + } + 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); } void getStr(std::string& str, int ioMode = 0) const { - const char *sep = fp::getIoSeparator(ioMode); - str = c1.getStr(ioMode); - str += sep; - str += c2.getStr(ioMode); + str.clear(); + cybozu::StringOutputStream os(str); + save(os, ioMode); } std::string getStr(int ioMode = 0) const { @@ -94,18 +101,18 @@ struct ElgamalT { } void setStr(const std::string& str, int ioMode = 0) { - std::istringstream is(str); - readStream(is, ioMode); + cybozu::StringInputStream is(str); + load(is, ioMode); } friend inline std::ostream& operator<<(std::ostream& os, const CipherText& self) { - int ioMode = fp::detectIoMode(Ec::getIoMode(), os); - return os << self.getStr(ioMode); + self.save(os, fp::detectIoMode(Ec::getIoMode(), os)); + return os; } friend inline std::istream& operator>>(std::istream& is, CipherText& self) { - int ioMode = fp::detectIoMode(Ec::getIoMode(), is); - return self.readStream(is, ioMode); + self.load(is, fp::detectIoMode(Ec::getIoMode(), is)); + return is; } // obsolete std::string toStr() const { return getStr(); } @@ -118,24 +125,31 @@ struct ElgamalT { */ struct Zkp { Zn c0, c1, s0, s1; - std::istream& readStream(std::istream& is, int ioMode) - { - c0.readStream(is, ioMode); - c1.readStream(is, ioMode); - s0.readStream(is, ioMode); - s1.readStream(is, ioMode); - return is; + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) + { + c0.load(is, ioMode); + c1.load(is, ioMode); + s0.load(is, ioMode); + s1.load(is, ioMode); + } + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const + { + const char sep = *fp::getIoSeparator(ioMode); + c0.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); + c1.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); + s0.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); + s1.save(os, ioMode); } void getStr(std::string& str, int ioMode = 0) const { - const char *sep = fp::getIoSeparator(ioMode); - str = c0.getStr(ioMode); - str += sep; - str += c1.getStr(ioMode); - str += sep; - str += s0.getStr(ioMode); - str += sep; - str += s1.getStr(ioMode); + str.clear(); + cybozu::StringOutputStream os(str); + save(os, ioMode); } std::string getStr(int ioMode = 0) const { @@ -145,18 +159,18 @@ struct ElgamalT { } void setStr(const std::string& str, int ioMode = 0) { - std::istringstream is(str); - readStream(is, ioMode); + cybozu::StringInputStream is(str); + load(is, ioMode); } friend inline std::ostream& operator<<(std::ostream& os, const Zkp& self) { - int ioMode = fp::detectIoMode(Zn::getIoMode(), os); - return os << self.getStr(ioMode); + self.save(os, fp::detectIoMode(Ec::getIoMode(), os)); + return os; } friend inline std::istream& operator>>(std::istream& is, Zkp& self) { - int ioMode = fp::detectIoMode(Zn::getIoMode(), is); - return self.readStream(is, ioMode); + self.load(is, fp::detectIoMode(Ec::getIoMode(), is)); + return is; } // obsolete std::string toStr() const { return getStr(); } @@ -347,30 +361,32 @@ struct ElgamalT { mulF(fm, m); Ec::add(c.c2, c.c2, fm); } - std::istream& readStream(std::istream& is, int ioMode) - { - std::string s; - is >> s; - bitSize = cybozu::atoi(s); - char c; - is.read(&c, 1); - if (c != ' ') throw cybozu::Exception("ElgamalT:PublicKey:readStream:bad separator") << int(c); - f.readStream(is, ioMode); - g.readStream(is, ioMode); - h.readStream(is, ioMode); + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) + { + cybozu::load(bitSize, is); + f.load(is, ioMode); + g.load(is, ioMode); + h.load(is, ioMode); init(bitSize, f, g, h); - return is; + } + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const + { + const char sep = *fp::getIoSeparator(ioMode); + cybozu::save(os, bitSize); + f.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); + g.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); + h.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); } void getStr(std::string& str, int ioMode = 0) const { - const char *sep = fp::getIoSeparator(ioMode); - str = cybozu::itoa(bitSize); - str += ' '; - str += f.getStr(ioMode); - str += sep; - str += g.getStr(ioMode); - str += sep; - str += h.getStr(ioMode); + str.clear(); + cybozu::StringOutputStream os(str); + save(os, ioMode); } std::string getStr(int ioMode = 0) const { @@ -380,18 +396,18 @@ struct ElgamalT { } void setStr(const std::string& str, int ioMode = 0) { - std::istringstream is(str); - readStream(is, ioMode); + cybozu::StringInputStream is(str); + load(is, ioMode); } friend inline std::ostream& operator<<(std::ostream& os, const PublicKey& self) { - int ioMode = fp::detectIoMode(Ec::getIoMode(), os); - return os << self.getStr(ioMode); + self.save(os, fp::detectIoMode(Ec::getIoMode(), os)); + return os; } friend inline std::istream& operator>>(std::istream& is, PublicKey& self) { - int ioMode = fp::detectIoMode(Ec::getIoMode(), is); - return self.readStream(is, ioMode); + self.load(is, fp::detectIoMode(Ec::getIoMode(), is)); + return is; } // obsolete std::string toStr() const { return getStr(); } @@ -552,18 +568,25 @@ struct ElgamalT { Ec::mul(c1z, c.c1, z); return c.c2 == c1z; } - std::istream& readStream(std::istream& is, int ioMode) + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) { - pub.readStream(is, ioMode); - z.readStream(is, ioMode); - return is; + pub.load(is, ioMode); + z.load(is, ioMode); + } + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const + { + const char sep = *fp::getIoSeparator(ioMode); + pub.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); + z.save(os, ioMode); } void getStr(std::string& str, int ioMode = 0) const { - const char *sep = fp::getIoSeparator(ioMode); - str = pub.getStr(ioMode); - str += sep; - str += z.getStr(ioMode); + str.clear(); + cybozu::StringOutputStream os(str); + save(os, ioMode); } std::string getStr(int ioMode = 0) const { @@ -573,18 +596,18 @@ struct ElgamalT { } void setStr(const std::string& str, int ioMode = 0) { - std::istringstream is(str); - readStream(is, ioMode); + cybozu::StringInputStream is(str); + load(is, ioMode); } friend inline std::ostream& operator<<(std::ostream& os, const PrivateKey& self) { - int ioMode = fp::detectIoMode(Ec::getIoMode(), os); - return os << self.getStr(ioMode); + self.save(os, fp::detectIoMode(Ec::getIoMode(), os)); + return os; } friend inline std::istream& operator>>(std::istream& is, PrivateKey& self) { - int ioMode = fp::detectIoMode(Ec::getIoMode(), is); - return self.readStream(is, ioMode); + self.load(is, fp::detectIoMode(Ec::getIoMode(), is)); + return is; } std::string toStr() const { return getStr(); } void fromStr(const std::string& str) { setStr(str); } diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 3632502..653dcaf 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -283,12 +283,6 @@ public: cybozu::StringInputStream is(str); load(is, ioMode); } - template<class InputStream> - InputStream& readStream(InputStream& is, int ioMode = 0) - { - load(is, ioMode); - return is; - } // return written bytes size_t serialize(void *buf, size_t maxBufSize) const { @@ -442,11 +436,13 @@ public: bool operator!=(const FpT& rhs) const { return !operator==(rhs); } friend inline std::ostream& operator<<(std::ostream& os, const FpT& self) { - return os << self.getStr(fp::detectIoMode(getIoMode(), os)); + self.save(os, fp::detectIoMode(getIoMode(), os)); + return os; } friend inline std::istream& operator>>(std::istream& is, FpT& self) { - return self.readStream(is, fp::detectIoMode(getIoMode(), is)); + self.load(is, fp::detectIoMode(getIoMode(), is)); + return is; } /* @note diff --git a/include/mcl/fp_tower.hpp b/include/mcl/fp_tower.hpp index f166d11..8f108c2 100644 --- a/include/mcl/fp_tower.hpp +++ b/include/mcl/fp_tower.hpp @@ -10,58 +10,6 @@ namespace mcl { -namespace local { - -template<class T1, class T2> -size_t serialize2(void *buf, size_t maxBufSize, const T1& p1, const T2& p2) -{ - char *p = reinterpret_cast<char*>(buf); - const size_t n1 = p1.serialize(p, maxBufSize); - if (n1 == 0) return 0; - p += n1; maxBufSize -= n1; - const size_t n2 = p2.serialize(p, maxBufSize); - if (n2 == 0) return 0; - return n1 + n2; -} - -template<class T1, class T2, class T3> -size_t serialize3(void *buf, size_t maxBufSize, const T1& p1, const T2& p2, const T3& p3) -{ - char *p = reinterpret_cast<char*>(buf); - const size_t n1 = serialize2(buf, maxBufSize, p1, p2); - if (n1 == 0) return 0; - p += n1; maxBufSize -= n1; - const size_t n2 = p3.serialize(p, maxBufSize); - if (n2 == 0) return 0; - return n1 + n2; -} - -template<class T1, class T2> -size_t deserialize2(T1& p1, T2& p2, const void *buf, size_t bufSize) -{ - const char *p = reinterpret_cast<const char*>(buf); - const size_t n1 = p1.deserialize(p, bufSize); - if (n1 == 0) return 0; - p += n1; bufSize -= n1; - const size_t n2 = p2.deserialize(p, bufSize); - if (n2 == 0) return 0; - return n1 + n2; -} - -template<class T1, class T2, class T3> -size_t deserialize3(T1& p1, T2& p2, T3& p3, const void *buf, size_t bufSize) -{ - const char *p = reinterpret_cast<const char*>(buf); - const size_t n1 = deserialize2(p1, p2, p, bufSize); - if (n1 == 0) return 0; - p += n1; bufSize -= n1; - const size_t n2 = p3.deserialize(p, bufSize); - if (n2 == 0) return 0; - return n1 + n2; -} - -} // local - template<class Fp> class FpDblT { typedef fp::Unit Unit; @@ -235,19 +183,22 @@ public: b.setArray(buf + n, n); } template<class InputStream> - void load(InputStream& is, int ioMode) + void load(InputStream& is, int ioMode = IoSerialize) { a.load(is, ioMode); b.load(is, ioMode); } - std::istream& readStream(std::istream& is, int ioMode) + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const { - load(is, ioMode); - return is; + const char sep = *fp::getIoSeparator(ioMode); + a.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); + b.save(os, ioMode); } void setStr(const std::string& str, int ioMode = 0) { - std::istringstream is(str); + cybozu::StringInputStream is(str); load(is, ioMode); } /* @@ -255,10 +206,9 @@ public: */ void getStr(std::string& str, int ioMode = 0) const { - const char *sep = fp::getIoSeparator(ioMode); - str = a.getStr(ioMode); - str += sep; - str += b.getStr(ioMode); + str.clear(); + cybozu::StringOutputStream os(str); + save(os, ioMode); } std::string getStr(int ioMode = 0) const { @@ -273,17 +223,22 @@ public: } friend std::ostream& operator<<(std::ostream& os, const Fp2T& self) { - return os << self.getStr(fp::detectIoMode(Fp::BaseFp::getIoMode(), os)); + self.save(os, fp::detectIoMode(Fp::BaseFp::getIoMode(), os)); + return os; } // return written bytes if sucess else 0 size_t serialize(void *buf, size_t maxBufSize) const { - return local::serialize2(buf, maxBufSize, a, b); + cybozu::MemoryOutputStream os(buf, maxBufSize); + save(os); + return os.getPos(); } // return positive read bytes if sucess else 0 size_t deserialize(const void *buf, size_t bufSize) { - return local::deserialize2(a, b, buf, bufSize); + cybozu::MemoryInputStream is(buf, bufSize); + load(is); + return is.getPos(); } bool isZero() const { return a.isZero() && b.isZero(); } bool isOne() const { return a.isOne() && b.isZero(); } @@ -765,24 +720,26 @@ struct Fp6T : public fp::Operator<Fp6T<Fp> > { b.load(is, ioMode); c.load(is, ioMode); } - std::istream& readStream(std::istream& is, int ioMode) + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const { - load(is, ioMode); - return is; + const char sep = *fp::getIoSeparator(ioMode); + a.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); + b.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); + c.save(os, ioMode); } void setStr(const std::string& str, int ioMode = 0) { - std::istringstream is(str); + cybozu::StringInputStream is(str); load(is, ioMode); } void getStr(std::string& str, int ioMode = 0) const { - const char *sep = fp::getIoSeparator(ioMode); - str = a.getStr(ioMode); - str += sep; - str += b.getStr(ioMode); - str += sep; - str += c.getStr(ioMode); + str.clear(); + cybozu::StringOutputStream os(str); + save(os, ioMode); } std::string getStr(int ioMode = 0) const { @@ -797,17 +754,22 @@ struct Fp6T : public fp::Operator<Fp6T<Fp> > { } friend std::ostream& operator<<(std::ostream& os, const Fp6T& self) { - return os << self.getStr(fp::detectIoMode(Fp::BaseFp::getIoMode(), os)); + self.save(os, fp::detectIoMode(Fp::BaseFp::getIoMode(), os)); + return os; } // return written bytes if sucess else 0 size_t serialize(void *buf, size_t maxBufSize) const { - return local::serialize3(buf, maxBufSize, a, b, c); + cybozu::MemoryOutputStream os(buf, maxBufSize); + save(os); + return os.getPos(); } // return positive read bytes if sucess else 0 size_t deserialize(const void *buf, size_t bufSize) { - return local::deserialize3(a, b, c, buf, bufSize); + cybozu::MemoryInputStream is(buf, bufSize); + load(is); + return is.getPos(); } static void add(Fp6T& z, const Fp6T& x, const Fp6T& y) { @@ -1183,27 +1145,29 @@ struct Fp12T : public fp::Operator<Fp12T<Fp> > { #endif } template<class InputStream> - void load(InputStream& is, int ioMode) + void load(InputStream& is, int ioMode = IoSerialize) { a.load(is, ioMode); b.load(is, ioMode); } - std::istream& readStream(std::istream& is, int ioMode) + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const { - load(is, ioMode); - return is; + const char sep = *fp::getIoSeparator(ioMode); + a.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); + b.save(os, ioMode); } void setStr(const std::string& str, int ioMode = 0) { - std::istringstream is(str); + cybozu::StringInputStream is(str); load(is, ioMode); } void getStr(std::string& str, int ioMode = 0) const { - const char *sep = fp::getIoSeparator(ioMode); - str = a.getStr(ioMode); - str += sep; - str += b.getStr(ioMode); + str.clear(); + cybozu::StringOutputStream os(str); + save(os, ioMode); } std::string getStr(int ioMode = 0) const { @@ -1213,22 +1177,27 @@ struct Fp12T : public fp::Operator<Fp12T<Fp> > { } friend std::istream& operator>>(std::istream& is, Fp12T& self) { - self.load(is, fp::detectIoMode(Fp::getIoMode(), is)); + self.load(is, fp::detectIoMode(Fp::BaseFp::getIoMode(), is)); return is; } friend std::ostream& operator<<(std::ostream& os, const Fp12T& self) { - return os << self.getStr(fp::detectIoMode(Fp::BaseFp::getIoMode(), os)); + self.save(os, fp::detectIoMode(Fp::BaseFp::getIoMode(), os)); + return os; } // return written bytes if sucess else 0 size_t serialize(void *buf, size_t maxBufSize) const { - return local::serialize2(buf, maxBufSize, a, b); + cybozu::MemoryOutputStream os(buf, maxBufSize); + save(os); + return os.getPos(); } // return positive read bytes if sucess else 0 size_t deserialize(const void *buf, size_t bufSize) { - return local::deserialize2(a, b, buf, bufSize); + cybozu::MemoryInputStream is(buf, bufSize); + load(is); + return is.getPos(); } }; diff --git a/include/mcl/she.hpp b/include/mcl/she.hpp index bfdef7c..cad0dfc 100644 --- a/include/mcl/she.hpp +++ b/include/mcl/she.hpp @@ -276,39 +276,24 @@ public: } throw cybozu::Exception("HashTable:log:not found"); } -#if 0 - size_t serialize(void *buf, size_t maxBufSize) const + template<class OutputStream> + void save(OutputStream& os) const { - const size_t kcvTotalSize = sizeof(kcv[0]) * kcv.size(); - uint8_t *p = reinterpret_cast<uint8_t*>(buf); - if (sizeof(uint32_t) * 2 + kcvTotalSize > maxBufSize) throw cybozu::Exception("HashTable:serialize:small maxBufSize") << maxBufSize; - cybozu::set32bitLE(p, static_cast<uint32_t>(kcv.size())); - p += 4; - cybozu::set32bitLE(p, tryNum_); - p += 4; - memcpy(p, &kcv[0], kcvTotalSize); - const writeSize = 8 + kcvTotalSize; - p += kcvTotalSize; - const size_t sizeP = P_.serialize(p, maxBufSize - writeSize); - if (sizeP == 0) return 0; - return writeSize + sizeP; + cybozu::save(os, kcv.size()); + cybozu::save(os, tryNum_); + cybozu::write(os, &kcv[0], sizeof(kcv[0]) * kcv.size()); + P_.save(os); } -#endif - void save(std::ostream& os) const + template<class InputStream> + void load(InputStream& is) { - saveUint64(os, kcv.size()); - saveUint64(os, tryNum_); - os.write((const char*)&kcv[0], sizeof(kcv[0]) * kcv.size()); - os << P_.getStr(mcl::IoArray); - } - void load(std::istream& is) - { - size_t hashSize = size_t(loadUint64(is)); - kcv.resize(hashSize); - tryNum_ = loadUint64(is); - is.read((char*)&kcv[0], sizeof(kcv[0]) * kcv.size()); - P_.readStream(is, mcl::IoArray); - I::mul(nextP_, P_, (hashSize * 2) + 1); + size_t kcvSize; + cybozu::load(kcvSize, is); + kcv.resize(kcvSize); + cybozu::load(tryNum_, is); + cybozu::read(&kcv[0], sizeof(kcv[0]) * kcvSize, is); + P_.load(is); + I::mul(nextP_, P_, (kcvSize * 2) + 1); I::neg(nextNegP_, nextP_); setWindowMethod(); } @@ -417,23 +402,30 @@ private: } void add(const CipherTextAT& c) { add(*this, *this, c); } void sub(const CipherTextAT& c) { sub(*this, *this, c); } - std::istream& readStream(std::istream& is, int ioMode) + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) { - S_.readStream(is, ioMode); - T_.readStream(is, ioMode); - return is; + S_.load(is, ioMode); + T_.load(is, ioMode); + } + 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); } void getStr(std::string& str, int ioMode = 0) const { - const char *sep = fp::getIoSeparator(ioMode); - str = S_.getStr(ioMode); - str += sep; - str += T_.getStr(ioMode); + str.clear(); + cybozu::StringOutputStream os(str); + save(os, ioMode); } void setStr(const std::string& str, int ioMode = 0) { - std::istringstream is(str); - readStream(is, ioMode); + cybozu::StringInputStream is(str); + load(is, ioMode); } std::string getStr(int ioMode = 0) const { @@ -443,11 +435,13 @@ private: } friend std::istream& operator>>(std::istream& is, CipherTextAT& self) { - return self.readStream(is, fp::detectIoMode(G::getIoMode(), is)); + self.load(is, fp::detectIoMode(G::getIoMode(), is)); + return is; } friend std::ostream& operator<<(std::ostream& os, const CipherTextAT& self) { - return os << self.getStr(fp::detectIoMode(G::getIoMode(), os)); + self.save(os, fp::detectIoMode(G::getIoMode(), os)); + return os; } bool operator==(const CipherTextAT& rhs) const { @@ -456,11 +450,15 @@ private: bool operator!=(const CipherTextAT& rhs) const { return !operator==(rhs); } size_t serialize(void *buf, size_t maxBufSize) const { - return mcl::local::serialize2(buf, maxBufSize, S_, T_); + cybozu::MemoryOutputStream os(buf, maxBufSize); + save(os); + return os.getPos(); } size_t deserialize(const void *buf, size_t bufSize) { - return mcl::local::deserialize2(S_, T_, buf, bufSize); + cybozu::MemoryInputStream is(buf, bufSize); + load(is); + return is.getPos(); } }; /* @@ -669,23 +667,30 @@ public: return isZero(c.a_); } } - std::istream& readStream(std::istream& is, int ioMode) + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) { - x_.readStream(is, ioMode); - y_.readStream(is, ioMode); - return is; + x_.load(is, ioMode); + y_.load(is, ioMode); + } + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const + { + const char sep = *fp::getIoSeparator(ioMode); + x_.save(os, ioMode); + if (sep) cybozu::writeChar(os, sep); + y_.save(os, ioMode); } void getStr(std::string& str, int ioMode = 0) const { - const char *sep = fp::getIoSeparator(ioMode); - str = x_.getStr(ioMode); - str += sep; - str += y_.getStr(ioMode); + str.clear(); + cybozu::StringOutputStream os(str); + save(os, ioMode); } void setStr(const std::string& str, int ioMode = 0) { - std::istringstream is(str); - readStream(is, ioMode); + cybozu::StringInputStream is(str); + load(is, ioMode); } std::string getStr(int ioMode = 0) const { @@ -695,11 +700,13 @@ public: } friend std::istream& operator>>(std::istream& is, SecretKey& self) { - return self.readStream(is, fp::detectIoMode(Fr::getIoMode(), is)); + self.load(is, fp::detectIoMode(Fr::getIoMode(), is)); + return is; } friend std::ostream& operator<<(std::ostream& os, const SecretKey& self) { - return os << self.getStr(fp::detectIoMode(Fr::getIoMode(), os)); + self.save(os, fp::detectIoMode(Fr::getIoMode(), os)); + return os; } bool operator==(const SecretKey& rhs) const { @@ -708,11 +715,15 @@ public: bool operator!=(const SecretKey& rhs) const { return !operator==(rhs); } size_t serialize(void *buf, size_t maxBufSize) const { - return mcl::local::serialize2(buf, maxBufSize, x_, y_); + cybozu::MemoryOutputStream os(buf, maxBufSize); + save(os); + return os.getPos(); } size_t deserialize(const void *buf, size_t bufSize) { - return mcl::local::deserialize2(x_, y_, buf, bufSize); + cybozu::MemoryInputStream is(buf, bufSize); + load(is); + return is.getPos(); } }; @@ -927,23 +938,30 @@ public: void reRand(CipherTextGT& c) const { reRand(c, local::g_rg); } void reRand(CipherText& c) const { reRand(c, local::g_rg); } - std::istream& readStream(std::istream& is, int ioMode) + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) { - xP_.readStream(is, ioMode); - yQ_.readStream(is, ioMode); - return is; + xP_.load(is, ioMode); + yQ_.load(is, ioMode); + } + 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); } void getStr(std::string& str, int ioMode = 0) const { - const char *sep = fp::getIoSeparator(ioMode); - str = xP_.getStr(ioMode); - str += sep; - str += yQ_.getStr(ioMode); + str.clear(); + cybozu::StringOutputStream os(str); + save(os, ioMode); } void setStr(const std::string& str, int ioMode = 0) { - std::istringstream is(str); - readStream(is, ioMode); + cybozu::StringInputStream is(str); + load(is, ioMode); } std::string getStr(int ioMode = 0) const { @@ -953,11 +971,13 @@ public: } friend std::istream& operator>>(std::istream& is, PublicKey& self) { - return self.readStream(is, fp::detectIoMode(G1::getIoMode(), is)); + self.load(is, fp::detectIoMode(G1::getIoMode(), is)); + return is; } friend std::ostream& operator<<(std::ostream& os, const PublicKey& self) { - return os << self.getStr(fp::detectIoMode(G1::getIoMode(), os)); + self.save(os, fp::detectIoMode(G1::getIoMode(), os)); + return os; } bool operator==(const PublicKey& rhs) const { @@ -966,11 +986,15 @@ public: bool operator!=(const PublicKey& rhs) const { return !operator==(rhs); } size_t serialize(void *buf, size_t maxBufSize) const { - return mcl::local::serialize2(buf, maxBufSize, xP_, yQ_); + cybozu::MemoryOutputStream os(buf, maxBufSize); + save(os); + return os.getPos(); } size_t deserialize(const void *buf, size_t bufSize) { - return mcl::local::deserialize2(xP_, yQ_, buf, bufSize); + cybozu::MemoryInputStream is(buf, bufSize); + load(is); + return is.getPos(); } }; @@ -1099,23 +1123,30 @@ public: } void add(const CipherTextA& c) { add(*this, *this, c); } void sub(const CipherTextA& c) { sub(*this, *this, c); } - std::istream& readStream(std::istream& is, int ioMode) + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) { - c1_.readStream(is, ioMode); - c2_.readStream(is, ioMode); - return is; + c1_.load(is, ioMode); + c2_.load(is, ioMode); + } + 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); } void getStr(std::string& str, int ioMode = 0) const { - const char *sep = fp::getIoSeparator(ioMode); - str = c1_.getStr(ioMode); - str += sep; - str += c2_.getStr(ioMode); + str.clear(); + cybozu::StringOutputStream os(str); + save(os, ioMode); } void setStr(const std::string& str, int ioMode = 0) { - std::istringstream is(str); - readStream(is, ioMode); + cybozu::StringInputStream is(str); + load(is, ioMode); } std::string getStr(int ioMode = 0) const { @@ -1125,11 +1156,13 @@ public: } friend std::istream& operator>>(std::istream& is, CipherTextA& self) { - return self.readStream(is, fp::detectIoMode(G1::getIoMode(), is)); + self.load(is, fp::detectIoMode(G1::getIoMode(), is)); + return is; } friend std::ostream& operator<<(std::ostream& os, const CipherTextA& self) { - return os << self.getStr(fp::detectIoMode(G1::getIoMode(), os)); + self.save(os, fp::detectIoMode(G1::getIoMode(), os)); + return os; } bool operator==(const CipherTextA& rhs) const { @@ -1208,26 +1241,33 @@ public: } void add(const CipherTextGT& c) { add(*this, *this, c); } void sub(const CipherTextGT& c) { sub(*this, *this, c); } - std::istream& readStream(std::istream& is, int ioMode) + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) { for (int i = 0; i < 4; i++) { - g_[i].readStream(is, ioMode); + g_[i].load(is, ioMode); } - return is; } - void getStr(std::string& str, int ioMode = 0) const + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const { - const char *sep = fp::getIoSeparator(ioMode); - str = g_[0].getStr(ioMode); + const char sep = *fp::getIoSeparator(ioMode); + g_[0].save(os, ioMode); for (int i = 1; i < 4; i++) { - str += sep; - str += g_[i].getStr(ioMode); + if (sep) cybozu::writeChar(os, sep); + g_[i].save(os, ioMode); } } + void getStr(std::string& str, int ioMode = 0) const + { + str.clear(); + cybozu::StringOutputStream os(str); + save(os, ioMode); + } void setStr(const std::string& str, int ioMode = 0) { - std::istringstream is(str); - readStream(is, ioMode); + cybozu::StringInputStream is(str); + load(is, ioMode); } std::string getStr(int ioMode = 0) const { @@ -1237,11 +1277,13 @@ public: } friend std::istream& operator>>(std::istream& is, CipherTextGT& self) { - return self.readStream(is, fp::detectIoMode(G1::getIoMode(), is)); + self.load(is, fp::detectIoMode(G1::getIoMode(), is)); + return is; } friend std::ostream& operator<<(std::ostream& os, const CipherTextGT& self) { - return os << self.getStr(fp::detectIoMode(G1::getIoMode(), os)); + self.save(os, fp::detectIoMode(G1::getIoMode(), os)); + return os; } bool operator==(const CipherTextGT& rhs) const { @@ -1253,23 +1295,15 @@ public: bool operator!=(const CipherTextGT& rhs) const { return !operator==(rhs); } size_t serialize(void *buf, size_t maxBufSize) const { - char *p = reinterpret_cast<char*>(buf); - const size_t n1 = mcl::local::serialize2(p, maxBufSize, g_[0], g_[1]); - if (n1 == 0) return 0; - p += n1; maxBufSize -= n1; - const size_t n2 = mcl::local::serialize2(p, maxBufSize, g_[2], g_[3]); - if (n2 == 0) return 0; - return n1 + n2; + cybozu::MemoryOutputStream os(buf, maxBufSize); + save(os); + return os.getPos(); } size_t deserialize(const void *buf, size_t bufSize) { - const char *p = reinterpret_cast<const char*>(buf); - const size_t n1 = mcl::local::deserialize2(g_[0], g_[1], p, bufSize); - if (n1 == 0) return 0; - p += n1; bufSize -= n1; - const size_t n2 = mcl::local::deserialize2(g_[2], g_[3], p, bufSize); - if (n2 == 0) return 0; - return n1 + n2; + cybozu::MemoryInputStream is(buf, bufSize); + load(is); + return is.getPos(); } }; @@ -1339,31 +1373,36 @@ public: 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); } - std::istream& readStream(std::istream& is, int ioMode) + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) { - is >> isMultiplied_; + cybozu::load(isMultiplied_, is); if (isMultiplied()) { - m_.readStream(is, ioMode); + m_.load(is, ioMode); } else { - a_.readStream(is, ioMode); + a_.load(is, ioMode); } - return is; } - void getStr(std::string& str, int ioMode = 0) const + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const { - const char *sep = fp::getIoSeparator(ioMode); - str = isMultiplied() ? "1" : "0"; - str += sep; + cybozu::save(os, isMultiplied_); if (isMultiplied()) { - str += m_.getStr(ioMode); + m_.save(os, ioMode); } else { - str += a_.getStr(ioMode); + a_.save(os, ioMode); } } + void getStr(std::string& str, int ioMode = 0) const + { + str.clear(); + cybozu::StringOutputStream os(str); + save(os, ioMode); + } void setStr(const std::string& str, int ioMode = 0) { - std::istringstream is(str); - readStream(is, ioMode); + cybozu::StringInputStream is(str); + load(is, ioMode); } std::string getStr(int ioMode = 0) const { @@ -1373,11 +1412,13 @@ public: } friend std::istream& operator>>(std::istream& is, CipherText& self) { - return self.readStream(is, fp::detectIoMode(G1::getIoMode(), is)); + self.load(is, fp::detectIoMode(G1::getIoMode(), is)); + return is; } friend std::ostream& operator<<(std::ostream& os, const CipherText& self) { - return os << self.getStr(fp::detectIoMode(G1::getIoMode(), os)); + self.save(os, fp::detectIoMode(G1::getIoMode(), os)); + return os; } bool operator==(const CipherTextGT& rhs) const { |