NTL_START_IMPL void ZZVec::SetSize(long n, long d) { if (n < 0 || d <= 0) Error("bad args to ZZVec::SetSize()"); if (v) Error("illegal ZZVec initialization"); len = n; bsize = d; if (n == 0) return; v = (ZZ*) NTL_MALLOC(n, sizeof(ZZ), 0); if (!v) Error("out of memory in ZZVec::SetSize()"); long i = 0; long m; long j; while (i < n) { m = ZZ_BlockConstructAlloc(v[i], d, n-i); for (j = 1; j < m; j++) ZZ_BlockConstructSet(v[i], v[i+j], j); i += m; } }
NTL_START_IMPL void WordVector::DoSetLength(long n) { long m; if (n < 0) { LogicError("negative length in vector::SetLength"); } if (NTL_OVERFLOW(n, NTL_BITS_PER_LONG, 0)) ResourceError("length too big in vector::SetLength"); if (n == 0) { if (rep) rep[-1] = 0; return; } if (!rep) { m = ((n+NTL_WordVectorMinAlloc-1)/NTL_WordVectorMinAlloc) * NTL_WordVectorMinAlloc; if (NTL_OVERFLOW(m, NTL_BITS_PER_LONG, 0)) ResourceError("length too big in vector::SetLength"); _ntl_ulong *p = (_ntl_ulong *) NTL_MALLOC(m, sizeof(_ntl_ulong), 2*sizeof(_ntl_ulong)); if (!p) { MemoryError(); } rep = p+2; rep[-1] = n; rep[-2] = m << 1; return; } long max_length = (rep[-2] >> 1); if (n <= max_length) { rep[-1] = n; return; } long frozen = (rep[-2] & 1); if (frozen) LogicError("Cannot grow this WordVector"); m = max(n, long(NTL_WordVectorExpansionRatio*max_length)); m = ((m+NTL_WordVectorMinAlloc-1)/NTL_WordVectorMinAlloc)*NTL_WordVectorMinAlloc; _ntl_ulong *p = rep - 2; if (NTL_OVERFLOW(m, NTL_BITS_PER_LONG, 0)) ResourceError("length too big in vector::SetLength"); p = (_ntl_ulong *) NTL_REALLOC(p, m, sizeof(_ntl_ulong), 2*sizeof(_ntl_ulong)); if (!p) { MemoryError(); } rep = p+2; rep[-1] = n; rep[-2] = m << 1; }
void ZZ_pInfoT::init() { ZZ B, M, M1, M2, M3; long n, i; long q, t; initialized = 1; sqr(B, p); LeftShift(B, B, NTL_FFTMaxRoot+NTL_FFTFudge); set(M); n = 0; while (M <= B) { UseFFTPrime(n); q = FFTPrime[n]; n++; mul(M, M, q); } NumPrimes = n; MaxRoot = CalcMaxRoot(q); double fn = double(n); if (8.0*fn*(fn+32) > NTL_FDOUBLE_PRECISION) Error("modulus too big"); if (8.0*fn*(fn+32) > NTL_FDOUBLE_PRECISION/double(NTL_SP_BOUND)) QuickCRT = 0; else QuickCRT = 1; if (!(x = (double *) NTL_MALLOC(n, sizeof(double), 0))) Error("out of space"); if (!(u = (long *) NTL_MALLOC(n, sizeof(long), 0))) Error("out of space"); ZZ_p_rem_struct_init(&rem_struct, n, p, FFTPrime); ZZ_p_crt_struct_init(&crt_struct, n, p, FFTPrime); if (ZZ_p_crt_struct_special(crt_struct)) return; ZZ qq, rr; DivRem(qq, rr, M, p); NegateMod(MinusMModP, rr, p); for (i = 0; i < n; i++) { q = FFTPrime[i]; long tt = rem(qq, q); mul(M2, p, tt); add(M2, M2, rr); div(M2, M2, q); // = (M/q) rem p div(M1, M, q); t = rem(M1, q); t = InvMod(t, q); mul(M3, M2, t); rem(M3, M3, p); ZZ_p_crt_struct_insert(crt_struct, i, M3); x[i] = ((double) t)/((double) q); u[i] = t; } }
NTL_START_IMPL zz_pInfoT::zz_pInfoT(long NewP, long maxroot) { ref_count = 1; if (maxroot < 0) Error("zz_pContext: maxroot may not be negative"); if (NewP <= 1) Error("zz_pContext: p must be > 1"); if (NumBits(NewP) > NTL_SP_NBITS) Error("zz_pContext: modulus too big"); ZZ P, B, M, M1, MinusM; long n, i; long q, t; p = NewP; pinv = 1/double(p); index = -1; conv(P, p); sqr(B, P); LeftShift(B, B, maxroot+NTL_FFTFudge); set(M); n = 0; while (M <= B) { UseFFTPrime(n); q = FFTPrime[n]; n++; mul(M, M, q); } if (n > 4) Error("zz_pInit: too many primes"); NumPrimes = n; PrimeCnt = n; MaxRoot = CalcMaxRoot(q); if (maxroot < MaxRoot) MaxRoot = maxroot; negate(MinusM, M); MinusMModP = rem(MinusM, p); if (!(CoeffModP = (long *) NTL_MALLOC(n, sizeof(long), 0))) Error("out of space"); if (!(x = (double *) NTL_MALLOC(n, sizeof(double), 0))) Error("out of space"); if (!(u = (long *) NTL_MALLOC(n, sizeof(long), 0))) Error("out of space"); for (i = 0; i < n; i++) { q = FFTPrime[i]; div(M1, M, q); t = rem(M1, q); t = InvMod(t, q); mul(M1, M1, t); CoeffModP[i] = rem(M1, p); x[i] = ((double) t)/((double) q); u[i] = t; } }