1 /* main-bigint-test.c */
3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008, 2009, 2010 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
24 #include "main-test-common.h"
27 #include "noekeon_prng.h"
29 #include "bigint2_io.h"
31 #include "performance_test.h"
33 char *algo_name = "BigInt2";
35 #define MAX(a,b) ((a) > (b) ? (a) : (b))
36 #define MIN(a,b) ((a) < (b) ? (a) : (b))
38 /*****************************************************************************
39 * additional validation-functions *
40 *****************************************************************************/
41 void test_echo_bigint(void) {
43 cli_putstr_P(PSTR("\r\necho test\r\n"));
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"));
50 cli_putstr_P(PSTR("\r\necho: "));
52 cli_putstr_P(PSTR("\r\n"));
57 void test_add_bigint(void){
59 printf_P(PSTR("\nadd test\n"));
61 printf_P(PSTR("\nenter a:"));
62 if (bigint_read_hex_echo(&a, 512)) {
63 printf_P(PSTR("\n end add test"));
66 printf_P(PSTR("\nenter b:"));
67 if (bigint_read_hex_echo(&b, 512)) {
69 printf_P(PSTR("\n end add test"));
72 printf_P(PSTR("\n "));
74 printf_P(PSTR(" + "));
76 printf_P(PSTR(" = "));
77 memset(&c, 0, sizeof(c));
78 bigint_add_u(&c, &a, &b);
80 cli_putstr_P(PSTR("\r\n"));
87 void test_sub_bigint(void){
89 printf_P(PSTR("\nadd test\n"));
91 printf_P(PSTR("\nenter a:"));
92 if (bigint_read_hex_echo(&a, 512)) {
93 printf_P(PSTR("\n end add test"));
96 printf_P(PSTR("\nenter b:"));
97 if (bigint_read_hex_echo(&b, 512)) {
99 printf_P(PSTR("\n end add test"));
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"));
118 void test_add_scale_bigint(void){
121 cli_putstr_P(PSTR("\r\nadd-scale test\r\n"));
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"));
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"));
133 cli_putstr_P(PSTR("\r\nenter scale:"));
136 cli_getsn_cecho(str, 7);
140 if(bigint_read_hex_echo(&scale)){
142 cli_putstr_P(PSTR("\r\n end add test"));
147 c_b = malloc((MAX(a.length_W, b.length_W+scale) + 2) * sizeof(bigint_word_t));
149 cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
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"));
174 void test_mul_bigint(void){
176 cli_putstr_P(PSTR("\r\nmul test\r\n"));
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"));
183 cli_putstr_P(PSTR("\r\nenter b:"));
184 if (bigint_read_hex_echo(&b, 0)) {
186 cli_putstr_P(PSTR("\r\n end mul test"));
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(" = "));
195 c_b = malloc((MAX(a.length_W, b.length_W) + 1) * 2 * sizeof(bigint_word_t));
197 cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
203 bigint_mul_schoolbook(&c, &a, &b);
204 bigint_print_hex(&c);
205 cli_putstr_P(PSTR("\r\n"));
213 void test_mul_mont_bigint(void){
214 bigint_t a, b, c, a_, b_, m_, res;
216 cli_putstr_P(PSTR("\r\nmul-mont test ( (a * b) % c )\r\n"));
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"));
223 cli_putstr_P(PSTR("\r\nenter b:"));
224 if (bigint_read_hex_echo(&b)) {
226 cli_putstr_P(PSTR("\r\n end mul test"));
229 cli_putstr_P(PSTR("\r\nenter c:"));
230 if (bigint_read_hex_echo(&c)) {
233 cli_putstr_P(PSTR("\r\n end mul test"));
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];
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);
263 void test_mul_word_bigint(void){
266 cli_putstr_P(PSTR("\r\nmul test\r\n"));
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"));
273 cli_putstr_P(PSTR("\r\nenter b:"));
274 if (bigint_read_hex_echo(&b, 0)) {
276 cli_putstr_P(PSTR("\r\n end mul test"));
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(" = "));
285 if (b.length_W > 1) {
288 cli_putstr_P(PSTR("\r\n end mul test"));
291 t = realloc(a.wordv, (a.length_W + 3) * sizeof(bigint_word_t));
293 cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
299 bigint_mul_word(&a, &a, b.wordv[0]);
300 bigint_print_hex(&a);
301 cli_putstr_P(PSTR("\r\n"));
307 void test_square_bigint(void){
309 cli_putstr_P(PSTR("\r\nsquare test\r\n"));
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"));
316 cli_putstr_P(PSTR("\r\n "));
317 bigint_print_hex(&a);
318 cli_putstr_P(PSTR("**2 = "));
320 c_b = malloc(a.length_W * 2 * sizeof(bigint_word_t));
322 cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
327 bigint_square(&c, &a);
328 bigint_print_hex(&c);
329 cli_putstr_P(PSTR("\r\n"));
335 void test_reduce_bigint(void){
337 cli_putstr_P(PSTR("\r\nreduce test\r\n"));
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"));
344 cli_putstr_P(PSTR("\r\nenter b:"));
345 if (bigint_read_hex_echo(&b, 0)) {
347 cli_putstr_P(PSTR("\r\n end reduce test"));
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"));
365 void test_div_bigint(void){
367 printf_P(PSTR("\ndiv test\n"));
369 printf_P(PSTR("\nenter a:"));
370 if (bigint_read_hex_echo(&a, 0)) {
371 printf_P(PSTR("\n end div test"));
374 printf_P(PSTR("\nenter b:"));
375 if (bigint_read_hex_echo(&b, 0)) {
377 printf_P(PSTR("\n end div test"));
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"));
402 void test_expmod_bigint(void){
405 cli_putstr_P(PSTR("\r\nexpnonentiation-modulo test\r\n"));
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"));
412 cli_putstr_P(PSTR("\r\nenter b:"));
413 if (bigint_read_hex_echo(&b)) {
415 cli_putstr_P(PSTR("\r\n end expmod test"));
418 cli_putstr_P(PSTR("\r\nenter c:"));
419 if (bigint_read_hex_echo(&c)) {
422 cli_putstr_P(PSTR("\r\n end expmod test"));
425 d_b = malloc(c.length_W * sizeof(bigint_word_t));
427 cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
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"));
453 void test_expmod_mont_bigint(void){
456 cli_putstr_P(PSTR("\r\nexpnonentiation-modulo-montgomory test\r\n"));
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"));
463 cli_putstr_P(PSTR("\r\nenter b:"));
464 if (bigint_read_hex_echo(&b)) {
466 cli_putstr_P(PSTR("\r\n end expmod test"));
469 cli_putstr_P(PSTR("\r\nenter c:"));
470 if (bigint_read_hex_echo(&c)) {
473 cli_putstr_P(PSTR("\r\n end expmod test"));
476 d_b = malloc(c.length_W * sizeof(bigint_word_t));
478 cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
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"));
505 void test_gcdext_bigint(void){
506 bigint_t a, b, c, d, e;
507 cli_putstr_P(PSTR("\r\ngcdext test\r\n"));
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"));
514 cli_putstr_P(PSTR("\r\nenter b:"));
515 if (bigint_read_hex_echo(&b, 0)) {
517 cli_putstr_P(PSTR("\r\n end gcdext test"));
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);
537 cli_putstr_P(PSTR("\r\n"));
546 void testrun_performance_bigint(void){
549 /*****************************************************************************
551 *****************************************************************************/
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";
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 },
592 int_realloc = realloc;
595 welcome_msg(algo_name);
596 cmd_interface(cmdlist);