-
Notifications
You must be signed in to change notification settings - Fork 2
/
GaloisField.cpp
314 lines (236 loc) · 8.83 KB
/
GaloisField.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "GaloisField.h"
namespace galois
{
GaloisField::GaloisField() : power(0), field_size(0)
{
alpha_to = new GFSymbol [1];
index_of = new GFSymbol [1];
mul_inverse = new GFSymbol [1];
mul_table = new GFSymbol *[1];
div_table = new GFSymbol *[1];
exp_table = new GFSymbol *[1];
prim_poly_hash = 0;
}
GaloisField::GaloisField(const int pwr, const unsigned int* primitive_poly) : power(pwr), field_size((1 << power)-1)
{
alpha_to = new GFSymbol [field_size + 1];
index_of = new GFSymbol [field_size + 1];
#if !defined(NO_GFLUT)
mul_table = new GFSymbol* [(field_size + 1)];
div_table = new GFSymbol* [(field_size + 1)];
exp_table = new GFSymbol* [(field_size + 1)];
mul_inverse = new GFSymbol [(field_size + 1) * 2];
for (unsigned int i = 0; i < (field_size + 1); i++)
{
mul_table[i] = new GFSymbol [(field_size + 1)];
div_table[i] = new GFSymbol [(field_size + 1)];
exp_table[i] = new GFSymbol [(field_size + 1)];
}
#else
mul_table = new GFSymbol *[1];
div_table = new GFSymbol *[1];
exp_table = new GFSymbol *[1];
mul_inverse = new GFSymbol [1];
#endif
prim_poly_hash = 0xAAAAAAAA;
for (unsigned int i = 0; i < power; i++)
{
prim_poly_hash += ((i & 1) == 0) ? ( (prim_poly_hash << 7) ^ primitive_poly[i] ^ (prim_poly_hash >> 3)) :
(~((prim_poly_hash << 11) ^ primitive_poly[i] ^ (prim_poly_hash >> 5)));
}
generate_field(primitive_poly);
}
GaloisField::GaloisField(const GaloisField& gf)
{
power = gf.power;
field_size = gf.field_size;
prim_poly_hash = gf.prim_poly_hash;
alpha_to = new GFSymbol [field_size + 1];
index_of = new GFSymbol [field_size + 1];
memcpy(alpha_to, gf.alpha_to, (field_size + 1) * sizeof(GFSymbol));
memcpy(index_of, gf.index_of, (field_size + 1) * sizeof(GFSymbol));
#if !defined(NO_GFLUT)
mul_table = new GFSymbol* [(field_size + 1)];
div_table = new GFSymbol* [(field_size + 1)];
exp_table = new GFSymbol* [(field_size + 1)];
mul_inverse = new GFSymbol [(field_size + 1) * 2];
for (unsigned int i = 0; i < (field_size + 1); i++)
{
mul_table[i] = new GFSymbol [(field_size + 1)];
div_table[i] = new GFSymbol [(field_size + 1)];
exp_table[i] = new GFSymbol [(field_size + 1)];
}
memcpy(mul_inverse, gf.mul_inverse, (field_size + 1) * sizeof(GFSymbol) * 2);
memcpy(mul_table, gf.mul_table, (field_size + 1) * sizeof(GFSymbol*));
memcpy(div_table, gf.div_table, (field_size + 1) * sizeof(GFSymbol*));
memcpy(exp_table, gf.exp_table, (field_size + 1) * sizeof(GFSymbol*));
for (unsigned int i = 0; i < (field_size + 1); i++)
{
memcpy(mul_table[i], gf.mul_table[i], (field_size + 1) * sizeof(GFSymbol));
memcpy(div_table[i], gf.div_table[i], (field_size + 1) * sizeof(GFSymbol));
memcpy(exp_table[i], gf.exp_table[i], (field_size + 1) * sizeof(GFSymbol));
}
#endif
}
GaloisField::~GaloisField()
{
if (alpha_to != NULL) delete [] alpha_to;
if (index_of != NULL) delete [] index_of;
#if !defined(NO_GFLUT)
for (unsigned int i = 0; i < (field_size + 1); i++)
{
if (mul_table[i] != NULL) delete [] mul_table[i];
if (div_table[i] != NULL) delete [] div_table[i];
if (exp_table[i] != NULL) delete [] exp_table[i];
}
if (mul_table != NULL) delete [] mul_table;
if (div_table != NULL) delete [] div_table;
if (exp_table != NULL) delete [] exp_table;
if (mul_inverse != NULL) delete [] mul_inverse;
#endif
}
bool GaloisField::operator==(const GaloisField& gf)
{
return (
(this->power == gf.power) &&
(this->prim_poly_hash == gf.prim_poly_hash)
) ;
}
GaloisField& GaloisField::operator=(const GaloisField& gf)
{
if (this == &gf)
return *this;
if (alpha_to != NULL) delete [] alpha_to;
if (index_of != NULL) delete [] index_of;
power = gf.power;
field_size = gf.field_size;
prim_poly_hash = gf.prim_poly_hash;
memcpy(alpha_to, gf.alpha_to, (field_size + 1) * sizeof(GFSymbol));
memcpy(index_of, gf.index_of, (field_size + 1) * sizeof(GFSymbol));
#if !defined(NO_GFLUT)
if (mul_table != NULL) delete [] mul_table;
if (div_table != NULL) delete [] div_table;
if (exp_table != NULL) delete [] exp_table;
if (mul_inverse != NULL) delete [] mul_inverse;
mul_table = new GFSymbol* [(field_size + 1)];
div_table = new GFSymbol* [(field_size + 1)];
exp_table = new GFSymbol* [(field_size + 1)];
mul_inverse = new GFSymbol [(field_size + 1) * 2];
for (unsigned int i = 0; i < (field_size + 1); i++)
{
mul_table[i] = new GFSymbol [(field_size + 1)];
div_table[i] = new GFSymbol [(field_size + 1)];
exp_table[i] = new GFSymbol [(field_size + 1)];
}
memcpy(mul_inverse, gf.mul_inverse, (field_size + 1) * sizeof(GFSymbol) * 2);
memcpy(mul_table, gf.mul_table, (field_size + 1) * sizeof(GFSymbol*));
memcpy(div_table, gf.div_table, (field_size + 1) * sizeof(GFSymbol*));
memcpy(exp_table, gf.exp_table, (field_size + 1) * sizeof(GFSymbol*));
for (unsigned int i = 0; i < (field_size + 1); i++)
{
memcpy(mul_table[i], gf.mul_table[i], (field_size + 1) * sizeof(GFSymbol));
memcpy(div_table[i], gf.div_table[i], (field_size + 1) * sizeof(GFSymbol));
memcpy(exp_table[i], gf.exp_table[i], (field_size + 1) * sizeof(GFSymbol));
}
#endif
return *this;
}
void GaloisField::generate_field(const unsigned int* prim_poly)
{
/*
Note: It is assumed that the degree of the primitive
polynomial will be equivelent to the m value as
in GF(2^m)
*/
/*
need to update using stanford method for prim-poly generation.
*/
int mask = 1;
alpha_to[power] = 0;
for (unsigned int i = 0; i < power; i++)
{
alpha_to[i] = mask;
index_of[alpha_to[i]] = i;
if (prim_poly[i] != 0)
{
alpha_to[power] ^= mask;
}
mask <<= 1;
}
index_of[alpha_to[power]] = power;
mask >>= 1;
for (unsigned int i = power + 1; i < field_size; i++)
{
if (alpha_to[i - 1] >= mask)
alpha_to[i] = alpha_to[power] ^ ((alpha_to[i - 1] ^ mask) << 1);
else
alpha_to[i] = alpha_to[i - 1] << 1;
index_of[alpha_to[i]] = i;
}
index_of[0] = GFERROR;
alpha_to[field_size] = 1;
#if !defined(NO_GFLUT)
for (unsigned int i = 0; i < field_size + 1; i++)
{
for (unsigned int j = 0; j < field_size + 1; j++)
{
mul_table[i][j] = gen_mul(i,j);
div_table[i][j] = gen_div(i,j);
exp_table[i][j] = gen_exp(i,j);
}
}
for (unsigned int i = 0; i < (field_size + 1); i++)
{
mul_inverse[i] = gen_inverse(i);
mul_inverse[i + (field_size + 1)] = mul_inverse[i];
}
#endif
}
GFSymbol GaloisField::fast_modulus(GFSymbol x)
{
while (x >= (int)field_size)
{
x -= (int)field_size;
x = (x >> power) + (x & (int)field_size);
}
return x;
}
GFSymbol GaloisField::gen_mul(const GFSymbol& a, const GFSymbol& b)
{
if ((a == 0) || (b == 0))
return 0;
else
return alpha_to[fast_modulus(index_of[a] + index_of[b])];
}
GFSymbol GaloisField::gen_div(const GFSymbol& a, const GFSymbol& b)
{
if ((a == 0) || (b == 0))
return 0;
else
return alpha_to[fast_modulus(index_of[a] - index_of[b] + field_size)];
}
GFSymbol GaloisField::gen_exp(const GFSymbol& a, const unsigned int& n)
{
if (a != 0)
{
if (n == 0)
return 1;
else
return alpha_to[fast_modulus(index_of[a] * n)];
}
else
return 0;
}
GFSymbol GaloisField::gen_inverse(const GFSymbol& val)
{
return alpha_to[fast_modulus(field_size - index_of[val])];
}
std::ostream& operator << (std::ostream& os, const GaloisField& gf)
{
for(unsigned int i = 0; i < gf.field_size + 1; i++)
{
os << i << "\t" << gf.alpha_to[i] << "\t" << gf.index_of[i] << std::endl;
}
return os;
}
}