-
Notifications
You must be signed in to change notification settings - Fork 1
/
galois.h
executable file
·338 lines (276 loc) · 8.75 KB
/
galois.h
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// This file is part of par2cmdline (a PAR 2.0 compatible file verification and
// repair tool). See http://parchive.sourceforge.net for details of PAR 2.0.
//
// Copyright (c) 2003 Peter Brian Clements
//
// par2cmdline is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// par2cmdline is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#ifndef __GALOIS_H__
#define __GALOIS_H__
template <const unsigned int bits, const unsigned int generator, typename valuetype> class GaloisTable;
template <const unsigned int bits, const unsigned int generator, typename valuetype> class Galois;
template <class g> class GaloisLongMultiplyTable;
// This source file defines the Galois object for carrying out
// arithmetic in GF(2^16) using the generator 0x1100B.
// Also defined are the GaloisTable object (which contains log and
// anti log tables for use in multiplication and division), and
// the GaloisLongMultiplyTable object (which contains tables for
// carrying out multiplation of 16-bit galois numbers 8 bits at a time).
template <const unsigned int bits, const unsigned int generator, typename valuetype>
class GaloisTable
{
public:
typedef valuetype ValueType;
GaloisTable(void);
enum
{
Bits = bits,
Count = 1<<Bits,
Limit = Count-1,
Generator = generator,
};
ValueType log[Count];
ValueType antilog[Count];
};
template <const unsigned int bits, const unsigned int generator, typename valuetype>
class Galois
{
public:
typedef valuetype ValueType;
// Basic constructors
Galois(void) {};
Galois(ValueType v);
// Copy and assignment
Galois(const Galois &right) {value = right.value;}
Galois& operator = (const Galois &right) { value = right.value; return *this;}
// Addition
Galois operator + (const Galois &right) const { return (value ^ right.value); }
Galois& operator += (const Galois &right) { value ^= right.value; return *this;}
// Subtraction
Galois operator - (const Galois &right) const { return (value ^ right.value); }
Galois& operator -= (const Galois &right) { value ^= right.value; return *this;}
// Multiplication
Galois operator * (const Galois &right) const;
Galois& operator *= (const Galois &right);
// Division
Galois operator / (const Galois &right) const;
Galois& operator /= (const Galois &right);
// Power
Galois pow(unsigned int right) const;
Galois operator ^ (unsigned int right) const;
Galois& operator ^= (unsigned int right);
// Cast to value and value access
operator ValueType(void) const {return value;}
ValueType Value(void) const {return value;}
// Direct log and antilog
ValueType Log(void) const;
ValueType ALog(void) const;
enum
{
Bits = GaloisTable<bits,generator,valuetype>::Bits,
Count = GaloisTable<bits,generator,valuetype>::Count,
Limit = GaloisTable<bits,generator,valuetype>::Limit,
};
protected:
ValueType value;
static GaloisTable<bits,generator,valuetype> table;
};
#ifdef LONGMULTIPLY
template <class g>
class GaloisLongMultiplyTable
{
public:
GaloisLongMultiplyTable(void);
typedef g G;
enum
{
Bytes = ((G::Bits + 7) >> 3),
Count = ((Bytes * (Bytes+1)) / 2),
};
G tables[Count * 256 * 256];
};
#endif
// Construct the log and antilog tables from the generator
template <const unsigned int bits, const unsigned int generator, typename valuetype>
inline GaloisTable<bits,generator,valuetype>::GaloisTable(void)
{
u32 b = 1;
for (u32 l=0; l<Limit; l++)
{
log[b] = (ValueType)l;
antilog[l] = (ValueType)b;
b <<= 1;
if (b & Count) b ^= Generator;
}
log[0] = (ValueType)Limit;
antilog[Limit] = 0;
}
// The one and only galois log/antilog table object
template <const unsigned int bits, const unsigned int generator, typename valuetype>
GaloisTable<bits,generator,valuetype> Galois<bits,generator,valuetype>::table;
template <const unsigned int bits, const unsigned int generator, typename valuetype>
inline Galois<bits,generator,valuetype>::Galois(typename Galois<bits,generator,valuetype>::ValueType v)
{
value = v;
}
template <const unsigned int bits, const unsigned int generator, typename valuetype>
inline Galois<bits,generator,valuetype> Galois<bits,generator,valuetype>::operator * (const Galois<bits,generator,valuetype> &right) const
{
if (value == 0 || right.value == 0) return 0;
unsigned int sum = table.log[value] + table.log[right.value];
if (sum >= Limit)
{
return table.antilog[sum-Limit];
}
else
{
return table.antilog[sum];
}
}
template <const unsigned int bits, const unsigned int generator, typename valuetype>
inline Galois<bits,generator,valuetype>& Galois<bits,generator,valuetype>::operator *= (const Galois<bits,generator,valuetype> &right)
{
if (value == 0 || right.value == 0)
{
value = 0;
}
else
{
unsigned int sum = table.log[value] + table.log[right.value];
if (sum >= Limit)
{
value = table.antilog[sum-Limit];
}
else
{
value = table.antilog[sum];
}
}
return *this;
}
template <const unsigned int bits, const unsigned int generator, typename valuetype>
inline Galois<bits,generator,valuetype> Galois<bits,generator,valuetype>::operator / (const Galois<bits,generator,valuetype> &right) const
{
if (value == 0) return 0;
assert(right.value != 0);
if (right.value == 0) {return 0;} // Division by 0!
int sum = table.log[value] - table.log[right.value];
if (sum < 0)
{
return table.antilog[sum+Limit];
}
else
{
return table.antilog[sum];
}
}
template <const unsigned int bits, const unsigned int generator, typename valuetype>
inline Galois<bits,generator,valuetype>& Galois<bits,generator,valuetype>::operator /= (const Galois<bits,generator,valuetype> &right)
{
if (value == 0) return *this;
assert(right.value != 0);
if (right.value == 0) {return *this;} // Division by 0!
int sum = table.log[value] - table.log[right.value];
if (sum < 0)
{
value = table.antilog[sum+Limit];
}
else
{
value = table.antilog[sum];
}
return *this;
}
template <const unsigned int bits, const unsigned int generator, typename valuetype>
inline Galois<bits,generator,valuetype> Galois<bits,generator,valuetype>::pow(unsigned int right) const
{
if (right == 0) return 1;
if (value == 0) return 0;
unsigned int sum = table.log[value] * right;
sum = (sum >> Bits) + (sum & Limit);
if (sum >= Limit)
{
return table.antilog[sum-Limit];
}
else
{
return table.antilog[sum];
}
}
template <const unsigned int bits, const unsigned int generator, typename valuetype>
inline Galois<bits,generator,valuetype> Galois<bits,generator,valuetype>::operator ^ (unsigned int right) const
{
if (right == 0) return 1;
if (value == 0) return 0;
unsigned int sum = table.log[value] * right;
sum = (sum >> Bits) + (sum & Limit);
if (sum >= Limit)
{
return table.antilog[sum-Limit];
}
else
{
return table.antilog[sum];
}
}
template <const unsigned int bits, const unsigned int generator, typename valuetype>
inline Galois<bits,generator,valuetype>& Galois<bits,generator,valuetype>::operator ^= (unsigned int right)
{
if (right == 1) {value = 1; return *this;}
if (value == 0) return *this;
unsigned int sum = table.log[value] * right;
sum = (sum >> Bits) + (sum & Limit);
if (sum >= Limit)
{
value = table.antilog[sum-Limit];
}
else
{
value = table.antilog[sum];
}
return *this;
}
template <const unsigned int bits, const unsigned int generator, typename valuetype>
inline valuetype Galois<bits,generator,valuetype>::Log(void) const
{
return table.log[value];
}
template <const unsigned int bits, const unsigned int generator, typename valuetype>
inline valuetype Galois<bits,generator,valuetype>::ALog(void) const
{
return table.antilog[value];
}
#ifdef LONGMULTIPLY
template <class g>
inline GaloisLongMultiplyTable<g>::GaloisLongMultiplyTable(void)
{
G *table = tables;
for (unsigned int i=0; i<Bytes; i++)
{
for (unsigned int j=i; j<Bytes; j++)
{
for (unsigned int ii=0; ii<256; ii++)
{
for (unsigned int jj=0; jj<256; jj++)
{
*table++ = G(ii << (8*i)) * G(jj << (8*j));
}
}
}
}
}
#endif
typedef Galois<8,0x11D,u8> Galois8;
typedef Galois<16,0x1100B,u16> Galois16;
#endif // __GALOIS_H__