M4RIE  0.20120613
 All Data Structures Files Functions Variables Macros Groups Pages
gf2e.h
Go to the documentation of this file.
1 
9 #ifndef M4RIE_GF2E_H
10 #define M4RIE_GF2E_H
11 
12 #include <m4ri/m4ri.h>
13 
18 typedef struct {
19  unsigned int degree;
20  word minpoly;
21  word **mul;
24  word *inv;
25  word *pow_gen;
26 } gf2e;
27 
34 void gf2e_free(gf2e *ff);
35 
42 static inline size_t gf2e_degree_to_w(const gf2e *ff) {
43  switch(ff->degree) {
44  case 2:
45  return 2;
46  case 3:
47  case 4:
48  return 4;
49  case 5:
50  case 6:
51  case 7:
52  case 8:
53  return 8;
54  case 9:
55  case 10:
56  case 11:
57  case 12:
58  case 13:
59  case 14:
60  case 15:
61  case 16:
62  return 16;
63  default:
64  m4ri_die("degree %d not supported.\n",ff->degree);
65  }
66  return 0;
67 }
68 
76 static inline word *gf2e_t16_init(const gf2e *ff, const word a) {
77  word *mul = (word*)m4ri_mm_calloc(1<<16, sizeof(word));
78 
79  const unsigned int w = gf2e_degree_to_w(ff);
80  const word mask_w = (1<<w)-1;
81  const word *x = ff->mul[a];
82 
86  for(word i=0; i<1<<16; i++) {
87  switch(w) {
88  case 2:
89  mul[i] = x[(i&mask_w)] | x[((i>>2)&mask_w)]<<2 | x[((i>>4)&mask_w)]<<4 | x[((i>>6)&mask_w)]<<6;
90  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;
91  break;
92  case 4:
93  mul[i] = x[(i&mask_w)] | x[((i>>4)&mask_w)]<<4 | x[((i>>8)&mask_w)]<<8 | x[((i>>12)&mask_w)]<<12;
94  break;
95  case 8:
96  mul[i] = x[(i&mask_w)] | x[((i>>8)&mask_w)]<<8;
97  break;
98  case 16:
99  mul[i] = x[(i&mask_w)];
100  break;
101  };
102  }
103  return mul;
104 }
105 
112 static inline void gf2e_t16_free(word *mul) {
113  m4ri_mm_free(mul);
114 }
115 
122 static inline void gf2e_make_pow_gen(gf2e *ff) {
123  unsigned int n = 2*ff->degree-1;
124  word *m = (word*)m4ri_mm_malloc( n * sizeof(word));
125  for(unsigned int i=0; i<n; i++) {
126  m[i] = 1<<i;
127  for(unsigned int j=i; j>=ff->degree; j--) {
128  if (m[i] & 1<<j)
129  m[i] ^= ff->minpoly<<(j - ff->degree);
130  }
131  }
132  ff->pow_gen = m;
133 }
134 
135 #endif //M4RIE_GF2E_H