aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2016-08-29 09:32:41 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2016-08-29 09:32:41 +0800
commit1ce6756d94235d625da0a867c8e603398ca3ee66 (patch)
tree1bed6c42a13b5c52cceeb2d7d6426d9755817554
parent42029fa58d0e9ea2a2ccfe08205abe6f89692cd9 (diff)
downloadtangerine-mcl-1ce6756d94235d625da0a867c8e603398ca3ee66.tar.gz
tangerine-mcl-1ce6756d94235d625da0a867c8e603398ca3ee66.tar.zst
tangerine-mcl-1ce6756d94235d625da0a867c8e603398ca3ee66.zip
move setIoMode function to global
-rw-r--r--include/mcl/ec.hpp57
-rw-r--r--include/mcl/fp.hpp42
-rw-r--r--include/mcl/fp_tower.hpp23
-rw-r--r--include/mcl/op.hpp13
-rw-r--r--src/fp.cpp16
-rw-r--r--test/ec_test.cpp16
-rw-r--r--test/mont_fp_test.cpp16
7 files changed, 80 insertions, 103 deletions
diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp
index ca9785d..2ad0e22 100644
--- a/include/mcl/ec.hpp
+++ b/include/mcl/ec.hpp
@@ -595,20 +595,24 @@ public:
*/
void getStr(std::string& str, int base = 10, bool withPrefix = false) const
{
+ if (base == 0) base = 10;
if (isZero()) {
str = '0';
return;
}
normalize();
+ const char *sep = getIoSeparator();
if (compressedExpression_) {
- str = "1 ";
+ str = '1';
+ str += sep;
str += x.getStr(base, withPrefix);
- const char *p = y.isOdd() ? " 1" : " 0";
- str += p;
+ str += sep;
+ str += y.isOdd() ? '1' : '0';
} else {
- str = "2 ";
+ str = '2';
+ str += sep;
str += x.getStr(base, withPrefix);
- str += ' ';
+ str += sep;
str += y.getStr(base, withPrefix);
}
}
@@ -620,41 +624,22 @@ public:
}
friend inline std::ostream& operator<<(std::ostream& os, const EcT& self)
{
- fp::IoMode ioMode = Fp::getIoMode();
- switch (ioMode) {
- default:
- case fp::IoAuto:
- case fp::IoBinary:
- case fp::IoDecimal:
- case fp::IoHeximal:
- {
- int base = ioMode;
- bool withPrefix = false;
- if (ioMode == fp::IoAuto) {
- const std::ios_base::fmtflags f = os.flags();
- if (f & std::ios_base::oct) throw cybozu::Exception("fpT:operator<<:oct is not supported");
- base = (f & std::ios_base::hex) ? 16 : 10;
- withPrefix = (f & std::ios_base::showbase) != 0;
- }
- return os << self.getStr(base, withPrefix);
- }
- case fp::IoArray:
- case fp::IoArrayRaw:
- if (self.isZero()) {
- return os << '0';
- }
- self.normalize();
- if (compressedExpression_) {
- return os << '1' << self.x << (self.y.isOdd() ? '1' : '0');
- } else {
- return os << '2' << self.x << self.y;
- }
+ int base = mcl::getIoMode();
+ bool withPrefix = false;
+ if (base == IoAuto) {
+ const std::ios_base::fmtflags f = os.flags();
+ if (f & std::ios_base::oct) throw cybozu::Exception("EcT:operator<<:oct is not supported");
+ base = (f & std::ios_base::hex) ? 16 : 10;
+ withPrefix = (f & std::ios_base::showbase) != 0;
}
+ std::string str;
+ self.getStr(str, base, withPrefix);
+ return os << str;
}
friend inline std::istream& operator>>(std::istream& is, EcT& self)
{
- fp::IoMode ioMode = Fp::getIoMode();
- if (ioMode == fp::IoArray || ioMode == fp::IoArrayRaw) {
+ IoMode ioMode = mcl::getIoMode();
+ if (ioMode == IoArray || ioMode == IoArrayRaw) {
char c = 0;
is >> c;
if (c == '0') {
diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp
index d38b2c8..d721be3 100644
--- a/include/mcl/fp.hpp
+++ b/include/mcl/fp.hpp
@@ -34,6 +34,10 @@ namespace mcl {
struct FpTag;
struct ZnTag;
+void setIoMode(IoMode ioMode);
+IoMode getIoMode();
+const char* getIoSeparator();
+
namespace fp {
void arrayToStr(std::string& str, const Unit *x, size_t n, int base, bool withPrefix);
@@ -54,15 +58,6 @@ const char *ModeToStr(Mode mode);
Mode StrToMode(const std::string& s);
-enum IoMode {
- IoAuto = 0, // dec or hex according to ios_base::fmtflags
- IoBinary = 2, // binary number without prefix
- IoDecimal = 10, // decimal number without prefix
- IoHeximal = 16, // heximal number without prefix
- IoArray = -1, // array of Unit
- IoArrayRaw = -2, // raw array of Unit without Montgomery conversion
-};
-
} // mcl::fp
template<class tag = FpTag, size_t maxBitSize = MCL_MAX_OP_BIT_SIZE>
@@ -74,7 +69,6 @@ class FpT : public fp::Operator<FpT<tag, maxBitSize> > {
template<class tag2, size_t maxBitSize2> friend class FpT;
Unit v_[maxSize];
static FpT<tag, maxBitSize> inv2_;
- static fp::IoMode ioMode_;
public:
template<class Fp> friend class FpDblT;
template<class Fp> friend class Fp2T;
@@ -237,10 +231,10 @@ public:
}
void setStr(const std::string& str, int base = 0)
{
- if (base == fp::IoArray || base == fp::IoArrayRaw) {
+ if (base == IoArray || base == IoArrayRaw) {
const size_t n = getBitSize() / 8;
if (str.size() != n) throw cybozu::Exception("FpT:setStr:bad size") << str.size() << n << base;
- if (base == fp::IoArray) {
+ if (base == IoArray) {
setArray(&str[0], n);
} else {
memcpy(v_, str.c_str(), n);
@@ -311,14 +305,14 @@ public:
void getStr(std::string& str, int base = 10, bool withPrefix = false) const
{
if (base == 0) base = 10;
- if (base == fp::IoArrayRaw) {
+ if (base == IoArrayRaw) {
str.resize(getBitSize() / 8);
memcpy(&str[0], v_, str.size());
return;
}
fp::Block b;
getBlock(b);
- if (base == fp::IoArray) {
+ if (base == IoArray) {
str.resize(getBitSize() / 8);
memcpy(&str[0], b.p, str.size());
return;
@@ -398,7 +392,7 @@ public:
{
int base = getIoMode();
bool withPrefix = false;
- if (base == fp::IoAuto) {
+ if (base == IoAuto) {
const std::ios_base::fmtflags f = os.flags();
if (f & std::ios_base::oct) throw cybozu::Exception("FpT:operator<<:oct is not supported");
base = (f & std::ios_base::hex) ? 16 : 10;
@@ -410,14 +404,14 @@ public:
}
friend inline std::istream& operator>>(std::istream& is, FpT& self)
{
- int base = ioMode_;
- if (base == fp::IoAuto) {
+ int base = getIoMode();
+ if (base == IoAuto) {
const std::ios_base::fmtflags f = is.flags();
if (f & std::ios_base::oct) throw cybozu::Exception("FpT:operator>>:oct is not supported");
base = (f & std::ios_base::hex) ? 16 : 0;
}
std::string str;
- if (base == fp::IoArray || base == fp::IoArrayRaw) {
+ if (base == IoArray || base == IoArrayRaw) {
str.resize(getBitSize() / 8);
is.read(&str[0], str.size());
} else {
@@ -426,16 +420,6 @@ public:
self.setStr(str, base);
return is;
}
- static inline void setIoMode(fp::IoMode ioMode)
- {
- ioMode_ = ioMode;
- }
- static inline fp::IoMode getIoMode() { return ioMode_; }
- static inline char getIoSeparator()
- {
- if (ioMode_ == fp::IoArray || ioMode_ == fp::IoArrayRaw) return '\0';
- return ' ';
- }
/*
@note
this compare functions is slow because of calling mul if isMont is true.
@@ -557,8 +541,6 @@ private:
template<class tag, size_t maxBitSize> fp::Op FpT<tag, maxBitSize>::op_;
template<class tag, size_t maxBitSize> FpT<tag, maxBitSize> FpT<tag, maxBitSize>::inv2_;
-template<class tag, size_t maxBitSize> fp::IoMode FpT<tag, maxBitSize>::ioMode_ = fp::IoAuto;
-
template<class T> void add(T& z, const T& x, const T& y) { T::add(z, x, y); }
template<class T> void sub(T& z, const T& x, const T& y) { T::sub(z, x, y); }
diff --git a/include/mcl/fp_tower.hpp b/include/mcl/fp_tower.hpp
index 2844e70..a86894b 100644
--- a/include/mcl/fp_tower.hpp
+++ b/include/mcl/fp_tower.hpp
@@ -88,18 +88,12 @@ public:
Fp::divBy2(y.a, x.a);
Fp::divBy2(y.b, x.b);
}
- static inline fp::IoMode getIoMode() { return Fp::getIoMode(); }
/*
Fp2T = <a> + ' ' + <b>
*/
friend std::ostream& operator<<(std::ostream& os, const Fp2T& self)
{
- fp::IoMode ioMode = Fp::getIoMode();
- if (ioMode == fp::IoArray || ioMode == fp::IoArrayRaw) {
- return os << self.a << self.b;
- } else {
- return os << self.a << ' ' << self.b;
- }
+ return os << self.a << mcl::getIoSeparator() << self.b;
}
friend std::istream& operator>>(std::istream& is, Fp2T& self)
{
@@ -422,12 +416,8 @@ struct Fp6T : public fp::Operator<Fp6T<Fp> > {
bool operator!=(const Fp6T& rhs) const { return !operator==(rhs); }
friend std::ostream& operator<<(std::ostream& os, const Fp6T& x)
{
- fp::IoMode ioMode = Fp::getIoMode();
- if (ioMode == fp::IoArray || ioMode == fp::IoArrayRaw) {
- return os << x.a << x.b << x.c;
- } else {
- return os << x.a << ' ' << x.b << ' ' << x.c;
- }
+ const char *sep = mcl::getIoSeparator();
+ return os << x.a << sep << x.b << sep << x.c;
}
friend std::istream& operator>>(std::istream& is, Fp6T& x)
{
@@ -692,12 +682,7 @@ struct Fp12T : public fp::Operator<Fp12T<Fp> > {
}
friend std::ostream& operator<<(std::ostream& os, const Fp12T& self)
{
- fp::IoMode ioMode = Fp::getIoMode();
- if (ioMode == fp::IoArray || ioMode == fp::IoArrayRaw) {
- return os << self.a << self.b;
- } else {
- return os << self.a << ' ' << self.b;
- }
+ return os << self.a << mcl::getIoSeparator() << self.b;
}
friend std::istream& operator>>(std::istream& is, Fp12T& self)
{
diff --git a/include/mcl/op.hpp b/include/mcl/op.hpp
index 6388ed1..3dcc8d4 100644
--- a/include/mcl/op.hpp
+++ b/include/mcl/op.hpp
@@ -15,7 +15,18 @@
#define MCL_USE_XBYAK
#endif
-namespace mcl { namespace fp {
+namespace mcl {
+
+enum IoMode {
+ IoAuto = 0, // dec or hex according to ios_base::fmtflags
+ IoBinary = 2, // binary number without prefix
+ IoDecimal = 10, // decimal number without prefix
+ IoHeximal = 16, // heximal number without prefix
+ IoArray = -1, // array of Unit
+ IoArrayRaw = -2, // raw array of Unit without Montgomery conversion
+};
+
+namespace fp {
#if defined(CYBOZU_OS_BIT) && (CYBOZU_OS_BIT == 32)
typedef uint32_t Unit;
diff --git a/src/fp.cpp b/src/fp.cpp
index 3343593..61e4c9b 100644
--- a/src/fp.cpp
+++ b/src/fp.cpp
@@ -11,7 +11,21 @@
#pragma warning(disable : 4127)
#endif
-namespace mcl { namespace fp {
+namespace mcl {
+
+static IoMode g_ioMode = mcl::IoAuto;
+
+void setIoMode(IoMode ioMode) { g_ioMode = ioMode; }
+
+IoMode getIoMode() { return g_ioMode; }
+
+const char* getIoSeparator()
+{
+ if (g_ioMode == IoArray || g_ioMode == IoArrayRaw) return "";
+ return " ";
+}
+
+namespace fp {
#ifdef MCL_USE_XBYAK
FpGenerator *Op::createFpGenerator()
diff --git a/test/ec_test.cpp b/test/ec_test.cpp
index dfa7e1b..3c0e45c 100644
--- a/test/ec_test.cpp
+++ b/test/ec_test.cpp
@@ -280,15 +280,15 @@ struct Test {
const Fp x(para.gx);
const Fp y(para.gy);
Ec P(x, y);
- const mcl::fp::IoMode tbl[] = {
- mcl::fp::IoBinary,
- mcl::fp::IoDecimal,
- mcl::fp::IoHeximal,
- mcl::fp::IoArray,
- mcl::fp::IoArrayRaw,
+ const mcl::IoMode tbl[] = {
+ mcl::IoBinary,
+ mcl::IoDecimal,
+ mcl::IoHeximal,
+ mcl::IoArray,
+ mcl::IoArrayRaw,
};
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
- Fp::setIoMode(tbl[i]);
+ mcl::setIoMode(tbl[i]);
{
std::stringstream ss;
ss << P;
@@ -305,7 +305,7 @@ struct Test {
CYBOZU_TEST_EQUAL(Q, R);
}
}
- Fp::setIoMode(mcl::fp::IoAuto);
+ mcl::setIoMode(mcl::IoAuto);
}
template<class F>
diff --git a/test/mont_fp_test.cpp b/test/mont_fp_test.cpp
index 205c851..0f7115a 100644
--- a/test/mont_fp_test.cpp
+++ b/test/mont_fp_test.cpp
@@ -292,15 +292,15 @@ struct Test {
{
Fp x(123);
const struct {
- mcl::fp::IoMode ioMode;
+ mcl::IoMode ioMode;
std::string expected;
} tbl[] = {
- { mcl::fp::IoBinary, "1111011" },
- { mcl::fp::IoDecimal, "123" },
- { mcl::fp::IoHeximal, "7b" },
+ { mcl::IoBinary, "1111011" },
+ { mcl::IoDecimal, "123" },
+ { mcl::IoHeximal, "7b" },
};
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
- Fp::setIoMode(tbl[i].ioMode);
+ mcl::setIoMode(tbl[i].ioMode);
for (int j = 0; j < 2; j++) {
std::stringstream ss;
if (j == 1) {
@@ -316,9 +316,9 @@ struct Test {
}
for (int i = 0; i < 2; i++) {
if (i == 0) {
- Fp::setIoMode(mcl::fp::IoArray);
+ mcl::setIoMode(mcl::IoArray);
} else {
- Fp::setIoMode(mcl::fp::IoArrayRaw);
+ mcl::setIoMode(mcl::IoArrayRaw);
}
std::stringstream ss;
ss << x;
@@ -327,7 +327,7 @@ struct Test {
ss >> y;
CYBOZU_TEST_EQUAL(x, y);
}
- Fp::setIoMode(mcl::fp::IoAuto);
+ mcl::setIoMode(mcl::IoAuto);
}
void edge()
{