Exemplo n.º 1
0
NTL_CLIENT

int main()
{
    ZZ_p::init(conv<ZZ>(17)); // define GF(17)

    ZZ_pX P;
    BuildIrred(P, 10); // generate an irreducible polynomial P
    // of degree 10 over GF(17)

    ZZ_pE::init(P); // define GF(17^10)

    ZZ_pEX f, g, h;  // declare polynomials over GF(17^10)

    random(f, 20);  // f is a random, monic polynomial of degree 20
    SetCoeff(f, 20);

    random(h, 20); // h is a random polynomial of degree less than 20

    g = MinPolyMod(h, f); // compute the minimum polynomial of h modulo f

    if (g == 0) Error("oops (1)"); // check that g != 0

    if (CompMod(g, h, f) != 0) // check that g(h) = 0 mod f
        Error("oops (2)");
}
Exemplo n.º 2
0
NTL_CLIENT

int main()
{
   zz_p::init(17);

   zz_pX P;
   BuildIrred(P, 10);

   zz_pE::init(P);

   zz_pEX f, g, h;

   random(f, 20);
   SetCoeff(f, 20);

   random(h, 20);

   g = MinPolyMod(h, f);

   if (deg(g) < 0) Error("bad zz_pEXTest (1)");
   if (CompMod(g, h, f) != 0)
      Error("bad zz_pEXTest (2)");


   
   vec_pair_zz_pEX_long v;

   long i;
   for (i = 0; i < 5; i++) {
      long n = RandomBnd(20)+1;
      cerr << n << " ";

      random(f, n);
      SetCoeff(f, n);

      v = CanZass(f);

      g = mul(v);
      if (f != g) cerr << "oops1\n";

      long i;
      for (i = 0; i < v.length(); i++)
         if (!DetIrredTest(v[i].a))
            Error("bad zz_pEXTest (3)");


   }

   cerr << "\n";

   cerr << "zz_pEXTest OK\n";
}
void EDFSplit(vec_ZZ_pEX& v, const ZZ_pEX& f, const ZZ_pEX& b, long d)
{
   ZZ_pEX a, g, h;
   ZZ_pEXModulus F;
   vec_ZZ_pE roots;
   
   build(F, f);
   long n = F.n;
   long r = n/d;
   random(a, n);
   TraceMap(g, a, d, F, b);
   MinPolyMod(h, g, F, r);
   FindRoots(roots, h);
   FindFactors(v, f, g, roots);
}
Exemplo n.º 4
0
void CharPolyMod(zz_pX& g, const zz_pX& a, const zz_pX& ff)
{
   zz_pX f = ff;
   MakeMonic(f);
   long n = deg(f);

   if (n <= 0 || deg(a) >= n) 
      Error("CharPoly: bad args");

   if (IsZero(a)) {
      clear(g);
      SetCoeff(g, n);
      return;
   }

   if (n > 90 || (zz_p::PrimeCnt() <= 1 && n > 45)) {
      zz_pX h;
      MinPolyMod(h, a, f);
      if (deg(h) == n) {
         g = h;
         return;
      }
   }

   if (zz_p::modulus() < n+1) {
      HessCharPoly(g, a, f);
      return;
   }

   vec_zz_p u(INIT_SIZE, n+1), v(INIT_SIZE, n+1);

   zz_pX h, h1;
   negate(h, a);
   long i;

   for (i = 0; i <= n; i++) {
      u[i] = i;
      add(h1, h, u[i]);
      resultant(v[i], f, h1);
   }

   interpolate(g, u, v);
}
Exemplo n.º 5
0
static CYTHON_INLINE struct ZZX* ZZX_minpoly_mod(struct ZZX* x, struct ZZX* y)
{
    ZZX* f = new ZZX();
    MinPolyMod(*f, *x, *y);
    return f;
}
Exemplo n.º 6
0
NTL_CLOSE_NNS

NTL_CLIENT

int main()
{
   GF2X p;

   BuildIrred(p, 200);

   GF2E::init(p);

   GF2EX f;

   SetCoeff(f, 41);
   SetCoeff(f, 1);
   SetCoeff(f, 0);

   GF2X a;
   SetCoeff(a, 117);
   SetCoeff(a, 10);
   SetCoeff(a, 0);

   GF2EX g, h;
   SetX(g);
   SetCoeff(g, 0, to_GF2E(a));

   MinPolyMod(h, g, f);

   f = h;

   vec_pair_GF2EX_long u;

   CanZass(u, f, 1);

   cerr << "factorization pattern:";
   long i;

   for (i = 0; i < u.length(); i++) {
      cerr << " ";
      long k = u[i].b;
      if (k > 1)
         cerr << k << "*";
      cerr << deg(u[i].a);
   }

   cerr << "\n\n\n";

   GF2EX ff;
   mul(ff, u);

   if (f != ff || u.length() != 11) {
      cerr << "GF2EXTest NOT OK\n";
      return 1;
   }

   {

   cerr << "multiplication test...\n";

   BuildIrred(p, 512);
   GF2E::init(p);

   GF2EX A, B, C, C1;


   random(A, 512);
   random(B, 512);

   double t;
   long i;

   t = GetTime();
   for (i = 0; i < 10; i++) PlainMul(C, A, B);
   t = GetTime() - t;
   cerr << "time for plain mul of degree 511 over GF(2^512): " << (t/10) << "s\n";

   t = GetTime();
   for (i = 0; i < 10; i++) mul(C1, A, B);
   t = GetTime() - t;
   cerr << "time for karatsuba mul of degree 511 over GF(2^512): " << (t/10) << "s\n";

   if (C != C1) {
      cerr << "GF2EXTest NOT OK\n";
      return 1;
   }

   }

   {

   cerr << "multiplication test...\n";

   BuildIrred(p, 16);
   GF2E::init(p);

   GF2EX A, B, C, C1;


   random(A, 512);
   random(B, 512);

   double t;

   t = GetTime();
   for (i = 0; i < 10; i++) PlainMul(C, A, B);
   t = GetTime() - t;
   cerr << "time for plain mul of degree 511 over GF(2^16): " << (t/10) << "s\n";

   t = GetTime();
   for (i = 0; i < 10; i++) mul(C1, A, B);
   t = GetTime() - t;
   cerr << "time for karatsuba mul of degree 511 over GF(2^16): " << (t/10) << "s\n";

   if (C != C1) {
      cerr << "GF2EXTest NOT OK\n";
      return 1;
   }

   }

   cerr << "GF2EXTest OK\n";
   return 0;
}
Exemplo n.º 7
0
void SFBerlekamp(vec_ZZ_pX& factors, const ZZ_pX& ff, long verbose)
{
   ZZ_pX f = ff;

   if (!IsOne(LeadCoeff(f)))
      Error("SFBerlekamp: bad args");

   if (deg(f) == 0) {
      factors.SetLength(0);
      return;
   }

   if (deg(f) == 1) {
      factors.SetLength(1);
      factors[0] = f;
      return;
   }

   double t;

   const ZZ& p = ZZ_p::modulus();

   long n = deg(f);

   ZZ_pXModulus F;

   build(F, f);

   ZZ_pX g, h;

   if (verbose) { cerr << "computing X^p..."; t = GetTime(); }
   PowerXMod(g, p, F);
   if (verbose) { cerr << (GetTime()-t) << "\n"; }

   vec_long D;
   long r;

   vec_ZZVec M;

   if (verbose) { cerr << "building matrix..."; t = GetTime(); }
   BuildMatrix(M, n, g, F, verbose);
   if (verbose) { cerr << (GetTime()-t) << "\n"; }

   if (verbose) { cerr << "diagonalizing..."; t = GetTime(); }
   NullSpace(r, D, M, verbose);
   if (verbose) { cerr << (GetTime()-t) << "\n"; }


   if (verbose) cerr << "number of factors = " << r << "\n";

   if (r == 1) {
      factors.SetLength(1);
      factors[0] = f;
      return;
   }

   if (verbose) { cerr << "factor extraction..."; t = GetTime(); }

   vec_ZZ_p roots;

   RandomBasisElt(g, D, M);
   MinPolyMod(h, g, F, r);
   if (deg(h) == r) M.kill();
   FindRoots(roots, h);
   FindFactors(factors, f, g, roots);

   ZZ_pX g1;
   vec_ZZ_pX S, S1;
   long i;

   while (factors.length() < r) {
      if (verbose) cerr << "+";
      RandomBasisElt(g, D, M);
      S.kill();
      for (i = 0; i < factors.length(); i++) {
         const ZZ_pX& f = factors[i];
         if (deg(f) == 1) {
            append(S, f);
            continue;
         }
         build(F, f);
         rem(g1, g, F);
         if (deg(g1) <= 0) {
            append(S, f);
            continue;
         }
         MinPolyMod(h, g1, F, min(deg(f), r-factors.length()+1));
         FindRoots(roots, h);
         S1.kill();
         FindFactors(S1, f, g1, roots);
         append(S, S1);
      }
      swap(factors, S);
   }

   if (verbose) { cerr << (GetTime()-t) << "\n"; }

   if (verbose) {
      cerr << "degrees:";
      long i;
      for (i = 0; i < factors.length(); i++)
         cerr << " " << deg(factors[i]);
      cerr << "\n";
   }
}