-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_tables.c
50 lines (44 loc) · 1.2 KB
/
gen_tables.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
#include "gen_table.h"
uint8_t gfaddTable[0xff * 0xff];
uint8_t gfmulTable[0xff * 0xff];
void print_table(uint8_t arr[], uint8_t colSize, uint8_t rowSize, char *string);
void write_table(uint8_t arr[], FILE *fstream_s);
/*
* Generate addition and multiplication tables in GF(2^8)
*/
int gentable(void) {
for (int i = 0; i < 0xff; i++) {
for (int j = 0; j < 0xff; j++) {
gfaddTable[0xff * (i) + j] = gfadd((uint8_t) i, (uint8_t) j);
}
}
for (int i = 0; i < 0xff; i++) {
for (int j = 0; j < 0xff; j++) {
gfmulTable[0xff * (i) + j] = gfmul((uint8_t) i, (uint8_t) j);
}
}
//print_table(gfaddTable);
// File to write the table to.
FILE *fstream = fopen("table.txt", "w");
write_table(gfaddTable, fstream);
fclose(fstream);
return 0;
}
void write_table(uint8_t arr[], FILE *fstream_s) {
for (int i = 0; i < 0xff; i++) {
for (int j = 0; j < 0xff; j++) {
fprintf(fstream_s, "%x, ", arr[0xff * i + j]);
}
fprintf(fstream_s, "\n");
}
}
void print_table(uint8_t arr[], uint8_t colSize, uint8_t rowSize, char *string) {
printf("%s\n", string);
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
printf("%x, ", arr[colSize * (i) + j]);
}
printf("\n");
}
printf("\n");
}