예제 #1
0
MAT key_genmat(gf_t *L, poly_t g)
{
  //L- Support
  //t- Number of errors, i.e.=30.
  //n- Length of the Goppa code, i.e.=2^11
  //m- The extension degree of the GF, i.e. =11
  //g- The generator polynomial.
  gf_t x,y;
  MAT H,R; 
  int i,j,k,r,n;
  int * perm, Laux[LENGTH];

  n=LENGTH;//2^11=2048
  r=NB_ERRORS*gf_extd();//32 x 11=352

  H=mat_ini(r,n);//initialize matrix with actual no. of bits.
  mat_set_to_zero(H); //set the matrix with all 0's.

  for(i=0;i< n;i++)
    {
      x = poly_eval(g,L[i]);//evaluate the polynomial at the point L[i].
      x = gf_inv(x);
      y = x;
      for(j=0;j<NB_ERRORS;j++)
	{
	  for(k=0;k<gf_extd();k++)
	    {
	      if(y & (1<<k))//if((y>>k) & 1)
		mat_set_coeff_to_one(H,j*gf_extd()+k,i);//the co-eff. are set in 2^0,...,2^11 ; 2^0,...,2^11 format along the rows/cols?
	    }
	  y = gf_mul(y,L[i]);
	}
    }//The H matrix is fed.
  
  perm = mat_rref(H);
  if (perm == NULL) {
    mat_free(H);
    return NULL;
  }
  
  R = mat_ini(n-r,r);
  mat_set_to_zero(R); //set the matrix with all 0's.
  for (i = 0; i < R->rown; ++i)
    for (j = 0; j < R->coln; ++j)
      if (mat_coeff(H,j,perm[i]))
	mat_change_coeff(R,i,j);

  for (i = 0; i < LENGTH; ++i)
    Laux[i] = L[perm[i]];
  for (i = 0; i < LENGTH; ++i)
    L[i] = Laux[i];

  mat_free(H);
  free(perm);

  return (R);
}
예제 #2
0
파일: poly.c 프로젝트: enascimento/supercop
// 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;
}
예제 #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;
}
예제 #4
0
파일: poly.c 프로젝트: enascimento/supercop
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;
}