Fp::serialize() supports IoSerializeHexStr

dev
MITSUNARI Shigeo 6 years ago
parent 093a13335d
commit 32a1082d3f
  1. 18
      include/mcl/fp.hpp
  2. 19
      test/fp_test.cpp

@ -238,12 +238,16 @@ public:
{ {
bool isMinus = false; bool isMinus = false;
*pb = false; *pb = false;
if (ioMode & (IoArray | IoArrayRaw | IoSerialize)) { if (ioMode & (IoArray | IoArrayRaw | IoSerialize | IoSerializeHexStr)) {
const size_t n = getByteSize(); const size_t n = getByteSize();
v_[op_.N - 1] = 0; v_[op_.N - 1] = 0;
if (cybozu::readSome(v_, n, is) != n) { size_t readSize;
return; if (ioMode & IoSerializeHexStr) {
readSize = mcl::fp::readHexStr(v_, n, is);
} else {
readSize = cybozu::readSome(v_, n, is);
} }
if (readSize != n) return;
} else { } else {
char buf[1024]; char buf[1024];
size_t n = fp::local::loadWord(buf, sizeof(buf), is); size_t n = fp::local::loadWord(buf, sizeof(buf), is);
@ -267,13 +271,17 @@ public:
void save(bool *pb, OutputStream& os, int ioMode) const void save(bool *pb, OutputStream& os, int ioMode) const
{ {
const size_t n = getByteSize(); const size_t n = getByteSize();
if (ioMode & (IoArray | IoArrayRaw | IoSerialize)) { if (ioMode & (IoArray | IoArrayRaw | IoSerialize | IoSerializeHexStr)) {
if (ioMode & IoArrayRaw) { if (ioMode & IoArrayRaw) {
cybozu::write(pb, os, v_, n); cybozu::write(pb, os, v_, n);
} else { } else {
fp::Block b; fp::Block b;
getBlock(b); getBlock(b);
cybozu::write(pb, os, b.p, n); if (ioMode & IoSerializeHexStr) {
*pb = mcl::fp::writeHexStr(os, b.p, n);
} else {
cybozu::write(pb, os, b.p, n);
}
} }
return; return;
} }

@ -768,6 +768,24 @@ CYBOZU_TEST_AUTO(getArray)
} }
} }
void serializeTest()
{
const char *tbl[] = { "0", "-1", "123" };
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
char buf[1024];
Fp x, y;
x.setStr(tbl[i]);
size_t n = x.serialize(buf, sizeof(buf));
CYBOZU_TEST_EQUAL(n, Fp::getByteSize());
y.deserialize(buf, n);
CYBOZU_TEST_EQUAL(x, y);
n = x.serialize(buf, sizeof(buf), mcl::IoSerializeHexStr);
CYBOZU_TEST_EQUAL(n, Fp::getByteSize() * 2);
y.deserialize(buf, n, mcl::IoSerializeHexStr);
CYBOZU_TEST_EQUAL(x, y);
}
}
#include <iostream> #include <iostream>
#if (defined(MCL_USE_LLVM) || defined(MCL_USE_XBYAK)) && (MCL_MAX_BIT_SIZE >= 521) #if (defined(MCL_USE_LLVM) || defined(MCL_USE_XBYAK)) && (MCL_MAX_BIT_SIZE >= 521)
@ -877,6 +895,7 @@ void sub(mcl::fp::Mode mode)
divBy2Test(); divBy2Test();
getStrTest(); getStrTest();
setHashOfTest(); setHashOfTest();
serializeTest();
} }
anotherFpTest(mode); anotherFpTest(mode);
setArrayTest2(mode); setArrayTest2(mode);

Loading…
Cancel
Save