diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index b3350b9..9b09486 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -686,7 +686,7 @@ public: } EcT P(*this); P.normalize(); - if (ioMode & IoSerialize) { + if (ioMode & (IoSerialize | IoSerializeHexStr)) { /* if (isMSBserialize()) { // n bytes @@ -712,7 +712,11 @@ public: } } } - cybozu::write(pb, os, buf, n + adj); + if (ioMode & IoSerializeHexStr) { + *pb = mcl::fp::writeHexStr(os, buf, n + adj); + } else { + cybozu::write(pb, os, buf, n + adj); + } return; } if (isZero()) { @@ -749,12 +753,18 @@ public: #else z = 1; #endif - if (ioMode & IoSerialize) { + if (ioMode & (IoSerialize | IoSerializeHexStr)) { const size_t n = Fp::getByteSize(); const size_t adj = isMSBserialize() ? 0 : 1; const size_t n1 = n + adj; char buf[sizeof(Fp) + 1]; - if (cybozu::readSome(buf, n1, is) != n1) { + size_t readSize; + if (ioMode & IoSerializeHexStr) { + readSize = mcl::fp::readHexStr(buf, n1, is); + } else { + readSize = cybozu::readSome(buf, n1, is); + } + if (readSize != n1) { *pb = false; return; } diff --git a/test/ec_test.cpp b/test/ec_test.cpp index ae42c67..a78357e 100644 --- a/test/ec_test.cpp +++ b/test/ec_test.cpp @@ -295,7 +295,7 @@ struct Test { ss >> Q; CYBOZU_TEST_EQUAL(P, Q); } - // IoSerialize + // IoSerialize, IoSerializeHexStr const size_t adj = Ec::isMSBserialize() ? 0 : 1; P.set(x, y); { @@ -305,12 +305,24 @@ struct Test { CYBOZU_TEST_EQUAL(P, Q); } { - P = -P; + std::string s = P.getStr(mcl::IoSerializeHexStr); + CYBOZU_TEST_EQUAL(s.size(), (Fp::getByteSize() + adj) * 2); + Q.setStr(s, mcl::IoSerializeHexStr); + CYBOZU_TEST_EQUAL(P, Q); + } + P = -P; + { std::string s = P.getStr(mcl::IoSerialize); CYBOZU_TEST_EQUAL(s.size(), Fp::getByteSize() + adj); Q.setStr(s, mcl::IoSerialize); CYBOZU_TEST_EQUAL(P, Q); } + { + std::string s = P.getStr(mcl::IoSerializeHexStr); + CYBOZU_TEST_EQUAL(s.size(), (Fp::getByteSize() + adj) * 2); + Q.setStr(s, mcl::IoSerializeHexStr); + CYBOZU_TEST_EQUAL(P, Q); + } P.clear(); { std::string s = P.getStr(mcl::IoSerialize); @@ -319,6 +331,15 @@ struct Test { Q.setStr(s, mcl::IoSerialize); CYBOZU_TEST_EQUAL(P, Q); } + { + std::string s = P.getStr(mcl::IoSerializeHexStr); + CYBOZU_TEST_EQUAL(s.size(), (Fp::getByteSize() + adj) * 2); + for (size_t i = 0; i < s.size(); i++) { + CYBOZU_TEST_EQUAL(s[i], '0'); + } + Q.setStr(s, mcl::IoSerializeHexStr); + CYBOZU_TEST_EQUAL(P, Q); + } } void ioMode() const {