-
Notifications
You must be signed in to change notification settings - Fork 0
/
gf_base2.c
404 lines (327 loc) · 9.34 KB
/
gf_base2.c
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gf_base2.h"
struct gf_base2 {
uint32_t m; // degree of GF (limited to 8 due to choice of uint8_t)
uint32_t g; // coefficients of irreducible polynomial g(x)
uint32_t order; // number of elements in this Galois Field
uint8_t * mult_tbl; //multiplcation table
uint8_t * mult_inv_tbl; //multiplicative inverse table
};
/* static struct for functions in this file only */
struct gf_base2 gf;
void
gf_cleanup() {
if (gf.mult_tbl)
free(gf.mult_tbl);
if (gf.mult_inv_tbl)
free(gf.mult_inv_tbl);
}
uint8_t
gf_long_mult(uint8_t x, uint8_t y) {
uint32_t yy = y; // may need to shift y to left
uint32_t prod = 0;
uint32_t g_msb = 1 << gf.m;
// Multiply
while (x) {
if (x & 1)
prod ^= yy;
x >>= 1;
yy <<= 1;
}
// Reduce by g(x)
for (int i = gf.m - 2; i >= 0; i--)
if (prod & (g_msb << i))
prod ^= (gf.g << i);
return (uint8_t) prod;
}
int
gf_init(const uint32_t m, const uint32_t g) {
const char * mem_err =
"Error allocating memory. GF initialization failed.\n";
/* Supports only up to degree 8 due to choice of uint8_t */
if (m > 8) {
printf(
"Unsupported value for degree %d. "
"Currently only support GF base 2 fields of up to 8th degree. "
"GF initialzation failed.\n",
m
);
return -1;
}
/* Check degree of g(x) */
if ((g >> m) != 1) {
printf(
"Incorrect value for g(x) = 0x%x. Highest degree of g(x) must be "
"equal to m (%d). GF initialization failed.\n",
g, m
);
return -1;
}
printf("Initializing GF(2^%d)...\n", m);
memset(&gf, 0, sizeof(gf));
gf.m = m;
gf.g = g;
gf.order = 1 << m;
/* table is 2^m x 2^m entries */
gf.mult_tbl = malloc(sizeof(*gf.mult_tbl) * gf.order * gf.order);
if (!gf.mult_tbl) {
printf("%s", mem_err);
gf_cleanup();
return -1;
}
for (int row = 0; row < gf.order; row++)
for (int col = 0; col < gf.order; col++)
gf.mult_tbl[row * gf.order + col] = gf_long_mult(row, col);
/* table is 2^m x 1 entries */
gf.mult_inv_tbl = malloc(sizeof(*gf.mult_inv_tbl) * gf.order);
if (!gf.mult_inv_tbl) {
printf("%s", mem_err);
gf_cleanup();
return -1;
}
/* Loop starts at 1; mult. inverse for 0 is undefined. */
for (int i = 1; i < gf.order; i++) {
for (int j = 0; j < gf.order; j++) {
if (gf.mult_tbl[i * gf.order + j] == 1) {
gf.mult_inv_tbl[i] = j;
break;
}
}
}
printf("GF(2^%d) initialization completed.\n\n", m);
return 0;
}
uint8_t
gf_add(uint8_t x, uint8_t y) {
return x ^ y;
}
uint8_t
gf_add_inv(uint8_t x) {
return x;
}
uint8_t
gf_mult(uint8_t x, uint8_t y) {
return gf.mult_tbl[x * gf.order + y];
}
uint8_t
gf_mult_inv(uint8_t x) {
return gf.mult_inv_tbl[x];
}
uint8_t
gf_pow(uint8_t x, uint8_t y) {
uint8_t res = 1;
for (int i = 0; i < y; i++) {
res = gf_mult(res, x);
}
return res;
}
void
gf_matrix_print(struct gf_matrix * x) {
for (int i = 0; i < x->rows; i++) {
for (int j = 0; j < x->cols; j++)
printf("%02x ", x->v[i * x->cols + j]);
printf("\n");
}
}
void
gf_print_mult_tbl() {
int i = 0;
int j = 0;
printf(
"Multiplication table for GF(2^%d) with g(x) = 0x%x\n\n",
gf.m, gf.g
);
/* Leave space for row headers */
printf(" ");
/* Print column headers */
for (i = 0; i < gf.order; i++)
printf("%02x ", i);
printf("\n");
printf(" + ");
for (i = 0; i < gf.order; i++)
printf("---");
printf("\n");
/* Print each row */
for (i = 0; i < gf.order; i++) {
printf("%02x | ", i);
for (j = 0; j < gf.order; j++) {
printf("%02x ", gf.mult_tbl[i * gf.order + j]);
}
printf("\n");
}
printf("\n");
}
void
gf_print_mult_inv_tbl() {
int i = 0;
int j = 0;
printf(
"Multiplicative inverse table for GF(2^%d) with g(x) = 0x%x\n\n",
gf.m, gf.g
);
for (i = 1; i < gf.order; i++)
printf("%02x : %02x\n", i, gf.mult_inv_tbl[i]);
printf("\n");
}
struct gf_matrix *
gf_matrix_create(int rows, int cols) {
const char * mem_err =
"Error allocating memory. Matrix initialization failed.";
int m_size = sizeof(uint8_t) * rows * cols;
struct gf_matrix * m = malloc(sizeof(*m));
if (!m) {
printf("%s\n", mem_err);
return NULL;
}
memset(m, 0, sizeof(*m));
m->rows = rows;
m->cols = cols;
m->v = malloc(m_size);
if (!m->v) {
printf("%s\n", mem_err);
return NULL;
}
memset(m->v, 0, m_size);
return m;
}
struct gf_matrix *
gf_matrix_create_from(struct gf_matrix * x) {
struct gf_matrix * m = gf_matrix_create(x->rows, x->cols);
if (!m)
return NULL;
for (int i = 0; i < x->rows * x-> cols; i++)
m->v[i] = x->v[i];
return m;
}
void
gf_matrix_delete(struct gf_matrix * x) {
if (x) {
if (x->v)
free(x->v);
free(x);
}
}
void
gf_matrix_identity_set(struct gf_matrix * x) {
int n = (x->rows < x->cols) ? x->rows : x->cols;
for (int r = 0; r < n; r++)
for (int c = 0; c < n; c++)
x->v[r * x->cols + c] = (r == c) ? 1 : 0;
}
uint8_t
gf_matrix_dot_prod(struct gf_matrix * x,
struct gf_matrix * y,
int row,
int col) {
uint8_t res = 0;
uint8_t prod = 0;
for (int i = 0; i < x->cols; i++) {
prod = gf_mult(x->v[row * x->cols + i], y->v[i * y->cols + col]);
res = gf_add(res, prod);
}
return res;
}
int
gf_matrix_mult(struct gf_matrix * x,
struct gf_matrix * y,
struct gf_matrix * prod) {
if (x->cols != y->rows) {
printf("Invalid dimensions for matrix multiplication.\n");
return -1;
}
if (x->rows != prod->rows || y->cols != prod->cols) {
printf("Incorrect matrix dimensions to hold product..\n");
return -1;
}
for (int i = 0; i < x->rows; i++)
for (int j = 0; j < y->cols; j++)
prod->v[i * prod->cols + j] = gf_matrix_dot_prod(x, y, i, j);
return 0;
}
void
gf_matrix_swap_rows(struct gf_matrix * x, int row1, int row2) {
uint8_t temp = 0;
for (int i = 0; i < x->cols; i++) {
temp = x->v[row1 * x->cols + i];
x->v[row1 * x->cols + i] = x->v[row2 * x->cols + i];
x->v[row2 * x->cols + i] = temp;
}
}
void
gf_matrix_swap_cols(struct gf_matrix * x, int col1, int col2) {
uint8_t temp = 0;
for (int i = 0; i < x->rows; i++) {
temp = x->v[i * x->cols + col1];
x->v[i * x->cols + col1] = x->v[i * x->cols + col2];
x->v[i * x->cols + col2] = temp;
}
}
int
gf_matrix_inv(struct gf_matrix * x, struct gf_matrix * inv) {
int rc = 0;
uint8_t mult_inv = 0;
uint8_t scale = 0;
int idx = 0;
int col = 0;
int row = 0;
int row2 = 0;
if (x->rows != x->cols) {
printf("Non-sqaure matrices are singular.\n");
return -1;
}
// make a copy of x so we don't modify the original
struct gf_matrix * m = gf_matrix_create_from(x);
gf_matrix_identity_set(inv);
for (row = 0; row < m->rows; row++) {
// index of the pivot
int pivot = row * m->cols + row;
// if the pivot is zero, find another row to swap
if (!m->v[pivot]) {
for (row2 = row + 1; row2 < m->rows; row2++) {
if (m->v[row2 * m->cols + row]) {
gf_matrix_swap_rows(m, row, row2);
gf_matrix_swap_rows(inv, row, row2);
break;
}
}
}
switch (m->v[pivot]) {
case 0:
printf("Cannot find inverse for the following matrix:\n");
gf_matrix_print(x);
printf("Got up to here:\n");
gf_matrix_print(m);
rc = -1;
goto inv_err;
case 1:
// no-op
break;
default:
// scale row i so pivot is 1
mult_inv = gf_mult_inv(m->v[pivot]);
for (col = 0; col < m->cols; col++) {
idx = row * m->cols + col;
m->v[idx] = gf_mult(mult_inv, m->v[idx]);
inv->v[idx] = gf_mult(mult_inv, inv->v[idx]);
}
} // switch pivot value
// zero out the ith column in other rows
for (row2 = 0; row2 < m->rows; row2++) {
// skip the current row
if (row2 == row)
continue;
scale = m->v[row2 * m->cols + row];
for (col = 0; col < m->cols; col++) {
m->v[row2 * m->cols + col]
= gf_add(m->v[row2 * m->cols + col], gf_mult(scale, m->v[row * m->cols + col]));
inv->v[row2 * inv->cols + col]
= gf_add(inv->v[row2 * inv->cols + col], gf_mult(scale, inv->v[row * inv->cols + col]));
}
}
} // for each row in the original matrix
inv_err:
gf_matrix_delete(m);
return rc;
}