diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 48bd1b7..4234fc5 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -363,13 +363,27 @@ public: toMont(); } /* - set (array mod p) - error if sizeof(S) * n > 64 + set (x as little endian) % p + error if size of x >= sizeof(Fp) * 2 */ template void setArrayMod(bool *pb, const S *x, size_t n) { - setArray_(pb, x, n, fp::Mod); + if (sizeof(S) * n > sizeof(fp::Unit) * op_.N * 2) { + *pb = false; + return; + } + mpz_class mx; + gmp::setArray(pb, mx, x, n); + if (!*pb) return; +#ifdef MCL_USE_VINT + op_.modp.modp(mx, mx); +#else + mx %= op_.mp; +#endif + gmp::getArray(pb, v_, op_.N, mx); + if (!*pb) return; + toMont(); } /* @@ -437,7 +451,7 @@ public: for (size_t i = 0; i < bufSize; i++) { swapBuf[bufSize - 1 - i] = p[i]; } - setArray_(pb, swapBuf, bufSize, mcl::fp::Mod); + setArrayMod(pb, swapBuf, bufSize); } void setByCSPRNG(bool *pb, fp::RandGen rg = fp::RandGen()) { diff --git a/include/mcl/gmp_util.hpp b/include/mcl/gmp_util.hpp index bc489cf..e444993 100644 --- a/include/mcl/gmp_util.hpp +++ b/include/mcl/gmp_util.hpp @@ -67,7 +67,9 @@ template void getArray(bool *pb, T *buf, size_t maxSize, const mpz_class& x) { #ifdef MCL_USE_VINT - *pb = fp::convertArrayAsLE(buf, maxSize, x.getUnit(), x.getUnitSize()); + const fp::Unit *src = x.getUnit(); + const size_t n = x.getUnitSize(); + *pb = fp::convertArrayAsLE(buf, maxSize, src, n); #else int n = x.get_mpz_t()->_mp_size; if (n < 0) {