forked from skair39/milagro-crypto-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ecp_arithmetics_ZZZ.c
443 lines (429 loc) · 14.7 KB
/
test_ecp_arithmetics_ZZZ.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
/**
* @file test_ecp_arithmetics_ZZZ.c
* @author Alessandro Budroni
* @brief Test for aritmetics with ECP_ZZZ
*
* 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 <stdlib.h>
#include <string.h>
#include "arch.h"
#include "amcl.h"
#include "utils.h"
#include "ecp_ZZZ.h"
#define LINE_LEN 1000
#define MAX_STRING 400
#define PIN 1234
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);
}
int read_ECP_ZZZ(ECP_ZZZ *ecp, char* string)
{
BIG_XXX x;
#if CURVETYPE_ZZZ!=MONTGOMERY
BIG_XXX y;
#endif
char *stringy = strchr(string,':');
stringy[0] = '\0';
read_BIG_XXX(x,string);
#if CURVETYPE_ZZZ==MONTGOMERY
return ECP_ZZZ_set(ecp,x);
#else
stringy++;
read_BIG_XXX(y,stringy);
return ECP_ZZZ_set(ecp,x,y);
#endif
}
int main(int argc, char** argv)
{
if (argc != 2)
{
printf("usage: ./test_ecp_arithmetics_ZZZ [path to test vector file]\n");
exit(EXIT_FAILURE);
}
int i=0, len=0;
char line[LINE_LEN];
char * linePtr = NULL;
ECP_ZZZ inf, ECPaux1;
BIG_XXX BIGaux1;
char oct[LINE_LEN];
octet OCTaux = {0,sizeof(oct),oct};
#if CURVETYPE_ZZZ!=MONTGOMERY
BIG_XXX BIGaux2;
FP_YYY FPaux1,FPaux2;
ECP_ZZZ ECPaux2;
#endif
ECP_ZZZ ecp1;
const char* ECP1line = "ECP1 = ";
#if CURVETYPE_ZZZ!=MONTGOMERY
ECP_ZZZ ecp2;
const char* ECP2line = "ECP2 = ";
ECP_ZZZ ecpsum;
const char* ECPsumline = "ECPsum = ";
ECP_ZZZ ecpneg;
const char* ECPnegline = "ECPneg = ";
ECP_ZZZ ecpsub;
const char* ECPsubline = "ECPsub = ";
#endif
ECP_ZZZ ecpdbl;
const char* ECPdblline = "ECPdbl = ";
BIG_XXX BIGscalar1;
const char* BIGscalar1line = "BIGscalar1 = ";
ECP_ZZZ ecpmul;
const char* ECPmulline = "ECPmul = ";
ECP_ZZZ ecpwrong;
const char* ECPwrongline = "ECPwrong = ";
ECP_ZZZ ecpinf;
const char* ECPinfline = "ECPinf = ";
#if CURVETYPE_ZZZ!=MONTGOMERY
ECP_ZZZ ecppinmul;
const char* ECPpinmulline = "ECPpinmul = ";
BIG_XXX BIGscalar2;
const char* BIGscalar2line = "BIGscalar2 = ";
ECP_ZZZ ecpmul2;
const char* ECPmul2line = "ECPmul2 = ";
ECP_ZZZ ecpeven;
const char* ECPevenline = "ECPeven = ";
ECP_ZZZ ecpodd;
const char* ECPoddline = "ECPodd = ";
#endif
#if CURVETYPE_ZZZ==MONTGOMERY
ECP_ZZZ ecpmul3;
const char* ECPmul3line = "ECPmul3 = ";
#endif
ECP_ZZZ_inf(&inf);
if(!ECP_ZZZ_isinf(&inf))
{
printf("ERROR setting ECP_ZZZ to infinity\n");
exit(EXIT_FAILURE);
}
FILE *fp;
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++;
if (!strncmp(line, ECP1line, strlen(ECP1line))) // get first test vector
{
len = strlen(ECP1line);
linePtr = line + len;
if(!read_ECP_ZZZ(&ecp1,linePtr) || ECP_ZZZ_isinf(&ecp1))
{
printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
#if CURVETYPE_ZZZ!=MONTGOMERY
ECP_ZZZ_get(BIGaux1,BIGaux2,&ecp1);
FP_YYY_nres(&FPaux1,BIGaux1);
FP_YYY_nres(&FPaux2,BIGaux2);
FP_YYY_sqr(&FPaux2,&FPaux2);
ECP_ZZZ_rhs(&FPaux1,&FPaux1);
FP_YYY_reduce(&FPaux1); // in case of lazy reduction
FP_YYY_reduce(&FPaux2); // in case of lazy reduction
if (!FP_YYY_equals(&FPaux1,&FPaux2)) // test if y^2=f(x)
{
printf("ERROR computing right hand side of equation ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
#endif
ECP_ZZZ_toOctet(&OCTaux,&ecp1);
ECP_ZZZ_fromOctet(&ECPaux1,&OCTaux);
if(!ECP_ZZZ_equals(&ECPaux1,&ecp1)) // test octet conversion
{
printf("ERROR converting ECP_ZZZ to/from OCTET, line %d\n",i);
exit(EXIT_FAILURE);
}
}
#if CURVETYPE_ZZZ!=MONTGOMERY
if (!strncmp(line, ECP2line, strlen(ECP2line))) // get second test vector
{
len = strlen(ECP2line);
linePtr = line + len;
if(!read_ECP_ZZZ(&ecp2,linePtr) || ECP_ZZZ_isinf(&ecp2))
{
printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
}
if (!strncmp(line, ECPsumline, strlen(ECPsumline)))
{
len = strlen(ECPsumline);
linePtr = line + len;
if(!read_ECP_ZZZ(&ecpsum,linePtr) || ECP_ZZZ_isinf(&ecpsum))
{
printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
ECP_ZZZ_copy(&ECPaux1,&ecp1);
ECP_ZZZ_add(&ECPaux1,&ecp2);
ECP_ZZZ_affine(&ECPaux1);
ECP_ZZZ_copy(&ECPaux2,&ecp2);
ECP_ZZZ_add(&ECPaux2,&ecp1);
ECP_ZZZ_affine(&ECPaux2);
if(!ECP_ZZZ_equals(&ECPaux1,&ecpsum) || !ECP_ZZZ_equals(&ECPaux2,&ecpsum)) // test addition P+Q and Q+P (commutativity)
{
printf("ERROR adding two ECPs, line %d\n",i);
exit(EXIT_FAILURE);
}
ECP_ZZZ_copy(&ECPaux1,&ecp1); // test associativity
ECP_ZZZ_add(&ECPaux1,&ecp2);
ECP_ZZZ_add(&ECPaux1,&ecpsum);
ECP_ZZZ_copy(&ECPaux2,&ecpsum);
ECP_ZZZ_add(&ECPaux2,&ecp2);
ECP_ZZZ_add(&ECPaux2,&ecp1);
if(!ECP_ZZZ_equals(&ECPaux1,&ECPaux2)) // test associativity (P+Q)+R = P+(Q+R)
{
printf("ERROR testing associativity between three ECPs, line %d\n",i);
exit(EXIT_FAILURE);
}
}
if (!strncmp(line, ECPsubline, strlen(ECPsubline)))
{
len = strlen(ECPsubline);
linePtr = line + len;
if(!read_ECP_ZZZ(&ecpsub,linePtr) || ECP_ZZZ_isinf(&ecpsub))
{
printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
ECP_ZZZ_copy(&ECPaux1,&ecp1);
ECP_ZZZ_sub(&ECPaux1,&ecp2);
ECP_ZZZ_affine(&ECPaux1);
if(!ECP_ZZZ_equals(&ECPaux1,&ecpsub)) // test subtraction P-Q
{
printf("ERROR computing subtraction of two ECPs, line %d\n",i);
exit(EXIT_FAILURE);
}
}
if (!strncmp(line, ECPnegline, strlen(ECPnegline)))
{
len = strlen(ECPnegline);
linePtr = line + len;
if(!read_ECP_ZZZ(&ecpneg,linePtr) || ECP_ZZZ_isinf(&ecpneg))
{
printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
ECP_ZZZ_copy(&ECPaux1,&ecp1);
ECP_ZZZ_neg(&ECPaux1);
ECP_ZZZ_affine(&ECPaux1);
if(!ECP_ZZZ_equals(&ECPaux1,&ecpneg))
{
printf("ERROR computing negative of ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
}
#endif
if (!strncmp(line, ECPdblline, strlen(ECPdblline)))
{
len = strlen(ECPdblline);
linePtr = line + len;
if(!read_ECP_ZZZ(&ecpdbl,linePtr) || ECP_ZZZ_isinf(&ecpdbl))
{
printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
ECP_ZZZ_copy(&ECPaux1,&ecp1);
ECP_ZZZ_dbl(&ECPaux1);
ECP_ZZZ_affine(&ECPaux1);
if(!ECP_ZZZ_equals(&ECPaux1,&ecpdbl))
{
ECP_ZZZ_outputxyz(&ECPaux1);
ECP_ZZZ_outputxyz(&ecpdbl);
printf("ERROR computing double of ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
}
#if CURVETYPE_ZZZ==MONTGOMERY
if (!strncmp(line, ECPmul3line, strlen(ECPmul3line)))
{
len = strlen(ECPmul3line);
linePtr = line + len;
if(!read_ECP_ZZZ(&ecpmul3,linePtr) || ECP_ZZZ_isinf(&ecpmul3))
{
printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
BIG_XXX_one(BIGaux1);
BIG_XXX_inc(BIGaux1,2);
BIG_XXX_norm(BIGaux1);
ECP_ZZZ_copy(&ECPaux1,&ecp1);
ECP_ZZZ_mul(&ECPaux1,BIGaux1);
ECP_ZZZ_affine(&ECPaux1);
if(!ECP_ZZZ_equals(&ECPaux1,&ecpmul3))
{
printf("ERROR computing multiplication of ECP_ZZZ by 3, line %d\n",i);
exit(EXIT_FAILURE);
}
ECP_ZZZ_copy(&ECPaux1,&ecpdbl);
ECP_ZZZ_add(&ECPaux1,&ecp1,&ecp1);
if(!ECP_ZZZ_equals(&ECPaux1,&ecpmul3))
{
printf("ERROR computing multiplication of ECP_ZZZ by 3, line %d\n",i);
exit(EXIT_FAILURE);
}
}
#endif
if (!strncmp(line, BIGscalar1line, strlen(BIGscalar1line)))
{
len = strlen(BIGscalar1line);
linePtr = line + len;
read_BIG_XXX(BIGscalar1,linePtr);
}
if (!strncmp(line, ECPmulline, strlen(ECPmulline)))
{
len = strlen(ECPmulline);
linePtr = line + len;
if(!read_ECP_ZZZ(&ecpmul,linePtr))
{
printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
ECP_ZZZ_copy(&ECPaux1,&ecp1);
ECP_ZZZ_mul(&ECPaux1,BIGscalar1);
ECP_ZZZ_affine(&ECPaux1);
if(!ECP_ZZZ_equals(&ECPaux1,&ecpmul))
{
ECP_ZZZ_outputxyz(&ecp1);
ECP_ZZZ_outputxyz(&ECPaux1);
ECP_ZZZ_outputxyz(&ecpmul);
printf("ERROR computing multiplication of ECP_ZZZ by a scalar, line %d\n",i);
exit(EXIT_FAILURE);
}
}
#if CURVETYPE_ZZZ!=MONTGOMERY
if (!strncmp(line, ECPpinmulline, strlen(ECPpinmulline)))
{
len = strlen(ECPpinmulline);
linePtr = line + len;
if(!read_ECP_ZZZ(&ecppinmul,linePtr))
{
printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
ECP_ZZZ_copy(&ECPaux1,&ecp1);
ECP_ZZZ_pinmul(&ECPaux1,PIN,14);
ECP_ZZZ_affine(&ECPaux1);
if(!ECP_ZZZ_equals(&ECPaux1,&ecppinmul))
{
printf("ERROR computing multiplication of ECP_ZZZ by small integer, line %d\n",i);
exit(EXIT_FAILURE);
}
}
if (!strncmp(line, BIGscalar2line, strlen(BIGscalar2line)))
{
len = strlen(BIGscalar2line);
linePtr = line + len;
read_BIG_XXX(BIGscalar2,linePtr);
}
if (!strncmp(line, ECPmul2line, strlen(ECPmul2line)))
{
len = strlen(ECPmul2line);
linePtr = line + len;
if(!read_ECP_ZZZ(&ecpmul2,linePtr))
{
printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
ECP_ZZZ_copy(&ECPaux1,&ecp1);
ECP_ZZZ_copy(&ECPaux2,&ecp2);
ECP_ZZZ_mul2(&ECPaux1,&ECPaux2,BIGscalar1,BIGscalar2);
ECP_ZZZ_affine(&ECPaux1);
if(!ECP_ZZZ_equals(&ECPaux1,&ecpmul2))
{
printf("ERROR computing linear combination of 2 ECPs, line %d\n",i);
exit(EXIT_FAILURE);
}
}
#endif
if (!strncmp(line, ECPwrongline, strlen(ECPwrongline)))
{
len = strlen(ECPwrongline);
linePtr = line + len;
if(read_ECP_ZZZ(&ecpwrong,linePtr) || !ECP_ZZZ_isinf(&ecpwrong) || !ECP_ZZZ_equals(&ecpwrong,&inf))
{
printf("ERROR identifying wrong ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
}
if (!strncmp(line, ECPinfline, strlen(ECPinfline)))
{
len = strlen(ECPinfline);
linePtr = line + len;
if(read_ECP_ZZZ(&ecpinf,linePtr) || !ECP_ZZZ_isinf(&ecpinf) || !ECP_ZZZ_equals(&ecpinf,&inf))
{
printf("ERROR identifying infinite point ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
}
#if CURVETYPE_ZZZ!=MONTGOMERY
if (!strncmp(line, ECPevenline, strlen(ECPevenline)))
{
len = strlen(ECPevenline);
linePtr = line + len;
if(!read_ECP_ZZZ(&ecpeven,linePtr))
{
printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
ECP_ZZZ_get(BIGaux1,BIGaux2,&ecp1);
BIG_XXX_norm(BIGaux1);
ECP_ZZZ_setx(&ECPaux1,BIGaux1,0);
if(!ECP_ZZZ_equals(&ECPaux1,&ecpeven))
{
printf("ERROR computing ECP_ZZZ from coordinate x and with y even, line %d\n",i);
exit(EXIT_FAILURE);
}
}
if (!strncmp(line, ECPoddline, strlen(ECPoddline)))
{
len = strlen(ECPoddline);
linePtr = line + len;
if(!read_ECP_ZZZ(&ecpodd,linePtr))
{
printf("ERROR getting test vector input ECP_ZZZ, line %d\n",i);
exit(EXIT_FAILURE);
}
ECP_ZZZ_setx(&ECPaux1,BIGaux1,1);
if(!ECP_ZZZ_equals(&ECPaux1,&ecpodd))
{
printf("ERROR computing ECP_ZZZ from coordinate x and with y odd, line %d\n",i);
exit(EXIT_FAILURE);
}
}
#endif
}
fclose(fp);
printf("SUCCESS TEST ARITMETIC OF ECP_ZZZ PASSED\n");
exit(EXIT_SUCCESS);
}