M4RIE  0.20111004
 All Data Structures Files Functions Variables Typedefs 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 /******************************************************************************
13 *
14 * M4RIE: Linear Algebra over GF(2^e)
15 *
16 * Copyright (C) 2010,2011 Martin Albrecht <martinralbrecht@googlemail.com>
17 *
18 * Distributed under the terms of the GNU General Public License (GPL)
19 * version 2 or higher.
20 *
21 * This code is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
25 *
26 * The full text of the GPL is available at:
27 *
28 * http://www.gnu.org/licenses/
29 ******************************************************************************/
30 
31 #include <m4ri/m4ri.h>
32 #include <m4rie/gf2x.h>
33 
38 #define M4RIE_MAX_DEGREE 16
39 
44 typedef struct gf2e_struct gf2e;
45 
50 struct gf2e_struct {
52  word minpoly;
54  word *pow_gen;
55  word *red;
56  word **_mul;
58  word (*inv)(const gf2e *ff, const word a);
59  word (*mul)(const gf2e *ff, const word a, const word b);
60 };
61 
68 gf2e *gf2e_init(const word minpoly);
69 
76 void gf2e_free(gf2e *ff);
77 
82 static inline word gf2e_inv(const gf2e *ff, word a) {
83  return gf2x_invmod(a, ff->minpoly, ff->degree);
84 }
85 
90 static inline word _gf2e_mul_table(const gf2e *ff, const word a, const word b) {
91  return ff->_mul[a][b];
92 }
93 
98 static inline word _gf2e_mul_arith(const gf2e *ff, const word a, const word b) {
99  const word res = gf2x_mul(a, b, ff->degree);
100  return res ^ ff->red[res>>ff->degree];
101 }
102 
107 static inline word gf2e_mul(const gf2e *ff, const word a, const word b) {
108  if( ff->_mul != NULL )
109  return _gf2e_mul_table(ff, a, b);
110  else
111  return _gf2e_mul_arith(ff, a, b);
112 }
113 
120 static inline size_t gf2e_degree_to_w(const gf2e *ff) {
121  switch(ff->degree) {
122  case 2:
123  return 2;
124  case 3:
125  case 4:
126  return 4;
127  case 5:
128  case 6:
129  case 7:
130  case 8:
131  return 8;
132  case 9:
133  case 10:
134  case 11:
135  case 12:
136  case 13:
137  case 14:
138  case 15:
139  case 16:
140  return 16;
141  default:
142  m4ri_die("degree %d not supported.\n",ff->degree);
143  }
144  return 0;
145 }
146 
154 static inline word *gf2e_t16_init(const gf2e *ff, const word a) {
155  word *mul = (word*)m4ri_mm_calloc(1<<16, sizeof(word));
156 
157  const deg_t w = gf2e_degree_to_w(ff);
158  const word mask_w = (1<<w)-1;
159 
163  for(word i=0; i<1<<16; i++) {
164  switch(w) {
165  case 2:
166  mul[i] = gf2e_mul(ff, a, ((i>>0)&mask_w))<<0 | gf2e_mul(ff, a, ((i>> 2)&mask_w))<< 2 | gf2e_mul(ff, a, ((i>> 4)&mask_w))<< 4 | gf2e_mul(ff, a, ((i>> 6)&mask_w))<< 6;
167  mul[i] |= gf2e_mul(ff, a, ((i>>8)&mask_w))<<8 | gf2e_mul(ff, a, ((i>>10)&mask_w))<<10 | gf2e_mul(ff, a, ((i>>12)&mask_w))<<12 | gf2e_mul(ff, a, ((i>>14)&mask_w))<<14;
168  break;
169  case 4:
170  mul[i] = gf2e_mul(ff, a, (i&mask_w)) | gf2e_mul(ff, a, ((i>>4)&mask_w))<<4 | gf2e_mul(ff, a, ((i>>8)&mask_w))<<8 | gf2e_mul(ff, a, ((i>>12)&mask_w))<<12;
171  break;
172  case 8:
173  mul[i] = gf2e_mul(ff, a, (i&mask_w)) | gf2e_mul(ff, a, ((i>>8)&mask_w))<<8;
174  break;
175  case 16:
176  mul[i] = gf2e_mul(ff, a, (i&mask_w));
177  break;
178  };
179  }
180  return mul;
181 }
182 
189 static inline void gf2e_t16_free(word *mul) {
190  m4ri_mm_free(mul);
191 }
192 
197 extern const word* irreducible_polynomials[17];
198 
199 #endif //M4RIE_GF2E_H
static void gf2e_t16_free(word *mul)
Free multiplication table.
Definition: gf2e.h:189
Definition: gf2e.h:50
int deg_t
Definition: gf2x.h:37
word(* mul)(const gf2e *ff, const word a, const word b)
Definition: gf2e.h:59
static size_t gf2e_degree_to_w(const gf2e *ff)
Definition: gf2e.h:120
word ** _mul
Definition: gf2e.h:56
deg_t degree
Definition: gf2e.h:51
static word * gf2e_t16_init(const gf2e *ff, const word a)
Definition: gf2e.h:154
void gf2e_free(gf2e *ff)
Definition: gf2e.c:56
static word gf2e_inv(const gf2e *ff, word a)
a^(-1) % minpoly
Definition: gf2e.h:82
word * red
Definition: gf2e.h:55
static word _gf2e_mul_arith(const gf2e *ff, const word a, const word b)
a*b in using a gf2x_mul() lookups.
Definition: gf2e.h:98
const word * irreducible_polynomials[17]
all Irreducible polynomials over GF(2) up to degree 16.
Definition: gf2e.c:84
gf2e * gf2e_init(const word minpoly)
Definition: gf2e.c:4
word(* inv)(const gf2e *ff, const word a)
Definition: gf2e.h:58
static word _gf2e_mul_table(const gf2e *ff, const word a, const word b)
a*b in using a table lookups.
Definition: gf2e.h:90
word minpoly
Definition: gf2e.h:52
static word gf2x_invmod(word a, word b, const deg_t d)
a^(-1) % b with deg(a), deg(b) <= d.
Definition: gf2x.h:152
word * pow_gen
Definition: gf2e.h:54
static word gf2x_mul(const word a, const word b, deg_t d)
a*b in with deg(a) and deg(b) < d.
Definition: gf2x.h:43
static word gf2e_mul(const gf2e *ff, const word a, const word b)
a*b in .
Definition: gf2e.h:107