diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-12-26 20:54:22 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-12-26 20:54:22 +0800 |
commit | 2859989e7e93617704f1cec25ef50ec68a2e38e2 (patch) | |
tree | ae684e45208313f6d467dcc39adea8d38881c4d4 | |
parent | 95f071a1ce398c8e6d31442b983b5fe0860a60e9 (diff) | |
download | dexon-mcl-2859989e7e93617704f1cec25ef50ec68a2e38e2.tar.gz dexon-mcl-2859989e7e93617704f1cec25ef50ec68a2e38e2.tar.zst dexon-mcl-2859989e7e93617704f1cec25ef50ec68a2e38e2.zip |
use hasNext for loadWord
-rw-r--r-- | include/mcl/ec.hpp | 75 | ||||
-rw-r--r-- | include/mcl/fp.hpp | 142 | ||||
-rw-r--r-- | include/mcl/fp_tower.hpp | 44 | ||||
-rw-r--r-- | include/mcl/op.hpp | 17 | ||||
-rw-r--r-- | sample/she_smpl.cpp | 12 | ||||
-rw-r--r-- | src/fp.cpp | 14 | ||||
-rw-r--r-- | test/bn_test.cpp | 18 | ||||
-rw-r--r-- | test/ec_test.cpp | 14 | ||||
-rw-r--r-- | test/fp_util_test.cpp | 44 | ||||
-rw-r--r-- | test/she_test.cpp | 4 |
10 files changed, 229 insertions, 155 deletions
diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index 299cd00..48f1b0d 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -672,7 +672,7 @@ public: } EcT P(*this); P.normalize(); - if (ioMode & IoFixedSizeByteSeq) { + if (ioMode & IoSerialize) { if (!isFixedSizeByteSeq()) throw cybozu::Exception("EcT:getStr:not supported ioMode") << ioMode; const size_t n = Fp::getByteSize(); if (isZero()) { @@ -714,40 +714,37 @@ public: int ioMode = fp::detectIoMode(getIoMode(), os); return os << self.getStr(ioMode); } - std::istream& readStream(std::istream& is, int ioMode) + 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 & IoFixedSizeByteSeq) { + if (ioMode & IoSerialize) { if (!isFixedSizeByteSeq()) throw cybozu::Exception("EcT:readStream:not supported ioMode") << ioMode; - std::string str; + char buf[sizeof(Fp)]; const size_t n = Fp::getByteSize(); - str.resize(n); - is.read(&str[0], n); - if (!is) throw cybozu::Exception("EcT:readStream:can't read") << n; - if (fp::isZeroArray(&str[0], n)) { + if (InputTag::readSome(is, buf, n) != n) throw cybozu::Exception("EcT:readStream:can't read") << n; + if (fp::isZeroArray(buf, n)) { clear(); - return is; + return; } - bool isYodd = (str[n - 1] >> 7) != 0; - str[n - 1] &= 0x7f; - x.setStr(str, ioMode); + bool isYodd = (buf[n - 1] >> 7) != 0; + buf[n - 1] &= 0x7f; + x.setStr(std::string(buf, n), ioMode); getYfromX(y, x, isYodd); } else { - char c = 0; - if (!(is >> c)) { - throw cybozu::Exception("EcT:readStream:no header"); - } + char c = InputTag::readChar(is); if (c == '0') { clear(); - return is; + return; } - x.readStream(is, ioMode); + x.load(is, ioMode); if (c == '1') { - y.readStream(is, ioMode); + y.load(is, ioMode); if (!isValid(x, y)) { throw cybozu::Exception("EcT:readStream:bad value") << ioMode << x << y; } @@ -755,8 +752,8 @@ public: bool isYodd = c == '3'; getYfromX(y, x, isYodd); } else if (c == '4') { - y.readStream(is, ioMode); - z.readStream(is, ioMode); + y.load(is, ioMode); + z.load(is, ioMode); } else { throw cybozu::Exception("EcT:readStream:bad format") << (int)c; } @@ -764,6 +761,10 @@ public: if (verifyOrder_ && !isValidOrder()) { throw cybozu::Exception("EcT:readStream:bad order") << *this; } + } + std::istream& readStream(std::istream& is, int ioMode = 0) + { + load(is, ioMode); return is; } friend inline std::istream& operator>>(std::istream& is, EcT& self) @@ -779,6 +780,11 @@ public: // return written bytes if sucess else 0 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; @@ -794,33 +800,14 @@ public: } } return n; +#endif } // return positive read bytes if sucess else 0 size_t deserialize(const void *buf, size_t bufSize) { - // QQQ : remove duplicated code -#ifdef MCL_EC_USE_AFFINE - inf_ = false; -#else - z = 1; -#endif - if (!isFixedSizeByteSeq()) return 0; - const size_t n = Fp::getByteSize(); - if (bufSize < n) return 0; - char p[sizeof(Fp)]; - memcpy(p, buf, n); - if (fp::isZeroArray(p, n)) { - clear(); - return n; - } - bool isYodd = (p[n - 1] >> 7) != 0; - p[n - 1] &= 0x7f; - if (x.deserialize(p, n) == 0) return 0; - getYfromX(y, x, isYodd); - if (verifyOrder_ && !isValidOrder()) { - return 0; - } - return n; + cybozu::MemoryInputStream is(buf, bufSize); + load(is, IoSerialize); + return is.getPos(); } // deplicated static void setCompressedExpression(bool compressedExpression = true) diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index d80672c..fc879c1 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -60,6 +60,38 @@ bool isEnableJIT(); // 1st call is not threadsafe // hash msg std::string hash(size_t bitSize, const void *msg, size_t msgSize); +namespace local { + +inline bool isSpace(char c) +{ + return c == ' ' || c == '\t' || c == '\r' || c == '\n'; +} + +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); + if (isSpace(c)) break; + s += c; + } +} + +} // local + } // mcl::fp template<class tag = FpTag, size_t maxBitSize = MCL_MAX_BIT_SIZE> @@ -206,66 +238,77 @@ public: { if (isMont()) op_.fromMont(v_, v_); } - std::istream& readStream(std::istream& is, int ioMode) + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) { - const size_t n = getByteSize(); - if (ioMode & (IoArray | IoFixedSizeByteSeq)) { - load(is); - return is; - } - if (ioMode & IoArrayRaw) { + typedef cybozu::InputStreamTag<InputStream> InputTag; + bool isMinus = false; + if (ioMode & (IoArray | IoArrayRaw | IoSerialize)) { uint8_t buf[sizeof(FpT)]; - is.read(reinterpret_cast<char*>(&buf[0]), n); + const size_t n = getByteSize(); + if (InputTag::readSome(is, buf, n) != n) throw cybozu::Exception("FpT:load:can't read") << n; fp::copyByteToUnitAsLE(v_, buf, n); - return is; + } else { + std::string str; + fp::local::loadWord(str, is); + fp::strToArray(&isMinus, v_, op_.N, str, ioMode); } - bool isMinus; - fp::streamToArray(&isMinus, v_, n, is, ioMode); - if (fp::isGreaterOrEqualArray(v_, op_.p, op_.N)) throw cybozu::Exception("FpT:readStream:large value"); + if (fp::isGreaterOrEqualArray(v_, op_.p, op_.N)) throw cybozu::Exception("FpT:load:large value"); if (isMinus) { neg(*this, *this); } - toMont(); - return is; - } - void setStr(const std::string& str, int ioMode = 0) - { - std::istringstream is(str); - readStream(is, ioMode); + if (!(ioMode & IoArrayRaw)) { + toMont(); + } } template<class OutputStream> - size_t save(OutputStream& os) const + void save(OutputStream& os, int ioMode = IoSerialize) const { - uint8_t buf[sizeof(FpT)]; + typedef cybozu::OutputStreamTag<OutputStream> OutputTag; + const size_t n = getByteSize(); + if (ioMode & (IoArray | IoArrayRaw | IoSerialize)) { + uint8_t buf[sizeof(FpT)]; + if (ioMode & IoArrayRaw) { + fp::copyUnitToByteAsLE(buf, v_, n); + } else { + fp::Block b; + getBlock(b); + fp::copyUnitToByteAsLE(buf, b.p, n); + } + os.write(buf, n); + return; + } fp::Block b; getBlock(b); - const size_t n = getByteSize(); - fp::copyUnitToByteAsLE(buf, b.p, n); - cybozu::saveRange(os, buf, n); - return n; + 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()); + } + void setStr(const std::string& str, int ioMode = 0) + { + cybozu::StringInputStream is(str); + load(is, ioMode); } template<class InputStream> - size_t load(InputStream& is) + InputStream& readStream(InputStream& is, int ioMode = 0) { - uint8_t buf[sizeof(FpT)]; - const size_t n = getByteSize(); - cybozu::loadRange(buf, n, is); - fp::copyByteToUnitAsLE(v_, buf, n); - if (fp::isGreaterOrEqualArray(v_, op_.p, op_.N)) return 0; - toMont(); - return n; + load(is, ioMode); + return is; } - // return written bytes if sucess else 0 + // return written bytes size_t serialize(void *buf, size_t maxBufSize) const { cybozu::MemoryOutputStream os(buf, maxBufSize); - return save(os); + save(os); + return os.getPos(); } - // return positive read bytes if sucess else 0 + // return read bytes size_t deserialize(const void *buf, size_t bufSize) { cybozu::MemoryInputStream is(buf, bufSize); - return load(is); + load(is); + return is.getPos(); } /* throw exception if x >= p @@ -313,31 +356,12 @@ public: { setHashOf(msg.data(), msg.size()); } -#ifdef _MSC_VER - #pragma warning(push) - #pragma warning(disable:4701) -#endif void getStr(std::string& str, int ioMode = 0) const { - const size_t n = getByteSize(); - if (ioMode & (IoArray | IoFixedSizeByteSeq)) { - str.resize(n); - serialize(&str[0], n); - return; - } - if (ioMode & IoArrayRaw) { - str.resize(n); - fp::copyUnitToByteAsLE(reinterpret_cast<uint8_t*>(&str[0]), v_, n); - return; - } - fp::Block b; - getBlock(b); - // use low 8-bit ioMode for Fp - fp::arrayToStr(str, b.p, b.n, ioMode & 255); + str.clear(); + cybozu::StringOutputStream os(str); + save(os, ioMode); } -#ifdef _MSC_VER - #pragma warning(pop) -#endif std::string getStr(int ioMode = 0) const { std::string str; diff --git a/include/mcl/fp_tower.hpp b/include/mcl/fp_tower.hpp index e31afec..f166d11 100644 --- a/include/mcl/fp_tower.hpp +++ b/include/mcl/fp_tower.hpp @@ -234,16 +234,21 @@ public: a.setArray(buf, n); b.setArray(buf + n, n); } + template<class InputStream> + void load(InputStream& is, int ioMode) + { + a.load(is, ioMode); + b.load(is, ioMode); + } std::istream& readStream(std::istream& is, int ioMode) { - a.readStream(is, ioMode); - b.readStream(is, ioMode); + load(is, ioMode); return is; } void setStr(const std::string& str, int ioMode = 0) { std::istringstream is(str); - readStream(is, ioMode); + load(is, ioMode); } /* Fp2T = <a> + ' ' + <b> @@ -263,7 +268,8 @@ public: } friend std::istream& operator>>(std::istream& is, Fp2T& self) { - return self.readStream(is, fp::detectIoMode(Fp::BaseFp::getIoMode(), is)); + self.load(is, fp::detectIoMode(Fp::BaseFp::getIoMode(), is)); + return is; } friend std::ostream& operator<<(std::ostream& os, const Fp2T& self) { @@ -752,17 +758,22 @@ struct Fp6T : public fp::Operator<Fp6T<Fp> > { return a == rhs.a && b == rhs.b && c == rhs.c; } bool operator!=(const Fp6T& rhs) const { return !operator==(rhs); } + template<class InputStream> + void load(InputStream& is, int ioMode) + { + a.load(is, ioMode); + b.load(is, ioMode); + c.load(is, ioMode); + } std::istream& readStream(std::istream& is, int ioMode) { - a.readStream(is, ioMode); - b.readStream(is, ioMode); - c.readStream(is, ioMode); + load(is, ioMode); return is; } void setStr(const std::string& str, int ioMode = 0) { std::istringstream is(str); - readStream(is, ioMode); + load(is, ioMode); } void getStr(std::string& str, int ioMode = 0) const { @@ -781,7 +792,8 @@ struct Fp6T : public fp::Operator<Fp6T<Fp> > { } friend std::istream& operator>>(std::istream& is, Fp6T& self) { - return self.readStream(is, fp::detectIoMode(Fp::BaseFp::getIoMode(), is)); + self.load(is, fp::detectIoMode(Fp::BaseFp::getIoMode(), is)); + return is; } friend std::ostream& operator<<(std::ostream& os, const Fp6T& self) { @@ -1170,16 +1182,21 @@ struct Fp12T : public fp::Operator<Fp12T<Fp> > { } #endif } + template<class InputStream> + void load(InputStream& is, int ioMode) + { + a.load(is, ioMode); + b.load(is, ioMode); + } std::istream& readStream(std::istream& is, int ioMode) { - a.readStream(is, ioMode); - b.readStream(is, ioMode); + load(is, ioMode); return is; } void setStr(const std::string& str, int ioMode = 0) { std::istringstream is(str); - readStream(is, ioMode); + load(is, ioMode); } void getStr(std::string& str, int ioMode = 0) const { @@ -1196,7 +1213,8 @@ struct Fp12T : public fp::Operator<Fp12T<Fp> > { } friend std::istream& operator>>(std::istream& is, Fp12T& self) { - return self.readStream(is, fp::detectIoMode(Fp::getIoMode(), is)); + self.load(is, fp::detectIoMode(Fp::getIoMode(), is)); + return is; } friend std::ostream& operator<<(std::ostream& os, const Fp12T& self) { diff --git a/include/mcl/op.hpp b/include/mcl/op.hpp index eb9dfbe..8c9b352 100644 --- a/include/mcl/op.hpp +++ b/include/mcl/op.hpp @@ -30,14 +30,14 @@ namespace mcl { byte string(not zero terminated, fixed size) IoArray | IoArrayRaw - IoArray = IoFixedSizeByteSeq + IoArray = IoSerialize // for Ec affine(0) | IoEcCompY | IoComp default : affine affine and IoEcCompY are available with ioMode for Fp - IoFixedSizeByteSeq ignores ioMode for Fp + IoSerialize ignores ioMode for Fp IoAuto dec or hex according to ios_base::fmtflags @@ -69,7 +69,7 @@ namespace mcl { "2 <x>" ; compressed for even y "3 <x>" ; compressed for odd y - IoFixedSizeByteSeq(fixed size = Fp::getByteSize()) + IoSerialize(fixed size = Fp::getByteSize()) use MSB of array of x for 1-bit y for prime p where (p % 8 != 0) [0] ; infinity <x> ; for even y @@ -87,7 +87,8 @@ enum IoMode { IoHexPrefix = IoHex | IoPrefix, IoEcAffine = 0, // affine coordinate IoEcCompY = 256, // 1-bit y representation of elliptic curve - IoFixedSizeByteSeq = 512, // use MBS for 1-bit y + IoSerialize = 512, // use MBS for 1-bit y + IoFixedSizeByteSeq = IoSerialize, // obsolete IoEcProj = 1024 // projective or jacobi coordinate }; @@ -297,17 +298,15 @@ private: }; /* - read data from is according to ioMode, - and set x[0, n) with abs(buf[0, bufSize/sizeof(Unit))) - @note byteSize is not num of Unit + conevrt string to array according to ioMode, */ -void streamToArray(bool *pIsMinus, Unit *x, size_t byteSize, std::istream& is, int ioMode); +void strToArray(bool *pIsMinus, Unit *x, size_t xN, const std::string& str, int ioMode); void arrayToStr(std::string& str, const Unit *x, size_t n, int ioMode); inline const char* getIoSeparator(int ioMode) { - return (ioMode & (IoArray | IoArrayRaw | IoFixedSizeByteSeq)) ? "" : " "; + return (ioMode & (IoArray | IoArrayRaw | IoSerialize)) ? "" : " "; } int detectIoMode(int ioMode, const std::ios_base& ios); diff --git a/sample/she_smpl.cpp b/sample/she_smpl.cpp index ab60684..06d8fac 100644 --- a/sample/she_smpl.cpp +++ b/sample/she_smpl.cpp @@ -96,21 +96,21 @@ void usePrimitiveCipherText() printf("err m=%d ok=%d\n", m, ok); } std::string s; - s = c1.getStr(mcl::IoFixedSizeByteSeq); // serialize + s = c1.getStr(mcl::IoSerialize); // serialize printf("c1 data size %d byte\n", (int)s.size()); - c2.setStr(s, mcl::IoFixedSizeByteSeq); + c2.setStr(s, mcl::IoSerialize); printf("deserialize %s\n", c1 == c2 ? "ok" : "ng"); - s = d1.getStr(mcl::IoFixedSizeByteSeq); // serialize + s = d1.getStr(mcl::IoSerialize); // serialize printf("d1 data size %d byte\n", (int)s.size()); - d2.setStr(s, mcl::IoFixedSizeByteSeq); + d2.setStr(s, mcl::IoSerialize); printf("deserialize %s\n", d1 == d2 ? "ok" : "ng"); - s = cm.getStr(mcl::IoFixedSizeByteSeq); // serialize + s = cm.getStr(mcl::IoSerialize); // serialize printf("cm data size %d byte\n", (int)s.size()); CipherTextGT cm2; - cm2.setStr(s, mcl::IoFixedSizeByteSeq); + cm2.setStr(s, mcl::IoSerialize); printf("deserialize %s\n", cm == cm2 ? "ok" : "ng"); } @@ -600,23 +600,17 @@ int detectIoMode(int ioMode, const std::ios_base& ios) return ioMode; } -void streamToArray(bool *pIsMinus, Unit *x, size_t byteSize, std::istream& is, int ioMode) +void strToArray(bool *pIsMinus, Unit *x, size_t xN, const std::string& str, int ioMode) { - assert(!(ioMode & (IoArray | IoArrayRaw | IoFixedSizeByteSeq))); - std::string str; - is >> str; + assert(!(ioMode & (IoArray | IoArrayRaw | IoSerialize))); // use low 8-bit ioMode for Fp ioMode &= 0xff; const char *p = verifyStr(pIsMinus, &ioMode, str); mpz_class mx; if (!gmp::setStr(mx, p, ioMode)) { - throw cybozu::Exception("fp:streamToArray:bad format") << ioMode << str; - } - const size_t n = (byteSize + sizeof(Unit) - 1) / sizeof(Unit); - gmp::getArray(x, n, mx); - if (!is) { - throw cybozu::Exception("streamToArray:can't read") << byteSize; + throw cybozu::Exception("fp:strToArray:bad format") << ioMode << str; } + gmp::getArray(x, xN, mx); } void copyAndMask(Unit *y, const void *x, size_t xByteSize, const Op& op, bool doMask) diff --git a/test/bn_test.cpp b/test/bn_test.cpp index 2d07199..b431232 100644 --- a/test/bn_test.cpp +++ b/test/bn_test.cpp @@ -325,8 +325,8 @@ void testTrivial(const G1& P, const G2& Q) void testIoAll(const G1& P, const G2& Q) { - int FpTbl[] = { 0, 2, 2|mcl::IoPrefix, 10, 16, 16|mcl::IoPrefix, mcl::IoArray, mcl::IoArrayRaw }; - int EcTbl[] = { mcl::IoEcAffine, mcl::IoEcProj, mcl::IoEcCompY, mcl::IoFixedSizeByteSeq }; + const int FpTbl[] = { 0, 2, 2|mcl::IoPrefix, 10, 16, 16|mcl::IoPrefix, mcl::IoArray, mcl::IoArrayRaw }; + const int EcTbl[] = { mcl::IoEcAffine, mcl::IoEcProj, mcl::IoEcCompY, mcl::IoSerialize }; for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(FpTbl); i++) { for (size_t j = 0; j < CYBOZU_NUM_OF_ARRAY(EcTbl); j++) { G1 P2 = P, P3; @@ -338,6 +338,14 @@ void testIoAll(const G1& P, const G2& Q) s = Q2.getStr(ioMode); Q3.setStr(s, ioMode); CYBOZU_TEST_EQUAL(Q2, Q3); + s = P.x.getStr(ioMode); + Fp Px; + Px.setStr(s, ioMode); + CYBOZU_TEST_EQUAL(P.x, Px); + s = Q.x.getStr(ioMode); + Fp2 Qx; + Qx.setStr(s, ioMode); + CYBOZU_TEST_EQUAL(Q.x, Qx); } } } @@ -356,10 +364,10 @@ CYBOZU_TEST_AUTO(naive) { for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(g_testSetTbl); i++) { const TestSet& ts = g_testSetTbl[i]; - printf("curve=%s\n", ts.name); + printf("i=%d curve=%s\n", int(i), ts.name); initPairing(ts.cp, g_mode); - G1 P(ts.g1.a, ts.g1.b); - G2 Q(Fp2(ts.g2.aa, ts.g2.ab), Fp2(ts.g2.ba, ts.g2.bb)); + const G1 P(ts.g1.a, ts.g1.b); + const G2 Q(Fp2(ts.g2.aa, ts.g2.ab), Fp2(ts.g2.ba, ts.g2.bb)); #ifdef ONLY_BENCH testPairing(P, Q, ts.e); clk.put(); diff --git a/test/ec_test.cpp b/test/ec_test.cpp index 171a912..a4c4a62 100644 --- a/test/ec_test.cpp +++ b/test/ec_test.cpp @@ -280,28 +280,28 @@ struct Test { ss >> Q; CYBOZU_TEST_EQUAL(P, Q); } - // IoFixedSizeByteSeq + // IoSerialize if (!Ec::isFixedSizeByteSeq()) return; P.set(x, y); { - std::string s = P.getStr(mcl::IoFixedSizeByteSeq); + std::string s = P.getStr(mcl::IoSerialize); CYBOZU_TEST_EQUAL(s.size(), Fp::getByteSize()); - Q.setStr(s, mcl::IoFixedSizeByteSeq); + Q.setStr(s, mcl::IoSerialize); CYBOZU_TEST_EQUAL(P, Q); } { P = -P; - std::string s = P.getStr(mcl::IoFixedSizeByteSeq); + std::string s = P.getStr(mcl::IoSerialize); CYBOZU_TEST_EQUAL(s.size(), Fp::getByteSize()); - Q.setStr(s, mcl::IoFixedSizeByteSeq); + Q.setStr(s, mcl::IoSerialize); CYBOZU_TEST_EQUAL(P, Q); } P.clear(); { - std::string s = P.getStr(mcl::IoFixedSizeByteSeq); + std::string s = P.getStr(mcl::IoSerialize); CYBOZU_TEST_EQUAL(s.size(), Fp::getByteSize()); CYBOZU_TEST_ASSERT(mcl::fp::isZeroArray(s.c_str(), s.size())); - Q.setStr(s, mcl::IoFixedSizeByteSeq); + Q.setStr(s, mcl::IoSerialize); CYBOZU_TEST_EQUAL(P, Q); } } diff --git a/test/fp_util_test.cpp b/test/fp_util_test.cpp index 1e99faf..2922721 100644 --- a/test/fp_util_test.cpp +++ b/test/fp_util_test.cpp @@ -2,6 +2,7 @@ #include "../src/conversion.hpp" #include <cybozu/test.hpp> #include <mcl/gmp_util.hpp> +#include <mcl/fp.hpp> CYBOZU_TEST_AUTO(toStr16) { @@ -207,3 +208,46 @@ CYBOZU_TEST_AUTO(maskArray) } #endif } + +CYBOZU_TEST_AUTO(stream) +{ + const char *nulTbl[] = { "", " ", " \t\t\n\n " }; + 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); + } + const struct { + const char *buf; + const char *expect[2]; + size_t n; + } tbl[] = { + { "\t\t \n\rabc\r\r\n def", { "abc", "def" }, 2 }, + { "123", { "123" }, 1 }, + { "123\n", { "123" }, 1 }, + { "123 456", { "123", "456" }, 2 }, + }; + for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { + const char *buf = tbl[i].buf; + { + cybozu::MemoryInputStream is(buf, strlen(buf)); + for (size_t j = 0; j < tbl[i].n; j++) { + std::string w; + mcl::fp::local::loadWord(w, is); + CYBOZU_TEST_EQUAL(w, tbl[i].expect[j]); + } + CYBOZU_TEST_ASSERT(!is.hasNext()); + } + { + std::istringstream is(buf); + for (size_t j = 0; j < tbl[i].n; j++) { + std::string w; + 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)); + } + } +} diff --git a/test/she_test.cpp b/test/she_test.cpp index 3f9fc38..5e5fdab 100644 --- a/test/she_test.cpp +++ b/test/she_test.cpp @@ -288,8 +288,8 @@ CYBOZU_TEST_AUTO(io) int m; for (int i = 0; i < 2; i++) { if (i == 1) { - Fp::setIoMode(mcl::IoFixedSizeByteSeq); - G1::setIoMode(mcl::IoFixedSizeByteSeq); + Fp::setIoMode(mcl::IoSerialize); + G1::setIoMode(mcl::IoSerialize); } SecretKey sec; sec.setByCSPRNG(); |