]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - host/bigint_test.rb
adding modulo-reduction to bigint
[avr-crypto-lib.git] / host / bigint_test.rb
index 9048f896b9750190960786779481d435bd5e8d4f..71f11955177dfb49f13c5032dac55bcebac4a3d2 100644 (file)
@@ -234,6 +234,55 @@ def square_test(a)
   return false
 end
 
+################################################################################
+# reduce_test                                                                  #
+################################################################################
+
+def reduce_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 not /[\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 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) 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]*=[\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)
+  line.chomp!
+  if(a_== a && b_ == b && c_ == (a%b))
+    $logfile.printf("[pass]: %s\n", line)
+    return true
+  else
+    $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
+    $logfile.printf(" ; should %s %% %s = %s\n", a.to_s(16), b.to_s(16), (a%b).to_s(16))
+    return false
+  end
+  return false
+end
+
 ################################################################################
 # run_test_add                                                                 #
 ################################################################################
@@ -322,6 +371,33 @@ def run_test_square(skip=0)
   end while length_a_B<4096/8
 end
 
+################################################################################
+# run_test_reduce                                                              #
+################################################################################
+
+def run_test_reduce(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 = reduce_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 = reduce_test(a, b)
+      screen_progress(v)      
+      end
+    length_a_B += 1
+    length_b_B += 1
+  end while length_a_B<4096/8
+end
+
 ################################################################################
 # MAIN                                                                         #
 ################################################################################
@@ -395,10 +471,12 @@ tests = Hash.new
 tests['a'] = proc {|x| run_test_add(x) }
 tests['m'] = proc {|x| run_test_mul(x) }
 tests['s'] = proc {|x| run_test_square(x) }
+tests['r'] = proc {|x| run_test_reduce(x) }
 init_str = Hash.new
 init_str['a'] = 'add-test'
 init_str['m'] = 'mul-test'
 init_str['s'] = 'square-test'
+init_str['r'] = 'reduce-test'
 
 srand(0xdeadbeef)
 
@@ -413,7 +491,7 @@ if opts['a']
     end  
   end
 else
-  'ams'.each_char do |x|
+  'amsr'.each_char do |x|
     if tests[x]
       puts init_str[x]
       init_system(init_str[x])