Fp::serialize() supports IoSerializeHexStr

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

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

@ -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>
#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();
getStrTest();
setHashOfTest();
serializeTest();
}
anotherFpTest(mode);
setArrayTest2(mode);

Loading…
Cancel
Save