M4RIE  0.20120415
 All Data Structures Files Functions Variables Defines
gf2e.h
Go to the documentation of this file.
00001 
00009 #ifndef M4RIE_GF2E_H
00010 #define M4RIE_GF2E_H
00011 
00012 #include <m4ri/m4ri.h>
00013 
00018 typedef struct {
00019   unsigned int degree; 
00020   word minpoly;   
00021   word **mul;   
00024   word *inv; 
00025   word *pow_gen;   
00026 } gf2e;
00027 
00034 void gf2e_free(gf2e *ff);
00035 
00042 static inline size_t gf2e_degree_to_w(const gf2e *ff) {
00043   switch(ff->degree) {
00044   case 2:
00045     return 2;
00046   case  3:
00047   case  4:
00048     return 4;
00049   case  5:
00050   case  6:
00051   case  7:
00052   case  8:
00053     return 8;
00054   case  9:
00055   case 10:
00056   case 11:
00057   case 12:
00058   case 13:
00059   case 14:
00060   case 15:
00061   case 16:
00062     return 16;
00063   default:
00064     m4ri_die("degree %d not supported.\n",ff->degree);
00065   }
00066   return 0;
00067 }
00068 
00076 static inline word *gf2e_t16_init(const gf2e *ff, const word a) {
00077   word *mul = (word*)m4ri_mm_calloc(1<<16, sizeof(word));
00078 
00079   const unsigned int w = gf2e_degree_to_w(ff);
00080   const word mask_w = (1<<w)-1;
00081   const word *x = ff->mul[a];
00082 
00086   for(word i=0; i<1<<16; i++) {
00087     switch(w) {
00088     case 2:
00089       mul[i]  = x[(i&mask_w)] | x[((i>>2)&mask_w)]<<2 | x[((i>>4)&mask_w)]<<4 | x[((i>>6)&mask_w)]<<6;
00090       mul[i] |= x[((i>>8)&mask_w)]<<8 | x[((i>>10)&mask_w)]<<10 | x[((i>>12)&mask_w)]<<12 | x[((i>>14)&mask_w)]<<14;
00091       break;
00092     case 4:
00093       mul[i]  = x[(i&mask_w)] | x[((i>>4)&mask_w)]<<4 | x[((i>>8)&mask_w)]<<8 | x[((i>>12)&mask_w)]<<12;
00094       break;
00095     case 8:
00096       mul[i]  = x[(i&mask_w)] | x[((i>>8)&mask_w)]<<8;
00097       break;
00098     case 16:
00099       mul[i]  = x[(i&mask_w)];
00100       break;
00101     };
00102   }
00103   return mul;
00104 }
00105 
00112 static inline void gf2e_t16_free(word *mul) {
00113   m4ri_mm_free(mul);
00114 }
00115 
00122 static inline void gf2e_make_pow_gen(gf2e *ff) {
00123   unsigned int n = 2*ff->degree-1;
00124   word *m = (word*)m4ri_mm_malloc( n * sizeof(word));
00125   for(unsigned int i=0; i<n; i++) {
00126     m[i] = 1<<i;
00127     for(unsigned int j=i; j>=ff->degree; j--) {
00128       if (m[i] & 1<<j)
00129         m[i] ^= ff->minpoly<<(j - ff->degree);
00130     }
00131   }
00132   ff->pow_gen = m;
00133 }
00134 
00135 #endif //M4RIE_GF2E_H