示例#1
0
文件: divisor.C 项目: syncom/libg2hec
divisor& divisor::random(divdeg_t dgr){ // Underlying curve is not touched
  assert(s_hcurve.is_valid_curve());

  // A random valid divisor is generated by the following algorithm:
  // generate a degree 1 divisor [x - a1, b1] by choosing a1 by random
  // then trying to solve quadratic equation
  // x^2 + h(a1)*x - f(a1) for b1.
  // Note that finding a root of an equation by calling routine
  // FindRoot(root, poly) may go into an infinite loop if poly does
  // not split completely.  We avoid this by calling irreducibility 
  // test routine DetIrredTest(poly).  After a degree 1 divisor is
  // found, this divisor is doubled by calling add_cantor() to return
  // a degree 2 polynomial.

  field_t a1, b1, f_of_a1, h_of_a1;

  poly_t poly;  // polynomial x^2 + h(a1)*x - f(a1)

  SetCoeff(poly, 2); // set degree 2 leading term to 1

  do {
    do{
      NTL_NNS random(a1);

      eval(f_of_a1, s_hcurve.get_f(), a1);

      eval(h_of_a1, s_hcurve.get_h(), a1);

      SetCoeff(poly, 1, h_of_a1);
      SetCoeff(poly, 0, - f_of_a1);

    } while ( DetIrredTest(poly) );

  FindRoot(b1, poly);

    // Set upoly = x - a1
    SetX(upoly);
    SetCoeff(upoly, 0, -a1);

    // Set vpoly = b1
    vpoly = b1;

    update();
  } while (*this == -*this); // Avoid getting unit after doubling

  // return a degree one divisor if dgr = 1
  if (dgr == DEGREE_1)
    return *this;

  // Double the degree 1 divisor to get a degree 2 divisor, otherwise
  add_cantor(*this, *this, *this);

  if (is_valid_divisor())
    return *this;

  cerr << "Random divisor failed to generate" << endl;
  abort();
}
示例#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";
}