void InnerProduct(ZZ_pX& x, const vec_ZZ_p& v, long low, long high, const vec_ZZ_pX& H, long n, ZZVec& t) { NTL_ZZRegister(s); long i, j; for (j = 0; j < n; j++) clear(t[j]); high = min(high, v.length()-1); for (i = low; i <= high; i++) { const vec_ZZ_p& h = H[i-low].rep; long m = h.length(); const ZZ& w = rep(v[i]); for (j = 0; j < m; j++) { mul(s, w, rep(h[j])); add(t[j], t[j], s); } } x.rep.SetLength(n); for (j = 0; j < n; j++) conv(x.rep[j], t[j]); x.normalize(); }
static void RandomBasisElt(ZZ_pX& g, const vec_long& D, const vec_ZZVec& M) { ZZ t1, t2; long n = D.length(); long i, j, s; g.rep.SetLength(n); vec_ZZ_p& v = g.rep; for (j = n-1; j >= 0; j--) { if (D[j] == -1) random(v[j]); else { i = D[j]; // v[j] = sum_{s=j+1}^{n-1} v[s]*M[i,s] clear(t1); for (s = j+1; s < n; s++) { mul(t2, rep(v[s]), M[i][s]); add(t1, t1, t2); } conv(v[j], t1); } } g.normalize(); }
void RightShift(ZZ_pX& x, const ZZ_pX& a, long n) { if (IsZero(a)) { clear(x); return; } if (n < 0) { if (n < -NTL_MAX_LONG) ResourceError("overflow in RightShift"); LeftShift(x, a, -n); return; } long da = deg(a); long i; if (da < n) { clear(x); return; } if (&x != &a) x.rep.SetLength(da-n+1); for (i = 0; i <= da-n; i++) x.rep[i] = a.rep[i+n]; if (&x == &a) x.rep.SetLength(da-n+1); x.normalize(); }
void randomizePlaintext(Plaintext &ptxt, unsigned deg, unsigned p) { ZZ_pX poly; poly.rep.SetLength(deg); for(unsigned i = 0; i < deg; i++) { poly.rep[i] = RandomBnd(p); } poly.normalize(); ptxt.Init(poly); }
void diff(ZZ_pX& x, const ZZ_pX& a) { long n = deg(a); long i; if (n <= 0) { clear(x); return; } if (&x != &a) x.rep.SetLength(n); for (i = 0; i <= n-1; i++) { mul(x.rep[i], a.rep[i+1], i+1); } if (&x == &a) x.rep.SetLength(n); x.normalize(); }
void ShiftSub(ZZ_pX& U, const ZZ_pX& V, long n) // assumes input does not alias output { if (IsZero(V)) return; long du = deg(U); long dv = deg(V); long d = max(du, n+dv); U.rep.SetLength(d+1); long i; for (i = du+1; i <= d; i++) clear(U.rep[i]); for (i = 0; i <= dv; i++) sub(U.rep[i+n], U.rep[i+n], V.rep[i]); U.normalize(); }
static void ZZ_pX_conv_modulus(ZZ_pX &fout, const ZZ_pX &fin, const ZZ_pContext &modout) { // Changes the modulus of fin to modout, and puts the result in fout. long i, n; n = fin.rep.length(); fout.rep.SetLength(n); ZZ_p* xp = fout.rep.elts(); const ZZ_p* ap = fin.rep.elts(); // I think it's enough to just restore modout once. // This should be true as long as the function rep taking a ZZ_p as an argument // and returning a ZZ works when the ZZ_p::modulus is incorrect. modout.restore(); for (i = 0; i < n; i++) { conv(xp[i], rep(ap[i])); } // We may have set a leading coefficient to 0, so we have to normalize fout.normalize(); }
void conv(ZZ_pX& x, const ZZX& a) { conv(x.rep, a.rep); x.normalize(); }