Beispiel #1
0
poly_t * poly_sqrtmod_init(poly_t g) {
    int i, t;
    poly_t * sqrt, aux, p, q, * sq_aux;

    t = poly_deg(g);

    sq_aux = malloc(t * sizeof (poly_t));
    for (i = 0; i < t; ++i)
        sq_aux[i] = poly_alloc(t - 1);
    poly_sqmod_init(g, sq_aux);

    q = poly_alloc(t - 1);
    p = poly_alloc(t - 1);
    poly_set_deg(p, 1);
    poly_set_coeff(p, 1, gf_unit());
    // q(z) = 0, p(z) = z
    for (i = 0; i < t * gf_extd() - 1; ++i) {
        // q(z) <- p(z)^2 mod g(z)
        poly_sqmod(q, p, sq_aux, t);
        // q(z) <-> p(z)
        aux = q;
        q = p;
        p = aux;
    }
    // p(z) = z^(2^(tm-1)) mod g(z) = sqrt(z) mod g(z)

    sqrt = malloc(t * sizeof (poly_t));
    for (i = 0; i < t; ++i)
        sqrt[i] = poly_alloc(t - 1);

    poly_set(sqrt[1], p);
    poly_calcule_deg(sqrt[1]);
    for(i = 3; i < t; i += 2) {
        poly_set(sqrt[i], sqrt[i - 2]);
        poly_shiftmod(sqrt[i], g);
        poly_calcule_deg(sqrt[i]);
    }

    for (i = 0; i < t; i += 2) {
        poly_set_to_zero(sqrt[i]);
        sqrt[i]->coeff[i / 2] = gf_unit();
        sqrt[i]->deg = i / 2;
    }

    for (i = 0; i < t; ++i)
        poly_free(sq_aux[i]);
    free(sq_aux);
    poly_free(p);
    poly_free(q);

    return sqrt;
}
Beispiel #2
0
// retourne le degré du plus petit facteur
int poly_degppf(poly_t g) {
    int i, d, res;
    poly_t *u, p, r, s;

    d = poly_deg(g);
    u = malloc(d * sizeof (poly_t *));
    for (i = 0; i < d; ++i)
        u[i] = poly_alloc(d + 1);
    poly_sqmod_init(g, u);

    p = poly_alloc(d - 1);
    poly_set_deg(p, 1);
    poly_set_coeff(p, 1, gf_unit());
    r = poly_alloc(d - 1);
    res = d;
    for (i = 1; i <= (d / 2) * gf_extd(); ++i) {
        poly_sqmod(r, p, u, d);
        // r = x^(2^i) mod g
        if ((i % gf_extd()) == 0) { // donc 2^i = (2^m)^j (m deg d'extension)
            poly_addto_coeff(r, 1, gf_unit());
            poly_calcule_deg(r); // le degré peut changer
            s = poly_gcd(g, r);
            if (poly_deg(s) > 0) {
                poly_free(s);
                res = i / gf_extd();
                break;
            }
            poly_free(s);
            poly_addto_coeff(r, 1, gf_unit());
            poly_calcule_deg(r); // le degré peut changer
        }
        // on se sert de s pour l'échange
        s = p;
        p = r;
        r = s;
    }

    poly_free(p);
    poly_free(r);
    for (i = 0; i < d; ++i) {
        poly_free(u[i]);
    }
    free(u);

    return res;
}
Beispiel #3
0
// Returns the degree of the smallest factor
int poly_degppf(poly_t g) {
  int i, d, res;
  poly_t *u, p, r, s;

  d = poly_deg(g);
  u = malloc(d * sizeof (poly_t *));
  for (i = 0; i < d; ++i)
    u[i] = poly_alloc(d + 1);
  poly_sqmod_init(g, u);

  p = poly_alloc(d - 1);
  poly_set_deg(p, 1);
  poly_set_coeff(p, 1, gf_unit());
  r = poly_alloc(d - 1);
  res = d;
  for (i = 1; i <= (d / 2) * gf_extd(); ++i) {
    poly_sqmod(r, p, u, d);
    // r = x^(2^i) mod g
    if ((i % gf_extd()) == 0) { // so 2^i = (2^m)^j (m ext. degree)
      poly_addto_coeff(r, 1, gf_unit());
      poly_calcule_deg(r); // The degree may change
      s = poly_gcd(g, r);
      if (poly_deg(s) > 0) {
	poly_free(s);
	res = i / gf_extd();
	break;
      }
      poly_free(s);
      poly_addto_coeff(r, 1, gf_unit());
      poly_calcule_deg(r); // The degree may change
    }
    // No need for the exchange s
    s = p;
    p = r;
    r = s;
  }

  poly_free(p);
  poly_free(r);
  for (i = 0; i < d; ++i) {
    poly_free(u[i]);
  }
  free(u);

  return res;
}