]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - host/bigint_test.rb
quickfix(tm) applied
[avr-crypto-lib.git] / host / bigint_test.rb
index eabaec515390bc3f50f156c4bf69e98f13bb7d25..df16e7d54f084392ee3b52761410a75c3a5e6f1f 100644 (file)
@@ -246,6 +246,68 @@ def mul_test(a,b)
   return false
 end
 
+################################################################################
+# add_scale_test                                                               #
+################################################################################
+
+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)
+  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)
+  $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)
+  $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)
+  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(" ; 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
+  return false
+end
+
 ################################################################################
 # square_test                                                                  #
 ################################################################################
@@ -492,6 +554,47 @@ def run_test_add(skip=0)
   end while length_a_B<4096/8
 end
 
+################################################################################
+# run_test_add_scale                                                           #
+################################################################################
+
+def run_test_add_scale(skip=0)
+  length_a_B = skip+1
+  length_b_B = skip+1
+  begin
+    $size = length_a_B
+    (0..16).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(a, b, scale)
+        screen_progress(v)
+        v = add_scale_test(b, a, scale)
+        screen_progress(v)
+      end
+    end
+    (0..16).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(a, b, scale)
+        screen_progress(v)      
+        v = add_scale_test(b, a, scale)
+        screen_progress(v)
+      end
+    end
+    length_a_B += 10
+    length_b_B += 10
+  end while length_a_B<4096/8
+end
+
 ################################################################################
 # run_test_mul                                                                 #
 ################################################################################
@@ -702,12 +805,14 @@ $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
 tests = Hash.new
 tests['a'] = proc {|x| run_test_add(x) }
 tests['m'] = proc {|x| run_test_mul(x) }
+tests['x'] = proc {|x| run_test_add_scale(x) }
 tests['s'] = proc {|x| run_test_square(x) }
 tests['r'] = proc {|x| run_test_reduce(x) }
 tests['e'] = proc {|x| run_test_expmod(x) }
 tests['g'] = proc {|x| run_test_gcdext(x) }
 init_str = Hash.new
 init_str['a'] = 'add-test'
+init_str['x'] = 'add-scale-test'
 init_str['m'] = 'mul-test'
 init_str['s'] = 'square-test'
 init_str['r'] = 'reduce-test'