aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2017-07-23 21:21:50 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2017-07-23 21:21:50 +0800
commit906aab70b3459f7acd77862c3bb6f6cb909175ed (patch)
tree301aaaec025f7d242eef8b5d218fe87009825c11
parent23ae4416e0b0514e6ed37188bf43eb3edd679013 (diff)
downloadtangerine-mcl-906aab70b3459f7acd77862c3bb6f6cb909175ed.tar.gz
tangerine-mcl-906aab70b3459f7acd77862c3bb6f6cb909175ed.tar.zst
tangerine-mcl-906aab70b3459f7acd77862c3bb6f6cb909175ed.zip
fix many bugs of Vint
-rw-r--r--include/mcl/bn.hpp2
-rw-r--r--include/mcl/fp.hpp4
-rw-r--r--include/mcl/gmp_util.hpp4
-rw-r--r--include/mcl/vint.hpp258
-rw-r--r--src/fp.cpp9
-rw-r--r--src/low_func.hpp6
-rw-r--r--test/fp_test.cpp12
-rw-r--r--test/vint_test.cpp15
8 files changed, 213 insertions, 97 deletions
diff --git a/include/mcl/bn.hpp b/include/mcl/bn.hpp
index 48df8b7..90c3bf4 100644
--- a/include/mcl/bn.hpp
+++ b/include/mcl/bn.hpp
@@ -521,7 +521,7 @@ struct ParamT {
p = eval(pCoff, z);
assert((p % 6) == 1);
r = eval(rCoff, z);
- Fp::init(gmp::getStr(p), mode);
+ Fp::init(p, mode);
Fp2::init(cp.xi_a);
b = cp.b;
Fp2 xi(cp.xi_a, 1);
diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp
index 69c37d4..ccb249d 100644
--- a/include/mcl/fp.hpp
+++ b/include/mcl/fp.hpp
@@ -20,7 +20,7 @@
#ifdef NDEBUG
#pragma comment(lib, "mcl.lib")
#else
- #pragma comment(lib, "mcld.lib")
+ #pragma comment(lib, "mcl.lib")
#endif
#endif
#endif
@@ -52,7 +52,7 @@ 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 char *buf, size_t bufSize);
+std::string littleEndianToHexStr(const void *buf, size_t bufSize);
bool isEnableJIT(); // 1st call is not threadsafe
diff --git a/include/mcl/gmp_util.hpp b/include/mcl/gmp_util.hpp
index 4a24510..819f183 100644
--- a/include/mcl/gmp_util.hpp
+++ b/include/mcl/gmp_util.hpp
@@ -384,7 +384,7 @@ inline size_t getUnitSize(const mpz_class& x)
#ifdef MCL_USE_VINT
return x.getUnitSize();
#else
- return abs(x.get_mpz_t()->_mp_size);
+ return std::abs(x.get_mpz_t()->_mp_size);
#endif
}
inline mpz_class abs(const mpz_class& x)
@@ -392,7 +392,7 @@ inline mpz_class abs(const mpz_class& x)
#ifdef MCL_USE_VINT
return Vint::abs(x);
#else
- return abs(x.get_mpz_t()->_mp_size);
+ return ::abs(x);
#endif
}
template<class RG>
diff --git a/include/mcl/vint.hpp b/include/mcl/vint.hpp
index d909923..f791367 100644
--- a/include/mcl/vint.hpp
+++ b/include/mcl/vint.hpp
@@ -142,6 +142,45 @@ inline void decStr2Int(T& x, const std::string& s)
}
}
+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
@@ -198,7 +237,7 @@ T addN(T *z, const T *x, const T *y, size_t n)
z[] = x[] + y
*/
template<class T>
-T add1(T *z, const T *x, size_t n, T y)
+T adds1(T *z, const T *x, size_t n, T y)
{
assert(n > 0);
T t = x[0] + y;
@@ -236,7 +275,7 @@ T addNM(T *z, const T *x, size_t xn, const T *y, size_t yn)
size_t min = yn;
T c = vint::addN(z, x, y, min);
if (max > min) {
- c = vint::add1(z + min, x + min, max - min, c);
+ c = vint::adds1(z + min, x + min, max - min, c);
}
return c;
}
@@ -267,7 +306,7 @@ T subN(T *z, const T *x, const T *y, size_t n)
out[] = x[n] - y
*/
template<class T>
-T sub1(T *z, const T *x, size_t n, T y)
+T subs1(T *z, const T *x, size_t n, T y)
{
assert(n > 0);
#if 0
@@ -309,7 +348,7 @@ EXIT_0:
@note accept z == x
*/
template<class T>
-T mul1(T *z, const T *x, size_t n, T y)
+T mulu1(T *z, const T *x, size_t n, T y)
{
assert(n > 0);
T H = 0;
@@ -346,12 +385,12 @@ static inline void mulNM(T *z, const T *x, size_t xn, const T *y, size_t yn)
copyN(p, y, yn);
y = p;
}
- z[xn] = mul1(&z[0], x, xn, y[0]);
+ z[xn] = vint::mulu1(&z[0], x, xn, y[0]);
clearN(z + xn + 1, yn - 1);
T *t2 = (T*)CYBOZU_ALLOCA(sizeof(T) * (xn + 1));
for (size_t i = 1; i < yn; i++) {
- t2[xn] = vint::mul1(&t2[0], x, xn, y[i]);
+ t2[xn] = vint::mulu1(&t2[0], x, xn, y[i]);
vint::addN(&z[i], &z[i], &t2[0], xn + 1);
}
}
@@ -371,7 +410,7 @@ static inline void sqrN(T *y, const T *x, size_t xn)
accept q == x
*/
template<class T>
-T div1(T *q, const T *x, size_t n, T y)
+T divu1(T *q, const T *x, size_t n, T y)
{
T r = 0;
for (int i = (int)n - 1; i >= 0; i--) {
@@ -384,7 +423,7 @@ T div1(T *q, const T *x, size_t n, T y)
@retval r = x[] % y
*/
template<class T>
-T mod1(const T *x, size_t n, T y)
+T modu1(const T *x, size_t n, T y)
{
T r = 0;
for (int i = (int)n - 1; i >= 0; i--) {
@@ -438,74 +477,96 @@ static inline double GetApp(const T *x, size_t xn, bool up)
return t;
}
+template<class T>
+size_t getRealSize(const T *x, size_t xn)
+{
+ int i = (int)xn - 1;
+ for (; i > 0; i--) {
+ if (x[i]) {
+ return i + 1;
+ }
+ }
+ return 1;
+}
+
/*
- q[] = x[xn] / y[yn] ; size of q = xn - yn + 1 if q
- r[] = x[xn] % y[yn] ; size of r = xn
+ q[qn] = x[xn] / y[yn] ; qn == xn - yn + 1 if xn >= yn if q
+ r[rn] = x[xn] % y[yn] ; rn = yn before getRealSiz
*/
template<class T>
-void divNM(T *q, T *r, const T *x, size_t xn, const T *y, size_t yn)
+void divNM(T *q, size_t qn, T *r, const T *x, size_t xn, const T *y, size_t yn)
{
assert(xn > 0 && yn > 0);
- if (x == q || x == r) {
+ assert(xn < yn || (q == 0 || qn == xn - yn + 1));
+ const size_t rn = yn;
+ xn = getRealSize(x, xn);
+ yn = getRealSize(y, yn);
+ if (x == q) {
T *p = (T*)CYBOZU_ALLOCA(sizeof(T) * xn);
copyN(p, x, xn);
x = p;
}
- if (y == q || y == r) {
+ if (y == q) {
T *p = (T*)CYBOZU_ALLOCA(sizeof(T) * yn);
copyN(p, y, yn);
y = p;
}
if (q) {
- clearN(q, xn - yn + 1);
+ clearN(q, qn);
}
if (yn > xn) {
+ /*
+ if y > x then q = 0 and r = x
+ */
copyN(r, x, xn);
+ clearN(r + xn, rn - xn);
return;
}
if (yn == 1) {
T t;
if (q) {
- t = div1(q, x, xn, y[0]);
+ t = divu1(q, x, xn, y[0]);
} else {
- t = mod1(x, xn, y[0]);
+ t = modu1(x, xn, y[0]);
}
r[0] = t;
- clearN(r + 1, xn - 1);
+ clearN(r + 1, rn - 1);
return;
}
-// assert(xn >= yn && yn >= 2);
+ assert(yn >= 2);
if (x == y) {
assert(xn == yn);
- clearN(r, xn);
+ clearN(r, rn);
if (q) {
q[0] = 1;
}
return;
}
- copyN(r, x, xn);
+ T *rr = (T*)CYBOZU_ALLOCA(sizeof(T) * xn);
+ copyN(rr, x, xn);
T *t = (T*)CYBOZU_ALLOCA(sizeof(T) * (yn + 1));
double yt = GetApp(y, yn, true);
- while (vint::compareNM(r, xn, y, yn) >= 0) {
+ while (vint::compareNM(rr, xn, y, yn) >= 0) {
size_t len = yn;
- double xt = GetApp(r, xn, false);
- if (vint::compareNM(&r[xn - len], yn, y, yn) < 0) {
+ double xt = GetApp(rr, xn, false);
+ if (vint::compareNM(&rr[xn - len], yn, y, yn) < 0) {
xt *= double(1ULL << (sizeof(T) * 8 - 1)) * 2;
len++;
}
T qt = T(xt / yt);
if (qt == 0) qt = 1;
- t[yn] = vint::mul1(&t[0], y, yn, qt);
- T b = vint::subN(&r[xn - len], &r[xn - len], &t[0], len);
+ t[yn] = vint::mulu1(&t[0], y, yn, qt);
+ T b = vint::subN(&rr[xn - len], &rr[xn - len], &t[0], len);
if (b) {
assert(!b);
}
if (q) q[xn - len] += qt;
- while (xn >= yn && r[xn - 1] == 0) {
+ while (xn >= yn && rr[xn - 1] == 0) {
xn--;
}
}
+ copyN(r, rr, rn);
}
/*
@@ -797,14 +858,14 @@ private:
{
size_t zn = xn + 1;
z.buf_.alloc(zn);
- z.buf_[zn - 1] = vint::add1(&z.buf_[0], &x[0], xn, y);
+ z.buf_[zn - 1] = vint::adds1(&z.buf_[0], &x[0], xn, y);
z.trim(zn);
}
static void usub1(VintT& z, const Buffer& x, size_t xn, Unit y)
{
size_t zn = xn;
z.buf_.alloc(zn);
- Unit c = vint::sub1(&z.buf_[0], &x[0], xn, y);
+ Unit c = vint::subs1(&z.buf_[0], &x[0], xn, y);
(void)c;
assert(!c);
z.trim(zn);
@@ -815,7 +876,7 @@ private:
z.buf_.alloc(xn);
Unit c = vint::subN(&z.buf_[0], &x[0], &y[0], yn);
if (xn > yn) {
- c = vint::sub1(&z.buf_[yn], &x[yn], xn - yn, c);
+ c = vint::subs1(&z.buf_[yn], &x[yn], xn - yn, c);
}
assert(!c);
z.trim(xn);
@@ -837,7 +898,7 @@ private:
z.isNeg_ = yNeg;
}
}
- static void _add1(VintT& z, const VintT& x, bool xNeg, int y, bool yNeg)
+ static void _adds1(VintT& z, const VintT& x, bool xNeg, int y, bool yNeg)
{
assert(y >= 0);
if ((xNeg ^ yNeg) == 0) {
@@ -854,6 +915,22 @@ private:
z.isNeg_ = yNeg;
}
}
+ static void _addu1(VintT& z, const VintT& x, bool xNeg, Unit y)
+ {
+ if (!xNeg) {
+ // same sign
+ uadd1(z, x.buf_, x.size(), y);
+ z.isNeg_ = xNeg;
+ return;
+ }
+ if (x.size() > 1 || x.buf_[0] >= y) {
+ usub1(z, x.buf_, x.size(), y);
+ z.isNeg_ = xNeg;
+ } else {
+ z = y - x.buf_[0];
+ z.isNeg_ = false;
+ }
+ }
/**
@param q [out] x / y if q != 0
@param r [out] x % y
@@ -871,12 +948,12 @@ private:
if (q) {
q->buf_.alloc(qn);
}
- r.buf_.alloc(xn);
- vint::divNM(q ? &q->buf_[0] : 0, &r.buf_[0], &x[0], xn, &y[0], yn);
+ r.buf_.alloc(yn);
+ vint::divNM(q ? &q->buf_[0] : 0, qn, &r.buf_[0], &x[0], xn, &y[0], yn);
if (q) {
q->trim(qn);
}
- r.trim(xn);
+ r.trim(yn);
}
struct MulMod {
const VintT *pm;
@@ -951,6 +1028,18 @@ public:
std::swap(size_, rhs.size_);
std::swap(isNeg_, rhs.isNeg_);
}
+ void dump() const
+ {
+ printf("size_=%d ", (int)size_);
+ for (size_t i = 0; i < size_; i++) {
+#if CYBOZU_OS_BIT == 32
+ printf("%08x", (uint32_t)buf_[size_ - 1 - i]);
+#else
+ printf("%016llx", (unsigned long long)buf_[size_ - 1 - i]);
+#endif
+ }
+ printf("\n");
+ }
/*
set positive value
@note assume little endian system
@@ -1012,7 +1101,7 @@ public:
std::vector<uint32_t> t;
while (!x.isZero()) {
- uint32_t r = divMod1(&x, x, i1e9);
+ uint32_t r = udivModu1(&x, x, i1e9);
t.push_back(r);
}
if (t.empty()) {
@@ -1026,7 +1115,7 @@ public:
break;
case 16:
{
- os << "0x" << std::hex;
+ os << std::hex;
const size_t n = size();
os << getUnit()[n - 1];
for (size_t i = 1; i < n; i++) {
@@ -1087,16 +1176,14 @@ public:
neg = true;
str = str.substr(1);
}
- if (str.size() >= 2 && str[0] == '0') {
- switch (str[1]) {
- case 'x':
- if (base != 0 && base != 16) throw cybozu::Exception("bad base in setStr(str)") << base;
- base = 16;
- str = str.substr(2);
- break;
- default:
- throw cybozu::Exception("not support base in setStr(str) 0") << str[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] == 'x') {
+ 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;
@@ -1118,6 +1205,9 @@ public:
setArray(&x[0], x.size());
}
break;
+ case 2:
+ binStr2Int(*this, str);
+ break;
default:
case 10:
decStr2Int(*this, str);
@@ -1150,6 +1240,8 @@ public:
uint32_t getLow32bit() const { return (uint32_t)buf_[0]; }
bool isOdd() const { return (buf_[0] & 1) == 1; }
bool isEven() const { return !isOdd(); }
+ const Unit *getUnit() const { return &buf_[0]; }
+ size_t getUnitSize() const { return size_; }
static void add(VintT& z, const VintT& x, const VintT& y)
{
_add(z, x, x.isNeg_, y, y.isNeg_);
@@ -1172,36 +1264,48 @@ public:
{
mul(y, x, x);
}
- static void add1(VintT& z, const VintT& x, int y)
+ static void addu1(VintT& z, const VintT& x, Unit y)
{
- if (y == invalidVar) throw cybozu::Exception("VintT:add1:bad y");
- _add1(z, x, x.isNeg_, std::abs(y), y < 0);
+ _addu1(z, x, x.isNeg_, y);
}
- static void sub1(VintT& z, const VintT& x, int y)
+ static void subu1(VintT& z, const VintT& x, Unit y)
{
- if (y == invalidVar) throw cybozu::Exception("VintT:sub1:bad y");
- _add1(z, x, x.isNeg_, std::abs(y), !(y < 0));
+ _addu1(z, x, x.isNeg_, y);
}
- static void mul1(VintT& z, const VintT& x, int y)
+ static void mulu1(VintT& z, const VintT& x, Unit y)
{
- if (y == invalidVar) throw cybozu::Exception("VintT:mul1:bad y");
size_t xn = x.size();
size_t zn = xn + 1;
- Unit absY = std::abs(y);
z.buf_.alloc(zn);
- z.buf_[zn - 1] = vint::mul1(&z.buf_[0], &x.buf_[0], xn, absY);
- z.isNeg_ = x.isNeg_ ^ (y < 0);
+ z.buf_[zn - 1] = vint::mulu1(&z.buf_[0], &x.buf_[0], xn, y);
+ z.isNeg_ = x.isNeg_;
z.trim(zn);
}
+ static void adds1(VintT& z, const VintT& x, int y)
+ {
+ if (y == invalidVar) throw cybozu::Exception("VintT:adds1:bad y");
+ _adds1(z, x, x.isNeg_, 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");
+ _adds1(z, x, x.isNeg_, 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");
+ mulu1(z, x, std::abs(y));
+ z.isNeg_ ^= (y < 0);
+ }
/*
@param q [out] q = x / y if q is not zero
@param x [in]
@param y [in] must be not zero
return x % y
*/
- static int divMod1(VintT *q, const VintT& x, int y)
+ static int divMods1(VintT *q, const VintT& x, int y)
{
- if (y == invalidVar) throw cybozu::Exception("VintT:divMod1:bad y");
+ if (y == invalidVar) throw cybozu::Exception("VintT:divMods1:bad y");
bool xNeg = x.isNeg_;
bool yNeg = y < 0;
Unit absY = std::abs(y);
@@ -1210,10 +1314,10 @@ public:
if (q) {
q->isNeg_ = xNeg ^ yNeg;
q->buf_.alloc(xn);
- r = vint::div1(&q->buf_[0], &x.buf_[0], xn, absY);
+ r = vint::divu1(&q->buf_[0], &x.buf_[0], xn, absY);
q->trim(xn);
} else {
- r = vint::mod1(&x.buf_[0], xn, absY);
+ r = vint::modu1(&x.buf_[0], xn, absY);
}
return xNeg ? -r : r;
}
@@ -1240,19 +1344,31 @@ public:
{
divMod(0, r, x, y);
}
- static void div1(VintT& q, const VintT& x, int y)
+ static void divs1(VintT& q, const VintT& x, int y)
{
- divMod1(&q, x, y);
+ divMods1(&q, x, y);
}
- static void mod1(VintT& r, const VintT& x, int y)
+ static void mods1(VintT& r, const VintT& x, int y)
{
bool xNeg = x.isNeg_;
- r = divMod1(0, x, y);
+ r = divMods1(0, x, y);
r.isNeg_ = xNeg;
}
+ static Unit udivModu1(VintT *q, const VintT& x, Unit y)
+ {
+ if (x.isNeg_) throw cybozu::Exception("VintT:udivu1:x is not negative") << x;
+ 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);
+ if (q) {
+ q->trim(xn);
+ q->isNeg_ = false;
+ }
+ return r;
+ }
/*
like Python
- 13 / 5 = 3 ... 2
+ 13 / 5 = 2 ... 3
13 / -5 = -3 ... -2
-13 / 5 = -3 ... 2
-13 / -5 = 2 ... -3
@@ -1554,8 +1670,6 @@ public:
VintT& operator--() { sub(*this, *this, 1); return *this; }
VintT operator++(int) { VintT c = *this; add(*this, *this, 1); return c; }
VintT operator--(int) { VintT c = *this; sub(*this, *this, 1); return c; }
- const Unit *getUnit() const { return &buf_[0]; }
- size_t getUnitSize() const { return size_; }
friend bool operator<(const VintT& x, const VintT& y) { return compare(x, y) < 0; }
friend bool operator>=(const VintT& x, const VintT& y) { return !operator<(x, y); }
friend bool operator>(const VintT& x, const VintT& y) { return compare(x, y) > 0; }
@@ -1569,11 +1683,11 @@ public:
VintT& operator%=(const VintT& rhs) { mod(*this, *this, rhs); return *this; }
VintT& operator&=(const VintT& rhs) { andBit(*this, *this, rhs); return *this; }
VintT& operator|=(const VintT& rhs) { orBit(*this, *this, rhs); return *this; }
- VintT& operator+=(int rhs) { add1(*this, *this, rhs); return *this; }
- VintT& operator-=(int rhs) { sub1(*this, *this, rhs); return *this; }
- VintT& operator*=(int rhs) { mul1(*this, *this, rhs); return *this; }
- VintT& operator/=(int rhs) { div1(*this, *this, rhs); return *this; }
- VintT& operator%=(int rhs) { mod1(*this, *this, rhs); return *this; }
+ VintT& operator+=(int rhs) { adds1(*this, *this, rhs); return *this; }
+ VintT& operator-=(int rhs) { subs1(*this, *this, rhs); return *this; }
+ VintT& operator*=(int rhs) { muls1(*this, *this, rhs); return *this; }
+ VintT& operator/=(int rhs) { divs1(*this, *this, rhs); return *this; }
+ VintT& operator%=(int rhs) { mods1(*this, *this, rhs); return *this; }
friend VintT operator+(const VintT& a, const VintT& b) { VintT c; add(c, a, b); return c; }
friend VintT operator-(const VintT& a, const VintT& b) { VintT c; sub(c, a, b); return c; }
friend VintT operator*(const VintT& a, const VintT& b) { VintT c; mul(c, a, b); return c; }
diff --git a/src/fp.cpp b/src/fp.cpp
index 7a34458..85e407f 100644
--- a/src/fp.cpp
+++ b/src/fp.cpp
@@ -161,12 +161,13 @@ std::string hexStrToLittleEndian(const char *buf, size_t bufSize)
}
// { 0xaf, 0x23, 0x01 } => "0123af"
-std::string littleEndianToHexStr(const char *buf, size_t bufSize)
+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, (uint8_t)buf[bufSize - 1 - i], false);
+ cybozu::itohex(&s[i * 2], 2, p[bufSize - 1 - i], false);
}
return s;
}
@@ -239,7 +240,7 @@ static inline void fp_invOpC(Unit *y, const Unit *x, const Op& op)
#ifdef MCL_USE_VINT
Vint vx, vy, vp;
vx.setArray(x, N);
- vy.setArray(op.p, N);
+ vp.setArray(op.p, N);
Vint::invMod(vy, vx, vp);
vy.getArray(y, N);
#else
@@ -334,7 +335,7 @@ void setOp(Op& op, Mode mode)
setOp2<N, Gtag, true>(op);
#ifdef MCL_USE_LLVM
if (mode != fp::FP_GMP && mode != fp::FP_GMP_MONT) {
-#if CYBOZU_HOST == CYBOZU_HOST_INTEL
+#if defined(MCL_USE_XBYAK) && CYBOZU_HOST == CYBOZU_HOST_INTEL
Xbyak::util::Cpu cpu;
if (cpu.has(Xbyak::util::Cpu::tBMI2)) {
setOp2<N, LBMI2tag, (N * UnitBitSize <= 256)>(op);
diff --git a/src/low_func.hpp b/src/low_func.hpp
index 8cbcf35..a482bf2 100644
--- a/src/low_func.hpp
+++ b/src/low_func.hpp
@@ -300,7 +300,7 @@ struct MulUnitPre {
static inline void func(Unit *z, const Unit *x, Unit y)
{
#ifdef MCL_USE_VINT
- z[N] = mcl::vint::mul1(z, x, N, y);
+ z[N] = mcl::vint::mulu1(z, x, N, y);
#else
z[N] = mpn_mul_1((mp_limb_t*)z, (const mp_limb_t*)x, N, y);
#endif
@@ -317,7 +317,7 @@ struct N1_Mod {
static inline void func(Unit *y, const Unit *x, const Unit *p)
{
#ifdef MCL_USE_VINT
- mcl::vint::divNM<Unit>(0, y, x, N + 1, p, N);
+ mcl::vint::divNM<Unit>(0, 0, y, x, N + 1, p, N);
#else
mp_limb_t q[2]; // not used
mpn_tdiv_qr(q, (mp_limb_t*)y, 0, (const mp_limb_t*)x, N + 1, (const mp_limb_t*)p, N);
@@ -380,7 +380,7 @@ struct Dbl_Mod {
static inline void func(Unit *y, const Unit *x, const Unit *p)
{
#ifdef MCL_USE_VINT
- mcl::vint::divNM<Unit>(0, y, x, N * 2, p, N);
+ mcl::vint::divNM<Unit>(0, 0, y, x, N * 2, p, N);
#else
mp_limb_t q[N + 1]; // not used
mpn_tdiv_qr(q, (mp_limb_t*)y, 0, (const mp_limb_t*)x, N * 2, (const mp_limb_t*)p, N);
diff --git a/test/fp_test.cpp b/test/fp_test.cpp
index fcd749b..628e0df 100644
--- a/test/fp_test.cpp
+++ b/test/fp_test.cpp
@@ -951,14 +951,14 @@ CYBOZU_TEST_AUTO(hexStrToLittleEndian)
{
const struct {
const char *in;
- char out[8];
+ uint8_t out[8];
size_t outSize;
} tbl[] = {
{ "", {}, 0 },
{ "0", { 0 }, 1 },
{ "12", { 0x12 }, 1 },
- { "abc", { (char)0xbc, (char)0x0a }, 2 },
- { "1234567890abcdef", { (char)0xef, (char)0xcd, (char)0xab, (char)0x90, 0x78, 0x56, 0x34, 0x12 }, 8 },
+ { "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;
@@ -972,15 +972,15 @@ CYBOZU_TEST_AUTO(hexStrToLittleEndian)
CYBOZU_TEST_AUTO(littleEndianToHexStr)
{
const struct {
- char in[8];
+ uint8_t in[8];
size_t inSize;
const char *out;
} tbl[] = {
{ {}, 0, "" },
{ { 0 }, 1, "00" },
{ { 0x12 }, 1, "12" },
- { { (char)0xbc, (char)0x0a }, 2, "0abc" },
- { { (char)0xef, (char)0xcd, (char)0xab, (char)0x90, 0x78, 0x56, 0x34, 0x12 }, 8, "1234567890abcdef" },
+ { { 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);
diff --git a/test/vint_test.cpp b/test/vint_test.cpp
index d95b518..1dd21dd 100644
--- a/test/vint_test.cpp
+++ b/test/vint_test.cpp
@@ -372,11 +372,11 @@ CYBOZU_TEST_AUTO(div1)
r = tbl[i].r;
z.setArray(tbl[i].c.p, tbl[i].c.n);
- u = (unsigned int)Vint::divMod1(&t, x, b);
+ u = (unsigned int)Vint::divMods1(&t, x, b);
CYBOZU_TEST_EQUAL(t, z);
CYBOZU_TEST_EQUAL(u, r);
- u = (unsigned int)Vint::divMod1(&x, x, b);
+ u = (unsigned int)Vint::divMods1(&x, x, b);
CYBOZU_TEST_EQUAL(x, z);
CYBOZU_TEST_EQUAL(u, r);
}
@@ -616,12 +616,13 @@ CYBOZU_TEST_AUTO(string)
size_t vn;
const char *str;
const char *hex;
+ const char *bin;
} tbl[] = {
- { { 0 }, 0, "0", "0x0" },
- { { 12345 }, 1, "12345", "0x3039" },
- { { 0xffffffff }, 1, "4294967295", "0xffffffff" },
- { { 0, 1 }, 2, "4294967296", "0x100000000" },
- { { 0, 0, 0, 0, 1 }, 5, "340282366920938463463374607431768211456", "0x100000000000000000000000000000000" },
+ { { 0 }, 0, "0", "0x0", "0b0" },
+ { { 12345 }, 1, "12345", "0x3039", "0b11000000111001" },
+ { { 0xffffffff }, 1, "4294967295", "0xffffffff", "0b11111111111111111111111111111111" },
+ { { 0, 1 }, 2, "4294967296", "0x100000000", "0b100000000000000000000000000000000" },
+ { { 0, 0, 0, 0, 1 }, 5, "340282366920938463463374607431768211456", "0x100000000000000000000000000000000", "0b100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" },
};
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
Vint x, y;