diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2018-05-21 04:14:20 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2018-05-21 04:14:20 +0800 |
commit | 7e327b6e1fcd0115e69e915579e45ef601800e29 (patch) | |
tree | 768771a0e20a2d3b03ade61b99651f71958e69b2 | |
parent | 0a537be1133a7ba75861eefe64abaeb3db3b3e06 (diff) | |
download | tangerine-mcl-7e327b6e1fcd0115e69e915579e45ef601800e29.tar.gz tangerine-mcl-7e327b6e1fcd0115e69e915579e45ef601800e29.tar.zst tangerine-mcl-7e327b6e1fcd0115e69e915579e45ef601800e29.zip |
reduce exception
-rw-r--r-- | include/mcl/conversion.hpp | 102 | ||||
-rw-r--r-- | include/mcl/fp.hpp | 51 | ||||
-rw-r--r-- | include/mcl/fp_tower.hpp | 43 | ||||
-rw-r--r-- | include/mcl/op.hpp | 5 | ||||
-rw-r--r-- | include/mcl/vint.hpp | 288 | ||||
-rw-r--r-- | src/fp.cpp | 77 | ||||
-rw-r--r-- | test/fp_test.cpp | 41 | ||||
-rw-r--r-- | test/vint_test.cpp | 6 |
8 files changed, 217 insertions, 396 deletions
diff --git a/include/mcl/conversion.hpp b/include/mcl/conversion.hpp index e4a4844..1000257 100644 --- a/include/mcl/conversion.hpp +++ b/include/mcl/conversion.hpp @@ -1,8 +1,6 @@ #pragma once -#include <vector> #include <cybozu/itoa.hpp> -#include <cybozu/atoi.hpp> -#include <mcl/util.hpp> +#include <cybozu/stream.hpp> /** @file @brief convertion bin/dec/hex <=> array @@ -15,6 +13,51 @@ namespace mcl { namespace fp { namespace local { +inline bool isSpace(char c) +{ + return c == ' ' || c == '\t' || c == '\r' || c == '\n'; +} +template<class InputStream> +bool skipSpace(char *c, InputStream& is) +{ + for (;;) { + if (!cybozu::readChar(c, is)) return false; + if (!isSpace(*c)) return true; + } +} + +template<class InputStream> +void loadWord(std::string& s, InputStream& is) +{ + s.clear(); + char c; + if (!skipSpace(&c, is)) return; + s = c; + for (;;) { + if (!cybozu::readChar(&c, is)) return; + if (isSpace(c)) break; + s += c; + } +} + +template<class InputStream> +size_t loadWord(char *buf, size_t bufSize, InputStream& is) +{ + if (bufSize == 0) return 0; + char c; + if (!skipSpace(&c, is)) return 0; + size_t pos = 0; + buf[pos++] = c; + for (;;) { + if (!cybozu::readChar(&c, is)) break; + if (isSpace(c)) break; + if (pos == bufSize) return 0; + buf[pos++] = c; + } + return pos; +} + + /* q = x[] / x @retval r = x[] % x @@ -127,6 +170,41 @@ bool binToUint(UT *px, const char *p, size_t size) return true; } +inline bool parsePrefix(size_t *readSize, bool *isMinus, int *base, const char *buf, size_t bufSize) +{ + if (bufSize == 0) return false; + size_t pos = 0; + if (*buf == '-') { + if (bufSize == 1) return false; + *isMinus = true; + buf++; + pos++; + } else { + *isMinus = false; + } + if (buf[0] == '0') { + if (bufSize > 1 && buf[1] == 'x') { + if (*base == 0 || *base == 16) { + *base = 16; + pos += 2; + } else { + return false; + } + } else if (bufSize > 1 && buf[1] == 'b') { + if (*base == 0 || *base == 2) { + *base = 2; + pos += 2; + } else { + return false; + } + } + } + if (*base == 0) *base = 10; + if (pos == bufSize) return false; + *readSize = pos; + return true; +} + } // mcl::fp::local /* @@ -340,4 +418,22 @@ size_t arrayToStr(char *buf, size_t bufSize, const UT *x, size_t n, int base, bo } } +template<class UT> +size_t strToArray(bool *pIsMinus, UT *x, size_t xN, const char *buf, size_t bufSize, int ioMode) +{ + ioMode &= 31; + size_t readSize; + if (!local::parsePrefix(&readSize, pIsMinus, &ioMode, buf, bufSize)) return 0; + switch (ioMode) { + case 10: + return decToArray(x, xN, buf + readSize, bufSize - readSize); + case 16: + return hexToArray(x, xN, buf + readSize, bufSize - readSize); + case 2: + return binToArray(x, xN, buf + readSize, bufSize - readSize); + default: + return 0; + } +} + } } // mcl::fp diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 7fb076e..bab3939 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -53,9 +53,6 @@ const char *ModeToStr(Mode mode); Mode StrToMode(const std::string& s); void dumpUnit(Unit x); -void UnitToHex(char *buf, size_t maxBufSize, Unit x); -std::string hexStrToLittleEndian(const char *buf, size_t bufSize); -std::string littleEndianToHexStr(const void *buf, size_t bufSize); bool isEnableJIT(); // 1st call is not threadsafe @@ -64,54 +61,6 @@ void getRandVal(Unit *out, RandGen& rg, const Unit *in, size_t bitSize); uint32_t sha256(void *out, uint32_t maxOutSize, const void *msg, uint32_t msgSize); uint32_t sha512(void *out, uint32_t maxOutSize, const void *msg, uint32_t msgSize); -namespace local { - -inline bool isSpace(char c) -{ - return c == ' ' || c == '\t' || c == '\r' || c == '\n'; -} -template<class InputStream> -bool skipSpace(char *c, InputStream& is) -{ - for (;;) { - if (!cybozu::readChar(c, is)) return false; - if (!isSpace(*c)) return true; - } -} - -template<class InputStream> -void loadWord(std::string& s, InputStream& is) -{ - s.clear(); - char c; - if (!skipSpace(&c, is)) return; - s = c; - for (;;) { - if (!cybozu::readChar(&c, is)) return; - if (isSpace(c)) break; - s += c; - } -} - -template<class InputStream> -size_t loadWord(char *buf, size_t bufSize, InputStream& is) -{ - if (bufSize == 0) return 0; - char c; - if (!skipSpace(&c, is)) return 0; - size_t pos = 0; - buf[pos++] = c; - for (;;) { - if (!cybozu::readChar(&c, is)) break; - if (isSpace(c)) break; - if (pos == bufSize) return 0; - buf[pos++] = c; - } - return pos; -} - -} // local - } // mcl::fp template<class tag = FpTag, size_t maxBitSize = MCL_MAX_BIT_SIZE> diff --git a/include/mcl/fp_tower.hpp b/include/mcl/fp_tower.hpp index 22afca0..052d743 100644 --- a/include/mcl/fp_tower.hpp +++ b/include/mcl/fp_tower.hpp @@ -11,7 +11,7 @@ namespace mcl { template<class Fp> -class FpDblT { +class FpDblT : public fp::Serializable<FpDblT<Fp> > { typedef fp::Unit Unit; Unit v_[Fp::maxSize * 2]; public: @@ -34,15 +34,42 @@ public: } printf("\n"); } - friend std::ostream& operator<<(std::ostream& os, const FpDblT& x) + template<class OutputStream> + void save(OutputStream& os, int, bool *pb) const { - char buf[20]; - const size_t n = getUnitSize(); - for (size_t i = 0; i < n; i++) { - mcl::fp::UnitToHex(buf, sizeof(buf), x.v_[n - 1 - i]); - os << buf; + char buf[1024]; + size_t n = mcl::fp::arrayToHex(buf, sizeof(buf), v_, getUnitSize()); + if (n == 0) { + *pb = false; + return; } - return os; + cybozu::write(os, buf + sizeof(buf) - n, sizeof(buf), pb); + } + template<class InputStream> + void load(InputStream& is, int, bool *pb) + { + char buf[1024]; + *pb = false; + size_t n = fp::local::loadWord(buf, sizeof(buf), is); + if (n == 0) return; + n = fp::hexToArray(v_, getUnitSize(), buf, n); + if (n == 0) return; + for (size_t i = n; i < getUnitSize(); i++) v_[i] = 0; + *pb = true; + } + template<class OutputStream> + void save(OutputStream& os, int ioMode = IoSerialize) const + { + bool b; + save(os, ioMode, &b); + if (!b) throw cybozu::Exception("fp:save") << ioMode; + } + template<class InputStream> + void load(InputStream& is, int ioMode = IoSerialize) + { + bool b; + load(is, ioMode, &b); + if (!b) throw cybozu::Exception("fp:load") << ioMode; } void clear() { diff --git a/include/mcl/op.hpp b/include/mcl/op.hpp index dc116e1..ecce493 100644 --- a/include/mcl/op.hpp +++ b/include/mcl/op.hpp @@ -324,11 +324,6 @@ private: void operator=(const Op&); }; -/* - conevrt string to array according to ioMode, -*/ -size_t strToArray(bool *pIsMinus, Unit *x, size_t xN, const char *buf, size_t bufSize, int ioMode); - inline const char* getIoSeparator(int ioMode) { return (ioMode & (IoArray | IoArrayRaw | IoSerialize)) ? "" : " "; diff --git a/include/mcl/vint.hpp b/include/mcl/vint.hpp index 098f0c2..fa28cc7 100644 --- a/include/mcl/vint.hpp +++ b/include/mcl/vint.hpp @@ -4,9 +4,6 @@ */ #include <cybozu/exception.hpp> #include <cybozu/bit_operation.hpp> -#include <cybozu/stream.hpp> -#include <cybozu/atoi.hpp> -#include <cybozu/itoa.hpp> #include <vector> #include <stdlib.h> #include <assert.h> @@ -14,6 +11,7 @@ #include <iostream> #include <mcl/util.hpp> #include <mcl/randgen.hpp> +#include <mcl/conversion.hpp> #ifdef __EMSCRIPTEN__ #define MCL_VINT_64BIT_PORTABLE @@ -165,99 +163,6 @@ inline uint64_t divUnit(uint64_t *pr, uint64_t H, uint64_t L, uint64_t y) } #endif -inline std::istream& getDigits(std::istream& is, std::string& str, bool allowNegative = false) -{ - std::ios_base::fmtflags keep = is.flags(); - size_t pos = 0; - char c; - while (is >> c) { - if (('0' <= c && c <= '9') /* digits */ - || (pos == 1 && (str[0] == '0' && c == 'x')) /* 0x.. */ - || ('a' <= c && c <= 'f') /* lowercase hex */ - || ('A' <= c && c <= 'F') /* uppercase hex */ - || (allowNegative && pos == 0 && c == '-')) { /* -digits */ - str.push_back(c); - if (pos == 0) { - is >> std::noskipws; - } - pos++; - } else { - is.unget(); - break; - } - } - is.flags(keep); - return is; -} - -template<class T> -inline void decStr2Int(T& x, const std::string& s) -{ - const size_t width = 9; - const typename T::Unit d = (uint32_t)std::pow(10.0, 9); - size_t size = s.size(); - size_t q = size / width; - size_t r = size % width; - const char *p = s.c_str(); - /* - split s and compute x - eg. 123456789012345678901234 => 123456, 789012345, 678901234 - */ - typename T::Unit v; - x = 0; - if (r) { - v = cybozu::atoi(p, r); - p += r; - x = v; - } - while (q) { - v = cybozu::atoi(p, width); - p += width; - x *= d; - x += v; - q--; - } -} - -inline uint32_t bin2uint32(const char *s, size_t n) -{ - uint32_t x = 0; - for (size_t i = 0; i < n; i++) { - x <<= 1; - char c = s[i]; - if (c != '0' && c != '1') throw cybozu::Exception("bin2uint32:bad char") << std::string(s, n); - if (c == '1') { - x |= 1; - } - } - return x; -} - -template<class T> -inline void binStr2Int(T& x, const std::string& s) -{ - const size_t width = 32; - size_t size = s.size(); - size_t q = size / width; - size_t r = size % width; - - const char *p = s.c_str(); - uint32_t v; - x = 0; - if (r) { - v = bin2uint32(p, r); - p += r; - T::addu1(x, x, v); - } - while (q) { - v = bin2uint32(p, width); - p += width; - x <<= width; - T::addu1(x, x, v); - q--; - } -} - /* compare x[] and y[] @retval positive if x > y @@ -942,7 +847,7 @@ private: bool isNeg_; void trim(size_t n) { - if (n == 0) throw cybozu::Exception("trim zero"); + assert(n > 0); int i = (int)n - 1; for (; i > 0; i--) { if (buf_[i]) { @@ -1108,7 +1013,7 @@ public: } VintT& operator=(int x) { - if (x == invalidVar) throw cybozu::Exception("VintT:operator=:invalidVar"); + assert(x != invalidVar); isNeg_ = x < 0; buf_.alloc(1); buf_[0] = std::abs(x); @@ -1204,56 +1109,23 @@ public: } void clear() { *this = 0; } template<class OutputStream> - void save(OutputStream& os, int base = 10) const + void save(OutputStream& os, int base, bool *pb) 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); - } - } - } 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); - } - } - } else { - assert(0); + if (isNeg_) cybozu::writeChar(os, '-', pb); + char buf[1024]; + size_t n = mcl::fp::arrayToStr(buf, sizeof(buf), &buf_[0], size_, base, false); + if (n == 0) { + *pb = false; + return; } + cybozu::write(os, buf + sizeof(buf) - n, n, pb); + } + template<class OutputStream> + void save(OutputStream& os, int base = 10) const + { + bool b; + save(os, base, &b); + if (!b) throw cybozu::Exception("Vint:save"); } std::string getStr(int base = 10) const { @@ -1303,56 +1175,22 @@ public: "0b..." => base = 2 otherwise => base = 10 */ + void setStr(const char *str, size_t strSize, int base, bool *pb) + { + const size_t maxN = 384 / (sizeof(MCL_SIZEOF_UNIT) * 8); + buf_.alloc(maxN); + *pb = false; + isNeg_ = false; + size_t n = fp::strToArray(&isNeg_, &buf_[0], maxN, str, strSize, base); + if (n == 0) return; + trim(n); + *pb = true; + } void setStr(std::string str, int base = 0) { - bool neg = false; - if (!str.empty() && str[0] == '-') { - neg = true; - str = str.substr(1); - } - if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') { - if (base != 0 && base != 16) throw cybozu::Exception("Vint:setStr bad base 0x)") << str << base; - base = 16; - str = str.substr(2); - } else if (str.size() >= 2 && str[0] == '0' && str[1] == 'b') { - if (base != 0 && base != 2) throw cybozu::Exception("Vint:setStr bad base 0b") << str << base; - base = 2; - str = str.substr(2); - } - if (base == 0) { - base = 10; - } - if (str.empty()) throw cybozu::Exception("empty string"); - - switch (base) { - case 16: - { - std::vector<uint32_t> x; - while (!str.empty()) { - size_t remain = std::min((int)str.size(), 8); - char *endp; - uint32_t v = strtoul(&str[str.size() - remain], &endp, 16); - if (*endp) goto ERR; - x.push_back(v); - str = str.substr(0, str.size() - remain); - } - setArray(&x[0], x.size()); - } - break; - case 2: - binStr2Int(*this, str); - break; - default: - case 10: - decStr2Int(*this, str); - break; - } - if (!isZero() && neg) { - isNeg_ = true; - } - return; - ERR: - throw cybozu::Exception("VintT:setStr") << str << base; + bool b; + setStr(str.c_str(), str.size(), base, &b); + if (!b) throw cybozu::Exception("Vint:setStr") << str; } static int compare(const VintT& x, const VintT& y) { @@ -1370,7 +1208,7 @@ public: } static int compares1(const VintT& x, int y) { - if (y == invalidVar) throw cybozu::Exception("VintT:compares1:bad y"); + assert(y != invalidVar); if (x.isNeg_ ^ (y < 0)) { if (x.isZero() && y == 0) return 0; return x.isNeg_ ? -1 : 1; @@ -1443,17 +1281,17 @@ public: } static void adds1(VintT& z, const VintT& x, int y) { - if (y == invalidVar) throw cybozu::Exception("VintT:adds1:bad y"); + assert(y != invalidVar); _adds1(z, x, std::abs(y), y < 0); } static void subs1(VintT& z, const VintT& x, int y) { - if (y == invalidVar) throw cybozu::Exception("VintT:subs1:bad y"); + assert(y != invalidVar); _adds1(z, x, std::abs(y), !(y < 0)); } static void muls1(VintT& z, const VintT& x, int y) { - if (y == invalidVar) throw cybozu::Exception("VintT:muls1:bad y"); + assert(y != invalidVar); mulu1(z, x, std::abs(y)); z.isNeg_ ^= (y < 0); } @@ -1465,7 +1303,7 @@ public: */ static int divMods1(VintT *q, const VintT& x, int y) { - if (y == invalidVar) throw cybozu::Exception("VintT:divMods1:bad y"); + assert(y != invalidVar); bool xNeg = x.isNeg_; bool yNeg = y < 0; Unit absY = std::abs(y); @@ -1516,7 +1354,7 @@ public: } static Unit udivModu1(VintT *q, const VintT& x, Unit y) { - if (x.isNeg_) throw cybozu::Exception("VintT:udivu1:x is not negative") << x; + assert(!x.isNeg_); size_t xn = x.size(); if (q) q->buf_.alloc(xn); Unit r = vint::divu1(q ? &q->buf_[0] : 0, &x.buf_[0], xn, y); @@ -1551,11 +1389,31 @@ public: { return os << x.getStr(os.flags() & std::ios_base::hex ? 16 : 10); } + template<class InputStream> + void load(InputStream& is, int ioMode, bool *pb) + { + *pb = false; + char buf[1024]; + size_t n = fp::local::loadWord(buf, sizeof(buf), is); + if (n == 0) return; + const size_t maxN = 384 / (sizeof(MCL_SIZEOF_UNIT) * 8); + buf_.alloc(maxN); + isNeg_ = false; + n = fp::strToArray(&isNeg_, &buf_[0], maxN, buf, n, ioMode); + if (n == 0) return; + trim(n); + *pb = true; + } + template<class InputStream> + void load(InputStream& is, int ioMode = 0) + { + bool b; + load(is, ioMode, &b); + if (!b) throw cybozu::Exception("Vint:load"); + } inline friend std::istream& operator>>(std::istream& is, VintT& x) { - std::string str; - vint::getDigits(is, str, true); - x.setStr(str); + x.load(is); return is; } // logical left shift (copy sign) @@ -1601,7 +1459,7 @@ public: // accept only non-negative value static void orBit(VintT& z, const VintT& x, const VintT& y) { - if (x.isNeg_ || y.isNeg_) throw cybozu::Exception("Vint:orBit:negative value is not supported"); + assert(!x.isNeg_ && !y.isNeg_); const VintT *px = &x, *py = &y; if (x.size() < y.size()) { std::swap(px, py); @@ -1618,7 +1476,7 @@ public: } static void andBit(VintT& z, const VintT& x, const VintT& y) { - if (x.isNeg_ || y.isNeg_) throw cybozu::Exception("Vint:andBit:negative value is not supported"); + assert(!x.isNeg_ && !y.isNeg_); const VintT *px = &x, *py = &y; if (x.size() < y.size()) { std::swap(px, py); @@ -1633,28 +1491,34 @@ public: } static void orBitu1(VintT& z, const VintT& x, Unit y) { - if (x.isNeg_) throw cybozu::Exception("Vint:orBit:negative value is not supported"); + assert(!x.isNeg_); z = x; z.buf_[0] |= y; } static void andBitu1(VintT& z, const VintT& x, Unit y) { - if (x.isNeg_) throw cybozu::Exception("Vint:andBit:negative value is not supported"); + assert(!x.isNeg_); z.buf_.alloc(1); z.buf_[0] = x.buf_[0] & y; z.size_ = 1; z.isNeg_ = false; } + /* + REMARK y >= 0; + */ static void pow(VintT& z, const VintT& x, const VintT& y) { - if (y.isNeg_) throw cybozu::Exception("Vint::pow:negative y") << y; + assert(!y.isNeg_); const VintT xx = x; z = 1; mcl::fp::powGeneric(z, xx, &y.buf_[0], y.size(), mul, sqr, (void (*)(VintT&, const VintT&))0); } + /* + REMARK y >= 0; + */ static void pow(VintT& z, const VintT& x, int64_t y) { - if (y < 0) throw cybozu::Exception("Vint::pow:negative y") << y; + assert(y >= 0); const VintT xx = x; z = 1; #if MCL_SIZEOF_UNIT == 8 @@ -1669,10 +1533,11 @@ public: } /* z = x ^ y mod m + REMARK y >= 0; */ static void powMod(VintT& z, const VintT& x, const VintT& y, const VintT& m) { - if (y.isNeg_) throw cybozu::Exception("Vint::pow:negative y") << y; + assert(!y.isNeg_); VintT zz; MulMod mulMod; SqrMod sqrMod; @@ -1685,9 +1550,11 @@ public: /* inverse mod y = 1/x mod m + REMARK x != 0 and m != 0; */ static void invMod(VintT& y, const VintT& x, const VintT& m) { + assert(!x.isZero() && !m.isZero()); if (x == 1) { y = 1; return; @@ -1696,7 +1563,6 @@ public: VintT t; VintT q; divMod(&q, t, m, x); - if (t.isZero()) throw cybozu::Exception("VintT:invMod:bad m") << m; VintT s = x; VintT b = -q; @@ -15,7 +15,6 @@ #include "proto.hpp" #include "low_func_llvm.hpp" #endif -#include <cybozu/atoi.hpp> #include <cybozu/itoa.hpp> #include <mcl/randgen.hpp> @@ -63,41 +62,6 @@ inline Unit getUnitAsLE(const void *p) #endif } -bool parsePrefix(size_t *readSize, bool *isMinus, int *base, const char *buf, size_t bufSize) -{ - if (bufSize == 0) return false; - size_t pos = 0; - if (*buf == '-') { - if (bufSize == 1) return false; - *isMinus = true; - buf++; - pos++; - } else { - *isMinus = false; - } - if (buf[0] == '0') { - if (bufSize > 1 && buf[1] == 'x') { - if (*base == 0 || *base == 16 || *base == (16 | IoPrefix)) { - *base = 16; - pos += 2; - } else { - return false; - } - } else if (bufSize > 1 && buf[1] == 'b') { - if (*base == 0 || *base == 2 || *base == (2 | IoPrefix)) { - *base = 2; - pos += 2; - } else { - return false; - } - } - } - if (*base == 0) *base = 10; - if (pos == bufSize) return false; - *readSize = pos; - return true; -} - const char *ModeToStr(Mode mode) { switch (mode) { @@ -139,43 +103,6 @@ void dumpUnit(Unit x) printf("%016llx", (unsigned long long)x); #endif } -void UnitToHex(char *buf, size_t maxBufSize, Unit x) -{ -#if MCL_SIZEOF_UNIT == 4 - CYBOZU_SNPRINTF(buf, maxBufSize, "%08x", (uint32_t)x); -#else - CYBOZU_SNPRINTF(buf, maxBufSize, "%016llx ", (unsigned long long)x); -#endif -} - -// "123af" => { 0xaf, 0x23, 0x01 } -std::string hexStrToLittleEndian(const char *buf, size_t bufSize) -{ - std::string s; - s.reserve((bufSize + 1) / 2); - while (bufSize >= 2) { - uint8_t c = cybozu::hextoi(&buf[bufSize - 2], 2); - s += char(c); - bufSize -= 2; - } - if (bufSize == 1) { - uint8_t c = cybozu::hextoi(&buf[0], 1); - s += char(c); - } - return s; -} - -// { 0xaf, 0x23, 0x01 } => "0123af" -std::string littleEndianToHexStr(const void *buf, size_t bufSize) -{ - std::string s; - s.resize(bufSize * 2); - const uint8_t *p = (const uint8_t *)buf; - for (size_t i = 0; i < bufSize; i++) { - cybozu::itohex(&s[i * 2], 2, p[bufSize - 1 - i], false); - } - return s; -} bool isEnableJIT() { @@ -619,13 +546,14 @@ int detectIoMode(int ioMode, const std::ios_base& ios) return ioMode; } +#if 0 size_t strToArray(bool *pIsMinus, Unit *x, size_t xN, const char *buf, size_t bufSize, int ioMode) { assert(!(ioMode & (IoArray | IoArrayRaw | IoSerialize))); // use low 8-bit ioMode for Fp ioMode &= 0xff; size_t readSize; - if (!parsePrefix(&readSize, pIsMinus, &ioMode, buf, bufSize)) return 0; + if (!mcl::fp::local::parsePrefix(&readSize, pIsMinus, &ioMode, buf, bufSize)) return 0; switch (ioMode) { case 10: return mcl::fp::decToArray(x, xN, buf + readSize, bufSize - readSize); @@ -637,6 +565,7 @@ size_t strToArray(bool *pIsMinus, Unit *x, size_t xN, const char *buf, size_t bu return 0; } } +#endif void copyAndMask(Unit *y, const void *x, size_t xByteSize, const Op& op, MaskMode maskMode) { diff --git a/test/fp_test.cpp b/test/fp_test.cpp index 00609a3..b826971 100644 --- a/test/fp_test.cpp +++ b/test/fp_test.cpp @@ -946,47 +946,6 @@ CYBOZU_TEST_AUTO(copyUnitToByteAsLE) CYBOZU_TEST_EQUAL(dst[0], 1u); } -CYBOZU_TEST_AUTO(hexStrToLittleEndian) -{ - const struct { - const char *in; - uint8_t out[8]; - size_t outSize; - } tbl[] = { - { "", {}, 0 }, - { "0", { 0 }, 1 }, - { "12", { 0x12 }, 1 }, - { "abc", { 0xbc, 0x0a }, 2 }, - { "1234567890abcdef", { 0xef, 0xcd, 0xab, 0x90, 0x78, 0x56, 0x34, 0x12 }, 8 }, - }; - for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { - const char *buf = tbl[i].in; - size_t bufSize = strlen(buf); - std::string s = mcl::fp::hexStrToLittleEndian(buf, bufSize); - CYBOZU_TEST_EQUAL(s.size(), tbl[i].outSize); - CYBOZU_TEST_ASSERT(memcmp(s.c_str(), tbl[i].out, s.size()) == 0); - } -} - -CYBOZU_TEST_AUTO(littleEndianToHexStr) -{ - const struct { - uint8_t in[8]; - size_t inSize; - const char *out; - } tbl[] = { - { {}, 0, "" }, - { { 0 }, 1, "00" }, - { { 0x12 }, 1, "12" }, - { { 0xbc, 0x0a }, 2, "0abc" }, - { { 0xef, 0xcd, 0xab, 0x90, 0x78, 0x56, 0x34, 0x12 }, 8, "1234567890abcdef" }, - }; - for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { - std::string s = mcl::fp::littleEndianToHexStr(tbl[i].in, tbl[i].inSize); - CYBOZU_TEST_EQUAL(s, tbl[i].out); - } -} - int main(int argc, char *argv[]) try { diff --git a/test/vint_test.cpp b/test/vint_test.cpp index 8a2aee5..a3ad8c1 100644 --- a/test/vint_test.cpp +++ b/test/vint_test.cpp @@ -1027,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), cybozu::Exception); +// CYBOZU_TEST_EXCEPTION(Vint::pow(y, x, -2), cybozu::Exception); #endif } @@ -1052,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"), cybozu::Exception); - CYBOZU_TEST_EXCEPTION(Vint("-2") & Vint("5"), cybozu::Exception); +// CYBOZU_TEST_EXCEPTION(Vint("-2") | Vint("5"), cybozu::Exception); +// CYBOZU_TEST_EXCEPTION(Vint("-2") & Vint("5"), cybozu::Exception); #endif x = 8; x |= 7; |