aboutsummaryrefslogtreecommitdiffstats
path: root/include/mcl
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2018-05-23 15:18:49 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2018-05-23 15:18:49 +0800
commit6b29693b6423791fb1590c2b7e8f307bcb5de09d (patch)
treeeeab0e407e3e015b51fd874991e47adba6564898 /include/mcl
parentfc5304717525678519811c171ac69333e3fcfe36 (diff)
downloadtangerine-mcl-6b29693b6423791fb1590c2b7e8f307bcb5de09d.tar.gz
tangerine-mcl-6b29693b6423791fb1590c2b7e8f307bcb5de09d.tar.zst
tangerine-mcl-6b29693b6423791fb1590c2b7e8f307bcb5de09d.zip
add getModulo for buf
Diffstat (limited to 'include/mcl')
-rw-r--r--include/mcl/fp.hpp4
-rw-r--r--include/mcl/gmp_util.hpp17
-rw-r--r--include/mcl/vint.hpp18
3 files changed, 37 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;