Beispiel #1
0
void BuildSparseIrred(GF2X& f, long n)
{
   if (n <= 0) Error("SparseIrred: n <= 0");

   if (NTL_OVERFLOW(n, 1, 0)) 
      Error("overflow in BuildSparseIrred");

   if (n == 1) {
      SetX(f);
      return;
   }

   if (n <= 2048) {
      if (GF2X_irred_tab[n][1] == 0) {
         clear(f);
         SetCoeff(f, n);
         SetCoeff(f, GF2X_irred_tab[n][0]);
         SetCoeff(f, 0);
      }
      else {
         clear(f);
         SetCoeff(f, n);
         SetCoeff(f, GF2X_irred_tab[n][0]);
         SetCoeff(f, GF2X_irred_tab[n][1]);
         SetCoeff(f, GF2X_irred_tab[n][2]);
         SetCoeff(f, 0);
      }

      return;
   }

   long k3, k2, k1;

   k3 = FindTrinom(n);
   if (k3) {
      clear(f);
      SetCoeff(f, n);
      SetCoeff(f, k3);
      SetCoeff(f, 0);
      return;
   }

   k3 = FindPent(n, k2, k1);
   if (k3) {
      clear(f);
      SetCoeff(f, n);
      SetCoeff(f, k3);
      SetCoeff(f, k2);
      SetCoeff(f, k1);
      SetCoeff(f, 0);
      return;
   }

   // the following is probably of only theoretical value...
   // it is reasonable to conjecture that for all n >= 2,
   // there is either an irreducible trinomial or pentanomial
   // of degree n.

   BuildIrred(f, n);
}
Beispiel #2
0
void to_mat_ZZ_p_crt_rep(mat_ZZ_p_crt_rep& X, const mat_ZZ_p& A)
{
   long n = A.NumRows();
   long m = A.NumCols();

   const MatPrime_crt_helper& H = get_MatPrime_crt_helper_info();
   long nprimes = H.GetNumPrimes();

   if (NTL_OVERFLOW(nprimes, CRT_BLK, 0))
      ResourceError("overflow"); // this is pretty academic

   X.rep.SetLength(nprimes);
   for (long k = 0; k < nprimes; k++) X.rep[k].SetDims(n, m);

   ZZ_pContext context;
   context.save();


   bool seq = (double(n)*double(m)*H.GetCost() < PAR_THRESH);

   // FIXME: right now, we just partition the rows, but if
   // #cols > #rows, we should perhaps partition the cols
   NTL_GEXEC_RANGE(seq, n, first, last)
   NTL_IMPORT(n)
   NTL_IMPORT(m)
   NTL_IMPORT(nprimes)

   context.restore();

   MatPrime_crt_helper_scratch scratch;
   Vec<MatPrime_residue_t> remainders_store;
   remainders_store.SetLength(nprimes*CRT_BLK);
   MatPrime_residue_t *remainders = remainders_store.elts();

   for (long i = first; i < last; i++) {
      const ZZ_p *a = A[i].elts();

      long jj = 0; 
      for (; jj <= m-CRT_BLK; jj += CRT_BLK) {
         for (long j = 0; j < CRT_BLK; j++)
            reduce(H, rep(a[jj+j]), remainders + j*nprimes, scratch);
         for (long k = 0; k < nprimes; k++) {
            MatPrime_residue_t *x = X.rep[k][i].elts();
            for (long j = 0; j < CRT_BLK; j++)
               x[jj+j] = remainders[j*nprimes+k];
         }
      }
      if (jj < m) {
         for (long j = 0; j < m-jj; j++)
            reduce(H, rep(a[jj+j]), remainders + j*nprimes, scratch);
         for (long k = 0; k < nprimes; k++) {
            MatPrime_residue_t *x = X.rep[k][i].elts();
            for (long j = 0; j < m-jj; j++)
               x[jj+j] = remainders[j*nprimes+k];
         }
      }
   }

   NTL_GEXEC_RANGE_END
}
Beispiel #3
0
void BuildIrred(GF2X& f, long n)
{
   if (n <= 0)
      Error("BuildIrred: n must be positive");

   if (NTL_OVERFLOW(n, 1, 0)) Error("overflow in BuildIrred");

   if (n == 1) {
      SetX(f);
      return;
   }

   GF2X g;

   _ntl_ulong i;

   i = 0;
   do {
      ConvertBits(g, 2*i+1);
      SetCoeff(g, n);
      i++;
   } while (!IterIrredTest(g));

   f = g;

}
Beispiel #4
0
void InnerProduct(zz_p& x, const vec_zz_p& a, const vec_zz_p& b,
                  long offset)
{
   if (offset < 0) LogicError("InnerProduct: negative offset");
   if (NTL_OVERFLOW(offset, 1, 0)) ResourceError("InnerProduct: offset too big");

   long n = min(a.length(), b.length()+offset);
   long i;

   long accum, t;
   long p = zz_p::modulus();
   mulmod_t pinv = zz_p::ModulusInverse();


   const zz_p *ap = a.elts();
   const zz_p *bp = b.elts();

   accum = 0;
   for (i = offset; i < n; i++) {
      t = MulMod(rep(ap[i]), rep(bp[i-offset]), p, pinv);
      accum = AddMod(accum, t, p);
   }

   x.LoopHole() = accum;
}
void LeftShift(zz_pX& x, const zz_pX& a, long n)
{
   if (IsZero(a)) {
      clear(x);
      return;
   }

   if (n < 0) {
      if (n < -NTL_MAX_LONG) 
         clear(x);
      else
         RightShift(x, a, -n);
      return;
   }

   if (NTL_OVERFLOW(n, 1, 0))
      Error("overflow in LeftShift");

   long m = a.rep.length();

   x.rep.SetLength(m+n);

   long i;
   for (i = m-1; i >= 0; i--)
      x.rep[i+n] = a.rep[i];

   for (i = 0; i < n; i++)
      clear(x.rep[i]);
}
void ProjectPowers(vec_zz_p& x, const vec_zz_p& a, long k,
                   const zz_pXArgument& H, const zz_pXModulus& F)

{
   long n = F.n;

   if (a.length() > n || k < 0 || NTL_OVERFLOW(k, 1, 0))
      Error("ProjectPowers: bad args");

   long m = H.H.length()-1;
   long l = (k+m-1)/m - 1;

   zz_pXMultiplier M;
   build(M, H.H[m], F);

   vec_zz_p s(INIT_SIZE, n);
   s = a;
   StripZeroes(s);

   x.SetLength(k);

   for (long i = 0; i <= l; i++) {
      long m1 = min(m, k-i*m);
      zz_p* w = &x[i*m];
      for (long j = 0; j < m1; j++)
         InnerProduct(w[j], H.H[j].rep, s);
      if (i < l)
         UpdateMap(s, s, M, F);
   }
}
Beispiel #7
0
void GF2X::SetMaxLength(long n)
{
   if (n < 0) LogicError("GF2X::SetMaxLength: negative length");
   if (NTL_OVERFLOW(n, 1, 0))
      ResourceError("GF2X::SetMaxLength: excessive length");
   long w = (n + NTL_BITS_PER_LONG - 1)/NTL_BITS_PER_LONG;
   xrep.SetMaxLength(w);
}
Beispiel #8
0
void xdouble::SetOutputPrecision(long p)
{
   if (p < 1) p = 1;

   if (NTL_OVERFLOW(p, 1, 0)) 
      ResourceError("xdouble: output precision too big");

   oprec = p;
}
Beispiel #9
0
void quad_float::SetOutputPrecision(long p)
{
   if (p < 1) p = 1;

   if (NTL_OVERFLOW(p, 1, 0)) 
      ResourceError("quad_float: output precision too big");

   oprec = p;
}
Beispiel #10
0
void InvPrec(RR& x, const RR& a, long p)
{
   if (p < 1 || NTL_OVERFLOW(p, 1, 0))
      Error("InvPrec: bad precsion");

   long old_p = RR::prec;
   RR::prec = p;
   inv(x, a);
   RR::prec = old_p;
}
Beispiel #11
0
void DivPrec(RR& x, const RR& a, const RR& b, long p)
{
   if (p < 1 || NTL_OVERFLOW(p, 1, 0))
      Error("DivPrec: bad precsion");

   long old_p = RR::prec;
   RR::prec = p;
   div(x, a, b);
   RR::prec = old_p;
}
Beispiel #12
0
void NegatePrec(RR& x, const RR& a, const RR& b, long p)
{
   if (p < 1 || NTL_OVERFLOW(p, 1, 0))
      Error("NegatePrec: bad precsion");

   long old_p = RR::prec;
   RR::prec = p;
   negate(x, a);
   RR::prec = old_p;
}
Beispiel #13
0
void ConvPrec(RR& x, const char *s, long p)
{
   if (p < 1 || NTL_OVERFLOW(p, 1, 0))
      Error("ConvPrec: bad precsion");

   long old_p = RR::prec;
   RR::prec = p;
   conv(x, s);
   RR::prec = old_p;
}
Beispiel #14
0
void SqrRootPrec(RR& x, const RR& a, long p)
{
   if (p < 1 || NTL_OVERFLOW(p, 1, 0))
      Error("SqrRootPrec: bad precsion");

   long old_p = RR::prec;
   RR::prec = p;
   SqrRoot(x, a);
   RR::prec = old_p;
}
Beispiel #15
0
void ConvPrec(RR& x, const quad_float& a, long p)
{
   if (p < 1 || NTL_OVERFLOW(p, 1, 0))
      Error("ConvPrec: bad precsion");

   long old_p = RR::prec;
   RR::prec = p;
   conv(x, a);
   RR::prec = old_p;
}
Beispiel #16
0
void TruncPrec(RR& x, const RR& a, const RR& b, long p)
{
   if (p < 1 || NTL_OVERFLOW(p, 1, 0))
      Error("TruncPrec: bad precsion");

   long old_p = RR::prec;
   RR::prec = p;
   trunc(x, a);
   RR::prec = old_p;
}
Beispiel #17
0
void ConvPrec(RR& x, unsigned long a, long p)
{
   if (p < 1 || NTL_OVERFLOW(p, 1, 0))
      Error("ConvPrec: bad precsion");

   long old_p = RR::prec;
   RR::prec = p;
   conv(x, a);
   RR::prec = old_p;
}
Beispiel #18
0
void RoundPrec(RR& x, const RR& a, const RR& b, long p)
{
   if (p < 1 || NTL_OVERFLOW(p, 1, 0))
      Error("RoundPrec: bad precsion");

   long old_p = RR::prec;
   RR::prec = p;
   round(x, a);
   RR::prec = old_p;
}
Beispiel #19
0
void CeilPrec(RR& x, const RR& a, const RR& b, long p)
{
   if (p < 1 || NTL_OVERFLOW(p, 1, 0))
      Error("CeilPrec: bad precsion");

   long old_p = RR::prec;
   RR::prec = p;
   ceil(x, a);
   RR::prec = old_p;
}
Beispiel #20
0
void MakeRRPrec(RR& x, const ZZ& a, long e, long p)
{
   if (p < 1 || NTL_OVERFLOW(p, 1, 0))
      Error("MakeRRPrec: bad precsion");

   long old_p = RR::prec;
   RR::prec = p;
   MakeRR(x, a, e);
   RR::prec = old_p;
}
Beispiel #21
0
void RR::SetOutputPrecision(long p)
{
   if (p < 1)
      p = 1;

   if (NTL_OVERFLOW(p, 1, 0))
      ResourceError("RR: output precision too high");

   oprec = p;
}
Beispiel #22
0
void RR::SetPrecision(long p)
{
   if (p < 53)
      p = 53;

   if (NTL_OVERFLOW(p, 1, 0))
      ResourceError("RR: precision too high");

   prec = p;
}
Beispiel #23
0
void ConvPrec(RR& x, const RR& a, long p)
{
   if (p < 1 || NTL_OVERFLOW(p, 1, 0))
      Error("ConvPrec: bad precsion");

   long old_p = RR::prec;
   RR::prec = p;
   normalize(x, a);
   RR::prec = old_p;
}
void MinPolySeq(zz_pX& h, const vec_zz_p& a, long m)
{
   if (m < 0 || NTL_OVERFLOW(m, 1, 0)) Error("MinPoly: bad args");
   if (a.length() < 2*m) Error("MinPoly: sequence too short");

   if (m > NTL_zz_pX_BERMASS_CROSSOVER)
      GCDMinPolySeq(h, a, m);
   else
      BerlekampMassey(h, a, m);
}
Beispiel #25
0
void ConvPrec(RR& x, const RR& a, long p)
{
   if (p < 1)
      LogicError("ConvPrec: bad precsion");
   if (NTL_OVERFLOW(p, 1, 0))
      ResourceError("ConvPrec: precsion too big");

   RRPush push;
   RR::prec = p;
   normalize(x, a);
}
Beispiel #26
0
void DivPrec(RR& x, const RR& a, const RR& b, long p)
{
   if (p < 1)
      LogicError("DivPrec: bad precsion");
   if (NTL_OVERFLOW(p, 1, 0))
      ResourceError("DivPrec: bad precsion");

   RRPush push;
   RR::prec = p;
   div(x, a, b);
}
Beispiel #27
0
void ConvPrec(RR& x, double a, long p)
{
   if (p < 1)
      LogicError("ConvPrec: bad precsion");
   if (NTL_OVERFLOW(p, 1, 0))
      ResourceError("ConvPrec: bad precsion");

   RRPush push;
   RR::prec = p;
   conv(x, a);
}
Beispiel #28
0
void InvPrec(RR& x, const RR& a, long p)
{
   if (p < 1)
      LogicError("InvPrec: bad precsion");
   if (NTL_OVERFLOW(p, 1, 0))
      ResourceError("InvPrec: bad precsion");

   RRPush push;
   RR::prec = p;
   inv(x, a);
}
Beispiel #29
0
void MakeRRPrec(RR& x, const ZZ& a, long e, long p)
{
   if (p < 1)
      LogicError("MakeRRPrec: bad precsion");
   if (NTL_OVERFLOW(p, 1, 0))
      ResourceError("MakeRRPrec: precsion too big");

   RRPush push;
   RR::prec = p;
   MakeRR(x, a, e);
}
Beispiel #30
0
void FactorInt(FacVec& fvec, long n)
{
   if (n <= 1) LogicError("internal error: FactorInt(FacVec,long n) with n<=1");

   if (NTL_OVERFLOW(n, 1, 0))
      ResourceError("internal error: FactorInt(FacVec,long n) with n too large");

   long NumFactors;
   long q;

   fvec.SetLength(2*NextPowerOfTwo(n));

   NumFactors = 0;
   q = 2;

   while (n != 1) {
      if (n%q == 0) {
         fvec[NumFactors].q = q;
         n = n/q;
         fvec[NumFactors].a = 1;
         fvec[NumFactors].val = q;
         while (n%q == 0) {
            n = n/q;
            (fvec[NumFactors].a)++;
            fvec[NumFactors].val *= q;
         }         
         fvec[NumFactors].link = -1;
         NumFactors++;
      }

      q++;
   }

   fvec.SetLength(2*NumFactors-1);

   long lo = 0;
   long hi = NumFactors - 1;

   while (lo < hi) {
      FindMin(fvec, lo, hi);
      FindMin(fvec, lo+1, hi);
      hi++;
      fvec[hi].link = lo;
      fvec[hi].val = fvec[lo].val * fvec[lo+1].val;
      lo += 2;
   }
}