# bigint_test.rb
=begin
This file is part of the AVR-Crypto-Lib.
- Copyright (C) 2008, 2009 Daniel Otte (daniel.otte@rub.de)
+ Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
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 #
################################################################################
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
################################################################################
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) }
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'
end
1
-$logile.close()
+$logfile.close()