diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2018-05-14 13:39:12 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2018-05-14 13:39:12 +0800 |
commit | f5d9cbf7040a867967dc7b74cf9d128f9191ca72 (patch) | |
tree | 13d5e36cf109407021f96b062381f545f953be2c | |
parent | 925c48010529a54630a997a92ede51bcc75f775c (diff) | |
download | tangerine-mcl-f5d9cbf7040a867967dc7b74cf9d128f9191ca72.tar.gz tangerine-mcl-f5d9cbf7040a867967dc7b74cf9d128f9191ca72.tar.zst tangerine-mcl-f5d9cbf7040a867967dc7b74cf9d128f9191ca72.zip |
remove stringstream in vint.hpp
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | include/mcl/vint.hpp | 90 | ||||
-rw-r--r-- | test/vint_test.cpp | 8 |
3 files changed, 62 insertions, 40 deletions
@@ -259,10 +259,10 @@ endif emcc -o $@ src/fp.cpp src/she_c384.cpp $(EMCC_OPT) -s TOTAL_MEMORY=67108864 ../mcl-wasm/mcl_c.js: src/bn_c256.cpp $(MCL_C_DEP) - emcc -o $@ src/fp.cpp src/bn_c256.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=256 + emcc -o $@ src/fp.cpp src/bn_c256.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=256 -DCYBOZU_MINIMUM_EXCEPTION ../mcl-wasm/mcl_c512.js: src/bn_c512.cpp $(MCL_C_DEP) - emcc -o $@ src/fp.cpp src/bn_c512.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=512 + emcc -o $@ src/fp.cpp src/bn_c512.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=512 -DCYBOZU_MINIMUM_EXCEPTION ../ecdsa-wasm/ecdsa_c.js: src/ecdsa_c.cpp src/fp.cpp include/mcl/ecdsa.hpp include/mcl/ecdsa.h Makefile emcc -o $@ src/fp.cpp src/ecdsa_c.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=256 diff --git a/include/mcl/vint.hpp b/include/mcl/vint.hpp index 95060c3..098f0c2 100644 --- a/include/mcl/vint.hpp +++ b/include/mcl/vint.hpp @@ -4,9 +4,10 @@ */ #include <cybozu/exception.hpp> #include <cybozu/bit_operation.hpp> +#include <cybozu/stream.hpp> #include <cybozu/atoi.hpp> +#include <cybozu/itoa.hpp> #include <vector> -#include <iomanip> #include <stdlib.h> #include <assert.h> #include <cmath> @@ -1202,45 +1203,64 @@ public: vint::clearN(x + n, maxSize - n); } void clear() { *this = 0; } - std::string getStr(int base = 10) const - { - std::ostringstream os; - if (isNeg_) os << '-'; - switch (base) { - case 10: - { - const uint32_t i1e9 = 1000000000U; - VintT x; - VintT::abs(x, *this); - - std::vector<uint32_t> t; - while (!x.isZero()) { - uint32_t r = udivModu1(&x, x, i1e9); - t.push_back(r); - } - if (t.empty()) { - return "0"; - } - os << t[t.size() - 1]; - for (size_t i = 1, n = t.size(); i < n; i++) { - os << std::setfill('0') << std::setw(9) << t[n - 1 - i]; + template<class OutputStream> + void save(OutputStream& os, int base = 10) const + { + if (isNeg_) cybozu::writeChar(os, '-'); + if (base == 10) { + const size_t width = 9; + const uint32_t i1e9 = 1000000000U; + VintT x; + VintT::abs(x, *this); + + std::vector<uint32_t> t; + while (!x.isZero()) { + uint32_t r = udivModu1(&x, x, i1e9); + t.push_back(r); + } + if (t.empty()) { + cybozu::writeChar(os, '0'); + return; + } + char buf[width]; + for (size_t i = 0, n = t.size(); i < n; i++) { + size_t len = cybozu::itoa_local::uintToDec(buf, width, t[n - 1 - i]); + assert(len > 0); + if (i == 0) { + os.write(buf + width - len, len); + } else { + for (size_t j = 0; j < width - len; j++) { + buf[j] = '0'; + } + os.write(buf, width); } } - break; - case 16: - { - os << std::hex; - const size_t n = size(); - os << getUnit()[n - 1]; - for (size_t i = 1; i < n; i++) { - os << std::setfill('0') << std::setw(sizeof(Unit) * 2) << getUnit()[n - 1 - i]; + } else if (base == 16) { + const size_t n = size(); + const size_t width = 16; + char buf[width]; + for (size_t i = 0; i < n; i++) { + size_t len = cybozu::itoa_local::uintToHex(buf, width, getUnit()[n - 1 - i], false); + assert(len > 0); + if (i == 0) { + os.write(buf + width - len, len); + } else { + for (size_t j = 0; j < width - len; j++) { + buf[j] = '0'; + } + os.write(buf, width); } } - break; - default: - throw cybozu::Exception("getStr:not supported base") << base; + } else { + assert(0); } - return os.str(); + } + std::string getStr(int base = 10) const + { + std::string s; + cybozu::StringOutputStream os(s); + save(os, base); + return s; } /* return bitSize(abs(*this)) diff --git a/test/vint_test.cpp b/test/vint_test.cpp index 183d447..8a2aee5 100644 --- a/test/vint_test.cpp +++ b/test/vint_test.cpp @@ -576,12 +576,14 @@ CYBOZU_TEST_AUTO(string) { { 0xffffffff }, 1, "4294967295", "0xffffffff", "0b11111111111111111111111111111111" }, { { 0, 1 }, 2, "4294967296", "0x100000000", "0b100000000000000000000000000000000" }, { { 0, 0, 0, 0, 1 }, 5, "340282366920938463463374607431768211456", "0x100000000000000000000000000000000", "0b100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" }, + { { 0, 0x0b22a000, 0xe2f768a0, 0xe086b93c, 0x2cd76f }, 5, "1000000000000000000000000000000000000000000000", "0x2cd76fe086b93ce2f768a00b22a00000000000", "0b101100110101110110111111100000100001101011100100111100111000101111011101101000101000000000101100100010101000000000000000000000000000000000000000000000" }, }; for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { Vint x, y; x.setArray(tbl[i].v,tbl[i].vn); CYBOZU_TEST_EQUAL(x.getStr(10), tbl[i].str); y.setStr(tbl[i].str); + CYBOZU_TEST_EQUAL(x.getStr(16), tbl[i].hex + 2); CYBOZU_TEST_EQUAL(x, y); x = 1; x.setStr(tbl[i].hex); @@ -1025,7 +1027,7 @@ CYBOZU_TEST_AUTO(pow) Vint::pow(y, x, 3); CYBOZU_TEST_EQUAL(y, -8); #ifndef MCL_AVOID_EXCEPTION_TEST - CYBOZU_TEST_EXCEPTION(Vint::pow(y, x, -2), std::exception); + CYBOZU_TEST_EXCEPTION(Vint::pow(y, x, -2), cybozu::Exception); #endif } @@ -1050,8 +1052,8 @@ CYBOZU_TEST_AUTO(andOr) z = x | y; CYBOZU_TEST_EQUAL(z, Vint("29348220482094820948208435244134352108849315802")); #ifndef MCL_AVOID_EXCEPTION_TEST - CYBOZU_TEST_EXCEPTION(Vint("-2") | Vint("5"), std::exception); - CYBOZU_TEST_EXCEPTION(Vint("-2") & Vint("5"), std::exception); + CYBOZU_TEST_EXCEPTION(Vint("-2") | Vint("5"), cybozu::Exception); + CYBOZU_TEST_EXCEPTION(Vint("-2") & Vint("5"), cybozu::Exception); #endif x = 8; x |= 7; |