forked from dsprenkels/sss
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_gf256.c
90 lines (68 loc) · 2.52 KB
/
test_gf256.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
#include "gf256.h"
#include <stdio.h>
#define ASSERT(x, args...) if(!(x)) {printf(args); fail=1;}
uint8_t test_exp_log_inverses(void);
uint8_t test_additive_inverses(void);
uint8_t test_additive_identity(void);
uint8_t test_multiplicitive_inverses(void);
uint8_t test_multiplicitive_identity(void);
uint8_t test_exp_log_inverses(void) {
uint8_t fail = 0;
for(uint16_t i=1; i<256; i++) {
uint8_t a = i;
ASSERT(gf256_exp(gf256_log(a)) == a, "Exp as inverse of log failed for %d.", a)
}
printf("Exp/Log Inverse: %s\n", fail ? "fail" : "pass");
return fail;
}
uint8_t test_additive_inverses(void) {
uint8_t fail = 0;
for(uint16_t i=0; i<256; i++) {
uint8_t a = i;
ASSERT(gf256_add(a,a)==0, "Additive inverse property failed for %d.\n", a)
}
printf("Additive Inverse: %s\n", fail ? "fail" : "pass");
return fail;
}
uint8_t test_additive_identity(void) {
uint8_t fail = 0;
for(uint16_t i=0; i<256; i++) {
uint8_t a = i;
ASSERT(gf256_add(a,0)==a, "Additive identity property failed for %d.\n", a)
ASSERT(gf256_add(0,a)==a, "Additive identity property failed for %d.\n", a)
}
printf("Additive Identity: %s\n", fail ? "fail" : "pass");
return fail;
}
uint8_t test_multiplicitive_inverses(void) {
uint8_t fail = 0;
for(uint16_t i=1; i<256; i++) {
uint8_t a = i;
uint8_t b = gf256_div(1,a);
ASSERT(gf256_div(a,a)==1, "Multiplicitive inverse property failed for %d.", a)
ASSERT(gf256_mult(a,b)==1, "Multiplicitive inverse property failed for %d.", a)
ASSERT(gf256_mult(b,a)==1, "Multiplicitive inverse property failed for %d.", a)
}
printf("Multiplicitive Inverse: %s\n", fail ? "fail" : "pass");
return fail;
}
uint8_t test_multiplicitive_identity(void) {
uint8_t fail = 0;
for(uint16_t i=1; i<256; i++) {
uint8_t a = i;
ASSERT(gf256_mult(a,1)==a, "Multiplicitive identity property failed for %d.", a)
ASSERT(gf256_mult(1,a)==a, "Multiplicitive identity property failed for %d.", a)
ASSERT(gf256_div(a,1)==a, "Multiplicitive identity property failed for %d.", a)
}
printf("Multiplicitive Identity: %s\n", fail ? "fail" : "pass");
return fail;
}
int main(void) {
uint8_t fail = 0;
fail = test_exp_log_inverses() || fail;
fail = test_additive_inverses() || fail;
fail = test_additive_identity() || fail;
fail = test_multiplicitive_inverses() || fail;
fail = test_multiplicitive_identity() || fail;
return fail;
}