-
Notifications
You must be signed in to change notification settings - Fork 1
/
fields.h
315 lines (254 loc) · 7.83 KB
/
fields.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
/*
* SPDX-License-Identifier: MIT
*/
#ifndef FAEST_FIELDS_H
#define FAEST_FIELDS_H
#include "macros.h"
#include "endian_compat.h"
#include <stdint.h>
#include <string.h>
FAEST_BEGIN_C_DECL
typedef uint8_t bf8_t;
typedef uint64_t bf64_t;
typedef struct {
uint64_t values[2];
} bf128_t;
typedef struct {
uint64_t values[3];
} bf192_t;
typedef struct {
uint64_t values[4];
} bf256_t;
// GF(2^8) implementation
ATTR_PURE ATTR_ALWAYS_INLINE static inline bf8_t bf8_load(const uint8_t* src) {
return *src;
}
ATTR_ALWAYS_INLINE static inline void bf8_store(uint8_t* dst, bf8_t src) {
*dst = src;
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf8_t bf8_zero() {
return 0;
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf8_t bf8_one() {
return 1;
}
bf8_t bf8_rand();
ATTR_CONST ATTR_ALWAYS_INLINE inline bf8_t bf8_add(bf8_t lhs, bf8_t rhs) {
return lhs ^ rhs;
}
ATTR_CONST bf8_t bf8_mul(bf8_t lhs, bf8_t rhs);
ATTR_CONST bf8_t bf8_inv(bf8_t lhs);
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf8_t bf8_from_bit(uint8_t bit) {
return bit & 1;
}
// GF(2^64) implementation
ATTR_PURE ATTR_ALWAYS_INLINE inline bf64_t bf64_load(const uint8_t* src) {
bf64_t ret;
memcpy(&ret, src, sizeof(ret));
#if defined(FAEST_IS_BIG_ENDIAN)
ret = le64toh(ret);
#endif
return ret;
}
ATTR_ALWAYS_INLINE inline void bf64_store(uint8_t* dst, bf64_t src) {
#if defined(FAEST_IS_BIG_ENDIAN)
src = htole64(src);
#endif
memcpy(dst, &src, sizeof(src));
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf64_t bf64_zero() {
return 0;
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf64_t bf64_one() {
return 1;
}
bf64_t bf64_rand();
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf64_t bf64_add(bf64_t lhs, bf64_t rhs) {
return lhs ^ rhs;
}
ATTR_CONST bf64_t bf64_mul(bf64_t lhs, bf64_t rhs);
ATTR_CONST bf64_t bf64_inv(bf64_t lhs);
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf64_t bf64_from_bit(uint8_t bit) {
return bit & 1;
}
// GF(2^128) implementation
ATTR_PURE ATTR_ALWAYS_INLINE static inline bf128_t bf128_load(const uint8_t* src) {
bf128_t ret;
#if defined(FAEST_IS_BIG_ENDIAN)
for (unsigned int i = 0; i != ARRAY_SIZE(ret.values); ++i, src += sizeof(uint64_t)) {
memcpy(&ret.values[i], src, sizeof(ret.values[i]));
ret.values[i] = le64toh(ret.values[i]);
}
#else
memcpy(&ret, src, sizeof(ret));
#endif
return ret;
}
ATTR_ALWAYS_INLINE static inline void bf128_store(uint8_t* dst, bf128_t src) {
#if defined(FAEST_IS_BIG_ENDIAN)
for (unsigned int i = 0; i != ARRAY_SIZE(src.values); ++i, dst += sizeof(uint64_t)) {
uint64_t tmp = htole64(src.values[i]);
memcpy(dst, &tmp, sizeof(tmp));
}
#else
memcpy(dst, &src, sizeof(src));
#endif
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf128_t bf128_from_bf64(bf64_t src) {
bf128_t ret;
ret.values[0] = src;
ret.values[1] = 0;
return ret;
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf128_t bf128_from_bf8(bf8_t src) {
bf128_t ret;
ret.values[0] = src;
ret.values[1] = 0;
return ret;
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf128_t bf128_from_bit(uint8_t bit) {
return bf128_from_bf8(bit & 1);
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf128_t bf128_zero() {
bf128_t r = {0};
return r;
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf128_t bf128_one() {
bf128_t r = {{1, 0}};
return r;
}
ATTR_PURE bf128_t bf128_byte_combine(const bf128_t* x);
ATTR_PURE bf128_t bf128_byte_combine_bits(uint8_t x);
bf128_t bf128_rand();
ATTR_CONST ATTR_ALWAYS_INLINE inline bf128_t bf128_add(bf128_t lhs, bf128_t rhs) {
lhs.values[0] ^= rhs.values[0];
lhs.values[1] ^= rhs.values[1];
return lhs;
}
ATTR_CONST bf128_t bf128_mul(bf128_t lhs, bf128_t rhs);
ATTR_CONST bf128_t bf128_mul_64(bf128_t lhs, bf64_t rhs);
ATTR_CONST bf128_t bf128_mul_bit(bf128_t lhs, uint8_t rhs);
ATTR_CONST bf128_t bf128_inv(bf128_t lhs);
ATTR_PURE bf128_t bf128_sum_poly(const bf128_t* xs);
// GF(2^192) implemenation
ATTR_PURE ATTR_ALWAYS_INLINE static inline bf192_t bf192_load(const uint8_t* src) {
bf192_t ret;
#if defined(FAEST_IS_BIG_ENDIAN)
for (unsigned int i = 0; i != ARRAY_SIZE(ret.values); ++i, src += sizeof(uint64_t)) {
memcpy(&ret.values[i], src, sizeof(ret.values[i]));
ret.values[i] = le64toh(ret.values[i]);
}
#else
memcpy(&ret, src, sizeof(ret));
#endif
return ret;
}
ATTR_ALWAYS_INLINE static inline void bf192_store(uint8_t* dst, bf192_t src) {
#if defined(FAEST_IS_BIG_ENDIAN)
for (unsigned int i = 0; i != ARRAY_SIZE(src.values); ++i, dst += sizeof(uint64_t)) {
uint64_t tmp = htole64(src.values[i]);
memcpy(dst, &tmp, sizeof(tmp));
}
#else
memcpy(dst, &src, sizeof(src));
#endif
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf192_t bf192_from_bf64(bf64_t src) {
bf192_t ret;
ret.values[0] = src;
ret.values[1] = ret.values[2] = 0;
return ret;
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf192_t bf192_from_bf8(bf8_t src) {
bf192_t ret;
ret.values[0] = src;
ret.values[1] = ret.values[2] = 0;
return ret;
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf192_t bf192_from_bit(uint8_t bit) {
return bf192_from_bf8(bit & 1);
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf192_t bf192_zero() {
bf192_t r = {0};
return r;
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf192_t bf192_one() {
bf192_t r = {{1, 0, 0}};
return r;
}
ATTR_PURE bf192_t bf192_byte_combine(const bf192_t* x);
ATTR_PURE bf192_t bf192_byte_combine_bits(uint8_t x);
bf192_t bf192_rand();
ATTR_CONST static inline bf192_t bf192_add(bf192_t lhs, bf192_t rhs) {
for (unsigned int i = 0; i != ARRAY_SIZE(lhs.values); ++i) {
lhs.values[i] ^= rhs.values[i];
}
return lhs;
}
ATTR_CONST bf192_t bf192_mul(bf192_t lhs, bf192_t rhs);
ATTR_CONST bf192_t bf192_mul_64(bf192_t lhs, bf64_t rhs);
ATTR_CONST bf192_t bf192_mul_bit(bf192_t lhs, uint8_t rhs);
ATTR_CONST bf192_t bf192_inv(bf192_t lhs);
ATTR_PURE bf192_t bf192_sum_poly(const bf192_t* xs);
// GF(2^256) implementation
ATTR_PURE ATTR_ALWAYS_INLINE static inline bf256_t bf256_load(const uint8_t* src) {
bf256_t ret;
#if defined(FAEST_IS_BIG_ENDIAN)
for (unsigned int i = 0; i != ARRAY_SIZE(ret.values); ++i, src += sizeof(uint64_t)) {
memcpy(&ret.values[i], src, sizeof(ret.values[i]));
ret.values[i] = le64toh(ret.values[i]);
}
#else
memcpy(&ret, src, sizeof(ret));
#endif
return ret;
}
ATTR_ALWAYS_INLINE static inline void bf256_store(uint8_t* dst, bf256_t src) {
#if defined(FAEST_IS_BIG_ENDIAN)
for (unsigned int i = 0; i != ARRAY_SIZE(src.values); ++i, dst += sizeof(uint64_t)) {
uint64_t tmp = htole64(src.values[i]);
memcpy(dst, &tmp, sizeof(tmp));
}
#else
memcpy(dst, &src, sizeof(src));
#endif
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf256_t bf256_from_bf64(bf64_t src) {
bf256_t ret;
ret.values[0] = src;
ret.values[1] = ret.values[2] = ret.values[3] = 0;
return ret;
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf256_t bf256_from_bf8(bf8_t src) {
bf256_t ret;
ret.values[0] = src;
ret.values[1] = ret.values[2] = ret.values[3] = 0;
return ret;
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf256_t bf256_from_bit(uint8_t bit) {
return bf256_from_bf8(bit & 1);
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf256_t bf256_zero() {
bf256_t r = {0};
return r;
}
ATTR_CONST ATTR_ALWAYS_INLINE static inline bf256_t bf256_one() {
bf256_t r = {{1, 0, 0, 0}};
return r;
}
ATTR_PURE bf256_t bf256_byte_combine(const bf256_t* x);
ATTR_PURE bf256_t bf256_byte_combine_bits(uint8_t x);
bf256_t bf256_rand();
ATTR_CONST static inline bf256_t bf256_add(bf256_t lhs, bf256_t rhs) {
for (unsigned int i = 0; i != ARRAY_SIZE(lhs.values); ++i) {
lhs.values[i] ^= rhs.values[i];
}
return lhs;
}
ATTR_CONST bf256_t bf256_mul(bf256_t lhs, bf256_t rhs);
ATTR_CONST bf256_t bf256_mul_64(bf256_t lhs, bf64_t rhs);
ATTR_CONST bf256_t bf256_mul_bit(bf256_t lhs, uint8_t rhs);
ATTR_CONST bf256_t bf256_inv(bf256_t lhs);
ATTR_PURE bf256_t bf256_sum_poly(const bf256_t* xs);
FAEST_END_C_DECL
#endif