From 2095a80e17e4e90fea3361d9190f0cebd5982aa2 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Wed, 10 Jun 2020 14:07:58 +0900 Subject: [PATCH] aggs supports serialize --- include/mcl/aggregate_sig.hpp | 60 +++++++++++++++++++++++++++++++---- test/aggregate_sig_test.cpp | 30 ++++++++++++++++++ 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/include/mcl/aggregate_sig.hpp b/include/mcl/aggregate_sig.hpp index f314057..9ee824c 100644 --- a/include/mcl/aggregate_sig.hpp +++ b/include/mcl/aggregate_sig.hpp @@ -62,16 +62,32 @@ public: friend class SecretKey; friend class PublicKey; public: + template + void load(bool *pb, InputStream& is, int ioMode = IoSerialize) + { + S_.load(pb, is, ioMode); + } + template + void save(bool *pb, OutputStream& os, int ioMode = IoSerialize) const + { + S_.save(pb, os, ioMode); + } +#ifndef CYBOZU_DONT_USE_EXCEPTION template void load(InputStream& is, int ioMode = IoSerialize) { - S_.load(is, ioMode); + bool b; + load(&b, is, ioMode); + if (!b) throw cybozu::Exception("Signature:load"); } template void save(OutputStream& os, int ioMode = IoSerialize) const { - S_.save(os, ioMode); + bool b; + save(&b, os, ioMode); + if (!b) throw cybozu::Exception("Signature:save"); } +#endif friend std::istream& operator>>(std::istream& is, Signature& self) { self.load(is, fp::detectIoMode(G1::getIoMode(), is)); @@ -155,16 +171,32 @@ public: friend class SecretKey; friend class Signature; public: + template + void load(bool *pb, InputStream& is, int ioMode = IoSerialize) + { + xQ_.load(pb, is, ioMode); + } + template + void save(bool *pb, OutputStream& os, int ioMode = IoSerialize) const + { + xQ_.save(pb, os, ioMode); + } +#ifndef CYBOZU_DONT_USE_EXCEPTION template void load(InputStream& is, int ioMode = IoSerialize) { - xQ_.load(is, ioMode); + bool b; + load(&b, is, ioMode); + if (!b) throw cybozu::Exception("PublicKey:load"); } template void save(OutputStream& os, int ioMode = IoSerialize) const { - xQ_.save(os, ioMode); + bool b; + save(&b, os, ioMode); + if (!b) throw cybozu::Exception("PublicKey:save"); } +#endif friend std::istream& operator>>(std::istream& is, PublicKey& self) { self.load(is, fp::detectIoMode(G2::getIoMode(), is)); @@ -208,16 +240,32 @@ public: friend class PublicKey; friend class Signature; public: + template + void load(bool *pb, InputStream& is, int ioMode = IoSerialize) + { + x_.load(pb, is, ioMode); + } + template + void save(bool *pb, OutputStream& os, int ioMode = IoSerialize) const + { + x_.save(pb, os, ioMode); + } +#ifndef CYBOZU_DONT_USE_EXCEPTION template void load(InputStream& is, int ioMode = IoSerialize) { - x_.load(is, ioMode); + bool b; + load(&b, is, ioMode); + if (!b) throw cybozu::Exception("SecretKey:load"); } template void save(OutputStream& os, int ioMode = IoSerialize) const { - x_.save(os, ioMode); + bool b; + save(&b, os, ioMode); + if (!b) throw cybozu::Exception("SecretKey:save"); } +#endif friend std::istream& operator>>(std::istream& is, SecretKey& self) { self.load(is, fp::detectIoMode(Fr::getIoMode(), is)); diff --git a/test/aggregate_sig_test.cpp b/test/aggregate_sig_test.cpp index c3a0e75..33b8609 100644 --- a/test/aggregate_sig_test.cpp +++ b/test/aggregate_sig_test.cpp @@ -21,6 +21,36 @@ CYBOZU_TEST_AUTO(init) CYBOZU_TEST_ASSERT(pub.verify(sig, m)); } +template +void serializeTest(const T& x) +{ + std::stringstream ss; + ss << x; + T y; + ss >> y; + CYBOZU_TEST_EQUAL(x, y); + char buf[1024]; + size_t n; + n = x.serialize(buf, sizeof(buf)); + CYBOZU_TEST_ASSERT(n > 0); + T z; + CYBOZU_TEST_EQUAL(z.deserialize(buf, n), n); + CYBOZU_TEST_EQUAL(x, z); +} + +CYBOZU_TEST_AUTO(serialize) +{ + SecretKey sec; + sec.init(); + PublicKey pub; + sec.getPublicKey(pub); + Signature sig; + sec.sign(sig, "abc"); + serializeTest(sec); + serializeTest(pub); + serializeTest(sig); +} + void aggregateTest(const std::vector& msgVec) { const size_t n = msgVec.size();