]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-bigint2-test.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / test_src / main-bigint2-test.c
1 /* main-bigint-test.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /*
20  * bigint test-suit
21  * 
22 */
23
24 #include "main-test-common.h"
25
26 #include "noekeon.h"
27 #include "noekeon_prng.h"
28 #include "bigint2.h"
29 #include "bigint2_io.h"
30
31 #include "performance_test.h"
32
33 char *algo_name = "BigInt2";
34
35 #define MAX(a,b) ((a) > (b) ? (a) : (b))
36 #define MIN(a,b) ((a) < (b) ? (a) : (b))
37
38 /*****************************************************************************
39  *  additional validation-functions                                                                                      *
40  *****************************************************************************/
41 void test_echo_bigint(void) {
42         bigint_t a;
43         cli_putstr_P(PSTR("\r\necho test\r\n"));
44         for (;;) {
45                 cli_putstr_P(PSTR("\r\nenter hex number:"));
46                 if (bigint_read_hex_echo(&a, 0)) {
47                         cli_putstr_P(PSTR("\r\n end echo test"));
48                         return;
49                 }
50                 cli_putstr_P(PSTR("\r\necho: "));
51                 bigint_print_hex(&a);
52                 cli_putstr_P(PSTR("\r\n"));
53                 free(a.wordv);
54         }
55 }
56
57 void test_add_bigint(void){
58         bigint_t a, b, c;
59         printf_P(PSTR("\nadd test\n"));
60         for (;;) {
61                 printf_P(PSTR("\nenter a:"));
62                 if (bigint_read_hex_echo(&a, 512)) {
63                         printf_P(PSTR("\n end add test"));
64                         return;
65                 }
66                 printf_P(PSTR("\nenter b:"));
67                 if (bigint_read_hex_echo(&b, 512)) {
68                         free(a.wordv);
69                         printf_P(PSTR("\n end add test"));
70                         return;
71                 }
72                 printf_P(PSTR("\n "));
73                 bigint_print_hex(&a);
74                 printf_P(PSTR(" + "));
75                 bigint_print_hex(&b);
76                 printf_P(PSTR(" = "));
77                 memset(&c, 0, sizeof(c));
78                 bigint_add_u(&c, &a, &b);
79                 bigint_print_hex(&c);
80                 cli_putstr_P(PSTR("\r\n"));
81                 free(a.wordv);
82                 free(b.wordv);
83                 bigint_free(&c);
84         }
85 }
86
87 void test_sub_bigint(void){
88     bigint_t a, b, c;
89     printf_P(PSTR("\nadd test\n"));
90     for (;;) {
91         printf_P(PSTR("\nenter a:"));
92         if (bigint_read_hex_echo(&a, 512)) {
93             printf_P(PSTR("\n end add test"));
94             return;
95         }
96         printf_P(PSTR("\nenter b:"));
97         if (bigint_read_hex_echo(&b, 512)) {
98             free(a.wordv);
99             printf_P(PSTR("\n end add test"));
100             return;
101         }
102         printf_P(PSTR("\n "));
103         bigint_print_hex(&a);
104         printf_P(PSTR(" - "));
105         bigint_print_hex(&b);
106         printf_P(PSTR(" = "));
107         memset(&c, 0, sizeof(c));
108         bigint_sub_u(&c, &a, &b);
109         bigint_print_hex(&c);
110         cli_putstr_P(PSTR("\r\n"));
111         free(a.wordv);
112         free(b.wordv);
113         bigint_free(&c);
114     }
115 }
116
117 #if 0
118 void test_add_scale_bigint(void){
119         bigint_t a, b, c;
120         uint16_t scale;
121         cli_putstr_P(PSTR("\r\nadd-scale test\r\n"));
122         for (;;) {
123                 cli_putstr_P(PSTR("\r\nenter a:"));
124                 if (bigint_read_hex_echo(&a)) {
125                         cli_putstr_P(PSTR("\r\n end add-scale test"));
126                         return;
127                 }
128                 cli_putstr_P(PSTR("\r\nenter b:"));
129                 if (bigint_read_hex_echo(&b)) {
130                         cli_putstr_P(PSTR("\r\n end add-scale test"));
131                         return;
132                 }
133                 cli_putstr_P(PSTR("\r\nenter scale:"));
134                 {
135                         char str[8];
136                         cli_getsn_cecho(str, 7);
137                         scale = atoi(str);
138                 }
139         /*
140                 if(bigint_read_hex_echo(&scale)){
141                         free(scale.wordv);
142                         cli_putstr_P(PSTR("\r\n end add test"));
143                         return;
144                 }
145         */
146                 bigint_word_t *c_b;
147                 c_b = malloc((MAX(a.length_W, b.length_W+scale) + 2) * sizeof(bigint_word_t));
148                 if(c_b==NULL){
149                         cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
150                         free(a.wordv);
151                         free(b.wordv);
152                         continue;
153                 }
154                 c.wordv = c_b;
155                 bigint_copy(&c, &a);
156                 bigint_add_scale_u(&c, &b, scale);
157                 cli_putstr_P(PSTR("\r\n "));
158                 bigint_print_hex(&a);
159                 cli_putstr_P(PSTR(" + "));
160                 bigint_print_hex(&b);
161                 cli_putstr_P(PSTR("<<8*"));
162                 cli_hexdump_rev(&scale, 2);
163                 cli_putstr_P(PSTR(" = "));
164                 bigint_print_hex(&c);
165                 cli_putstr_P(PSTR("\r\n"));
166                 free(a.wordv);
167                 free(b.wordv);
168                 free(c_b);
169         }
170 }
171 #endif
172
173
174 void test_mul_bigint(void){
175         bigint_t a, b, c;
176         cli_putstr_P(PSTR("\r\nmul test\r\n"));
177         for (;;) {
178                 cli_putstr_P(PSTR("\r\nenter a:"));
179                 if (bigint_read_hex_echo(&a, 0)) {
180                         cli_putstr_P(PSTR("\r\n end mul test"));
181                         return;
182                 }
183                 cli_putstr_P(PSTR("\r\nenter b:"));
184                 if (bigint_read_hex_echo(&b, 0)) {
185                         free(a.wordv);
186                         cli_putstr_P(PSTR("\r\n end mul test"));
187                         return;
188                 }
189                 cli_putstr_P(PSTR("\r\n "));
190                 bigint_print_hex(&a);
191                 cli_putstr_P(PSTR(" * "));
192                 bigint_print_hex(&b);
193                 cli_putstr_P(PSTR(" = "));
194                 bigint_word_t *c_b;
195                 c_b = malloc((MAX(a.length_W, b.length_W) + 1) * 2 * sizeof(bigint_word_t));
196                 if (c_b==NULL) {
197                         cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
198                         free(a.wordv);
199                         free(b.wordv);
200                         continue;
201                 }
202                 c.wordv = c_b;
203                 bigint_mul_schoolbook(&c, &a, &b);
204                 bigint_print_hex(&c);
205                 cli_putstr_P(PSTR("\r\n"));
206                 free(a.wordv);
207                 free(b.wordv);
208                 free(c_b);
209         }
210 }
211
212 #if 0
213 void test_mul_mont_bigint(void){
214     bigint_t a, b, c, a_, b_, m_, res;
215     bigint_length_t s;
216     cli_putstr_P(PSTR("\r\nmul-mont test ( (a * b) % c )\r\n"));
217     for (;;) {
218         cli_putstr_P(PSTR("\r\nenter a:"));
219         if (bigint_read_hex_echo(&a)) {
220             cli_putstr_P(PSTR("\r\n end mul test"));
221             return;
222         }
223         cli_putstr_P(PSTR("\r\nenter b:"));
224         if (bigint_read_hex_echo(&b)) {
225             free(a.wordv);
226             cli_putstr_P(PSTR("\r\n end mul test"));
227             return;
228         }
229         cli_putstr_P(PSTR("\r\nenter c:"));
230         if (bigint_read_hex_echo(&c)) {
231             free(a.wordv);
232             free(b.wordv);
233             cli_putstr_P(PSTR("\r\n end mul test"));
234             return;
235         }
236         s = c.length_W;
237         cli_putstr_P(PSTR("\r\n ("));
238         bigint_print_hex(&a);
239         cli_putstr_P(PSTR(" * "));
240         bigint_print_hex(&b);
241         cli_putstr_P(PSTR(") % "));
242         bigint_print_hex(&c);
243         cli_putstr_P(PSTR(" = "));
244         bigint_word_t res_w[s], a_w_[s], b_w_[s], m_w_[s + 1];
245         res.wordv = res_w;
246         a_.wordv = a_w_;
247         b_.wordv = b_w_;
248         m_.wordv = m_w_;
249         bigint_mont_gen_m_(&m_, &c);
250         bigint_mont_trans(&a_, &a, &c);
251         bigint_mont_trans(&b_, &b, &c);
252         bigint_mont_mul(&res, &a_, &b_, &c, &m_);
253         bigint_mont_red(&res, &res, &c, &m_);
254         bigint_print_hex(&res);
255         putchar('\n');
256         free(a.wordv);
257         free(b.wordv);
258         free(c.wordv);
259     }
260 }
261 #endif
262
263 void test_mul_word_bigint(void){
264     bigint_t a, b;
265     bigint_word_t *t;
266     cli_putstr_P(PSTR("\r\nmul test\r\n"));
267     for (;;) {
268         cli_putstr_P(PSTR("\r\nenter a:"));
269         if (bigint_read_hex_echo(&a, 0)) {
270             cli_putstr_P(PSTR("\r\n end mul test"));
271             return;
272         }
273         cli_putstr_P(PSTR("\r\nenter b:"));
274         if (bigint_read_hex_echo(&b, 0)) {
275             free(a.wordv);
276             cli_putstr_P(PSTR("\r\n end mul test"));
277             return;
278         }
279         cli_putstr_P(PSTR("\r\n "));
280         bigint_print_hex(&a);
281         cli_putstr_P(PSTR(" * "));
282         bigint_print_hex(&b);
283         cli_putstr_P(PSTR(" = "));
284
285         if (b.length_W > 1) {
286             free(a.wordv);
287             free(b.wordv);
288             cli_putstr_P(PSTR("\r\n end mul test"));
289         }
290
291         t = realloc(a.wordv, (a.length_W + 3) * sizeof(bigint_word_t));
292         if (t == NULL) {
293             cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
294             free(a.wordv);
295             free(b.wordv);
296             continue;
297         }
298         a.wordv = t;
299         bigint_mul_word(&a, &a, b.wordv[0]);
300         bigint_print_hex(&a);
301         cli_putstr_P(PSTR("\r\n"));
302         free(a.wordv);
303         free(b.wordv);
304     }
305 }
306
307 void test_square_bigint(void){
308         bigint_t a, c;
309         cli_putstr_P(PSTR("\r\nsquare test\r\n"));
310         for(;;){
311                 cli_putstr_P(PSTR("\r\nenter a:"));
312                 if(bigint_read_hex_echo(&a, 0)){
313                         cli_putstr_P(PSTR("\r\n end square test"));
314                         return;
315                 }
316                 cli_putstr_P(PSTR("\r\n "));
317                 bigint_print_hex(&a);
318                 cli_putstr_P(PSTR("**2 = "));
319                 bigint_word_t *c_b;
320                 c_b = malloc(a.length_W * 2 * sizeof(bigint_word_t));
321                 if(c_b == NULL){
322                         cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
323                         free(a.wordv);
324                         continue;
325                 }
326                 c.wordv = c_b;
327                 bigint_square(&c, &a);
328                 bigint_print_hex(&c);
329                 cli_putstr_P(PSTR("\r\n"));
330                 free(a.wordv);
331                 free(c_b);
332         }
333 }
334
335 void test_reduce_bigint(void){
336         bigint_t a, b, c;
337         cli_putstr_P(PSTR("\r\nreduce test\r\n"));
338         for (;;) {
339                 cli_putstr_P(PSTR("\r\nenter a:"));
340                 if (bigint_read_hex_echo(&a, 0)) {
341                         cli_putstr_P(PSTR("\r\n end reduce test"));
342                         return;
343                 }
344                 cli_putstr_P(PSTR("\r\nenter b:"));
345                 if (bigint_read_hex_echo(&b, 0)) {
346                         free(a.wordv);
347                         cli_putstr_P(PSTR("\r\n end reduce test"));
348                         return;
349                 }
350                 cli_putstr_P(PSTR("\r\n "));
351                 bigint_print_hex(&a);
352                 cli_putstr_P(PSTR(" % "));
353                 bigint_print_hex(&b);
354                 cli_putstr_P(PSTR(" = "));
355                 memset(&c, 0, sizeof(c));
356                 bigint_divide(NULL, &c, &a, &b);
357                 bigint_print_hex(&c);
358                 cli_putstr_P(PSTR("\r\n"));
359         bigint_free(&c);
360         bigint_free(&b);
361                 bigint_free(&a);
362         }
363 }
364
365 void test_div_bigint(void){
366     bigint_t a, b, c, d;
367     printf_P(PSTR("\ndiv test\n"));
368     for (;;) {
369         printf_P(PSTR("\nenter a:"));
370         if (bigint_read_hex_echo(&a, 0)) {
371             printf_P(PSTR("\n end div test"));
372             return;
373         }
374         printf_P(PSTR("\nenter b:"));
375         if (bigint_read_hex_echo(&b, 0)) {
376             free(a.wordv);
377             printf_P(PSTR("\n end div test"));
378             return;
379         }
380         printf_P(PSTR("\n "));
381         bigint_print_hex(&a);
382         printf_P(PSTR(" / "));
383         bigint_print_hex(&b);
384         printf_P(PSTR(" = "));
385         memset(&c, 0, sizeof(c));
386         memset(&d, 0, sizeof(d));
387         bigint_divide(&d, &c, &a, &b);
388         bigint_print_hex(&d);
389         printf_P(PSTR("; R = "));
390         bigint_print_hex(&c);
391         printf_P(PSTR("\n"));
392         bigint_free(&d);
393         bigint_free(&c);
394         bigint_free(&b);
395         bigint_free(&a);
396     }
397 }
398
399
400 #if 0
401 /* d = a**b % c */
402 void test_expmod_bigint(void){
403         bigint_t a, b, c, d;
404         bigint_word_t *d_b;
405         cli_putstr_P(PSTR("\r\nexpnonentiation-modulo test\r\n"));
406         for (;;) {
407                 cli_putstr_P(PSTR("\r\nenter a:"));
408                 if (bigint_read_hex_echo(&a)) {
409                         cli_putstr_P(PSTR("\r\n end expmod test"));
410                         return;
411                 }
412                 cli_putstr_P(PSTR("\r\nenter b:"));
413                 if (bigint_read_hex_echo(&b)) {
414                         free(a.wordv);
415                         cli_putstr_P(PSTR("\r\n end expmod test"));
416                         return;
417                 }
418                 cli_putstr_P(PSTR("\r\nenter c:"));
419                 if (bigint_read_hex_echo(&c)) {
420                         free(a.wordv);
421                         free(b.wordv);
422                         cli_putstr_P(PSTR("\r\n end expmod test"));
423                         return;
424                 }
425                 d_b = malloc(c.length_W * sizeof(bigint_word_t));
426                 if(d_b==NULL){
427                         cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
428                         free(a.wordv);
429                         free(b.wordv);
430                         free(c.wordv);
431                         continue;
432                 }
433                 d.wordv = d_b;
434                 cli_putstr_P(PSTR("\r\n "));
435                 bigint_print_hex(&a);
436                 cli_putstr_P(PSTR("**"));
437                 bigint_print_hex(&b);
438                 cli_putstr_P(PSTR(" % "));
439                 bigint_print_hex(&c);
440                 cli_putstr_P(PSTR(" = "));
441                 bigint_expmod_u_sam(&d, &a, &b, &c);
442                 bigint_print_hex(&d);
443                 cli_putstr_P(PSTR("\r\n"));
444                 free(a.wordv);
445                 free(b.wordv);
446                 free(c.wordv);
447                 free(d.wordv);
448
449         }
450 }
451
452 /* d = a**b % c */
453 void test_expmod_mont_bigint(void){
454     bigint_t a, b, c, d;
455     bigint_word_t *d_b;
456     cli_putstr_P(PSTR("\r\nexpnonentiation-modulo-montgomory test\r\n"));
457     for (;;) {
458         cli_putstr_P(PSTR("\r\nenter a:"));
459         if (bigint_read_hex_echo(&a)) {
460             cli_putstr_P(PSTR("\r\n end expmod test"));
461             return;
462         }
463         cli_putstr_P(PSTR("\r\nenter b:"));
464         if (bigint_read_hex_echo(&b)) {
465             free(a.wordv);
466             cli_putstr_P(PSTR("\r\n end expmod test"));
467             return;
468         }
469         cli_putstr_P(PSTR("\r\nenter c:"));
470         if (bigint_read_hex_echo(&c)) {
471             free(a.wordv);
472             free(b.wordv);
473             cli_putstr_P(PSTR("\r\n end expmod test"));
474             return;
475         }
476         d_b = malloc(c.length_W * sizeof(bigint_word_t));
477         if (d_b == NULL) {
478             cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
479             free(a.wordv);
480             free(b.wordv);
481             free(c.wordv);
482             continue;
483         }
484         d.wordv = d_b;
485         cli_putstr_P(PSTR("\r\n "));
486         bigint_print_hex(&a);
487         cli_putstr_P(PSTR("**"));
488         bigint_print_hex(&b);
489         cli_putstr_P(PSTR(" % "));
490         bigint_print_hex(&c);
491         cli_putstr_P(PSTR(" = "));
492         bigint_expmod_u_mont_sam(&d, &a, &b, &c);
493         bigint_print_hex(&d);
494         cli_putstr_P(PSTR("\r\n"));
495         free(a.wordv);
496         free(b.wordv);
497         free(c.wordv);
498         free(d.wordv);
499
500     }
501 }
502
503 #endif
504
505 void test_gcdext_bigint(void){
506         bigint_t a, b, c, d, e;
507         cli_putstr_P(PSTR("\r\ngcdext test\r\n"));
508         for (;;) {
509                 cli_putstr_P(PSTR("\r\nenter a:"));
510                 if (bigint_read_hex_echo(&a, 0)) {
511                         cli_putstr_P(PSTR("\r\n end gcdext test"));
512                         return;
513                 }
514                 cli_putstr_P(PSTR("\r\nenter b:"));
515                 if (bigint_read_hex_echo(&b, 0)) {
516                         bigint_free(&a);
517                         cli_putstr_P(PSTR("\r\n end gcdext test"));
518                         return;
519                 }
520
521                 memset(&c, 0, sizeof(c));
522         memset(&d, 0, sizeof(d));
523         memset(&e, 0, sizeof(e));
524                 cli_putstr_P(PSTR("\r\n gcdext( "));
525                 bigint_print_hex(&a);
526                 cli_putstr_P(PSTR(", "));
527                 bigint_print_hex(&b);
528                 cli_putstr_P(PSTR(") => "));
529                 bigint_gcdext(&c, &d, &e, &a, &b);
530                 cli_putstr_P(PSTR("a = "));
531                 bigint_print_hex(&d);
532                 cli_putstr_P(PSTR("; b = "));
533                 bigint_print_hex(&e);
534                 cli_putstr_P(PSTR("; gcd = "));
535                 bigint_print_hex(&c);
536
537                 cli_putstr_P(PSTR("\r\n"));
538                 bigint_free(&a);
539         bigint_free(&b);
540         bigint_free(&c);
541         bigint_free(&d);
542         bigint_free(&e);
543         }
544 }
545
546 void testrun_performance_bigint(void){
547
548 }
549 /*****************************************************************************
550  *  main                                                                                                                                         *
551  *****************************************************************************/
552
553 const char echo_test_str[]        PROGMEM = "echo-test";
554 const char add_test_str[]         PROGMEM = "add-test";
555 const char sub_test_str[]         PROGMEM = "sub-test";
556 const char add_scale_test_str[]   PROGMEM = "add-scale-test";
557 const char mul_test_str[]         PROGMEM = "mul-test";
558 const char mul_mont_test_str[]    PROGMEM = "mul-mont-test";
559 const char mul_word_test_str[]    PROGMEM = "mul-word-test";
560 const char square_test_str[]      PROGMEM = "square-test";
561 const char reduce_test_str[]      PROGMEM = "reduce-test";
562 const char div_test_str[]         PROGMEM = "div-test";
563 const char expmod_test_str[]      PROGMEM = "expmod-test";
564 const char expmod_mont_test_str[] PROGMEM = "expmod-mont-test";
565 const char gcdext_test_str[]      PROGMEM = "gcdext-test";
566 const char quick_test_str[]       PROGMEM = "quick-test";
567 const char performance_str[]      PROGMEM = "performance";
568 const char echo_str[]             PROGMEM = "echo";
569
570 const cmdlist_entry_t cmdlist[] PROGMEM = {
571         { add_test_str,         NULL, test_add_bigint               },
572     { sub_test_str,         NULL, test_sub_bigint               },
573 //      { add_scale_test_str,   NULL, test_add_scale_bigint         },
574         { mul_test_str,         NULL, test_mul_bigint               },
575 //    { mul_mont_test_str,    NULL, test_mul_mont_bigint          },
576     { mul_word_test_str,    NULL, test_mul_word_bigint          },
577         { square_test_str,      NULL, test_square_bigint            },
578     { reduce_test_str,      NULL, test_reduce_bigint            },
579         { div_test_str,         NULL, test_div_bigint               },
580 //    { expmod_test_str,      NULL, test_expmod_bigint            },
581 //    { expmod_mont_test_str, NULL, test_expmod_mont_bigint       },
582         { gcdext_test_str,      NULL, test_gcdext_bigint            },
583 //      { quick_test_str,       NULL, test_gcdext_simple            },
584         { echo_test_str,        NULL, test_echo_bigint              },
585         { performance_str,      NULL, testrun_performance_bigint    },
586         { echo_str,         (void*)1, (void_fpt)echo_ctrl           },
587         { NULL,                 NULL, NULL                          }
588 };
589
590 int main (void){
591     main_setup();
592     int_realloc = realloc;
593     int_free = free;
594     for(;;){
595         welcome_msg(algo_name);
596                 cmd_interface(cmdlist);
597         }
598 }