aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2018-01-08 16:01:15 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2018-01-08 16:01:15 +0800
commitb16c67e7e38eb186aa0af59177a7f614ab0da985 (patch)
treef3baacd2888ff148c107e6124907de0c073084a4
parentc0aa527ff47ddc20b51491a1a55c949b41ec7a15 (diff)
downloadtangerine-mcl-b16c67e7e38eb186aa0af59177a7f614ab0da985.tar.gz
tangerine-mcl-b16c67e7e38eb186aa0af59177a7f614ab0da985.tar.zst
tangerine-mcl-b16c67e7e38eb186aa0af59177a7f614ab0da985.zip
add fp::Serializable for getStr, setStr, serialize, deserialize
-rw-r--r--include/mcl/ec.hpp35
-rw-r--r--include/mcl/fp.hpp37
-rw-r--r--include/mcl/fp_tower.hpp108
-rw-r--r--include/mcl/operator.hpp40
-rw-r--r--include/mcl/she.hpp164
-rw-r--r--test/ec_test.cpp6
-rw-r--r--test/fp_test.cpp5
7 files changed, 72 insertions, 323 deletions
diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp
index dd8cb84..c312fcd 100644
--- a/include/mcl/ec.hpp
+++ b/include/mcl/ec.hpp
@@ -36,7 +36,7 @@ enum Mode {
y^2 = x^3 + az^4 + bz^6 (Jacobi) x = X/Z^2, y = Y/Z^3
*/
template<class _Fp>
-class EcT {
+class EcT : public fp::Serializable<EcT<_Fp> > {
enum {
zero,
minus3,
@@ -703,20 +703,6 @@ public:
P.y.save(os, ioMode);
}
}
- /*
- see mcl/op.hpp for the format of ioMode
- */
- void getStr(std::string& str, int ioMode = 0) const
- {
- cybozu::StringOutputStream os(str);
- save(os, ioMode);
- }
- std::string getStr(int ioMode = 0) const
- {
- std::string str;
- getStr(str, ioMode);
- return str;
- }
template<class InputStream>
void load(InputStream& is, int ioMode = IoSerialize)
{
@@ -775,25 +761,6 @@ public:
self.save(os, fp::detectIoMode(getIoMode(), os));
return os;
}
- void setStr(const std::string& str, int ioMode = 0)
- {
- cybozu::StringInputStream is(str);
- load(is, ioMode);
- }
- // return written bytes if sucess
- size_t serialize(void *buf, size_t maxBufSize) const
- {
- cybozu::MemoryOutputStream os(buf, maxBufSize);
- save(os, IoSerialize);
- return os.getPos();
- }
- // return positive read bytes if sucess
- size_t deserialize(const void *buf, size_t bufSize)
- {
- cybozu::MemoryInputStream is(buf, bufSize);
- load(is, IoSerialize);
- return is.getPos();
- }
// deplicated
static void setCompressedExpression(bool compressedExpression = true)
{
diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp
index 653dcaf..58b1f1f 100644
--- a/include/mcl/fp.hpp
+++ b/include/mcl/fp.hpp
@@ -90,9 +90,11 @@ void loadWord(std::string& s, InputStream& is)
} // mcl::fp
template<class tag = FpTag, size_t maxBitSize = MCL_MAX_BIT_SIZE>
-class FpT : public fp::Operator<FpT<tag, maxBitSize> > {
+class FpT : public fp::Serializable<FpT<tag, maxBitSize>,
+ fp::Operator<FpT<tag, maxBitSize> > > {
typedef fp::Unit Unit;
typedef fp::Operator<FpT<tag, maxBitSize> > Operator;
+ typedef fp::Serializable<FpT<tag, maxBitSize>, Operator> Serializer;
public:
static const size_t maxSize = (maxBitSize + fp::UnitBitSize - 1) / fp::UnitBitSize;
private:
@@ -194,7 +196,7 @@ public:
FpT(int64_t x) { operator=(x); }
explicit FpT(const std::string& str, int base = 0)
{
- setStr(str, base);
+ Serializer::setStr(str, base);
}
FpT& operator=(int64_t x)
{
@@ -278,25 +280,6 @@ public:
fp::arrayToStr(str, b.p, b.n, ioMode & 255);
cybozu::write(os, str.c_str(), str.size());
}
- void setStr(const std::string& str, int ioMode = 0)
- {
- cybozu::StringInputStream is(str);
- load(is, ioMode);
- }
- // return written bytes
- size_t serialize(void *buf, size_t maxBufSize) const
- {
- cybozu::MemoryOutputStream os(buf, maxBufSize);
- save(os);
- return os.getPos();
- }
- // return read bytes
- size_t deserialize(const void *buf, size_t bufSize)
- {
- cybozu::MemoryInputStream is(buf, bufSize);
- load(is);
- return is.getPos();
- }
/*
throw exception if x >= p
*/
@@ -343,18 +326,6 @@ public:
{
setHashOf(msg.data(), msg.size());
}
- void getStr(std::string& str, int ioMode = 0) const
- {
- str.clear();
- cybozu::StringOutputStream os(str);
- save(os, ioMode);
- }
- std::string getStr(int ioMode = 0) const
- {
- std::string str;
- getStr(str, ioMode);
- return str;
- }
void getMpz(mpz_class& x) const
{
fp::Block b;
diff --git a/include/mcl/fp_tower.hpp b/include/mcl/fp_tower.hpp
index 8f108c2..36c0c51 100644
--- a/include/mcl/fp_tower.hpp
+++ b/include/mcl/fp_tower.hpp
@@ -108,7 +108,8 @@ template<class Fp> class BNT;
x = a + bi
*/
template<class Fp>
-class Fp2T : public fp::Operator<Fp2T<Fp> > {
+class Fp2T : public fp::Serializable<Fp2T<Fp>,
+ fp::Operator<Fp2T<Fp> > > {
typedef fp::Unit Unit;
typedef FpDblT<Fp> FpDbl;
static uint32_t xi_a_;
@@ -188,6 +189,9 @@ public:
a.load(is, ioMode);
b.load(is, ioMode);
}
+ /*
+ Fp2T = <a> + ' ' + <b>
+ */
template<class OutputStream>
void save(OutputStream& os, int ioMode = IoSerialize) const
{
@@ -196,26 +200,6 @@ public:
if (sep) cybozu::writeChar(os, sep);
b.save(os, ioMode);
}
- void setStr(const std::string& str, int ioMode = 0)
- {
- cybozu::StringInputStream is(str);
- load(is, ioMode);
- }
- /*
- Fp2T = <a> + ' ' + <b>
- */
- void getStr(std::string& str, int ioMode = 0) const
- {
- str.clear();
- cybozu::StringOutputStream os(str);
- save(os, ioMode);
- }
- std::string getStr(int ioMode = 0) const
- {
- std::string str;
- getStr(str, ioMode);
- return str;
- }
friend std::istream& operator>>(std::istream& is, Fp2T& self)
{
self.load(is, fp::detectIoMode(Fp::BaseFp::getIoMode(), is));
@@ -226,20 +210,6 @@ public:
self.save(os, fp::detectIoMode(Fp::BaseFp::getIoMode(), os));
return os;
}
- // return written bytes if sucess else 0
- size_t serialize(void *buf, size_t maxBufSize) const
- {
- cybozu::MemoryOutputStream os(buf, maxBufSize);
- save(os);
- return os.getPos();
- }
- // return positive read bytes if sucess else 0
- size_t deserialize(const void *buf, size_t bufSize)
- {
- cybozu::MemoryInputStream is(buf, bufSize);
- load(is);
- return is.getPos();
- }
bool isZero() const { return a.isZero() && b.isZero(); }
bool isOne() const { return a.isOne() && b.isZero(); }
bool operator==(const Fp2T& rhs) const { return a == rhs.a && b == rhs.b; }
@@ -676,7 +646,8 @@ template<class Fp> Fp2T<Fp> Fp2T<Fp>::g3[Fp2T<Fp>::gN];
x = a + b v + c v^2
*/
template<class Fp>
-struct Fp6T : public fp::Operator<Fp6T<Fp> > {
+struct Fp6T : public fp::Serializable<Fp6T<Fp>,
+ fp::Operator<Fp6T<Fp> > > {
typedef Fp2T<Fp> Fp2;
typedef Fp2DblT<Fp> Fp2Dbl;
typedef Fp BaseFp;
@@ -730,23 +701,6 @@ struct Fp6T : public fp::Operator<Fp6T<Fp> > {
if (sep) cybozu::writeChar(os, sep);
c.save(os, ioMode);
}
- void setStr(const std::string& str, int ioMode = 0)
- {
- cybozu::StringInputStream is(str);
- load(is, ioMode);
- }
- void getStr(std::string& str, int ioMode = 0) const
- {
- str.clear();
- cybozu::StringOutputStream os(str);
- save(os, ioMode);
- }
- std::string getStr(int ioMode = 0) const
- {
- std::string str;
- getStr(str, ioMode);
- return str;
- }
friend std::istream& operator>>(std::istream& is, Fp6T& self)
{
self.load(is, fp::detectIoMode(Fp::BaseFp::getIoMode(), is));
@@ -757,20 +711,6 @@ struct Fp6T : public fp::Operator<Fp6T<Fp> > {
self.save(os, fp::detectIoMode(Fp::BaseFp::getIoMode(), os));
return os;
}
- // return written bytes if sucess else 0
- size_t serialize(void *buf, size_t maxBufSize) const
- {
- cybozu::MemoryOutputStream os(buf, maxBufSize);
- save(os);
- return os.getPos();
- }
- // return positive read bytes if sucess else 0
- size_t deserialize(const void *buf, size_t bufSize)
- {
- cybozu::MemoryInputStream is(buf, bufSize);
- load(is);
- return is.getPos();
- }
static void add(Fp6T& z, const Fp6T& x, const Fp6T& y)
{
Fp2::add(z.a, x.a, y.a);
@@ -947,7 +887,8 @@ struct Fp6T : public fp::Operator<Fp6T<Fp> > {
x = a + b w
*/
template<class Fp>
-struct Fp12T : public fp::Operator<Fp12T<Fp> > {
+struct Fp12T : public fp::Serializable<Fp12T<Fp>,
+ fp::Operator<Fp12T<Fp> > > {
typedef Fp2T<Fp> Fp2;
typedef Fp6T<Fp> Fp6;
typedef Fp BaseFp;
@@ -1158,23 +1099,6 @@ struct Fp12T : public fp::Operator<Fp12T<Fp> > {
if (sep) cybozu::writeChar(os, sep);
b.save(os, ioMode);
}
- void setStr(const std::string& str, int ioMode = 0)
- {
- cybozu::StringInputStream is(str);
- load(is, ioMode);
- }
- void getStr(std::string& str, int ioMode = 0) const
- {
- str.clear();
- cybozu::StringOutputStream os(str);
- save(os, ioMode);
- }
- std::string getStr(int ioMode = 0) const
- {
- std::string str;
- getStr(str, ioMode);
- return str;
- }
friend std::istream& operator>>(std::istream& is, Fp12T& self)
{
self.load(is, fp::detectIoMode(Fp::BaseFp::getIoMode(), is));
@@ -1185,20 +1109,6 @@ struct Fp12T : public fp::Operator<Fp12T<Fp> > {
self.save(os, fp::detectIoMode(Fp::BaseFp::getIoMode(), os));
return os;
}
- // return written bytes if sucess else 0
- size_t serialize(void *buf, size_t maxBufSize) const
- {
- cybozu::MemoryOutputStream os(buf, maxBufSize);
- save(os);
- return os.getPos();
- }
- // return positive read bytes if sucess else 0
- size_t deserialize(const void *buf, size_t bufSize)
- {
- cybozu::MemoryInputStream is(buf, bufSize);
- load(is);
- return is.getPos();
- }
};
/*
diff --git a/include/mcl/operator.hpp b/include/mcl/operator.hpp
index 3acd6c6..19b7956 100644
--- a/include/mcl/operator.hpp
+++ b/include/mcl/operator.hpp
@@ -29,7 +29,7 @@ struct Empty {};
T must have add, sub, mul, inv, neg
*/
template<class T, class E = Empty<T> >
-struct Operator : E {
+struct Operator : public E {
template<class S> MCL_FORCE_INLINE T& operator+=(const S& rhs) { T::add(static_cast<T&>(*this), static_cast<const T&>(*this), rhs); return static_cast<T&>(*this); }
template<class S> MCL_FORCE_INLINE T& operator-=(const S& rhs) { T::sub(static_cast<T&>(*this), static_cast<const T&>(*this), rhs); return static_cast<T&>(*this); }
template<class S> friend MCL_FORCE_INLINE T operator+(const T& a, const S& b) { T c; T::add(c, a, b); return c; }
@@ -117,5 +117,43 @@ private:
template<class T, class E>
void (*Operator<T, E>::powArrayGLV)(T& z, const T& x, const Unit *y, size_t yn, bool isNegative, bool constTime);
+/*
+ T must have save and load
+*/
+template<class T, class E = Empty<T> >
+struct Serializable : public E {
+ void setStr(const std::string& str, int ioMode = 0)
+ {
+ cybozu::StringInputStream is(str);
+ static_cast<T&>(*this).load(is, ioMode);
+ }
+ void getStr(std::string& str, int ioMode = 0) const
+ {
+ str.clear();
+ cybozu::StringOutputStream os(str);
+ static_cast<const T&>(*this).save(os, ioMode);
+ }
+ std::string getStr(int ioMode = 0) const
+ {
+ std::string str;
+ getStr(str, ioMode);
+ return str;
+ }
+ // return written bytes
+ size_t serialize(void *buf, size_t maxBufSize) const
+ {
+ cybozu::MemoryOutputStream os(buf, maxBufSize);
+ static_cast<const T&>(*this).save(os);
+ return os.getPos();
+ }
+ // return read bytes
+ size_t deserialize(const void *buf, size_t bufSize)
+ {
+ cybozu::MemoryInputStream is(buf, bufSize);
+ static_cast<T&>(*this).load(is);
+ return is.getPos();
+ }
+};
+
} } // mcl::fp
diff --git a/include/mcl/she.hpp b/include/mcl/she.hpp
index d3b0057..d10f6b4 100644
--- a/include/mcl/she.hpp
+++ b/include/mcl/she.hpp
@@ -363,7 +363,7 @@ struct SHET {
static bool useDecG2ViaGT_;
private:
template<class G>
- class CipherTextAT {
+ class CipherTextAT : public fp::Serializable<CipherTextAT<G> > {
G S_, T_;
friend class SecretKey;
friend class PublicKey;
@@ -428,23 +428,6 @@ private:
if (sep) cybozu::writeChar(os, sep);
T_.save(os, ioMode);
}
- void getStr(std::string& str, int ioMode = 0) const
- {
- str.clear();
- cybozu::StringOutputStream os(str);
- save(os, ioMode);
- }
- void setStr(const std::string& str, int ioMode = 0)
- {
- cybozu::StringInputStream is(str);
- load(is, ioMode);
- }
- std::string getStr(int ioMode = 0) const
- {
- std::string str;
- getStr(str, ioMode);
- return str;
- }
friend std::istream& operator>>(std::istream& is, CipherTextAT& self)
{
self.load(is, fp::detectIoMode(G::getIoMode(), is));
@@ -460,18 +443,6 @@ private:
return S_ == rhs.S_ && T_ == rhs.T_;
}
bool operator!=(const CipherTextAT& rhs) const { return !operator==(rhs); }
- size_t serialize(void *buf, size_t maxBufSize) const
- {
- cybozu::MemoryOutputStream os(buf, maxBufSize);
- save(os);
- return os.getPos();
- }
- size_t deserialize(const void *buf, size_t bufSize)
- {
- cybozu::MemoryInputStream is(buf, bufSize);
- load(is);
- return is.getPos();
- }
};
/*
g1 = millerLoop(P1, Q)
@@ -574,7 +545,7 @@ public:
only one element is necessary for each G1 and G2.
this is better than David Mandell Freeman's algorithm
*/
- class SecretKey {
+ class SecretKey : public fp::Serializable<SecretKey> {
Fr x_, y_;
void getPowOfePQ(GT& v, const CipherTextGT& c) const
{
@@ -725,23 +696,6 @@ public:
if (sep) cybozu::writeChar(os, sep);
y_.save(os, ioMode);
}
- void getStr(std::string& str, int ioMode = 0) const
- {
- str.clear();
- cybozu::StringOutputStream os(str);
- save(os, ioMode);
- }
- void setStr(const std::string& str, int ioMode = 0)
- {
- cybozu::StringInputStream is(str);
- load(is, ioMode);
- }
- std::string getStr(int ioMode = 0) const
- {
- std::string str;
- getStr(str, ioMode);
- return str;
- }
friend std::istream& operator>>(std::istream& is, SecretKey& self)
{
self.load(is, fp::detectIoMode(Fr::getIoMode(), is));
@@ -757,18 +711,6 @@ public:
return x_ == rhs.x_ && y_ == rhs.y_;
}
bool operator!=(const SecretKey& rhs) const { return !operator==(rhs); }
- size_t serialize(void *buf, size_t maxBufSize) const
- {
- cybozu::MemoryOutputStream os(buf, maxBufSize);
- save(os);
- return os.getPos();
- }
- size_t deserialize(const void *buf, size_t bufSize)
- {
- cybozu::MemoryInputStream is(buf, bufSize);
- load(is);
- return is.getPos();
- }
};
private:
/*
@@ -892,7 +834,8 @@ private:
}
};
public:
- class PublicKey : public PublicKeyMethod<PublicKey> {
+ class PublicKey : public fp::Serializable<PublicKey,
+ PublicKeyMethod<PublicKey> > {
G1 xP_;
G2 yQ_;
friend class SecretKey;
@@ -985,23 +928,6 @@ public:
if (sep) cybozu::writeChar(os, sep);
yQ_.save(os, ioMode);
}
- void getStr(std::string& str, int ioMode = 0) const
- {
- str.clear();
- cybozu::StringOutputStream os(str);
- save(os, ioMode);
- }
- void setStr(const std::string& str, int ioMode = 0)
- {
- cybozu::StringInputStream is(str);
- load(is, ioMode);
- }
- std::string getStr(int ioMode = 0) const
- {
- std::string str;
- getStr(str, ioMode);
- return str;
- }
friend std::istream& operator>>(std::istream& is, PublicKey& self)
{
self.load(is, fp::detectIoMode(G1::getIoMode(), is));
@@ -1017,21 +943,10 @@ public:
return xP_ == rhs.xP_ && yQ_ == rhs.yQ_;
}
bool operator!=(const PublicKey& rhs) const { return !operator==(rhs); }
- size_t serialize(void *buf, size_t maxBufSize) const
- {
- cybozu::MemoryOutputStream os(buf, maxBufSize);
- save(os);
- return os.getPos();
- }
- size_t deserialize(const void *buf, size_t bufSize)
- {
- cybozu::MemoryInputStream is(buf, bufSize);
- load(is);
- return is.getPos();
- }
};
- class PrecomputedPublicKey : public PublicKeyMethod<PrecomputedPublicKey> {
+ class PrecomputedPublicKey : public fp::Serializable<PrecomputedPublicKey,
+ PublicKeyMethod<PrecomputedPublicKey> > {
typedef local::InterfaceForHashTable<GT, false> GTasEC;
typedef mcl::fp::WindowMethod<GTasEC> GTwin;
template<class T>
@@ -1159,23 +1074,6 @@ public:
if (sep) cybozu::writeChar(os, sep);
c2_.save(os, ioMode);
}
- void getStr(std::string& str, int ioMode = 0) const
- {
- str.clear();
- cybozu::StringOutputStream os(str);
- save(os, ioMode);
- }
- void setStr(const std::string& str, int ioMode = 0)
- {
- cybozu::StringInputStream is(str);
- load(is, ioMode);
- }
- std::string getStr(int ioMode = 0) const
- {
- std::string str;
- getStr(str, ioMode);
- return str;
- }
friend std::istream& operator>>(std::istream& is, CipherTextA& self)
{
self.load(is, fp::detectIoMode(G1::getIoMode(), is));
@@ -1193,7 +1091,7 @@ public:
bool operator!=(const CipherTextA& rhs) const { return !operator==(rhs); }
};
- class CipherTextGT {
+ class CipherTextGT : public fp::Serializable<CipherTextGT> {
GT g_[4];
friend class SecretKey;
friend class PublicKey;
@@ -1282,23 +1180,6 @@ public:
g_[i].save(os, ioMode);
}
}
- void getStr(std::string& str, int ioMode = 0) const
- {
- str.clear();
- cybozu::StringOutputStream os(str);
- save(os, ioMode);
- }
- void setStr(const std::string& str, int ioMode = 0)
- {
- cybozu::StringInputStream is(str);
- load(is, ioMode);
- }
- std::string getStr(int ioMode = 0) const
- {
- std::string str;
- getStr(str, ioMode);
- return str;
- }
friend std::istream& operator>>(std::istream& is, CipherTextGT& self)
{
self.load(is, fp::detectIoMode(G1::getIoMode(), is));
@@ -1317,21 +1198,9 @@ public:
return true;
}
bool operator!=(const CipherTextGT& rhs) const { return !operator==(rhs); }
- size_t serialize(void *buf, size_t maxBufSize) const
- {
- cybozu::MemoryOutputStream os(buf, maxBufSize);
- save(os);
- return os.getPos();
- }
- size_t deserialize(const void *buf, size_t bufSize)
- {
- cybozu::MemoryInputStream is(buf, bufSize);
- load(is);
- return is.getPos();
- }
};
- class CipherText {
+ class CipherText : public fp::Serializable<CipherText> {
bool isMultiplied_;
CipherTextA a_;
CipherTextGT m_;
@@ -1419,23 +1288,6 @@ public:
a_.save(os, ioMode);
}
}
- void getStr(std::string& str, int ioMode = 0) const
- {
- str.clear();
- cybozu::StringOutputStream os(str);
- save(os, ioMode);
- }
- void setStr(const std::string& str, int ioMode = 0)
- {
- cybozu::StringInputStream is(str);
- load(is, ioMode);
- }
- std::string getStr(int ioMode = 0) const
- {
- std::string str;
- getStr(str, ioMode);
- return str;
- }
friend std::istream& operator>>(std::istream& is, CipherText& self)
{
self.load(is, fp::detectIoMode(G1::getIoMode(), is));
diff --git a/test/ec_test.cpp b/test/ec_test.cpp
index a4c4a62..6cf4b33 100644
--- a/test/ec_test.cpp
+++ b/test/ec_test.cpp
@@ -15,6 +15,12 @@ struct tagZn;
typedef mcl::FpT<tagZn> Zn;
typedef mcl::EcT<Fp> Ec;
+CYBOZU_TEST_AUTO(sizeof)
+{
+ CYBOZU_TEST_EQUAL(sizeof(Fp), sizeof(mcl::fp::Unit) * Fp::maxSize);
+ CYBOZU_TEST_EQUAL(sizeof(Ec), sizeof(Fp) * 3);
+}
+
struct Test {
const mcl::EcParam& para;
Test(const mcl::EcParam& para, mcl::fp::Mode fpMode, mcl::ec::Mode ecMode)
diff --git a/test/fp_test.cpp b/test/fp_test.cpp
index 32e6f4a..00609a3 100644
--- a/test/fp_test.cpp
+++ b/test/fp_test.cpp
@@ -19,6 +19,11 @@
typedef mcl::FpT<> Fp;
+CYBOZU_TEST_AUTO(sizeof)
+{
+ CYBOZU_TEST_EQUAL(sizeof(Fp), sizeof(mcl::fp::Unit) * Fp::maxSize);
+}
+
void cstrTest()
{
const struct {