Print
Short Description
Download Print...
Description
Gf.h #ifndef GF_H #define GF_H typedef unsigned short gf_t; int gf_extension_degree, gf_cardinality, gf_multiplicative_order; gf_t * gf_log; gf_t * gf_exp; /* MACROs for certain operations */ #define gf_extd() gf_extension_degree #define gf_card() gf_cardinality #define gf_ord() gf_multiplicative_order #define #define #define #define #define
gf_unit() gf_zero() gf_add(x, gf_exp(i) gf_log(x)
1 0 y) ((x) ^ (y)) gf_exp[i] /* alpha^i */ gf_log[x] /* return i when x=alpha^i */
// residual modulo q-1 // when -q < d < 0, we get (q-1+d) // when 0 gf_extd())) /* we obtain a value between 0 and (q-1) included, the class of 0 is represented by 0 or q-1 (this is why we write _K->exp[q-1]=_K->exp[0]=1)*/ #define gf_mul_fast(x, y) ((y) ? gf_exp[_gf_modq_1(gf_log[x] + gf_log[y])] : 0) #define gf_mul(x, y) ((x) ? gf_mul_fast(x, y) : 0) #define gf_square(x) ((x) ? gf_exp[_gf_modq_1(gf_log[x] gf_extd()) i = (i & (gf_ord())) + (i >> gf_extd()); return gf_exp[i]; } } // u8rnd is a function returning a random byte gf_t gf_rand(int (*u8rnd)()) { return (u8rnd() ^ (u8rnd() deg)
#define #define #define #define #define #define #define
poly_size(p) ((p)->size) poly_set_deg(p, d) ((p)->deg = (d)) poly_coeff(p, i) ((p)->coeff[i]) poly_set_coeff(p, i, a) ((p)->coeff[i] = (a)) poly_addto_coeff(p, i, a) ((p)->coeff[i] = gf_add((p)->coeff[i], (a))) poly_multo_coeff(p, i, a) ((p)->coeff[i] = gf_mul((p)->coeff[i], (a))) poly_tete(p) ((p)->coeff[(p)->deg])
/****** poly.c ******/ int poly_calcule_deg(poly_t p); poly_t poly_alloc(int d); poly_t poly_alloc_from_string(int d, const unsigned char * s); poly_t poly_copy(poly_t p); void poly_free(poly_t p); void poly_set_to_zero(poly_t p); void poly_set(poly_t p, poly_t q); poly_t poly_mul(poly_t p, poly_t q); void poly_rem(poly_t p, poly_t g); void poly_sqmod_init(poly_t g, poly_t * sq); void poly_sqmod(poly_t res, poly_t p, poly_t * sq, int d); poly_t poly_gcd(poly_t p1, poly_t p2); poly_t poly_quo(poly_t p, poly_t d); gf_t poly_eval(poly_t p, gf_t a); int poly_degppf(poly_t g); void poly_eeaux(poly_t * u, poly_t * v, poly_t p, poly_t g, int t); poly_t poly_t poly_t #endif
* poly_syndrome_init(poly_t generator, gf_t *support, int n); * poly_sqrtmod_init(poly_t g); poly_randgen_irred(int t, int (*u8rnd)()); /* POLY_H */
POLY.C #include #include #include #include "gf.h" #include "poly.h" / *********************************************************************************** **********/ ////////////////////////////////////POLYNOMIAL Functions/////////////////////////////////////// / *********************************************************************************** **********/ poly_t poly_alloc(int d) { poly_t p; p = (poly_t) malloc(sizeof (struct polynome)); p->deg = -1; p->size = d + 1; p->coeff = (gf_t *) calloc(p->size, sizeof (gf_t)); return p; }
// assumes s has the proper allocated size poly_t poly_alloc_from_string(int d, const unsigned char * s) { poly_t p; p = (poly_t) malloc(sizeof (struct polynome)); p->deg = -1; p->size = d + 1; p->coeff = (gf_t *) s; return p; } poly_t poly_copy(poly_t p) { poly_t q;
}
q = (poly_t) malloc(sizeof (struct polynome)); q->deg = p->deg; q->size = p->size; q->coeff = (gf_t *) calloc(q->size, sizeof (gf_t)); memcpy(q->coeff, p->coeff, p->size * sizeof (gf_t)); return q;
void poly_free(poly_t p) { free(p->coeff); free(p); } void poly_set_to_zero(poly_t p) { memset(p->coeff, 0, p->size * sizeof (gf_t)); p->deg = -1; } int poly_calcule_deg(poly_t p) { int d = p->size - 1; while ((d >= 0) && (p->coeff[d] == gf_zero())) --d; p->deg = d; return d; } // copy q in p void poly_set(poly_t p, poly_t int d = p->size - q->size; if (d < 0) { memcpy(p->coeff, q->coeff, poly_calcule_deg(p); } else { memcpy(p->coeff, q->coeff, memset(p->coeff + q->size, p->deg = q->deg; } }
q) { p->size * sizeof (gf_t));
q->size * sizeof (gf_t)); 0, d * sizeof (gf_t));
gf_t poly_eval_aux(gf_t * coeff, gf_t a, int d) { gf_t b; b = coeff[d--];
for (; d >= 0; --d) if (b != gf_zero()) b = gf_add(gf_mul(b, a), coeff[d]); else b = coeff[d]; return b; } poly_t poly_mul(poly_t p, poly_t q) { int i,j,dp,dq; poly_t r; poly_calcule_deg(p); poly_calcule_deg(q); dp = poly_deg(p); dq = poly_deg(q); r=poly_alloc(dp+dq); for (i = 0; i elem[j*A->coln+max+1]) A=mat_rowxor(A,j,i);//check the arg. order. } for(j=i-1;j>=0;j--)//fill the column with 0's upwards too. { if(mat_coeff(A,j,(max)))//(A->elem[j*A->coln+max+1]) A=mat_rowxor(A,j,i); }
} }//end for(i) }
return(perm);
void mat_vec_mul(unsigned long *cR, unsigned char *x, binmat_t A) { int i,j; unsigned long *pt; memset(cR,0,A->rwdcnt*sizeof(long)); pt = A->elem; for(i=0;irown;i++)//extract the first column in the form of char array. { if((x[i/8]>>(i%8))&1) for (j = 0; j < A->rwdcnt; ++j) cR[j] ^= *pt++; else pt += A->rwdcnt; } } binmat_t mat_mul(binmat_t A, binmat_t B) { binmat_t C; int i,j,k; if (A->coln != B->rown) exit(0); C = mat_ini(A->rown, B->coln); memset(C->elem,0,C->alloc_size); for(i=0;irown;i++) for(j=0;jcoln;j++) for (k = 0; k < A->coln; ++k) if (mat_coeff(A,i,k) && mat_coeff(B,k,j)) mat_change_coeff(C,i,j); }
return C;
Key pair #include
#include #include #include #include #include
"sizes.h" "gf.h" "poly.h" "matrix.h"
__inline int u8rnd() { return random() & 0xff; } __inline unsigned int u32rnd() { return u8rnd() ^ (u8rnd()coeff, (NB_ERRORS + 1) * sizeof (gf_t)); sk += (NB_ERRORS + 1) * sizeof (gf_t); poly_free(g); for (i = 0; i < NB_ERRORS; ++i) { memcpy(sk, sqrtmod[i]->coeff, NB_ERRORS * sizeof (gf_t)); sk += NB_ERRORS * sizeof (gf_t); poly_free(sqrtmod[i]); } free(sqrtmod); memcpy(pk, R->elem, R->alloc_size); mat_free(R); return 1; }
View more...
Comments