aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2018-09-24 20:54:18 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2018-09-24 20:54:18 +0800
commit32a1082d3f03f9445ae734aaca16958e7f0ec612 (patch)
tree922ac5da8884646c3b35b8b2cbe70b57d4450c91
parent093a13335dd5390ad08a539c127c2b6663f41a88 (diff)
downloaddexon-mcl-32a1082d3f03f9445ae734aaca16958e7f0ec612.tar.gz
dexon-mcl-32a1082d3f03f9445ae734aaca16958e7f0ec612.tar.zst
dexon-mcl-32a1082d3f03f9445ae734aaca16958e7f0ec612.zip
Fp::serialize() supports IoSerializeHexStr
-rw-r--r--include/mcl/fp.hpp18
-rw-r--r--test/fp_test.cpp19
2 files changed, 32 insertions, 5 deletions
diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp
index 31a1b3a..ca3feae 100644
--- a/include/mcl/fp.hpp
+++ b/include/mcl/fp.hpp
@@ -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,13 +271,17 @@ 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);
- 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;
}
diff --git a/test/fp_test.cpp b/test/fp_test.cpp
index 71d6986..5a478fe 100644
--- a/test/fp_test.cpp
+++ b/test/fp_test.cpp
@@ -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);