X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=host%2Fbigint_test.rb;h=d942b3748737cc6ca5bbdd8f76a6f67507a1487c;hb=997bf1010de28a0a7246b792254a1ec6a59d5d4d;hp=df16e7d54f084392ee3b52761410a75c3a5e6f1f;hpb=8fcc61325708d1e3b90e7b23135b9a1ab206d115;p=avr-crypto-lib.git diff --git a/host/bigint_test.rb b/host/bigint_test.rb index df16e7d..d942b37 100644 --- a/host/bigint_test.rb +++ b/host/bigint_test.rb @@ -23,7 +23,7 @@ $debug = false require 'rubygems' require 'serialport' require 'getopt/std' -require 'ftools' +require 'fileutils' require 'date' $buffer_size = 0 $conffile_check = Hash.new @@ -37,7 +37,7 @@ def readconfigfile(fname, conf) return conf if $conffile_check[fname]==1 $conffile_check[fname]=1 section = "default" - if not File.exists?(fname) + if ! File.exists?(fname) return conf end file = File.open(fname, "r") @@ -49,7 +49,7 @@ def readconfigfile(fname, conf) conf[m[1]] = Hash.new next end - next if not /=/.match(line) + next if ! /=/.match(line) m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line) if m[1]=="include" Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) } @@ -127,6 +127,9 @@ end ################################################################################ def init_system(test_prog) + begin + line = $sp.gets() + end while line!=nil $sp.print("echo off \r") print("DBG i: " + "echo off \r"+"\n") if $debug sleep 0.1 @@ -135,6 +138,29 @@ def init_system(test_prog) sleep 1 end +################################################################################ +# wait_for_prompt # +################################################################################ + +def wait_for_prompt(prompt) + prompt = /[\s]*#{prompt}[\s]*/ if(prompt.class == String) + start_time = Time.now.to_i + acc = '' + begin + line = $sp.gets() + puts("DBG got (#{__LINE__}): "+line) if $debug && line + line = "" if line==nil + if /^(Error:|Crypto-VS).*/.match(line) + puts line + return false + end + if (Time.now.to_i- start_time) > $max_timeout + return false + end + acc += line + end while ! m=prompt.match(acc) + return m +end ################################################################################ # screen_progress # @@ -161,7 +187,7 @@ def add_test(a,b) puts line return false end - end while not /[\s]*enter a:[\s]*/.match(line) + end while ! /[\s]*enter a:[\s]*/.match(line) $sp.print(a.to_s(16)+" ") begin line = $sp.gets() @@ -171,7 +197,7 @@ def add_test(a,b) puts line return false end - end while not /[\s]*enter b:[\s]*/.match(line) + end while ! /[\s]*enter b:[\s]*/.match(line) $sp.print(b.to_s(16)+" ") begin line = $sp.gets() @@ -181,7 +207,7 @@ def add_test(a,b) puts line return false end - end while not m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line) + end while ! m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\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) @@ -210,7 +236,7 @@ def mul_test(a,b) puts line return false end - end while not /[\s]*enter a:[\s]*/.match(line) + end while ! /[\s]*enter a:[\s]*/.match(line) $sp.print(a.to_s(16)+" ") begin line = $sp.gets() @@ -220,7 +246,7 @@ def mul_test(a,b) puts line return false end - end while not /[\s]*enter b:[\s]*/.match(line) + end while ! /[\s]*enter b:[\s]*/.match(line) $sp.print(b.to_s(16)+" ") begin line = $sp.gets() @@ -230,7 +256,7 @@ def mul_test(a,b) puts line return false end - end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+\*[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line) + end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+\*[\s]+([+-]?[0-9a-fA-F]*)[\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) @@ -249,59 +275,42 @@ end ################################################################################ # add_scale_test # ################################################################################ +def add_scale_test_dummy(a, b, scale) + should = a + (b<<(8*scale)) + printf("[dummy] %s + %s <<8*%04x = %s\n",a.to_s(16), b.to_s(16), scale, should.to_s(16)) +end def add_scale_test(a, b, scale) - begin - line = $sp.gets() - line = "" if line==nil - puts("DBG got (#{__LINE__}): "+line) if $debug - if /^Error:.*/.match(line) - puts line - return false - end - end while not /[\s]*enter a:[\s]*/.match(line) + m = wait_for_prompt("enter a:") + return false if !m puts("DBG put (#{__LINE__}): "+a.to_s(16)+" ") if $debug $sp.print(a.to_s(16)+" ") - begin - line = $sp.gets() - line = "" if line==nil - puts("DBG got (#{__LINE__}): "+line) if $debug - if /^Error:.*/.match(line) - puts line - return false - end - end while not /[\s]*enter b:[\s]*/.match(line) + m = wait_for_prompt("enter b:") + return false if !m + puts("DBG put (#{__LINE__}): "+b.to_s(16)+" ") if $debug $sp.print(b.to_s(16)+" ") - begin - line = $sp.gets() - line = "" if line==nil - puts("DBG got (#{__LINE__}): "+line) if $debug - if /^Error:.*/.match(line) - puts line - return false - end - end while not /[\s]*enter scale:[\s]*/.match(line) + m = wait_for_prompt("enter scale:") + return false if !m + puts("DBG put (#{__LINE__}): "+scale.to_s(10)+" ") if $debug $sp.print(scale.to_s(10)+"\r") - begin - line = $sp.gets() - line = "" if line==nil - puts("DBG got (#{__LINE__}): "+line) if $debug - if /^Error:.*/.match(line) - puts line - return false - end - end while not m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\s]*<<8\*[\s]*([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line) + should = a + (b<<(8*scale)) + m = wait_for_prompt(/[\s]*([-]?[0-9a-fA-F]+)[\s]+\+[\s]+([+-]?[0-9a-fA-F]+)[\s]*<<8\*[\s]*([+-]?[0-9a-fA-F]+)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/) + if !m + $logfile.printf("[fail (CRASH)]:") + $logfile.printf(" ; should %s + %s << 8*%s = %s\n", a.to_s(16), b.to_s(16), scale.to_s(16), should.to_s(16)) + return false + end + line = m[0] a_ = m[1].to_i(16) b_ = m[2].to_i(16) s_ = m[3].to_i(16) c_ = m[4].to_i(16) line.chomp! - should = a + (b<<(8*scale)) if(a_== a && b_ == b && s_ == scale && c_ == should ) $logfile.printf("[pass]: %s\n", line) return true else - $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (scale==s_)?"":"s",(c_==should)?"":"c",line) + $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (scale==s_)?"":"s",(c_==should)?"":"c",line) $logfile.printf(" ; should %s + %s << 8*%s = %s\n", a.to_s(16), b.to_s(16), scale.to_s(16), should.to_s(16)) return false end @@ -321,7 +330,7 @@ def square_test(a) puts line return false end - end while not /[\s]*enter a:[\s]*/.match(line) + end while ! /[\s]*enter a:[\s]*/.match(line) $sp.print(a.to_s(16)+" ") begin line = $sp.gets() @@ -331,7 +340,7 @@ def square_test(a) puts line return false end - end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line) + end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line) a_ = m[1].to_i(16) c_ = m[2].to_i(16) line.chomp! @@ -359,7 +368,7 @@ def reduce_test(a,b) puts line return false end - end while not /[\s]*enter a:[\s]*/.match(line) + end while ! /[\s]*enter a:[\s]*/.match(line) $sp.print(a.to_s(16)+" ") begin line = $sp.gets() @@ -369,7 +378,7 @@ def reduce_test(a,b) puts line return false end - end while not /[\s]*enter b:[\s]*/.match(line) + end while ! /[\s]*enter b:[\s]*/.match(line) $sp.print(b.to_s(16)+" ") line='' begin @@ -381,7 +390,7 @@ def reduce_test(a,b) puts line return false end - end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line) + end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\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) @@ -410,7 +419,7 @@ def expmod_test(a,b,c) puts line return false end - end while not /[\s]*enter a:[\s]*/.match(line) + end while ! /[\s]*enter a:[\s]*/.match(line) $sp.print(a.to_s(16)+" ") begin line = $sp.gets() @@ -420,7 +429,7 @@ def expmod_test(a,b,c) puts line return false end - end while not /[\s]*enter b:[\s]*/.match(line) + end while ! /[\s]*enter b:[\s]*/.match(line) $sp.print(b.to_s(16)+" ") begin line = $sp.gets() @@ -430,7 +439,7 @@ def expmod_test(a,b,c) puts line return false end - end while not /[\s]*enter c:[\s]*/.match(line) + end while ! /[\s]*enter c:[\s]*/.match(line) $sp.print(c.to_s(16)+" ") line='' begin @@ -442,7 +451,7 @@ def expmod_test(a,b,c) puts line return false end - end while not m=/[\s]*([+-]?[0-9a-fA-F]*)\*\*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line) + end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)\*\*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\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) @@ -472,7 +481,7 @@ def gcdext_test(a,b) puts line return false end - end while not /[\s]*enter a:[\s]*/.match(line) + end while ! /[\s]*enter a:[\s]*/.match(line) $sp.print(a.to_s(16)+" ") begin line = $sp.gets() @@ -482,7 +491,7 @@ def gcdext_test(a,b) puts line return false end - end while not /[\s]*enter b:[\s]*/.match(line) + end while ! /[\s]*enter b:[\s]*/.match(line) $sp.print(b.to_s(16)+" ") line='' begin @@ -495,7 +504,7 @@ def gcdext_test(a,b) puts line return false end - end while not m=/gcdext\([\s]*([+-]?[0-9a-fA-F]*)[\s]*,[\s]*([+-]?[0-9a-fA-F]*)[\s]*\)[\s]*=> a = ([+-]?[0-9a-fA-F]+); b = ([+-]?[0-9a-fA-F]+); gcd = ([+-]?[0-9a-fA-F]+)/.match(line) + end while ! m=/gcdext\([\s]*([+-]?[0-9a-fA-F]*)[\s]*,[\s]*([+-]?[0-9a-fA-F]*)[\s]*\)[\s]*=> a = ([+-]?[0-9a-fA-F]+); b = ([+-]?[0-9a-fA-F]+); gcd = ([+-]?[0-9a-fA-F]+)/.match(line) a_ = m[1].to_i(16) b_ = m[2].to_i(16) c_ = m[3].to_i(16) @@ -563,7 +572,7 @@ def run_test_add_scale(skip=0) length_b_B = skip+1 begin $size = length_a_B - (0..16).each do |i| + (0..4).each do |i| scales = [0, 300] 16.times { scales << rand(301) } scales.sort! @@ -576,12 +585,12 @@ def run_test_add_scale(skip=0) screen_progress(v) end end - (0..16).each do |i| + (0..4).each do |i| scales = [0, 300] 16.times { scales << rand(301) } scales.sort! scales.each do |scale| - b_size = rand(length_b_B+1) + b_size = rand(length_b_B+1)+1 a = rand(256**length_a_B) b = rand(256**b_size) v = add_scale_test(a, b, scale) @@ -595,6 +604,39 @@ def run_test_add_scale(skip=0) end while length_a_B<4096/8 end +def run_test_add_scale_dummy(skip=0) + length_a_B = skip+1 + length_b_B = skip+1 + begin + $size = length_a_B + (0..4).each do |i| + scales = [0, 300] + 16.times { scales << rand(301) } + scales.sort! + scales.each do |scale| + a = rand(256**length_a_B) + b = rand(256**length_a_B) + v = add_scale_test_dummy(a, b, scale) + v = add_scale_test_dummy(b, a, scale) + end + end + (0..4).each do |i| + scales = [0, 300] + 16.times { scales << rand(301) } + scales.sort! + scales.each do |scale| + b_size = rand(length_b_B+1) + a = rand(256**length_a_B) + b = rand(256**b_size) + v = add_scale_test_dummy(a, b, scale) + v = add_scale_test_dummy(b, a, scale) + end + end + length_a_B += 10 + length_b_B += 10 + end while length_a_B<4096/8 +end + ################################################################################ # run_test_mul # ################################################################################ @@ -733,6 +775,36 @@ def run_test_gcdext(skip=0) end while length_a_B<4096/8 end +def init_serialport(conf) + puts("serial port interface version: " + SerialPort::VERSION); + $linewidth = 64 + $linepos = 0 + $testno = 0 + params = { "baud" => conf["PORT"]["baud"].to_i, + "data_bits" => conf["PORT"]["databits"].to_i, + "stop_bits" => conf["PORT"]["stopbits"].to_i, + "parity" => SerialPort::NONE } + params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd" + params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even" + params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark" + params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space" + + puts("\nPort: "+conf["PORT"]["port"]+"@" + + params["baud"].to_s + + " " + + params["data_bits"].to_s + + conf["PORT"]["paraty"][0,1].upcase + + params["stop_bits"].to_s + + "\n") + + $sp = SerialPort.new(conf["PORT"]["port"], params) + + $sp.read_timeout=1000; # 5 minutes + $sp.flow_control = SerialPort::SOFT + + reset_system() +end + ################################################################################ # MAIN # ################################################################################ @@ -746,34 +818,9 @@ conf = readconfigfile("testport.conf", conf) conf = readconfigfile(opts["f"], conf) if opts["f"] #puts conf.inspect +init_serialport(conf) -puts("serial port interface version: " + SerialPort::VERSION); -$linewidth = 64 -$linepos = 0 -$testno = 0 -params = { "baud" => conf["PORT"]["baud"].to_i, - "data_bits" => conf["PORT"]["databits"].to_i, - "stop_bits" => conf["PORT"]["stopbits"].to_i, - "parity" => SerialPort::NONE } -params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd" -params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even" -params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark" -params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space" - -puts("\nPort: "+conf["PORT"]["port"]+"@" + - params["baud"].to_s + - " " + - params["data_bits"].to_s + - conf["PORT"]["paraty"][0,1].upcase + - params["stop_bits"].to_s + - "\n") - -$sp = SerialPort.new(conf["PORT"]["port"], params) - -$sp.read_timeout=1000; # 5 minutes -$sp.flow_control = SerialPort::SOFT - -reset_system() + $max_timeout = 5 * 60 if opts['d'] $debug = true @@ -787,12 +834,16 @@ if File.exists?(logfilename) i+=1 end while(File.exists?(logfilename)) while(i>2) do - File.move(sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt'), - sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt'), true) + n1 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt') + n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt') + File.rename(n1, n2) + printf("%s -> %s\n", n1, n2) i-=1 end - File.move(sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt'), - sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt'), true) + n1 = sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt') + n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt') + File.rename(n1, n2) + printf("%s -> %s\n", n1, n2) logfilename = conf['PORT']['testlogbase']+'bigint.txt' end $logfile = File.open(logfilename, 'w')