diff --git a/include/mcl/fp_tower.hpp b/include/mcl/fp_tower.hpp index 95722e2..8d79a7e 100644 --- a/include/mcl/fp_tower.hpp +++ b/include/mcl/fp_tower.hpp @@ -283,9 +283,13 @@ public: template void load(bool *pb, InputStream& is, int ioMode) { - a.load(pb, is, ioMode); + Fp *ap = &a, *bp = &b; + if (Fp::isETHserialization_ && ioMode & (IoSerialize | IoSerializeHexStr)) { + fp::swap_(ap, bp); + } + ap->load(pb, is, ioMode); if (!*pb) return; - b.load(pb, is, ioMode); + bp->load(pb, is, ioMode); } /* Fp2T = + ' ' + @@ -293,14 +297,18 @@ public: template void save(bool *pb, OutputStream& os, int ioMode) const { + const Fp *ap = &a, *bp = &b; + if (Fp::isETHserialization_ && ioMode & (IoSerialize | IoSerializeHexStr)) { + fp::swap_(ap, bp); + } const char sep = *fp::getIoSeparator(ioMode); - a.save(pb, os, ioMode); + ap->save(pb, os, ioMode); if (!*pb) return; if (sep) { cybozu::writeChar(pb, os, sep); if (!*pb) return; } - b.save(pb, os, ioMode); + bp->save(pb, os, ioMode); } bool isZero() const { return a.isZero() && b.isZero(); } bool isOne() const { return a.isOne() && b.isZero(); } diff --git a/test/fp_tower_test.cpp b/test/fp_tower_test.cpp index a7123f7..c26c5d7 100644 --- a/test/fp_tower_test.cpp +++ b/test/fp_tower_test.cpp @@ -146,6 +146,18 @@ void testFp2() CYBOZU_TEST_ASSERT(Fp2::squareRoot(z, y)); CYBOZU_TEST_EQUAL(z * z, y); } + + // serialize + for (int i = 0; i < 2; i++) { + Fp::setETHserialization(i == 0); + Fp2 x, y; + x.setStr("0x1234567789345 0x23424324"); + char buf[256]; + size_t n = x.serialize(buf, sizeof(buf)); + CYBOZU_TEST_ASSERT(n > 0); + CYBOZU_TEST_EQUAL(y.deserialize(buf, n), n); + CYBOZU_TEST_EQUAL(x, y); + } } void testFp6sqr(const Fp2& a, const Fp2& b, const Fp2& c, const Fp6& x) @@ -390,6 +402,7 @@ void test(const char *p, mcl::fp::Mode mode) Fp::init(xi_a, p, mode); printf("mode=%s\n", mcl::fp::ModeToStr(mode)); Fp2::init(); + printf("bitSize=%d\n", (int)Fp::getBitSize()); #if 0 if (Fp::getBitSize() > 256) { printf("not support p=%s\n", p); @@ -446,7 +459,7 @@ void testAll() }; for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { const char *p = tbl[i]; - printf("prime=%s %d\n", p, (int)(strlen(p) - 2) * 4); + printf("prime=%s\n", p); test(p, mcl::fp::FP_GMP); #ifdef MCL_USE_LLVM test(p, mcl::fp::FP_LLVM);