diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-12-27 09:55:47 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-12-27 09:55:47 +0800 |
commit | f3ab053e8c747ca2ea226b264e30460e2f35b683 (patch) | |
tree | 4309eee000929f1857b1350dc509e3a718800b46 | |
parent | 2859989e7e93617704f1cec25ef50ec68a2e38e2 (diff) | |
download | tangerine-mcl-f3ab053e8c747ca2ea226b264e30460e2f35b683.tar.gz tangerine-mcl-f3ab053e8c747ca2ea226b264e30460e2f35b683.tar.zst tangerine-mcl-f3ab053e8c747ca2ea226b264e30460e2f35b683.zip |
use new steram api
-rw-r--r-- | include/mcl/ec.hpp | 14 | ||||
-rw-r--r-- | include/mcl/elgamal.hpp | 4 | ||||
-rw-r--r-- | include/mcl/fp.hpp | 33 | ||||
-rw-r--r-- | test/fp_util_test.cpp | 8 |
4 files changed, 23 insertions, 36 deletions
diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index 48f1b0d..3278e4f 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -717,17 +717,16 @@ public: template<class InputStream> void load(InputStream& is, int ioMode = IoSerialize) { - typedef cybozu::InputStreamTag<InputStream> InputTag; #ifdef MCL_EC_USE_AFFINE inf_ = false; #else z = 1; #endif if (ioMode & IoSerialize) { - if (!isFixedSizeByteSeq()) throw cybozu::Exception("EcT:readStream:not supported ioMode") << ioMode; + if (!isFixedSizeByteSeq()) throw cybozu::Exception("EcT:load:not supported ioMode") << ioMode; char buf[sizeof(Fp)]; const size_t n = Fp::getByteSize(); - if (InputTag::readSome(is, buf, n) != n) throw cybozu::Exception("EcT:readStream:can't read") << n; + if (cybozu::readSome(buf, n, is) != n) throw cybozu::Exception("EcT:load:can't read") << n; if (fp::isZeroArray(buf, n)) { clear(); return; @@ -737,7 +736,8 @@ public: x.setStr(std::string(buf, n), ioMode); getYfromX(y, x, isYodd); } else { - char c = InputTag::readChar(is); + char c = 0; + if (!cybozu::readChar(&c, is)) throw cybozu::Exception("EcT:load:no header"); if (c == '0') { clear(); return; @@ -746,7 +746,7 @@ public: if (c == '1') { y.load(is, ioMode); if (!isValid(x, y)) { - throw cybozu::Exception("EcT:readStream:bad value") << ioMode << x << y; + throw cybozu::Exception("EcT:load:bad value") << ioMode << x << y; } } else if (c == '2' || c == '3') { bool isYodd = c == '3'; @@ -755,11 +755,11 @@ public: y.load(is, ioMode); z.load(is, ioMode); } else { - throw cybozu::Exception("EcT:readStream:bad format") << (int)c; + throw cybozu::Exception("EcT:load:bad format") << (int)c; } } if (verifyOrder_ && !isValidOrder()) { - throw cybozu::Exception("EcT:readStream:bad order") << *this; + throw cybozu::Exception("EcT:load:bad order") << *this; } } std::istream& readStream(std::istream& is, int ioMode = 0) diff --git a/include/mcl/elgamal.hpp b/include/mcl/elgamal.hpp index 028368e..0f8a379 100644 --- a/include/mcl/elgamal.hpp +++ b/include/mcl/elgamal.hpp @@ -77,7 +77,6 @@ struct ElgamalT { { c1.readStream(is, ioMode); c2.readStream(is, ioMode); - if (!is) throw cybozu::Exception("ElgamalT:CipherText:readStream"); return is; } void getStr(std::string& str, int ioMode = 0) const @@ -125,7 +124,6 @@ struct ElgamalT { c1.readStream(is, ioMode); s0.readStream(is, ioMode); s1.readStream(is, ioMode); - if (!is) throw cybozu::Exception("ElgamalT:Zkp:readStream"); return is; } void getStr(std::string& str, int ioMode = 0) const @@ -360,7 +358,6 @@ struct ElgamalT { f.readStream(is, ioMode); g.readStream(is, ioMode); h.readStream(is, ioMode); - if (!is) throw cybozu::Exception("ElgamalT:PublicKey:readStream"); init(bitSize, f, g, h); return is; } @@ -559,7 +556,6 @@ struct ElgamalT { { pub.readStream(is, ioMode); z.readStream(is, ioMode); - if (!is) throw cybozu::Exception("ElgamalT:CipherText:readStream"); return is; } void getStr(std::string& str, int ioMode = 0) const diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index fc879c1..3632502 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -68,23 +68,18 @@ inline bool isSpace(char c) } template<class InputStream> -char skipSpace(InputStream& is) -{ - typedef cybozu::InputStreamTag<InputStream> InputTag; - while (InputTag::hasNext(is)) { - char c = InputTag::readChar(is); - if (!isSpace(c)) return c; - } - throw cybozu::Exception("skipSpace:read"); -} - -template<class InputStream> void loadWord(std::string& s, InputStream& is) { - typedef cybozu::InputStreamTag<InputStream> InputTag; - s = skipSpace(is); - while (InputTag::hasNext(is)) { - char c = InputTag::readChar(is); + s.clear(); + char c; + // skip space + for (;;) { + if (!cybozu::readChar(&c, is)) return; + if (!isSpace(c)) break; + } + s = c; + for (;;) { + if (!cybozu::readChar(&c, is)) return; if (isSpace(c)) break; s += c; } @@ -241,12 +236,11 @@ public: template<class InputStream> void load(InputStream& is, int ioMode = IoSerialize) { - typedef cybozu::InputStreamTag<InputStream> InputTag; bool isMinus = false; if (ioMode & (IoArray | IoArrayRaw | IoSerialize)) { uint8_t buf[sizeof(FpT)]; const size_t n = getByteSize(); - if (InputTag::readSome(is, buf, n) != n) throw cybozu::Exception("FpT:load:can't read") << n; + if (cybozu::readSome(buf, n, is) != n) throw cybozu::Exception("FpT:load:can't read") << n; fp::copyByteToUnitAsLE(v_, buf, n); } else { std::string str; @@ -264,7 +258,6 @@ public: template<class OutputStream> void save(OutputStream& os, int ioMode = IoSerialize) const { - typedef cybozu::OutputStreamTag<OutputStream> OutputTag; const size_t n = getByteSize(); if (ioMode & (IoArray | IoArrayRaw | IoSerialize)) { uint8_t buf[sizeof(FpT)]; @@ -275,7 +268,7 @@ public: getBlock(b); fp::copyUnitToByteAsLE(buf, b.p, n); } - os.write(buf, n); + cybozu::write(os, buf, n); return; } fp::Block b; @@ -283,7 +276,7 @@ public: std::string str; // use low 8-bit ioMode for Fp fp::arrayToStr(str, b.p, b.n, ioMode & 255); - OutputTag::write(os, str.c_str(), str.size()); + cybozu::write(os, str.c_str(), str.size()); } void setStr(const std::string& str, int ioMode = 0) { diff --git a/test/fp_util_test.cpp b/test/fp_util_test.cpp index 2922721..c57e616 100644 --- a/test/fp_util_test.cpp +++ b/test/fp_util_test.cpp @@ -215,8 +215,9 @@ CYBOZU_TEST_AUTO(stream) for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(nulTbl); i++) { const char *p = nulTbl[i]; cybozu::MemoryInputStream is(p, strlen(p)); - std::string w; - CYBOZU_TEST_EXCEPTION(mcl::fp::local::loadWord(w, is), std::exception); + std::string w = "abc"; + mcl::fp::local::loadWord(w, is); + CYBOZU_TEST_ASSERT(w.empty()); } const struct { const char *buf; @@ -237,7 +238,6 @@ CYBOZU_TEST_AUTO(stream) mcl::fp::local::loadWord(w, is); CYBOZU_TEST_EQUAL(w, tbl[i].expect[j]); } - CYBOZU_TEST_ASSERT(!is.hasNext()); } { std::istringstream is(buf); @@ -246,8 +246,6 @@ CYBOZU_TEST_AUTO(stream) mcl::fp::local::loadWord(w, is); CYBOZU_TEST_EQUAL(w, tbl[i].expect[j]); } - typedef cybozu::InputStreamTag<std::istringstream> InputTag; - CYBOZU_TEST_ASSERT(!InputTag::hasNext(is)); } } } |