refactor setArrayMod

2merge^2
MITSUNARI Shigeo 4 years ago
parent dd0b16ca3f
commit 852804f15b
  1. 22
      include/mcl/fp.hpp
  2. 4
      include/mcl/gmp_util.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<class S>
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())
{

@ -67,7 +67,9 @@ template<class T>
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) {

Loading…
Cancel
Save