diff --git a/include/mcl/conversion.hpp b/include/mcl/conversion.hpp index bd77e54..6f24344 100644 --- a/include/mcl/conversion.hpp +++ b/include/mcl/conversion.hpp @@ -24,15 +24,18 @@ namespace mcl { namespace fp { template bool setArrayAsLE(D *dst, size_t dstN, const S *src, size_t srcN) { + char assert_D_is_unsigned[D(-1) < 0 ? -1 : 1]; char assert_S_is_unsigned[S(-1) < 0 ? -1 : 1]; + (void)assert_D_is_unsigned; (void)assert_S_is_unsigned; if (sizeof(D) * dstN < sizeof(S) * srcN) return false; size_t pos = 0; size_t i = 0; while (i < dstN) { if (sizeof(D) < sizeof(S)) { - S s = src[pos++]; + S s = (pos < srcN) ? src[pos++] : 0; for (size_t j = 0; j < sizeof(S); j += sizeof(D)) { + assert(i < dstN); dst[i++] = D(s); s >>= sizeof(D) * 8; } diff --git a/include/mcl/gmp_util.hpp b/include/mcl/gmp_util.hpp index 87fa6a4..96e0605 100644 --- a/include/mcl/gmp_util.hpp +++ b/include/mcl/gmp_util.hpp @@ -16,6 +16,8 @@ #endif #include #include +#include + #ifdef _MSC_VER #pragma warning(push) #pragma warning(disable : 4616) @@ -61,24 +63,18 @@ void setArray(bool *pb, mpz_class& z, const T *buf, size_t n) buf[0, size) = x buf[size, maxSize) with zero */ -template -bool getArray_(T *buf, size_t maxSize, const U *x, int xn)//const mpz_srcptr x) -{ - const size_t bufByteSize = sizeof(T) * maxSize; - if (xn < 0) return false; - size_t xByteSize = sizeof(*x) * xn; - if (xByteSize > bufByteSize) return false; - memcpy(buf, x, xByteSize); - memset((char*)buf + xByteSize, 0, bufByteSize - xByteSize); - return true; -} template void getArray(bool *pb, T *buf, size_t maxSize, const mpz_class& x) { #ifdef MCL_USE_VINT - *pb = getArray_(buf, maxSize, x.getUnit(), (int)x.getUnitSize()); + *pb = fp::setArrayAsLE(buf, maxSize, x.getUnit(), x.getUnitSize()); #else - *pb = getArray_(buf, maxSize, x.get_mpz_t()->_mp_d, x.get_mpz_t()->_mp_size); + int n = x.get_mpz_t()->_mp_size; + if (n < 0) { + *pb = false; + return; + } + *pb = fp::setArrayAsLE(buf, maxSize, x.get_mpz_t()->_mp_d, n); #endif } inline void set(mpz_class& z, uint64_t x)