Ec::serialize supports IoSerializeHexStr

dev
MITSUNARI Shigeo 6 years ago
parent 32a1082d3f
commit d78847f34d
  1. 18
      include/mcl/ec.hpp
  2. 25
      test/ec_test.cpp

@ -686,7 +686,7 @@ public:
} }
EcT P(*this); EcT P(*this);
P.normalize(); P.normalize();
if (ioMode & IoSerialize) { if (ioMode & (IoSerialize | IoSerializeHexStr)) {
/* /*
if (isMSBserialize()) { if (isMSBserialize()) {
// n bytes // 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; return;
} }
if (isZero()) { if (isZero()) {
@ -749,12 +753,18 @@ public:
#else #else
z = 1; z = 1;
#endif #endif
if (ioMode & IoSerialize) { if (ioMode & (IoSerialize | IoSerializeHexStr)) {
const size_t n = Fp::getByteSize(); const size_t n = Fp::getByteSize();
const size_t adj = isMSBserialize() ? 0 : 1; const size_t adj = isMSBserialize() ? 0 : 1;
const size_t n1 = n + adj; const size_t n1 = n + adj;
char buf[sizeof(Fp) + 1]; 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; *pb = false;
return; return;
} }

@ -295,7 +295,7 @@ struct Test {
ss >> Q; ss >> Q;
CYBOZU_TEST_EQUAL(P, Q); CYBOZU_TEST_EQUAL(P, Q);
} }
// IoSerialize // IoSerialize, IoSerializeHexStr
const size_t adj = Ec::isMSBserialize() ? 0 : 1; const size_t adj = Ec::isMSBserialize() ? 0 : 1;
P.set(x, y); P.set(x, y);
{ {
@ -305,12 +305,24 @@ struct Test {
CYBOZU_TEST_EQUAL(P, Q); 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); std::string s = P.getStr(mcl::IoSerialize);
CYBOZU_TEST_EQUAL(s.size(), Fp::getByteSize() + adj); CYBOZU_TEST_EQUAL(s.size(), Fp::getByteSize() + adj);
Q.setStr(s, mcl::IoSerialize); Q.setStr(s, mcl::IoSerialize);
CYBOZU_TEST_EQUAL(P, Q); 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(); P.clear();
{ {
std::string s = P.getStr(mcl::IoSerialize); std::string s = P.getStr(mcl::IoSerialize);
@ -319,6 +331,15 @@ struct Test {
Q.setStr(s, mcl::IoSerialize); Q.setStr(s, mcl::IoSerialize);
CYBOZU_TEST_EQUAL(P, Q); 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 void ioMode() const
{ {

Loading…
Cancel
Save