diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2018-05-23 15:18:49 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2018-05-23 15:18:49 +0800 |
commit | 6b29693b6423791fb1590c2b7e8f307bcb5de09d (patch) | |
tree | eeab0e407e3e015b51fd874991e47adba6564898 | |
parent | fc5304717525678519811c171ac69333e3fcfe36 (diff) | |
download | tangerine-mcl-6b29693b6423791fb1590c2b7e8f307bcb5de09d.tar.gz tangerine-mcl-6b29693b6423791fb1590c2b7e8f307bcb5de09d.tar.zst tangerine-mcl-6b29693b6423791fb1590c2b7e8f307bcb5de09d.zip |
add getModulo for buf
-rw-r--r-- | include/mcl/fp.hpp | 4 | ||||
-rw-r--r-- | include/mcl/gmp_util.hpp | 17 | ||||
-rw-r--r-- | include/mcl/vint.hpp | 18 | ||||
-rw-r--r-- | test/fp_test.cpp | 4 | ||||
-rw-r--r-- | test/vint_test.cpp | 7 |
5 files changed, 48 insertions, 2 deletions
diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 4ee59e4..6c39d1f 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -134,6 +134,10 @@ public: mpz_class p(mstr); init(p, mode); } + static inline size_t getModulo(char *buf, size_t bufSize) + { + return gmp::getStr(buf, bufSize, op_.mp); + } static inline void getModulo(std::string& pstr) { gmp::getStr(pstr, op_.mp); diff --git a/include/mcl/gmp_util.hpp b/include/mcl/gmp_util.hpp index 78cafc1..47c1ab8 100644 --- a/include/mcl/gmp_util.hpp +++ b/include/mcl/gmp_util.hpp @@ -112,6 +112,23 @@ inline bool setStr(mpz_class& z, const std::string& str, int base = 0) return z.set_str(str, base) == 0; #endif } +/* + set buf with string terminated by '\0' + return strlen(buf) if success else 0 +*/ +inline size_t getStr(char *buf, size_t bufSize, const mpz_class& z, int base = 10) +{ +#ifdef MCL_USE_VINT + return z.getStr(buf, bufSize, base); +#else + std::string str = z.get_str(base); + if (str.size() < bufSize) { + memcpy(buf, str.c_str(), str.size() + 1); + return str.size(); + } + return 0; +#endif +} inline void getStr(std::string& str, const mpz_class& z, int base = 10) { #ifdef MCL_USE_VINT diff --git a/include/mcl/vint.hpp b/include/mcl/vint.hpp index ad1699b..5530b46 100644 --- a/include/mcl/vint.hpp +++ b/include/mcl/vint.hpp @@ -1109,7 +1109,7 @@ public: } void clear() { *this = 0; } template<class OutputStream> - void save(OutputStream& os, int base, bool *pb) const + void save(bool *pb, OutputStream& os, int base = 10) const { if (isNeg_) cybozu::writeChar(pb, os, '-'); char buf[1024]; @@ -1124,9 +1124,23 @@ public: void save(OutputStream& os, int base = 10) const { bool b; - save(os, base, &b); + save(&b, os, base); if (!b) throw cybozu::Exception("Vint:save"); } + /* + set buf with string terminated by '\0' + return strlen(buf) if success else 0 + */ + size_t getStr(char *buf, size_t bufSize, int base = 10) const + { + cybozu::MemoryOutputStream os(buf, bufSize); + bool b; + save(&b, os, base); + const size_t n = os.getPos(); + if (!b || n == bufSize - 1) return 0; + buf[n] = '\0'; + return n; + } std::string getStr(int base = 10) const { std::string s; diff --git a/test/fp_test.cpp b/test/fp_test.cpp index b826971..f81e0c8 100644 --- a/test/fp_test.cpp +++ b/test/fp_test.cpp @@ -333,6 +333,10 @@ void compareTest() } std::string str; Fp::getModulo(str); + char buf[1024]; + size_t n = Fp::getModulo(buf, sizeof(buf)); + CYBOZU_TEST_EQUAL(n, str.size()); + CYBOZU_TEST_EQUAL(buf, str.c_str()); mpz_class half(str); half = (half - 1) / 2; Fp x; diff --git a/test/vint_test.cpp b/test/vint_test.cpp index a3ad8c1..0eea8a9 100644 --- a/test/vint_test.cpp +++ b/test/vint_test.cpp @@ -582,8 +582,15 @@ CYBOZU_TEST_AUTO(string) Vint x, y; x.setArray(tbl[i].v,tbl[i].vn); CYBOZU_TEST_EQUAL(x.getStr(10), tbl[i].str); + char buf[1024]; + size_t n = x.getStr(buf, sizeof(buf), 10); + CYBOZU_TEST_ASSERT(n > 0); + CYBOZU_TEST_EQUAL(tbl[i].str, buf); y.setStr(tbl[i].str); CYBOZU_TEST_EQUAL(x.getStr(16), tbl[i].hex + 2); + n = x.getStr(buf, sizeof(buf), 16); + CYBOZU_TEST_ASSERT(n > 0); + CYBOZU_TEST_EQUAL(tbl[i].hex + 2, buf); CYBOZU_TEST_EQUAL(x, y); x = 1; x.setStr(tbl[i].hex); |