diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index ca0c9d5..43749d9 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -105,6 +105,7 @@ public: const Unit *getUnit() const { return v_; } static inline size_t getUnitSize() { return op_.N; } static inline size_t getBitSize() { return op_.bitSize; } + static inline size_t getByteSize() { return (op_.bitSize + 7) / 8; } static inline const fp::Op& getOp() { return op_; } void dump() const { @@ -221,7 +222,7 @@ public: void setStr(const std::string& str, int ioMode = 0) { if (ioMode & (IoArray | IoArrayRaw)) { - const size_t n = getBitSize() / 8; + const size_t n = getByteSize(); if (str.size() != n) throw cybozu::Exception("FpT:setStr:bad size") << str.size() << n << ioMode; if (ioMode & IoArrayRaw) { memcpy(v_, str.c_str(), n); @@ -289,15 +290,16 @@ public: } void getStr(std::string& str, int ioMode = 10) const { + const size_t n = getByteSize(); if (ioMode & IoArrayRaw) { - str.resize(getBitSize() / 8); + str.resize(n); memcpy(&str[0], v_, str.size()); return; } fp::Block b; getBlock(b); if (ioMode & IoArray) { - str.resize(getBitSize() / 8); + str.resize(n); memcpy(&str[0], b.p, str.size()); return; } @@ -380,7 +382,7 @@ public: int ioMode = fp::detectIoMode(getIoMode(), is); std::string str; if (ioMode & (IoArray | IoArrayRaw)) { - str.resize(getBitSize() / 8); + str.resize(getByteSize()); is.read(&str[0], str.size()); } else { is >> str; diff --git a/src/gen.cpp b/src/gen.cpp index c74dee7..54f66e9 100644 --- a/src/gen.cpp +++ b/src/gen.cpp @@ -796,9 +796,9 @@ struct Code : public mcl::Generator { { this->privateFuncList = &privateFuncList; gen_once(); - uint32_t end = ((maxBitSize + unit - 1) / unit) * unit; - for (uint32_t i = 64; i <= end; i += unit) { - setBit(i); + uint32_t end = ((maxBitSize + unit - 1) / unit); + for (uint32_t n = 1; n <= end; n++) { + setBit(n * unit); gen_all(); gen_addsub(); gen_mul(); diff --git a/test/fp_test.cpp b/test/fp_test.cpp index 4ac1302..67aeb69 100644 --- a/test/fp_test.cpp +++ b/test/fp_test.cpp @@ -176,7 +176,7 @@ void ioModeTest() } std::stringstream ss; ss << x; - CYBOZU_TEST_EQUAL(ss.str().size(), Fp::getBitSize() / 8); + CYBOZU_TEST_EQUAL(ss.str().size(), Fp::getByteSize()); Fp y; ss >> y; CYBOZU_TEST_EQUAL(x, y);