From: bg Date: Tue, 8 Jul 2014 19:15:30 +0000 (+0200) Subject: supporting division test for bigint2 X-Git-Url: https://git.cryptolib.org/?a=commitdiff_plain;h=226bfa4c5fa7435f253221d76506f8d977bfb91b;p=avr-crypto-lib.git supporting division test for bigint2 --- diff --git a/bigint2/bigint2.c b/bigint2/bigint2.c index 449070b..4b5dbe0 100644 --- a/bigint2/bigint2.c +++ b/bigint2/bigint2.c @@ -700,9 +700,10 @@ int bigint_divide(bigint_t *q, bigint_t *r, const bigint_t *a, const bigint_t *b } i = la - lb; if (q) { - if ((ret = check_size(q, (i + BIGINT_WORD_SIZE - 1) / BIGINT_WORD_SIZE))) { + if ((ret = check_size(q, (i + BIGINT_WORD_SIZE) / BIGINT_WORD_SIZE))) { return ret; } + q->length_W = (i + BIGINT_WORD_SIZE) / BIGINT_WORD_SIZE; memset(q->wordv, 0, q->allocated_W * sizeof(bigint_word_t)); } if (r) { diff --git a/host/bigint2_test.rb b/host/bigint2_test.rb index df3ca3f..be37427 100644 --- a/host/bigint2_test.rb +++ b/host/bigint2_test.rb @@ -473,6 +473,62 @@ def reduce_test(a,b) return false end +################################################################################ +# div_test # +################################################################################ + +def div_test(a, b) + begin + line = $sp.gets() + line = "" if line==nil + puts("DBG got: "+line) if $debug + if /^Error:.*/.match(line) + puts line + return false + end + end while ! /[\s]*enter a:[\s]*/.match(line) + $sp.print(a.to_s(16) + " ") + begin + line = $sp.gets() + line = "" if line == nil + puts("DBG got: " + line) if $debug + if /^Error:.*/.match(line) + puts line + return false + end + end while ! /[\s]*enter b:[\s]*/.match(line) + $sp.print(b.to_s(16) + " ") + line = '' + begin + line_tmp = $sp.gets() + line_tmp = '' if line_tmp == nil + line += line_tmp + puts("DBG got: " + line) if $debug + if /^Error:.*/.match(line) + puts line + return false + end + end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+\/[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+);[\s]*R[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line) + a_ = m[1].to_i(16) + b_ = m[2].to_i(16) + c_ = m[3].to_i(16) + d_ = m[4].to_i(16) + line.strip! + if (a_== a && b_ == b && c_ == (a / b) && d_ == (a % b)) + $logfile.printf("[pass (%d)]: %s\n", $testno, line) + return true + else + $logfile.printf("[fail (%s%s%s%s) (%d)]: %s", + (a == a_) ? "" : "a", + (b == b_) ? "" : "b", + (c_ == a / b) ? "" : "c", + (d_ == a % b) ? "" : "d", $testno, line) + $logfile.printf(" ; should %s %% %s = %s; R = %s\n", a.to_s(16), b.to_s(16), (a / b).to_s(16), (a % b).to_s(16)) + return false + end + return false +end + ################################################################################ # mulmod_test # ################################################################################ @@ -892,7 +948,34 @@ def run_test_reduce(skip=0) end length_a_B += 1 length_b_B += 1 - end while length_a_B<4096/8 + end while length_a_B < 4096 / 8 +end + +################################################################################ +# run_test_div # +################################################################################ + +def run_test_div(skip=0) + length_a_B = skip + 1 + length_b_B = skip + 1 + begin + $size = length_a_B + (0..16).each do |i| + a = rand(256 ** length_a_B) + b = rand(256 ** length_a_B) + 1 + v = div_test(a, b) + screen_progress(v) + end + (0..16).each do |i| + b_size = rand(length_b_B + 1) + a = rand(256 ** length_a_B) + b = rand(256 ** b_size) + 1 + v = div_test(a, b) + screen_progress(v) + end + length_a_B += 1 + length_b_B += 1 + end while length_a_B < 4096 / 8 end ################################################################################ @@ -1106,6 +1189,7 @@ $logfile.printf("seed = 0x%X\n", random_seed) tests = Hash.new tests['a'] = proc {|x| run_test_add(x) } tests['b'] = proc {|x| run_test_sub(x) } +tests['d'] = proc {|x| run_test_div(x) } tests['m'] = proc {|x| run_test_mul(x) } tests['M'] = proc {|x| run_test_mulmod(x) } tests['n'] = proc {|x| run_test_mul_word(x) } @@ -1118,6 +1202,7 @@ tests['g'] = proc {|x| run_test_gcdext(x) } init_str = Hash.new init_str['a'] = 'add-test' init_str['b'] = 'sub-test' +init_str['d'] = 'div-test' init_str['x'] = 'add-scale-test' init_str['m'] = 'mul-test' init_str['M'] = 'mul-mont-test' @@ -1153,4 +1238,4 @@ else end 1 -$logile.close() +$logfile.close() diff --git a/test_src/main-bigint2-test.c b/test_src/main-bigint2-test.c index 9f3f9de..45e2c49 100644 --- a/test_src/main-bigint2-test.c +++ b/test_src/main-bigint2-test.c @@ -362,6 +362,41 @@ void test_reduce_bigint(void){ } } +void test_div_bigint(void){ + bigint_t a, b, c, d; + printf_P(PSTR("\ndiv test\n")); + for (;;) { + printf_P(PSTR("\nenter a:")); + if (bigint_read_hex_echo(&a, 0)) { + printf_P(PSTR("\n end div test")); + return; + } + printf_P(PSTR("\nenter b:")); + if (bigint_read_hex_echo(&b, 0)) { + free(a.wordv); + printf_P(PSTR("\n end div test")); + return; + } + printf_P(PSTR("\n ")); + bigint_print_hex(&a); + printf_P(PSTR(" / ")); + bigint_print_hex(&b); + printf_P(PSTR(" = ")); + memset(&c, 0, sizeof(c)); + memset(&d, 0, sizeof(d)); + bigint_divide(&d, &c, &a, &b); + bigint_print_hex(&d); + printf_P(PSTR("; R = ")); + bigint_print_hex(&c); + printf_P(PSTR("\n")); + bigint_free(&d); + bigint_free(&c); + bigint_free(&b); + bigint_free(&a); + } +} + + #if 0 /* d = a**b % c */ void test_expmod_bigint(void){ @@ -524,6 +559,7 @@ const char mul_mont_test_str[] PROGMEM = "mul-mont-test"; const char mul_word_test_str[] PROGMEM = "mul-word-test"; const char square_test_str[] PROGMEM = "square-test"; const char reduce_test_str[] PROGMEM = "reduce-test"; +const char div_test_str[] PROGMEM = "div-test"; const char expmod_test_str[] PROGMEM = "expmod-test"; const char expmod_mont_test_str[] PROGMEM = "expmod-mont-test"; const char gcdext_test_str[] PROGMEM = "gcdext-test"; @@ -539,7 +575,8 @@ const cmdlist_entry_t cmdlist[] PROGMEM = { // { mul_mont_test_str, NULL, test_mul_mont_bigint }, { mul_word_test_str, NULL, test_mul_word_bigint }, { square_test_str, NULL, test_square_bigint }, - { reduce_test_str, NULL, test_reduce_bigint }, + { reduce_test_str, NULL, test_reduce_bigint }, + { div_test_str, NULL, test_div_bigint }, // { expmod_test_str, NULL, test_expmod_bigint }, // { expmod_mont_test_str, NULL, test_expmod_mont_bigint }, { gcdext_test_str, NULL, test_gcdext_bigint },