-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtest_extras.c
389 lines (352 loc) · 12.4 KB
/
test_extras.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
/***********************************************************************************
* FourQlib: a high-performance crypto library based on the elliptic curve FourQ
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* Abstract: utility functions for tests
************************************************************************************/
#include "../FourQ_internal.h"
#include "../FourQ_params.h"
#include "test_extras.h"
#if (OS_TARGET == OS_WIN)
#include <windows.h>
#include <intrin.h>
#endif
#if (OS_TARGET == OS_LINUX) && (TARGET == TARGET_ARM || TARGET == TARGET_ARM64)
#include <time.h>
#endif
#include <stdlib.h>
#include <string.h>
int64_t cpucycles(void)
{ // Access system counter for benchmarking
#if (OS_TARGET == OS_WIN) && (TARGET == TARGET_AMD64 || TARGET == TARGET_x86)
return __rdtsc();
#elif (OS_TARGET == OS_WIN) && (TARGET == TARGET_ARM)
return __rdpmccntr64();
#elif (OS_TARGET == OS_LINUX) && (TARGET == TARGET_AMD64 || TARGET == TARGET_x86)
unsigned int hi, lo;
asm volatile ("rdtsc\n\t" : "=a" (lo), "=d"(hi));
return ((int64_t)lo) | (((int64_t)hi) << 32);
#elif (OS_TARGET == OS_LINUX) && (TARGET == TARGET_ARM || TARGET == TARGET_ARM64)
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
return (int64_t)(time.tv_sec*1e9 + time.tv_nsec);
#else
return 0;
#endif
}
int fp2compare64(uint64_t* a, uint64_t* b)
{ // Comparing uint64_t digits of two quadratic extension field elements, ai=bi? : (0) equal, (1) unequal
// NOTE: this function does not have constant-time execution. TO BE USED FOR TESTING ONLY.
unsigned int i;
for (i = 0; i < (2*NWORDS64_FIELD); i++) {
if (a[i] != b[i]) return 1;
}
return 0;
}
void random_scalar_test(uint64_t* a)
{ // Generating a pseudo-random scalar value in [0, 2^256-1]
// NOTE: distribution is not fully uniform. TO BE USED FOR TESTING ONLY.
unsigned char* string = (unsigned char*)&a[0];
unsigned int i;
for (i = 0; i < (sizeof(uint64_t)*NWORDS64_ORDER); i++) {
string[i] = (unsigned char)rand();
}
}
void fp2random1271_test(f2elm_t a)
{ // Generating a pseudo-random GF(p^2) element a+b*i, where a,b in [0, 2^127-1]
// NOTE: distribution is not fully uniform. TO BE USED FOR TESTING ONLY.
digit_t mask_7fff = (digit_t)-1 >> 1;
random_scalar_test((uint64_t*)&a[0]);
a[0][NWORDS_FIELD - 1] &= mask_7fff;
a[1][NWORDS_FIELD - 1] &= mask_7fff;
}
void random_order_test(digit_t* a)
{ // Generating a pseudo-random element in [0, order-1]
// SECURITY NOTE: distribution is not fully uniform. TO BE USED FOR TESTING ONLY.
int i;
unsigned char* string = (unsigned char*)a;
for (i = 0; i < 31; i++) {
string[i] = (unsigned char)rand(); // Obtain 246-bit number
}
string[30] &= 0x3F;
string[31] = 0;
subtract_mod_order(a, (digit_t*)&curve_order, a);
return;
}
bool verify_mLSB_recoding(uint64_t* scalar, int* digits)
{ // Verification of the mLSB-set's recoding algorithm used in fixed-base scalar multiplication
unsigned int j, l = L_FIXEDBASE, d = D_FIXEDBASE;
uint64_t temp, temp2, carry, borrow, generated_scalar[NWORDS64_ORDER] = {0};
int i, digit;
for (i = (l-1); i >= 0; i--)
{
// Shift generated scalar to the left by 1 (multiply by 2)
temp = ((generated_scalar[0] >> (RADIX64-1)) & 1) ;
generated_scalar[0] = generated_scalar[0] << 1;
for (j = 1; j < NWORDS64_ORDER; j++) {
temp2 = ((generated_scalar[j] >> (RADIX64-1)) & 1) ;
generated_scalar[j] = (generated_scalar[j] << 1) | temp;
temp = temp2;
}
// generated scalar + digit_i
if (i < (int)d) {
digit = digits[i] | 1;
if (digit >= 0) {
generated_scalar[0] = generated_scalar[0] + digit;
carry = (generated_scalar[0] < (unsigned int)digit);
for (j = 1; j < NWORDS64_ORDER; j++)
{
generated_scalar[j] = generated_scalar[j] + carry;
carry = (generated_scalar[j] < carry);
}
} else {
borrow = 0;
temp = (uint64_t)(-digit);
for (j = 0; j < NWORDS64_ORDER; j++)
{
temp2 = generated_scalar[j] - temp;
carry = (generated_scalar[j] < temp);
generated_scalar[j] = temp2 - borrow;
borrow = carry || (temp2 < borrow);
temp = 0;
}
}
} else {
digit = digits[i]*(digits[i-(i/d)*d] | 1);
if (digit >= 0) {
generated_scalar[0] = generated_scalar[0] + digit;
carry = (generated_scalar[0] < (unsigned int)digit);
for (j = 1; j < NWORDS64_ORDER; j++)
{
generated_scalar[j] = generated_scalar[j] + carry;
carry = (generated_scalar[j] < carry);
}
} else {
borrow = 0;
temp = (uint64_t)(-digit);
for (j = 0; j < NWORDS64_ORDER; j++)
{
temp2 = generated_scalar[j] - temp;
carry = (generated_scalar[j] < temp);
generated_scalar[j] = temp2 - borrow;
borrow = carry || (temp2 < borrow);
temp = 0;
}
}
}
}
for (j = 0; j < NWORDS64_ORDER; j++)
{
if (scalar[j] != generated_scalar[j])
return false;
}
return true;
}
static inline bool fpeq1271_unsafe(felm_t in1, felm_t in2)
{
return memcmp(in1, in2, sizeof(felm_t)) == 0;
}
void hash2curve_unsafe(f2elm_t r, point_t out)
{ // (Unsafe, non-constant-time version of) hash to curve function for testing
digit_t *r0 = (digit_t*)r[0], *r1 = (digit_t*)r[1];
felm_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18;
felm_t one = {0};
one[0] = 1;
digit_t* x0 = (digit_t*)out->x[0];
digit_t* x1 = (digit_t*)out->x[1];
digit_t* y0 = (digit_t*)out->y[0];
digit_t* y1 = (digit_t*)out->y[1];
fpadd1271(r0, r1, t0);
fpsub1271(r0, r1, t1);
fpmul1271(t0, t1, t0);
fpmul1271(r0, r1, t1);
fpadd1271(t1, t1, t1);
fpadd1271(t1, t1, t2);
fpadd1271(t0, t2, t2);
fpadd1271(t0, t0, t0);
fpsub1271(t0, t1, t3);
fpadd1271(t3, one, t0);
fpmul1271(A0, t0, t4);
fpmul1271(A1, t2, t1);
fpsub1271(t1, t4, t4);
fpmul1271(A1, t0, t5);
fpmul1271(A0, t2, t1);
fpadd1271(t1, t5, t1);
fpadd1271(t0, t2, t5);
fpsub1271(t0, t2, t6);
fpmul1271(t5, t6, t6);
fpmul1271(t2, t0, t5);
fpadd1271(t5, t5, t5);
fpmul1271(con1, t3, t7);
fpsub1271(t6, t7, t8);
fpmul1271(con2, t2, t7);
fpadd1271(t7, t8, t8);
fpmul1271(con1, t2, t7);
fpsub1271(t5, t7, t9);
fpmul1271(con2, t3, t7);
fpsub1271(t9, t7, t9);
fpmul1271(t4, t8, t5);
fpmul1271(t1, t9, t7);
fpadd1271(t5, t7, t7);
fpmul1271(t4, t9, t5);
fpmul1271(t1, t8, t10);
fpsub1271(t5, t10, t10);
fpsqr1271(t7, t5);
fpsqr1271(t10, t7);
fpadd1271(t5, t7, t5);
fpexp1251(t5, t7);
fpsqr1271(t7, t7);
fpmul1271(t5, t7, t7);
fpcopy1271(A0, t8);
fpcopy1271(A1, t9);
fpneg1271(t8);
fpneg1271(t9);
fpadd1271(A0, t4, t5);
fpsub1271(A1, t1, t11);
if (fpeq1271_unsafe(t7, one)) {
fpcopy1271(t8, t3);
fpcopy1271(t9, t10);
} else {
fpcopy1271(t5, t3);
fpcopy1271(t11, t10);
}
fpmul1271(t0, t3, t5);
fpmul1271(t2, t10, t8);
fpsub1271(t5, t8, t8);
fpmul1271(t2, t3, t5);
fpmul1271(t0, t10, t9);
fpadd1271(t5, t9, t9);
fpadd1271(t3, t10, t5);
fpsub1271(t3, t10, t11);
fpmul1271(t5, t11, t5);
fpmul1271(t3, t10, t11);
fpadd1271(t11, t11, t11);
fpmul1271(t3, t4, t12);
fpmul1271(t1, t10, t13);
fpadd1271(t12, t13, t13);
fpmul1271(t4, t10, t14);
fpmul1271(t1, t3, t12);
fpsub1271(t14, t12, t12);
fpsub1271(t5, t13, t5);
fpsub1271(t11, t12, t11);
fpadd1271(t5, t6, t5);
fpmul1271(t0, t2, t6);
fpadd1271(t6, t6, t6);
fpadd1271(t11, t6, t11);
fpmul1271(t5, t8, t6);
fpmul1271(t9, t11, t12);
fpsub1271(t6, t12, t6);
fpmul1271(t5, t9, t12);
fpmul1271(t8, t11, t8);
fpadd1271(t12, t8, t12);
fpadd1271(t6, t6, t6);
fpadd1271(t6, t6, t6);
fpadd1271(t6, t6, t6);
fpadd1271(t6, t6, t6);
fpadd1271(t12, t12, t12);
fpadd1271(t12, t12, t12);
fpadd1271(t12, t12, t12);
fpadd1271(t12, t12, t12);
fpadd1271(t0, t3, t14);
fpadd1271(t14, t14, t14);
fpadd1271(t2, t10, t8);
fpadd1271(t8, t8, t8);
fpmul1271(t6, t14, t4);
fpmul1271(t8, t12, t1);
fpsub1271(t4, t1, t4);
fpmul1271(t12, t14, t9);
fpmul1271(t6, t8, t1);
fpadd1271(t1, t9, t1);
fpsqr1271(t12, t5);
fpsqr1271(t6, t9);
fpadd1271(t5, t9, t9);
fpsqr1271(t1, t5);
fpsqr1271(t4, t11);
fpadd1271(t11, t5, t11);
fpsqr1271(t11, t5);
fpmul1271(t5, t9, t5);
fpexp1251(t5, t7);
fpsqr1271(t7, t13);
fpsqr1271(t13, t13);
fpmul1271(t11, t13, t13);
fpmul1271(t9, t13, t13);
fpmul1271(t5, t13, t13);
fpmul1271(t13, t7, t7);
fpmul1271(t5, t7, t7);
fpadd1271(t6, t7, t5);
fpdiv1271(t5);
fpexp1251(t5, t9);
fpsqr1271(t9, t11);
fpsqr1271(t11, t11);
fpmul1271(t5, t11, t11);
fpmul1271(t5, t9, t9);
fpmul1271(t11, t12, t11);
fpsqr1271(t9, t7);
fpadd1271(one, one, t15);
fpcopy1271(t11, t16);
fpcopy1271(t15, x0);
fpneg1271(x0);
if (fpeq1271_unsafe(t5, t7)) {
fpcopy1271(t15, t17);
fpcopy1271(t16, t18);
} else {
fpcopy1271(t16, t17);
fpcopy1271(x0, t18);
}
fpadd1271(t13, t13, t13);
fpsub1271(t3, t0, y0);
fpsub1271(t10, t2, y1);
fpmul1271(y0, t6, t16);
fpmul1271(y1, t12, t15);
fpsub1271(t16, t15, t15);
fpmul1271(y0, t12, y0);
fpmul1271(t6, y1, t16);
fpadd1271(t16, y0, t16);
fpmul1271(t15, t4, x0);
fpmul1271(t1, t16, y0);
fpadd1271(x0, y0, y0);
fpmul1271(t4, t16, y1);
fpmul1271(t1, t15, x0);
fpsub1271(y1, x0, y1);
fpmul1271(y0, t13, y0);
fpmul1271(y1, t13, y1);
fpmul1271(b0, t3, t15);
fpmul1271(b1, t10, x0);
fpsub1271(t15, x0, t15);
fpmul1271(b0, t10, t16);
fpmul1271(b1, t3, x0);
fpadd1271(t16, x0, t16);
fpmul1271(t15, t4, t5);
fpmul1271(t1, t16, x0);
fpadd1271(x0, t5, x0);
fpmul1271(t4, t16, x1);
fpmul1271(t1, t15, t5);
fpsub1271(x1, t5, x1);
fpmul1271(x0, t0, t5);
fpmul1271(x1, t2, t15);
fpsub1271(t5, t15, t15);
fpmul1271(x1, t0, t5);
fpmul1271(x0, t2, t16);
fpadd1271(t5, t16, t16);
fpmul1271(t15, t14, t5);
fpmul1271(t16, t8, x0);
fpsub1271(t5, x0, x0);
fpmul1271(t15, t8, t5);
fpmul1271(t16, t14, x1);
fpadd1271(x1, t5, x1);
fpmul1271(x0, t17, t5);
fpmul1271(x1, t18, t15);
fpsub1271(t5, t15, t15);
fpmul1271(t17, x1, t5);
fpmul1271(t18, x0, t16);
fpadd1271(t16, t5, t16);
fpmul1271(t13, t9, t13);
fpmul1271(t15, t13, x0);
fpmul1271(t16, t13, x1);
// Clear cofactor
point_extproj_t P;
point_setup(out, P);
cofactor_clearing(P);
eccnorm(P, out);
}