forked from skair39/milagro-crypto-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fp4_arithmetics_YYY.c
503 lines (478 loc) · 15.7 KB
/
test_fp4_arithmetics_YYY.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/**
* @file test_fp4_arithmetics_YYY.c
* @author Alessandro Budroni
* @brief Test for aritmetics with FP4_YYY
*
* LICENSE
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "arch.h"
#include "amcl.h"
#include "utils.h"
#include "fp4_YYY.h"
#define LINE_LEN 10000
#define MAX_STRING 300
void read_BIG_XXX(BIG_XXX A, char* string)
{
int len;
char support[LINE_LEN];
BIG_XXX_zero(A);
len = strlen(string)+1;
amcl_hex2bin(string,support,len);
len = (len-1)/2;
BIG_XXX_fromBytesLen(A,support,len);
BIG_XXX_norm(A);
}
void read_FP2_YYY(FP2_YYY *fp2, char* stringx)
{
char *stringy, *end;
BIG_XXX x,y;
stringx++;
stringy = strchr(stringx,',');
if (stringy == NULL)
{
printf("ERROR unexpected test vector\n");
exit(EXIT_FAILURE);
}
stringy[0] = '\0';
stringy++;
end = strchr(stringy,']');
if (end == NULL)
{
printf("ERROR unexpected test vector\n");
exit(EXIT_FAILURE);
}
end[0] = '\0';
read_BIG_XXX(x,stringx);
read_BIG_XXX(y,stringy);
FP2_YYY_from_BIGs(fp2,x,y);
}
void read_FP4_YYY(FP4_YYY *fp4, char* stringx1)
{
char *stringx2, *stringy1, *stringy2, *end;
BIG_XXX x1,x2,y1,y2;
FP2_YYY x,y;
stringx1 += 2;
stringx2 = strchr(stringx1,',');
if (stringx2 == NULL)
{
printf("ERROR unexpected test vector\n");
exit(EXIT_FAILURE);
}
stringx2[0] = '\0';
stringx2 ++;
stringy1 = strchr(stringx2,']');
if (stringy1 == NULL)
{
printf("ERROR unexpected test vector\n");
exit(EXIT_FAILURE);
}
stringy1[0] = '\0';
stringy1 += 3;
stringy2 = strchr(stringy1,',');
if (stringy2 == NULL)
{
printf("ERROR unexpected test vector\n");
exit(EXIT_FAILURE);
}
stringy2[0] = '\0';
stringy2++;
end = strchr(stringy2,']');
if (end == NULL)
{
printf("ERROR unexpected test vector\n");
exit(EXIT_FAILURE);
}
end[0] = '\0';
read_BIG_XXX(x1,stringx1);
read_BIG_XXX(x2,stringx2);
read_BIG_XXX(y1,stringy1);
read_BIG_XXX(y2,stringy2);
FP2_YYY_from_BIGs(&x,x1,x2);
FP2_YYY_from_BIGs(&y,y1,y2);
FP4_YYY_from_FP2s(fp4,&x,&y);
}
int main(int argc, char** argv)
{
if (argc != 2)
{
printf("usage: ./test_fp4_arithmetics_YYY [path to test vector file]\n");
exit(EXIT_FAILURE);
}
int i = 0, len = 0, j = 0;
FILE *fp;
char line[LINE_LEN];
char * linePtr = NULL;
FP4_YYY FP4aux1, FP4aux2, FP4aux3, FP4aux4;
FP4_YYY FP4_1;
const char* FP4_1line = "FP4_1 = ";
FP4_YYY FP4_2;
const char* FP4_2line = "FP4_2 = ";
FP4_YYY FP4add;
const char* FP4addline = "FP4add = ";
FP4_YYY FP4neg;
const char* FP4negline = "FP4neg = ";
FP4_YYY FP4sub;
const char* FP4subline = "FP4sub = ";
FP4_YYY FP4conj;
const char* FP4conjline = "FP4conj = ";
FP4_YYY FP4nconj;
const char* FP4nconjline = "FP4nconj = ";
FP2_YYY FP2sc;
const char* FP2scline = "FP2sc = ";
FP4_YYY FP4pmul;
const char* FP4pmulline = "FP4pmul = ";
FP4_YYY FP4imul;
const char* FP4imulline = "FP4imul = ";
FP4_YYY FP4sqr;
const char* FP4sqrline = "FP4sqr = ";
FP4_YYY FP4mul;
const char* FP4mulline = "FP4mul = ";
FP4_YYY FP4inv;
const char* FP4invline = "FP4inv = ";
FP4_YYY FP4mulj;
const char* FP4muljline = "FP4mulj = ";
BIG_XXX BIGsc;
const char* BIGscline = "BIGsc = ";
FP4_YYY FP4pow;
const char* FP4powline = "FP4pow = ";
FP4_YYY FP4_xtrA;
const char* FP4_xtrAline = "FP4_xtrA = ";
FP4_YYY FP4_xtrD;
const char* FP4_xtrDline = "FP4_xtrD = ";
// Set to zero
FP4_YYY_zero(&FP4aux1);
FP4_YYY_zero(&FP4aux2);
// Testing equal function and set zero function
if(!FP4_YYY_equals(&FP4aux1,&FP4aux2) || !FP4_YYY_iszilch(&FP4aux1) || !FP4_YYY_iszilch(&FP4aux2) || !FP4_YYY_isreal(&FP4aux1))
{
printf("ERROR comparing FP4s or setting FP4 to zero FP\n");
exit(EXIT_FAILURE);
}
// Set to one
FP4_YYY_one(&FP4aux1);
FP4_YYY_one(&FP4aux2);
// Testing equal function and set one function
if(!FP4_YYY_equals(&FP4aux1,&FP4aux2) || !FP4_YYY_isunity(&FP4aux1) || !FP4_YYY_isunity(&FP4aux2) || !FP4_YYY_isreal(&FP4aux1) || FP4_YYY_iszilch(&FP4aux1))
{
printf("ERROR comparing FP4s or setting FP4 to unity FP\n");
exit(EXIT_FAILURE);
}
fp = fopen(argv[1], "r");
if (fp == NULL)
{
printf("ERROR opening test vector file\n");
exit(EXIT_FAILURE);
}
while (fgets(line, LINE_LEN, fp) != NULL)
{
i++;
// Read first FP4 and perform some tests
if (!strncmp(line,FP4_1line, strlen(FP4_1line)))
{
len = strlen(FP4_1line);
linePtr = line + len;
read_FP4_YYY(&FP4_1,linePtr);
// test FP4_from_FP2s
FP4_YYY_from_FP2s(&FP4aux1,&FP4_1.a,&FP4_1.b);
if(!FP4_YYY_equals(&FP4aux1,&FP4_1))
{
printf("ERROR in generating FP4 from two FP2s, line %d\n",i);
exit(EXIT_FAILURE);
}
// test FP4_from_FP2 and FP4_isreal
FP4_YYY_from_FP2(&FP4aux1,&FP4_1.a);
FP4_YYY_copy(&FP4aux2,&FP4_1);
FP2_YYY_zero(&FP4aux2.b);
if(!FP4_YYY_equals(&FP4aux1,&FP4aux2) || !FP4_YYY_isreal(&FP4aux1))
{
printf("ERROR in generating FP4 from one FP2, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Read second FP4
if (!strncmp(line,FP4_2line, strlen(FP4_2line)))
{
len = strlen(FP4_2line);
linePtr = line + len;
read_FP4_YYY(&FP4_2,linePtr);
}
// Addition test
if (!strncmp(line,FP4addline, strlen(FP4addline)))
{
len = strlen(FP4addline);
linePtr = line + len;
read_FP4_YYY(&FP4add,linePtr);
FP4_YYY_copy(&FP4aux1,&FP4_1);
FP4_YYY_copy(&FP4aux2,&FP4_2);
FP4_YYY_add(&FP4aux1,&FP4aux1,&FP4aux2);
// test commutativity P+Q = Q+P
FP4_YYY_copy(&FP4aux3,&FP4_1);
FP4_YYY_add(&FP4aux2,&FP4aux2,&FP4aux3);
FP4_YYY_reduce(&FP4aux1);
FP4_YYY_norm(&FP4aux1);
FP4_YYY_reduce(&FP4aux2);
FP4_YYY_norm(&FP4aux2);
if(!FP4_YYY_equals(&FP4aux1,&FP4add) || !FP4_YYY_equals(&FP4aux2,&FP4add))
{
printf("ERROR adding two FP4, line %d\n",i);
exit(EXIT_FAILURE);
}
// test associativity (P+Q)+R = P+(Q+R)
FP4_YYY_copy(&FP4aux1,&FP4_1);
FP4_YYY_copy(&FP4aux3,&FP4_1);
FP4_YYY_copy(&FP4aux2,&FP4_2);
FP4_YYY_copy(&FP4aux4,&FP4add);
FP4_YYY_add(&FP4aux1,&FP4aux1,&FP4aux2);
FP4_YYY_add(&FP4aux1,&FP4aux1,&FP4aux4);
FP4_YYY_add(&FP4aux2,&FP4aux2,&FP4aux4);
FP4_YYY_add(&FP4aux2,&FP4aux2,&FP4aux3);
FP4_YYY_reduce(&FP4aux1);
FP4_YYY_reduce(&FP4aux2);
FP4_YYY_norm(&FP4aux1);
FP4_YYY_norm(&FP4aux2);
if(!FP4_YYY_equals(&FP4aux1,&FP4aux2))
{
printf("ERROR testing associativity between three FP4s, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Test negative of an FP4
if (!strncmp(line,FP4negline, strlen(FP4negline)))
{
len = strlen(FP4negline);
linePtr = line + len;
read_FP4_YYY(&FP4neg,linePtr);
FP4_YYY_copy(&FP4aux1,&FP4_1);
FP4_YYY_neg(&FP4aux1,&FP4aux1);
FP4_YYY_reduce(&FP4aux1);
FP4_YYY_norm(&FP4aux1);
if(!FP4_YYY_equals(&FP4aux1,&FP4neg))
{
printf("ERROR in computing negative of FP4, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Subtraction test
if (!strncmp(line,FP4subline, strlen(FP4subline)))
{
len = strlen(FP4subline);
linePtr = line + len;
read_FP4_YYY(&FP4sub,linePtr);
FP4_YYY_copy(&FP4aux1,&FP4_1);
FP4_YYY_copy(&FP4aux2,&FP4_2);
FP4_YYY_sub(&FP4aux1,&FP4aux1,&FP4aux2);
FP4_YYY_reduce(&FP4aux1);
FP4_YYY_norm(&FP4aux1);
if(FP4_YYY_equals(&FP4aux1,&FP4sub) == 0)
{
printf("ERROR subtraction between two FP4, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Test conjugate
if (!strncmp(line,FP4conjline, strlen(FP4conjline)))
{
len = strlen(FP4conjline);
linePtr = line + len;
read_FP4_YYY(&FP4conj,linePtr);
FP4_YYY_copy(&FP4aux1,&FP4_1);
FP4_YYY_conj(&FP4aux1,&FP4aux1);
FP4_YYY_reduce(&FP4aux1);
FP4_YYY_norm(&FP4aux1);
if(!FP4_YYY_equals(&FP4aux1,&FP4conj))
{
printf("ERROR computing conjugate of FP4, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Test negative conjugate
if (!strncmp(line,FP4nconjline, strlen(FP4nconjline)))
{
len = strlen(FP4nconjline);
linePtr = line + len;
read_FP4_YYY(&FP4nconj,linePtr);
FP4_YYY_copy(&FP4aux1,&FP4_1);
FP4_YYY_nconj(&FP4aux1,&FP4aux1);
FP4_YYY_reduce(&FP4aux1);
FP4_YYY_norm(&FP4aux1);
if(!FP4_YYY_equals(&FP4aux1,&FP4nconj))
{
printf("ERROR computing negative conjugate of FP4, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Read multiplicator
if (!strncmp(line,FP2scline, strlen(FP2scline)))
{
len = strlen(FP2scline);
linePtr = line + len;
read_FP2_YYY(&FP2sc,linePtr);
}
// Multiplication by FP2
if (!strncmp(line,FP4pmulline, strlen(FP4pmulline)))
{
len = strlen(FP4pmulline);
linePtr = line + len;
read_FP4_YYY(&FP4pmul,linePtr);
FP4_YYY_pmul(&FP4aux1,&FP4_1,&FP2sc);
FP4_YYY_reduce(&FP4aux1);
FP4_YYY_norm(&FP4aux1);
if(!FP4_YYY_equals(&FP4aux1,&FP4pmul))
{
printf("ERROR in multiplication by FP2, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Multiplication by j = 0..10
if (!strncmp(line,FP4imulline, strlen(FP4imulline)))
{
len = strlen(FP4imulline);
linePtr = line + len;
read_FP4_YYY(&FP4imul,linePtr);
FP4_YYY_imul(&FP4aux1,&FP4_1,j);
j++;
FP4_YYY_reduce(&FP4aux1);
FP4_YYY_norm(&FP4aux1);
if(!FP4_YYY_equals(&FP4aux1,&FP4imul))
{
printf("ERROR in multiplication by small integer, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Square test
if (!strncmp(line,FP4sqrline, strlen(FP4sqrline)))
{
len = strlen(FP4sqrline);
linePtr = line + len;
read_FP4_YYY(&FP4sqr,linePtr);
FP4_YYY_copy(&FP4aux1,&FP4_1);
FP4_YYY_sqr(&FP4aux1,&FP4aux1);
FP4_YYY_reduce(&FP4aux1);
FP4_YYY_norm(&FP4aux1);
if(!FP4_YYY_equals(&FP4aux1,&FP4sqr))
{
printf("ERROR in squaring FP4, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Multiplication between two FP4s
if (!strncmp(line,FP4mulline, strlen(FP4mulline)))
{
len = strlen(FP4mulline);
linePtr = line + len;
read_FP4_YYY(&FP4mul,linePtr);
FP4_YYY_mul(&FP4aux1,&FP4_1,&FP4_2);
FP4_YYY_reduce(&FP4aux1);
FP4_YYY_norm(&FP4aux1);
if(!FP4_YYY_equals(&FP4aux1,&FP4mul))
{
printf("ERROR in multiplication between two FP4s, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Inverse
if (!strncmp(line,FP4invline, strlen(FP4invline)))
{
len = strlen(FP4invline);
linePtr = line + len;
read_FP4_YYY(&FP4inv,linePtr);
FP4_YYY_copy(&FP4aux1,&FP4_1);
FP4_YYY_inv(&FP4aux1,&FP4aux1);
FP4_YYY_reduce(&FP4aux1);
FP4_YYY_norm(&FP4aux1);
if(!FP4_YYY_equals(&FP4aux1,&FP4inv))
{
printf("ERROR in computing inverse of FP4, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Test multiplication of an FP4 instance by sqrt(1+sqrt(-1))
if (!strncmp(line,FP4muljline, strlen(FP4muljline)))
{
len = strlen(FP4muljline);
linePtr = line + len;
read_FP4_YYY(&FP4mulj,linePtr);
FP4_YYY_copy(&FP4aux1,&FP4_1);
FP4_YYY_times_i(&FP4aux1);
FP4_YYY_reduce(&FP4aux1);
FP4_YYY_norm(&FP4aux1);
if(!FP4_YYY_equals(&FP4aux1,&FP4mulj))
{
printf("ERROR in multiplication of an FP4 instance by sqrt(1+sqrt(-1)), line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Read exponent
if (!strncmp(line,BIGscline, strlen(BIGscline)))
{
len = strlen(BIGscline);
linePtr = line + len;
read_BIG_XXX(BIGsc,linePtr);
}
// Raise FP4 by BIG power
if (!strncmp(line,FP4powline, strlen(FP4powline)))
{
len = strlen(FP4powline);
linePtr = line + len;
read_FP4_YYY(&FP4pow,linePtr);
FP4_YYY_pow(&FP4aux1,&FP4_1,BIGsc);
FP4_YYY_reduce(&FP4aux1);
if(!FP4_YYY_equals(&FP4aux1,&FP4pow))
{
printf("ERROR in raising FP4 by BIG power, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Test the XTR addition function r=w*x-conj(x)*y+z
if (!strncmp(line,FP4_xtrAline, strlen(FP4_xtrAline)))
{
len = strlen(FP4_xtrAline);
linePtr = line + len;
read_FP4_YYY(&FP4_xtrA,linePtr);
FP4_YYY_xtr_A(&FP4aux1,&FP4_1,&FP4_2,&FP4add,&FP4sub);
FP4_YYY_reduce(&FP4aux1);
if(!FP4_YYY_equals(&FP4aux1,&FP4_xtrA))
{
printf("ERROR in testing the XTR addition function r=w*x-conj(x)*y+z, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Test the XTR doubling function r=x^2-2*conj(x)
if (!strncmp(line,FP4_xtrDline, strlen(FP4_xtrDline)))
{
len = strlen(FP4_xtrDline);
linePtr = line + len;
read_FP4_YYY(&FP4_xtrD,linePtr);
FP4_YYY_xtr_D(&FP4aux1,&FP4_1);
FP4_YYY_reduce(&FP4aux1);
if(!FP4_YYY_equals(&FP4aux1,&FP4_xtrD))
{
printf("ERROR in testing the XTR doubling function r=x^2-2*conj(x), line %d\n",i);
exit(EXIT_FAILURE);
}
}
}
fclose(fp);
printf("SUCCESS TEST ARITMETIC OF FP PASSED\n");
exit(EXIT_SUCCESS);
}