remove getStr/setStr for extension of Fp

dev
MITSUNARI Shigeo 9 years ago
parent c37114a643
commit dece687158
  1. 112
      include/mcl/fp_tower.hpp
  2. 12
      test/fp_tower_test.cpp

@ -99,50 +99,15 @@ public:
Fp2T& operator/=(const Fp2T& x) { div(*this, *this, x); return *this; } Fp2T& operator/=(const Fp2T& x) { div(*this, *this, x); return *this; }
Fp2T operator-() const { Fp2T x; neg(x, *this); return x; } Fp2T operator-() const { Fp2T x; neg(x, *this); return x; }
/* /*
Fp2T = '[' + <a> + ',' + <b> + ']' Fp2T = <a> + ' ' + <b>
*/ */
void getStr(std::string& str, int base = 10, bool withPrefix = false) const
{
str = '[';
str += a.getStr(base, withPrefix);
str += ',';
str += b.getStr(base, withPrefix);
str += ']';
}
void setStr(const std::string& str, int base = 0)
{
const size_t size = str.size();
const size_t pos = str.find(',');
if (size >= 5 && str[0] == '[' && pos != std::string::npos && str[size - 1] == ']') {
a.setStr(str.substr(1, pos - 1), base);
b.setStr(str.substr(pos + 1, size - pos - 2), base);
return;
}
throw cybozu::Exception("Fp2T:setStr:bad format") << str;
}
std::string getStr(int base = 10, bool withPrefix = false) const
{
std::string str;
getStr(str, base, withPrefix);
return str;
}
friend inline std::ostream& operator<<(std::ostream& os, const Fp2T& self) friend inline std::ostream& operator<<(std::ostream& os, const Fp2T& self)
{ {
const std::ios_base::fmtflags f = os.flags(); return os << self.a << ' ' << self.b;
if (f & std::ios_base::oct) throw cybozu::Exception("Fp2T:operator<<:oct is not supported");
const int base = (f & std::ios_base::hex) ? 16 : 10;
const bool showBase = (f & std::ios_base::showbase) != 0;
return os << self.getStr(base, showBase);
} }
friend inline std::istream& operator>>(std::istream& is, Fp2T& self) friend inline std::istream& operator>>(std::istream& is, Fp2T& self)
{ {
const std::ios_base::fmtflags f = is.flags(); return is >> self.a >> self.b;
if (f & std::ios_base::oct) throw cybozu::Exception("fpT:operator>>:oct is not supported");
const int base = (f & std::ios_base::hex) ? 16 : 0;
std::string str;
is >> str;
self.setStr(str, base);
return is;
} }
bool isZero() const { return a.isZero() && b.isZero(); } bool isZero() const { return a.isZero() && b.isZero(); }
bool operator==(const Fp2T& rhs) const { return a == rhs.a && b == rhs.b; } bool operator==(const Fp2T& rhs) const { return a == rhs.a && b == rhs.b; }
@ -382,58 +347,13 @@ struct Fp6T {
return a == rhs.a && b == rhs.b && c == rhs.c; return a == rhs.a && b == rhs.b && c == rhs.c;
} }
bool operator!=(const Fp6T& rhs) const { return !operator==(rhs); } bool operator!=(const Fp6T& rhs) const { return !operator==(rhs); }
void getStr(std::string& str, int base = 10, bool withPrefix = false) const
{
str = '[';
str += a.getStr(base, withPrefix);
str += ',';
str += b.getStr(base, withPrefix);
str += ',';
str += c.getStr(base, withPrefix);
str += ']';
}
void setStr(const std::string& str, int base = 0)
{
const size_t size = str.size();
if (size >= 4 + 5 * 3 && str[0] == '[' && str[size - 1] == ']') { // '[' + <a> + ',' + <b> + ',' + <c> + ']'
size_t pos = str.find(']', 1);
if (pos != std::string::npos) {
a.setStr(str.substr(1, pos - 1 + 1), base);
if (str[pos + 1] == ',') {
pos += 2;
size_t pos2 = str.find(']', pos);
if (pos2 != std::string::npos) {
b.setStr(str.substr(pos, pos2 - pos + 1), base);
if (str[pos2 + 1] == ',') {
pos = pos2 + 2;
pos2 = str.find(']', pos);
if (pos2 != std::string::npos) {
c.setStr(str.substr(pos, pos2 - pos + 1), base);
return;
}
}
}
}
}
}
throw cybozu::Exception("Fp6T:setStr:bad format") << str;
}
std::string getStr(int base = 10, bool withPrefix = false) const
{
std::string str;
getStr(str, base, withPrefix);
return str;
}
friend std::ostream& operator<<(std::ostream& os, const Fp6T& x) friend std::ostream& operator<<(std::ostream& os, const Fp6T& x)
{ {
return os << "[" << x.a << ",\n " << x.b << ",\n " << x.c << "]"; return os << x.a << ' ' << x.b << ' ' << x.c;
} }
friend std::istream& operator>>(std::istream& is, Fp6T& x) friend std::istream& operator>>(std::istream& is, Fp6T& x)
{ {
char c1, c2, c3, c4; return is >> x.a >> x.b >> x.c;
is >> c1 >> x.a_ >> c2 >> x.b_ >> c3 >> x.c_ >> c4;
if (c1 == '[' && c2 == ',' && c3 == ',' && c4 == ']') return is;
throw std::ios_base::failure("bad Fp6T");
} }
inline friend Fp6T operator+(const Fp6T& x, const Fp6T& y) { Fp6T z; add(z, x, y); return z; } inline friend Fp6T operator+(const Fp6T& x, const Fp6T& y) { Fp6T z; add(z, x, y); return z; }
inline friend Fp6T operator-(const Fp6T& x, const Fp6T& y) { Fp6T z; sub(z, x, y); return z; } inline friend Fp6T operator-(const Fp6T& x, const Fp6T& y) { Fp6T z; sub(z, x, y); return z; }
@ -628,27 +548,13 @@ struct Fp12T {
Fp6::neg(z.a, x.a); Fp6::neg(z.a, x.a);
Fp6::neg(z.b, x.b); Fp6::neg(z.b, x.b);
} }
void getStr(std::string& str, int base = 10, bool withPrefix = false) const friend inline std::ostream& operator<<(std::ostream& os, const Fp12T& self)
{
str = '[';
str += a.getStr(base, withPrefix);
str += ',';
str += b.getStr(base, withPrefix);
str += ']';
}
std::string getStr(int base = 10, bool withPrefix = false) const
{ {
std::string str; return os << self.a << ' ' << self.b;
getStr(str, base, withPrefix);
return str;
} }
friend inline std::ostream& operator<<(std::ostream& os, const Fp12T& self) friend inline std::istream& operator>>(std::istream& is, Fp12T& self)
{ {
const std::ios_base::fmtflags f = os.flags(); return is >> self.a >> self.b;
if (f & std::ios_base::oct) throw cybozu::Exception("Fp12T:operator<<:oct is not supported");
const int base = (f & std::ios_base::hex) ? 16 : 10;
const bool showBase = (f & std::ios_base::showbase) != 0;
return os << self.getStr(base, showBase);
} }
}; };

@ -23,9 +23,9 @@ void testFp2()
x.b = 2; x.b = 2;
{ {
std::string s; std::stringstream os;
s = x.getStr(); os << x;
y.setStr(s); os >> y;
CYBOZU_TEST_EQUAL(x, y); CYBOZU_TEST_EQUAL(x, y);
} }
y.a = 3; y.a = 3;
@ -97,9 +97,9 @@ void testFp6()
Fp6 y(Fp2(-1, 1), Fp2(4, -3), Fp2(-6, 2)); Fp6 y(Fp2(-1, 1), Fp2(4, -3), Fp2(-6, 2));
Fp6 z, w; Fp6 z, w;
{ {
std::string s; std::stringstream ss;
s = x.getStr(); ss << x;
z.setStr(s); ss >> z;
CYBOZU_TEST_EQUAL(x, z); CYBOZU_TEST_EQUAL(x, z);
} }
Fp6::add(z, x, y); Fp6::add(z, x, y);

Loading…
Cancel
Save