big endian serialization for Fp2

pull/2/head
MITSUNARI Shigeo 6 years ago
parent 838f081855
commit 0234589658
  1. 16
      include/mcl/fp_tower.hpp
  2. 15
      test/fp_tower_test.cpp

@ -283,9 +283,13 @@ public:
template<class InputStream> template<class InputStream>
void load(bool *pb, InputStream& is, int ioMode) 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; if (!*pb) return;
b.load(pb, is, ioMode); bp->load(pb, is, ioMode);
} }
/* /*
Fp2T = <a> + ' ' + <b> Fp2T = <a> + ' ' + <b>
@ -293,14 +297,18 @@ public:
template<class OutputStream> template<class OutputStream>
void save(bool *pb, OutputStream& os, int ioMode) const 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); const char sep = *fp::getIoSeparator(ioMode);
a.save(pb, os, ioMode); ap->save(pb, os, ioMode);
if (!*pb) return; if (!*pb) return;
if (sep) { if (sep) {
cybozu::writeChar(pb, os, sep); cybozu::writeChar(pb, os, sep);
if (!*pb) return; if (!*pb) return;
} }
b.save(pb, os, ioMode); bp->save(pb, os, ioMode);
} }
bool isZero() const { return a.isZero() && b.isZero(); } bool isZero() const { return a.isZero() && b.isZero(); }
bool isOne() const { return a.isOne() && b.isZero(); } bool isOne() const { return a.isOne() && b.isZero(); }

@ -146,6 +146,18 @@ void testFp2()
CYBOZU_TEST_ASSERT(Fp2::squareRoot(z, y)); CYBOZU_TEST_ASSERT(Fp2::squareRoot(z, y));
CYBOZU_TEST_EQUAL(z * 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) 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); Fp::init(xi_a, p, mode);
printf("mode=%s\n", mcl::fp::ModeToStr(mode)); printf("mode=%s\n", mcl::fp::ModeToStr(mode));
Fp2::init(); Fp2::init();
printf("bitSize=%d\n", (int)Fp::getBitSize());
#if 0 #if 0
if (Fp::getBitSize() > 256) { if (Fp::getBitSize() > 256) {
printf("not support p=%s\n", p); 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++) { for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
const char *p = 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); test(p, mcl::fp::FP_GMP);
#ifdef MCL_USE_LLVM #ifdef MCL_USE_LLVM
test(p, mcl::fp::FP_LLVM); test(p, mcl::fp::FP_LLVM);

Loading…
Cancel
Save