aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/mcl/fp.hpp19
-rw-r--r--test/ec_test.cpp34
2 files changed, 51 insertions, 2 deletions
diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp
index 11d6b9e..5225955 100644
--- a/include/mcl/fp.hpp
+++ b/include/mcl/fp.hpp
@@ -59,8 +59,8 @@ enum IoMode {
IoBinary = 2, // binary number without prefix
IoDecimal = 10, // decimal number without prefix
IoHeximal = 16, // heximal number without prefix
- IoArray = 17, // array of Unit
- IoArrayRaw = 18, // raw array of Unit without Montgomery conversion
+ IoArray = -1, // array of Unit
+ IoArrayRaw = -2, // raw array of Unit without Montgomery conversion
};
} // mcl::fp
@@ -293,10 +293,25 @@ public:
fp::getRandVal(v_, rg, op_.p, op_.bitSize);
toMont();
}
+ /*
+ may use IoMode for base
+ ignore withPrefix if base = IoArray or IoArrayRaw
+ */
void getStr(std::string& str, int base = 10, bool withPrefix = false) const
{
+ if (base == 0) base = 10;
+ if (base == fp::IoArrayRaw) {
+ str.resize(getBitSize() / 8);
+ memcpy(&str[0], v_, str.size());
+ return;
+ }
fp::Block b;
getBlock(b);
+ if (base == fp::IoArray) {
+ str.resize(getBitSize() / 8);
+ memcpy(&str[0], b.p, str.size());
+ return;
+ }
fp::arrayToStr(str, b.p, b.n, base, withPrefix);
}
std::string getStr(int base = 10, bool withPrefix = false) const
diff --git a/test/ec_test.cpp b/test/ec_test.cpp
index 1e039f9..dfa7e1b 100644
--- a/test/ec_test.cpp
+++ b/test/ec_test.cpp
@@ -274,6 +274,39 @@ struct Test {
CYBOZU_TEST_EQUAL(P, Q);
}
}
+ void ioMode() const
+ {
+ puts("test ioMode");
+ 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,
+ };
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
+ Fp::setIoMode(tbl[i]);
+ {
+ std::stringstream ss;
+ ss << P;
+ Ec Q;
+ ss >> Q;
+ CYBOZU_TEST_EQUAL(P, Q);
+ }
+ {
+ std::stringstream ss;
+ Ec Q;
+ ss << Q;
+ Ec R;
+ ss >> R;
+ CYBOZU_TEST_EQUAL(Q, R);
+ }
+ }
+ Fp::setIoMode(mcl::fp::IoAuto);
+ }
template<class F>
void test(F f, const char *msg) const
@@ -311,6 +344,7 @@ mul 499.00usec
mul_fp();
squareRoot();
str();
+ ioMode();
}
private:
Test(const Test&);