]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-bigint2-test.c
first publication of bigint2-dev
[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) 2008, 2009, 2010  Daniel Otte (daniel.otte@rub.de)
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 #if 0
366 /* d = a**b % c */
367 void test_expmod_bigint(void){
368         bigint_t a, b, c, d;
369         bigint_word_t *d_b;
370         cli_putstr_P(PSTR("\r\nexpnonentiation-modulo test\r\n"));
371         for (;;) {
372                 cli_putstr_P(PSTR("\r\nenter a:"));
373                 if (bigint_read_hex_echo(&a)) {
374                         cli_putstr_P(PSTR("\r\n end expmod test"));
375                         return;
376                 }
377                 cli_putstr_P(PSTR("\r\nenter b:"));
378                 if (bigint_read_hex_echo(&b)) {
379                         free(a.wordv);
380                         cli_putstr_P(PSTR("\r\n end expmod test"));
381                         return;
382                 }
383                 cli_putstr_P(PSTR("\r\nenter c:"));
384                 if (bigint_read_hex_echo(&c)) {
385                         free(a.wordv);
386                         free(b.wordv);
387                         cli_putstr_P(PSTR("\r\n end expmod test"));
388                         return;
389                 }
390                 d_b = malloc(c.length_W * sizeof(bigint_word_t));
391                 if(d_b==NULL){
392                         cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
393                         free(a.wordv);
394                         free(b.wordv);
395                         free(c.wordv);
396                         continue;
397                 }
398                 d.wordv = d_b;
399                 cli_putstr_P(PSTR("\r\n "));
400                 bigint_print_hex(&a);
401                 cli_putstr_P(PSTR("**"));
402                 bigint_print_hex(&b);
403                 cli_putstr_P(PSTR(" % "));
404                 bigint_print_hex(&c);
405                 cli_putstr_P(PSTR(" = "));
406                 bigint_expmod_u_sam(&d, &a, &b, &c);
407                 bigint_print_hex(&d);
408                 cli_putstr_P(PSTR("\r\n"));
409                 free(a.wordv);
410                 free(b.wordv);
411                 free(c.wordv);
412                 free(d.wordv);
413
414         }
415 }
416
417 /* d = a**b % c */
418 void test_expmod_mont_bigint(void){
419     bigint_t a, b, c, d;
420     bigint_word_t *d_b;
421     cli_putstr_P(PSTR("\r\nexpnonentiation-modulo-montgomory test\r\n"));
422     for (;;) {
423         cli_putstr_P(PSTR("\r\nenter a:"));
424         if (bigint_read_hex_echo(&a)) {
425             cli_putstr_P(PSTR("\r\n end expmod test"));
426             return;
427         }
428         cli_putstr_P(PSTR("\r\nenter b:"));
429         if (bigint_read_hex_echo(&b)) {
430             free(a.wordv);
431             cli_putstr_P(PSTR("\r\n end expmod test"));
432             return;
433         }
434         cli_putstr_P(PSTR("\r\nenter c:"));
435         if (bigint_read_hex_echo(&c)) {
436             free(a.wordv);
437             free(b.wordv);
438             cli_putstr_P(PSTR("\r\n end expmod test"));
439             return;
440         }
441         d_b = malloc(c.length_W * sizeof(bigint_word_t));
442         if (d_b == NULL) {
443             cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
444             free(a.wordv);
445             free(b.wordv);
446             free(c.wordv);
447             continue;
448         }
449         d.wordv = d_b;
450         cli_putstr_P(PSTR("\r\n "));
451         bigint_print_hex(&a);
452         cli_putstr_P(PSTR("**"));
453         bigint_print_hex(&b);
454         cli_putstr_P(PSTR(" % "));
455         bigint_print_hex(&c);
456         cli_putstr_P(PSTR(" = "));
457         bigint_expmod_u_mont_sam(&d, &a, &b, &c);
458         bigint_print_hex(&d);
459         cli_putstr_P(PSTR("\r\n"));
460         free(a.wordv);
461         free(b.wordv);
462         free(c.wordv);
463         free(d.wordv);
464
465     }
466 }
467
468 #endif
469
470 void test_gcdext_bigint(void){
471         bigint_t a, b, c, d, e;
472         cli_putstr_P(PSTR("\r\ngcdext test\r\n"));
473         for (;;) {
474                 cli_putstr_P(PSTR("\r\nenter a:"));
475                 if (bigint_read_hex_echo(&a, 0)) {
476                         cli_putstr_P(PSTR("\r\n end gcdext test"));
477                         return;
478                 }
479                 cli_putstr_P(PSTR("\r\nenter b:"));
480                 if (bigint_read_hex_echo(&b, 0)) {
481                         bigint_free(&a);
482                         cli_putstr_P(PSTR("\r\n end gcdext test"));
483                         return;
484                 }
485
486                 memset(&c, 0, sizeof(c));
487         memset(&d, 0, sizeof(d));
488         memset(&e, 0, sizeof(e));
489                 cli_putstr_P(PSTR("\r\n gcdext( "));
490                 bigint_print_hex(&a);
491                 cli_putstr_P(PSTR(", "));
492                 bigint_print_hex(&b);
493                 cli_putstr_P(PSTR(") => "));
494                 bigint_gcdext(&c, &d, &e, &a, &b);
495                 cli_putstr_P(PSTR("a = "));
496                 bigint_print_hex(&d);
497                 cli_putstr_P(PSTR("; b = "));
498                 bigint_print_hex(&e);
499                 cli_putstr_P(PSTR("; gcd = "));
500                 bigint_print_hex(&c);
501
502                 cli_putstr_P(PSTR("\r\n"));
503                 bigint_free(&a);
504         bigint_free(&b);
505         bigint_free(&c);
506         bigint_free(&d);
507         bigint_free(&e);
508         }
509 }
510
511 void testrun_performance_bigint(void){
512
513 }
514 /*****************************************************************************
515  *  main                                                                                                                                         *
516  *****************************************************************************/
517
518 const char echo_test_str[]        PROGMEM = "echo-test";
519 const char add_test_str[]         PROGMEM = "add-test";
520 const char sub_test_str[]         PROGMEM = "sub-test";
521 const char add_scale_test_str[]   PROGMEM = "add-scale-test";
522 const char mul_test_str[]         PROGMEM = "mul-test";
523 const char mul_mont_test_str[]    PROGMEM = "mul-mont-test";
524 const char mul_word_test_str[]    PROGMEM = "mul-word-test";
525 const char square_test_str[]      PROGMEM = "square-test";
526 const char reduce_test_str[]      PROGMEM = "reduce-test";
527 const char expmod_test_str[]      PROGMEM = "expmod-test";
528 const char expmod_mont_test_str[] PROGMEM = "expmod-mont-test";
529 const char gcdext_test_str[]      PROGMEM = "gcdext-test";
530 const char quick_test_str[]       PROGMEM = "quick-test";
531 const char performance_str[]      PROGMEM = "performance";
532 const char echo_str[]             PROGMEM = "echo";
533
534 const cmdlist_entry_t cmdlist[] PROGMEM = {
535         { add_test_str,         NULL, test_add_bigint               },
536     { sub_test_str,         NULL, test_sub_bigint               },
537 //      { add_scale_test_str,   NULL, test_add_scale_bigint         },
538         { mul_test_str,         NULL, test_mul_bigint               },
539 //    { mul_mont_test_str,    NULL, test_mul_mont_bigint          },
540     { mul_word_test_str,    NULL, test_mul_word_bigint          },
541         { square_test_str,      NULL, test_square_bigint            },
542         { reduce_test_str,      NULL, test_reduce_bigint            },
543 //    { expmod_test_str,      NULL, test_expmod_bigint            },
544 //    { expmod_mont_test_str, NULL, test_expmod_mont_bigint       },
545         { gcdext_test_str,      NULL, test_gcdext_bigint            },
546 //      { quick_test_str,       NULL, test_gcdext_simple            },
547         { echo_test_str,        NULL, test_echo_bigint              },
548         { performance_str,      NULL, testrun_performance_bigint    },
549         { echo_str,         (void*)1, (void_fpt)echo_ctrl           },
550         { NULL,                 NULL, NULL                          }
551 };
552
553 int main (void){
554     main_setup();
555     int_realloc = realloc;
556     int_free = free;
557     for(;;){
558         welcome_msg(algo_name);
559                 cmd_interface(cmdlist);
560         }
561 }