-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-bench-ffa.c
144 lines (120 loc) · 2.49 KB
/
test-bench-ffa.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
#include <stdio.h>
#include <stdint.h>
#include "test.h"
#include "bench.h"
#include "gf254.h"
#undef INLINE
#define INLINE /* */
#include "gf254.c"
int arith(void) {
gf_t a, b, c, d, e;
__m128i _a, _b, _c, _d;
int code = 0;
TEST_BEGIN("addition is commutative") {
gf254_rnd(a);
gf254_rnd(b);
gf254_add(c, a, b);
gf254_add(d, b, a);
TEST_ASSERT(gf254_cmp(c, d) == 0, end);
}
TEST_END;
TEST_BEGIN("addition is associative") {
gf254_rnd(a);
gf254_rnd(b);
gf254_rnd(c);
gf254_add(d, a, b);
gf254_add(d, d, c);
gf254_add(e, b, c);
gf254_add(e, a, e);
TEST_ASSERT(gf254_cmp(d, e) == 0, end);
}
TEST_END;
TEST_BEGIN("multiplication is commutative") {
gf254_rnd(a);
gf254_rnd(b);
gf254_mul(c, a, b);
gf254_mul(d, b, a);
TEST_ASSERT(gf254_cmp(c, d) == 0, end);
}
TEST_END;
TEST_BEGIN("multiplication is associative") {
gf254_rnd(a);
gf254_rnd(b);
gf254_rnd(c);
gf254_mul(d, a, b);
gf254_mul(d, d, c);
gf254_mul(e, b, c);
gf254_mul(e, a, e);
TEST_ASSERT(gf254_cmp(d, e) == 0, end);
}
TEST_END;
TEST_BEGIN("squaring is consistent") {
gf254_rnd(a);
gf254_sqr(b, a);
gf254_mul(c, a, a);
TEST_ASSERT(gf254_cmp(b, c) == 0, end);
}
TEST_END;
TEST_BEGIN("inversion is consistent") {
uint64_t _one[] = { 1, 0, 0, 0 };
gf254_rnd(a);
gf254_inv(b, a);
gf254_mul(c, a, b);
low_rdc(&c[0], &c[1]);
gf254_inp(d, _one);
TEST_ASSERT(gf254_cmp(c, d) == 0, end);
}
TEST_END;
code = 1;
end:
return code;
}
void bench(void) {
gf_t a, b, c, d, e, f;
BENCH_BEGIN("gf254_mul") {
gf254_rnd(a);
gf254_rnd(b);
gf254_rnd(c);
BENCH_ADD(gf254_mul(a, a, b));
} BENCH_END;
BENCH_BEGIN("gf254_mul1") {
gf254_rnd(a);
gf254_rnd(b);
gf254_rnd(c);
BENCH_ADD(gf254_mul1(c, a, b));
} BENCH_END;
BENCH_BEGIN("gf254_mul_gamma") {
gf254_rnd(a);
gf254_rnd(b);
gf254_rnd(c);
BENCH_ADD(gf254_mul_gamma(c, a));
} BENCH_END;
BENCH_BEGIN("gf254_sqr") {
gf254_rnd(a);
gf254_rnd(c);
BENCH_ADD(gf254_sqr(c, a));
} BENCH_END;
BENCH_BEGIN("gf254_inv") {
gf254_rnd(a);
gf254_rnd(c);
BENCH_ADD(gf254_inv(c, a));
} BENCH_END;
BENCH_BEGIN("gf254_cswap") {
gf254_rnd(a);
gf254_rnd(c);
BENCH_ADD(gf254_cswap(c, a, 0));
} BENCH_END;
BENCH_BEGIN("gf254_sel") {
gf254_rnd(a);
gf254_rnd(b);
gf254_rnd(c);
BENCH_ADD(gf254_sel(c, a, b, 0));
} BENCH_END;
}
int main(int argc, char *argv[]) {
printf("\n-- Running tests for finite field arithmetic:\n\n");
arith();
printf("\n-- Running benchmarks:\n\n");
bench();
return 0;
}