forked from skair39/milagro-crypto-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_big_arithmetics_XXX.c
360 lines (347 loc) · 11 KB
/
test_big_arithmetics_XXX.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
/**
* @file test_big_consistency.c
* @author Alessandro Budroni
* @brief Test for aritmetics with BIG_XXX
*
* 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 "big_XXX.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_DBIG_XXX(DBIG_XXX A, char* string)
{
int len;
char support[LINE_LEN];
BIG_XXX_dzero(A);
len = strlen(string)+1;
amcl_hex2bin(string,support,len);
len = (len-1)/2;
BIG_XXX_dfromBytesLen(A,support,len);
BIG_XXX_dnorm(A);
}
int main(int argc, char** argv)
{
if (argc != 2)
{
printf("usage: ./test_BIG_XXX_arithmetics [path to test vector file]\n");
exit(EXIT_FAILURE);
}
int i=0, len=0, bitlen=0;
FILE *fp;
char line[LINE_LEN];
char * linePtr = NULL;
BIG_XXX supp,mod,div;
DBIG_XXX dsupp;
BIG_XXX BIG1;
const char* BIG1line = "BIG1 = ";
BIG_XXX BIG2;
const char* BIG2line = "BIG2 = ";
BIG_XXX BIGsum;
const char* BIGsumline = "BIGsum = ";
BIG_XXX BIGsub;
const char* BIGsubline = "BIGsub = ";
BIG_XXX BIG1mod2;
const char* BIG1mod2line = "BIG1mod2 = ";
BIG_XXX BIG2mod1;
const char* BIG2mod1line = "BIG2mod1 = ";
DBIG_XXX BIGmul;
const char* BIGmulline = "BIGmul = ";
DBIG_XXX BIG1sqr;
const char* BIG1sqrline = "BIG1sqr = ";
DBIG_XXX BIG2sqr;
const char* BIG2sqrline = "BIG2sqr = ";
BIG_XXX BIG1sqrmod2;
const char* BIG1sqrmod2line = "BIG1sqrmod2 = ";
BIG_XXX BIG1modneg2;
const char* BIG1modneg2line = "BIG1modneg2 = ";
int nbitBIG = 0;
const char* nbitBIGline = "nbitBIG = ";
int nbitDBIG = 0;
const char* nbitDBIGline = "nbitDBIG = ";
BIG_XXX BIGdiv;
const char* BIGdivline = "BIGdiv = ";
BIG_XXX BIGdivmod;
const char* BIGdivmodline = "BIGdivmod = ";
DBIG_XXX BIGpxmul;
const char* BIGpxmulline = "BIGpxmul = ";
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, BIG1line, strlen(BIG1line)))
{
len = strlen(BIG1line);
linePtr = line + len;
read_BIG_XXX(BIG1,linePtr);
}
// test comparison
if (!strncmp(line,BIG2line, strlen(BIG2line)))
{
len = strlen(BIG2line);
linePtr = line + len;
read_BIG_XXX(BIG2,linePtr);
if (BIG_XXX_comp(BIG1,BIG2) < 0)
{
printf("ERROR comparing two BIGs, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// test addition
if (!strncmp(line,BIGsumline, strlen(BIGsumline)))
{
BIG_XXX_zero(supp);
BIG_XXX_add(supp,BIG1,BIG2);
len = strlen(BIGsumline);
linePtr = line + len;
read_BIG_XXX(BIGsum,linePtr);
BIG_XXX_norm(supp);
if (BIG_XXX_comp(BIGsum,supp) != 0)
{
printf("ERROR adding two BIGs, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// test subtraction
if (!strncmp(line,BIGsubline, strlen(BIGsubline)))
{
BIG_XXX_zero(supp);
BIG_XXX_sub(supp,BIG1,BIG2);
len = strlen(BIGsubline);
linePtr = line + len;
read_BIG_XXX(BIGsub,linePtr);
BIG_XXX_norm(supp);
if (BIG_XXX_comp(BIGsub,supp) != 0)
{
printf("ERROR subtracting two BIGs, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// test modulo 1
if (!strncmp(line,BIG1mod2line, strlen(BIG1mod2line)))
{
BIG_XXX_zero(supp);
BIG_XXX_copy(supp,BIG1);
BIG_XXX_mod(supp,BIG2);
len = strlen(BIG1mod2line);
linePtr = line + len;
read_BIG_XXX(BIG1mod2,linePtr);
BIG_XXX_norm(supp);
if (BIG_XXX_comp(BIG1mod2,supp) != 0)
{
printf("ERROR reducing modulo BIG, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// test modulo 2
if (!strncmp(line,BIG2mod1line, strlen(BIG2mod1line)))
{
BIG_XXX_zero(supp);
BIG_XXX_copy(supp,BIG2);
BIG_XXX_mod(supp,BIG1);
len = strlen(BIG2mod1line);
linePtr = line + len;
read_BIG_XXX(BIG2mod1,linePtr);
BIG_XXX_norm(supp);
if (BIG_XXX_comp(BIG2mod1,supp) != 0)
{
printf("ERROR reducing modulo BIG, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// test multiplication
if (!strncmp(line,BIGmulline, strlen(BIGmulline)))
{
BIG_XXX_dzero(dsupp);
BIG_XXX_mul(dsupp,BIG1,BIG2);
len = strlen(BIGmulline);
linePtr = line + len;
read_DBIG_XXX(BIGmul,linePtr);
BIG_XXX_dnorm(dsupp);
if (BIG_XXX_dcomp(BIGmul,dsupp) != 0)
{
printf("ERROR multiplication BIG, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// test square 1
if (!strncmp(line,BIG1sqrline, strlen(BIG1sqrline)))
{
BIG_XXX_dzero(dsupp);
BIG_XXX_sqr(dsupp,BIG1);
len = strlen(BIG1sqrline);
linePtr = line + len;
read_DBIG_XXX(BIG1sqr,linePtr);
BIG_XXX_dnorm(dsupp);
if (BIG_XXX_dcomp(BIG1sqr,dsupp) != 0)
{
printf("ERROR squaring BIG 1, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// test square 2
if (!strncmp(line,BIG2sqrline, strlen(BIG2sqrline)))
{
BIG_XXX_dzero(dsupp);
BIG_XXX_sqr(dsupp,BIG2);
len = strlen(BIG2sqrline);
linePtr = line + len;
read_DBIG_XXX(BIG2sqr,linePtr);
BIG_XXX_dnorm(dsupp);
if (BIG_XXX_dcomp(BIG2sqr,dsupp) != 0)
{
printf("ERROR squaring BIG 2, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// test square mod
if (!strncmp(line,BIG1sqrmod2line, strlen(BIG1sqrmod2line)))
{
BIG_XXX_zero(supp);
BIG_XXX_copy(supp,BIG1);
BIG_XXX_modsqr(supp,supp,BIG2);
len = strlen(BIG1sqrmod2line);
linePtr = line + len;
read_BIG_XXX(BIG1sqrmod2,linePtr);
BIG_XXX_norm(supp);
if (BIG_XXX_comp(BIG1sqrmod2,supp) != 0)
{
printf("ERROR reducing squaring modulo BIG, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// test negative and modulo
if (!strncmp(line,BIG1modneg2line, strlen(BIG1modneg2line)))
{
BIG_XXX_zero(supp);
BIG_XXX_copy(supp,BIG1);
BIG_XXX_modneg(supp,supp,BIG2);
len = strlen(BIG1modneg2line);
linePtr = line + len;
read_BIG_XXX(BIG1modneg2,linePtr);
BIG_XXX_norm(supp);
if (BIG_XXX_comp(BIG1modneg2,supp) != 0)
{
printf("ERROR negative reduced modulo BIG, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// test counting bit BIG
if (!strncmp(line,nbitBIGline, strlen(nbitBIGline)))
{
len = strlen(nbitBIGline);
nbitBIG = BIG_XXX_nbits(BIG1);
linePtr = line + len;
sscanf(linePtr,"%d\n",&bitlen);
if (nbitBIG != bitlen)
{
printf("ERROR counting bit BIG, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// test counting bit DBIG
if (!strncmp(line,nbitDBIGline, strlen(nbitDBIGline)))
{
len = strlen(nbitDBIGline);
nbitDBIG = BIG_XXX_dnbits(BIGmul);
linePtr = line + len;
sscanf(linePtr,"%d\n",&bitlen);
if (nbitDBIG != bitlen)
{
printf("ERROR counting bit DBIG, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// test division
if (!strncmp(line,BIGdivline, strlen(BIGdivline)))
{
BIG_XXX_dzero(dsupp);
BIG_XXX_zero(supp);
BIG_XXX_dcopy(dsupp,BIGmul);
BIG_XXX_ddiv(supp,dsupp,BIGsum);
len = strlen(BIGdivline);
linePtr = line + len;
read_BIG_XXX(BIGdiv,linePtr);
BIG_XXX_norm(supp);
if (BIG_XXX_comp(BIGdiv,supp) != 0)
{
printf("ERROR division BIG, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// test division with modulo
if (!strncmp(line,BIGdivmodline, strlen(BIGdivmodline)))
{
read_BIG_XXX(mod,"E186EB30EF");
read_BIG_XXX(div,"0ED5066C6815047425DF");
BIG_XXX_zero(supp);
BIG_XXX_copy(supp,BIG1);
BIG_XXX_moddiv(supp,supp,div,mod);
len = strlen(BIGdivmodline);
linePtr = line + len;
read_BIG_XXX(BIGdivmod,linePtr);
BIG_XXX_norm(supp);
if (BIG_XXX_comp(BIGdivmod,supp) != 0)
{
printf("ERROR division modulo BIG, line %d\n",i);
exit(EXIT_FAILURE);
}
}
// test small multiplication
if (!strncmp(line,BIGpxmulline, strlen(BIGpxmulline)))
{
BIG_XXX_dzero(dsupp);
BIG_XXX_pxmul(dsupp,BIG1,nbitDBIG);
len = strlen(BIGpxmulline);
linePtr = line + len;
read_DBIG_XXX(BIGpxmul,linePtr);
BIG_XXX_dnorm(dsupp);
if (BIG_XXX_dcomp(BIGpxmul,dsupp) != 0)
{
printf("ERROR small multiplication BIG, line %d\n",i);
exit(EXIT_FAILURE);
}
}
}
fclose(fp);
printf("SUCCESS TEST ARITMETIC OF BIG_XXX PASSED\n");
exit(EXIT_SUCCESS);
}