forked from skair39/milagro-crypto-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fp2_arithmetics.c
445 lines (423 loc) · 13.8 KB
/
test_fp2_arithmetics.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
/**
* @file test_fp_arithmetics.c
* @author Alessandro Budroni
* @brief Test for aritmetics with FP
*
* 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 "arch.h"
#include "amcl.h"
#include "utils.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LINE_LEN 10000
#define MAX_STRING 300
void read_BIG(BIG A, char* string)
{
int len;
char support[LINE_LEN];
BIG_zero(A);
len = strlen(string)+1;
amcl_hex2bin(string,support,len);
len = (len-1)/2;;
BIG_fromBytesLen(A,support,len);
BIG_norm(A);
}
void read_FP2(FP2 *fp2, char* stringx)
{
char *stringy;
BIG x,y;
stringy = strchr(stringx,',');
stringy[0] = '\0';
stringy++;
read_BIG(x,stringx);
read_BIG(y,stringy);
FP2_from_BIGs(fp2,x,y);
}
int main(int argc, char** argv)
{
if (argc != 2)
{
printf("usage: ./test_fp2_arithmetics [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;
BIG M;
FP2 FP2aux1, FP2aux2, FP2aux3, FP2aux4;
FP2 FP2_1;
const char* FP2_1line = "FP2_1 = ";
FP2 FP2_2;
const char* FP2_2line = "FP2_2 = ";
FP2 FP2add;
const char* FP2addline = "FP2add = ";
FP2 FP2neg;
const char* FP2negline = "FP2neg = ";
FP2 FP2sub;
const char* FP2subline = "FP2sub = ";
FP2 FP2conj;
const char* FP2conjline = "FP2conj = ";
BIG BIGsc;
const char* BIGscline = "BIGsc = ";
FP2 FP2pmul;
const char* FP2pmulline = "FP2pmul = ";
FP2 FP2imul;
const char* FP2imulline = "FP2imul = ";
FP2 FP2sqr;
const char* FP2sqrline = "FP2sqr = ";
FP2 FP2mul;
const char* FP2mulline = "FP2mul = ";
FP2 FP2inv;
const char* FP2invline = "FP2inv = ";
FP2 FP2div2;
const char* FP2div2line = "FP2div2 = ";
FP2 FP2_mulip;
const char* FP2_mulipline = "FP2_mulip = ";
FP2 FP2_divip;
const char* FP2_divipline = "FP2_divip = ";
FP2 FP2pow;
const char* FP2powline = "FP2pow = ";
BIG_rcopy(M,Modulus);
// Set to zero
FP2_zero(&FP2aux1);
FP2_zero(&FP2aux2);
// Testing equal function and set zero function
if(!FP2_equals(&FP2aux1,&FP2aux2) || !FP2_iszilch(&FP2aux1) || !FP2_iszilch(&FP2aux2))
{
printf("ERROR comparing FP2s or setting FP2 to zero FP\n");
exit(EXIT_FAILURE);
}
// Set to one
FP2_one(&FP2aux1);
FP2_one(&FP2aux2);
// Testing equal function and set one function
if(!FP2_equals(&FP2aux1,&FP2aux2) || !FP2_isunity(&FP2aux1) || !FP2_isunity(&FP2aux2))
{
printf("ERROR comparing FP2s or setting FP2 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 FP2 and perform some tests
if (!strncmp(line,FP2_1line, strlen(FP2_1line)))
{
len = strlen(FP2_1line);
linePtr = line + len;
read_FP2(&FP2_1,linePtr);
FP2_cmove(&FP2aux1,&FP2_1,0);
if(FP2_equals(&FP2aux1,&FP2_1) != 0)
{
printf("ERROR in conditional copy of FP2, line %d\n",i);
exit(EXIT_FAILURE);
}
FP2_cmove(&FP2aux1,&FP2_1,1);
if(FP2_equals(&FP2aux1,&FP2_1) != 1)
{
printf("ERROR in conditional copy of FP2, line %d\n",i);
exit(EXIT_FAILURE);
}
FP2_from_FPs(&FP2aux1,FP2_1.a,FP2_1.b);
if(FP2_equals(&FP2aux1,&FP2_1) != 1)
{
printf("ERROR in generating FP2 from two FPs, line %d\n",i);
exit(EXIT_FAILURE);
}
FP2_from_BIGs(&FP2aux1,FP2_1.a,FP2_1.b);
FP_redc(FP2aux1.a);
FP_redc(FP2aux1.b);
if(FP2_equals(&FP2aux1,&FP2_1) != 1)
{
printf("ERROR in generating FP2 from two BIGs, line %d\n",i);
exit(EXIT_FAILURE);
}
FP2_from_FP(&FP2aux1,FP2_1.a);
FP2_copy(&FP2aux2,&FP2_1);
BIG_zero(FP2aux2.b);
if(FP2_equals(&FP2aux1,&FP2aux2) != 1)
{
printf("ERROR in generating FP2 from one FP, line %d\n",i);
exit(EXIT_FAILURE);
}
FP2_from_BIG(&FP2aux1,FP2_1.a);
FP_redc(FP2aux1.a);
FP2_copy(&FP2aux2,&FP2_1);
BIG_zero(FP2aux2.b);
if(FP2_equals(&FP2aux1,&FP2aux2) != 1)
{
printf("ERROR in generating FP2 from one BIG, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Read second FP2
if (!strncmp(line,FP2_2line, strlen(FP2_2line)))
{
len = strlen(FP2_2line);
linePtr = line + len;
read_FP2(&FP2_2,linePtr);
}
// Addition tests
if (!strncmp(line,FP2addline, strlen(FP2addline)))
{
len = strlen(FP2addline);
linePtr = line + len;
read_FP2(&FP2add,linePtr);
FP2_copy(&FP2aux1,&FP2_1);
FP2_copy(&FP2aux2,&FP2_2);
FP2_add(&FP2aux1,&FP2aux1,&FP2aux2);
// test commutativity P+Q = Q+P
FP2_copy(&FP2aux3,&FP2_1);
FP2_add(&FP2aux2,&FP2aux2,&FP2aux3);
FP2_reduce(&FP2aux1);
FP2_norm(&FP2aux1);
FP2_reduce(&FP2aux2);
FP2_norm(&FP2aux2);
if(!FP2_equals(&FP2aux1,&FP2add) || !FP2_equals(&FP2aux2,&FP2add))
{
printf("ERROR adding two FP2, line %d\n",i);
exit(EXIT_FAILURE);
}
// test associativity (P+Q)+R = P+(Q+R)
FP2_copy(&FP2aux1,&FP2_1);
FP2_copy(&FP2aux3,&FP2_1);
FP2_copy(&FP2aux2,&FP2_2);
FP2_copy(&FP2aux4,&FP2add);
FP2_add(&FP2aux1,&FP2aux1,&FP2aux2);
FP2_add(&FP2aux1,&FP2aux1,&FP2aux4);
FP2_add(&FP2aux2,&FP2aux2,&FP2aux4);
FP2_add(&FP2aux2,&FP2aux2,&FP2aux3);
FP2_reduce(&FP2aux1);
FP2_reduce(&FP2aux2);
FP2_norm(&FP2aux1);
FP2_norm(&FP2aux2);
if(!FP2_equals(&FP2aux1,&FP2aux2))
{
printf("ERROR testing associativity between three FP2s, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Negative an FP2
if (!strncmp(line,FP2negline, strlen(FP2negline)))
{
len = strlen(FP2negline);
linePtr = line + len;
read_FP2(&FP2neg,linePtr);
FP2_copy(&FP2aux1,&FP2_1);
FP2_neg(&FP2aux1,&FP2aux1);
FP2_reduce(&FP2aux1);
FP2_norm(&FP2aux1);
if(!FP2_equals(&FP2aux1,&FP2neg))
{
printf("ERROR in computing negative of FP2, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Subtraction test
if (!strncmp(line,FP2subline, strlen(FP2subline)))
{
len = strlen(FP2subline);
linePtr = line + len;
read_FP2(&FP2sub,linePtr);
FP2_copy(&FP2aux1,&FP2_1);
FP2_copy(&FP2aux2,&FP2_2);
FP2_sub(&FP2aux1,&FP2aux1,&FP2aux2);
FP2_reduce(&FP2aux1);
FP2_norm(&FP2aux1);
if(FP2_equals(&FP2aux1,&FP2sub) == 0)
{
printf("ERROR subtraction between two FP2, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Compute conjugate
if (!strncmp(line,FP2conjline, strlen(FP2conjline)))
{
len = strlen(FP2conjline);
linePtr = line + len;
read_FP2(&FP2conj,linePtr);
FP2_copy(&FP2aux1,&FP2_1);
FP2_conj(&FP2aux1,&FP2aux1);
FP2_reduce(&FP2aux1);
FP2_norm(&FP2aux1);
if(!FP2_equals(&FP2aux1,&FP2conj))
{
printf("ERROR computing conjugate of FP2, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Read multiplicator
if (!strncmp(line,BIGscline, strlen(BIGscline)))
{
len = strlen(BIGscline);
linePtr = line + len;
read_BIG(BIGsc,linePtr);
}
// Multiplication by BIGsc
if (!strncmp(line,FP2pmulline, strlen(FP2pmulline)))
{
len = strlen(FP2pmulline);
linePtr = line + len;
read_FP2(&FP2pmul,linePtr);
FP2_pmul(&FP2aux1,&FP2_1,BIGsc);
FP_nres(FP2aux1.a);
FP_nres(FP2aux1.b);
if(!FP2_equals(&FP2aux1,&FP2pmul))
{
printf("ERROR in multiplication by BIG, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Raise FP2 by power BIGsc
if (!strncmp(line,FP2powline, strlen(FP2powline)))
{
len = strlen(FP2powline);
linePtr = line + len;
read_FP2(&FP2pow,linePtr);
FP2_pow(&FP2aux1,&FP2_1,BIGsc);
FP2_reduce(&FP2aux1);
if(!FP2_equals(&FP2aux1,&FP2pow))
{
printf("ERROR in raising FP2 by power BIG, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Multiplication by j = 1..10
if (!strncmp(line,FP2imulline, strlen(FP2imulline)))
{
len = strlen(FP2imulline);
linePtr = line + len;
read_FP2(&FP2imul,linePtr);
FP2_imul(&FP2aux1,&FP2_1,j);
j++;
FP2_reduce(&FP2aux1);
FP2_norm(&FP2aux1);
if(!FP2_equals(&FP2aux1,&FP2imul))
{
printf("ERROR in multiplication by small integer, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Square and square root
if (!strncmp(line,FP2sqrline, strlen(FP2sqrline)))
{
len = strlen(FP2sqrline);
linePtr = line + len;
read_FP2(&FP2sqr,linePtr);
FP2_copy(&FP2aux1,&FP2_1);
FP2_sqr(&FP2aux1,&FP2aux1);
FP2_reduce(&FP2aux1);
FP2_norm(&FP2aux1);
if(!FP2_equals(&FP2aux1,&FP2sqr))
{
printf("ERROR in squaring FP2, line %d\n",i);
exit(EXIT_FAILURE);
}
FP2_sqrt(&FP2aux1,&FP2aux1);
FP2_neg(&FP2aux2,&FP2aux1);
if(!FP2_equals(&FP2aux1,&FP2_1) && !FP2_equals(&FP2aux2,&FP2_1))
{
printf("ERROR square/square root consistency FP2, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Multiplication between two FP2s
if (!strncmp(line,FP2mulline, strlen(FP2mulline)))
{
len = strlen(FP2mulline);
linePtr = line + len;
read_FP2(&FP2mul,linePtr);
FP2_mul(&FP2aux1,&FP2_1,&FP2_2);
FP2_reduce(&FP2aux1);
FP2_norm(&FP2aux1);
if(!FP2_equals(&FP2aux1,&FP2mul))
{
printf("ERROR in multiplication between two FP2s, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Inverse
if (!strncmp(line,FP2invline, strlen(FP2invline)))
{
len = strlen(FP2invline);
linePtr = line + len;
read_FP2(&FP2inv,linePtr);
FP2_copy(&FP2aux1,&FP2_1);
FP2_inv(&FP2aux1,&FP2aux1);
if(!FP2_equals(&FP2aux1,&FP2inv))
{
printf("ERROR in computing inverse of FP2, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Divide an FP2 by 2
if (!strncmp(line,FP2div2line, strlen(FP2div2line)))
{
len = strlen(FP2div2line);
linePtr = line + len;
read_FP2(&FP2div2,linePtr);
FP2_div2(&FP2aux1,&FP2_1);
if(!FP2_equals(&FP2aux1,&FP2div2))
{
printf("ERROR in computing division FP2 by 2, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Multiply an FP2 by (1+sqrt(-1))
if (!strncmp(line,FP2_mulipline, strlen(FP2_mulipline)))
{
len = strlen(FP2_mulipline);
linePtr = line + len;
read_FP2(&FP2_mulip,linePtr);
FP2_copy(&FP2aux1,&FP2_1);
FP2_mul_ip(&FP2aux1);
if(!FP2_equals(&FP2aux1,&FP2_mulip))
{
printf("ERROR in computing multiplication of FP2 by (1+sqrt(-1)), line %d\n",i);
exit(EXIT_FAILURE);
}
}
// Divide an FP2 by (1+sqrt(-1))
if (!strncmp(line,FP2_divipline, strlen(FP2_divipline)))
{
len = strlen(FP2_divipline);
linePtr = line + len;
read_FP2(&FP2_divip,linePtr);
FP2_copy(&FP2aux1,&FP2_1);
FP2_div_ip(&FP2aux1);
if(!FP2_equals(&FP2aux1,&FP2_divip))
{
printf("ERROR in computing division of FP2 by (1+sqrt(-1)), line %d\n",i);
exit(EXIT_FAILURE);
}
}
}
fclose(fp);
printf("SUCCESS TEST ARITMETIC OF FP PASSED\n");
exit(EXIT_SUCCESS);
}