Permalink
Browse files

add fp::Serializable for getStr, setStr, serialize, deserialize

  • Loading branch information...
herumi committed Jan 8, 2018
1 parent c0aa527 commit b16c67e7e38eb186aa0af59177a7f614ab0da985
Showing with 72 additions and 323 deletions.
  1. +1 −34 include/mcl/ec.hpp
  2. +4 −33 include/mcl/fp.hpp
  3. +9 −99 include/mcl/fp_tower.hpp
  4. +39 −1 include/mcl/operator.hpp
  5. +8 −156 include/mcl/she.hpp
  6. +6 −0 test/ec_test.cpp
  7. +5 −0 test/fp_test.cpp
View
@@ -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 @@ class EcT {
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 @@ class EcT {
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)
{
View
@@ -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 @@ class FpT : public fp::Operator<FpT<tag, maxBitSize> > {
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 @@ class FpT : public fp::Operator<FpT<tag, maxBitSize> > {
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 @@ class FpT : public fp::Operator<FpT<tag, maxBitSize> > {
{
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;
View
@@ -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 @@ class Fp2T : public fp::Operator<Fp2T<Fp> > {
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 @@ class Fp2T : public fp::Operator<Fp2T<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);
}
/*
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 @@ class Fp2T : public fp::Operator<Fp2T<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();
}
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();
}
};
/*
View
@@ -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 @@ struct Operator : E {
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
Oops, something went wrong.

0 comments on commit b16c67e

Please sign in to comment.