]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - host/bigint_test.rb
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / host / bigint_test.rb
index 42f6080d84892af524c6f3a525f79d3429ba5980..032ae9e3cf1f4bee7e38eac6781dfc854b0922bf 100644 (file)
@@ -2,7 +2,7 @@
 # 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
@@ -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) }
@@ -80,6 +80,7 @@ end
 ################################################################################
 
 def gcdext(x,y)
+  return  [0, 0, 0] if(x == 0 || y == 0)
   g=1
   while(x&1==0 && y&1==0) do
     x>>=1
@@ -158,7 +159,7 @@ def wait_for_prompt(prompt)
       return false
     end
   acc += line
-  end while not m=prompt.match(acc)
+  end while ! m=prompt.match(acc)
   return m
 end
 
@@ -171,7 +172,7 @@ def screen_progress(v)
   end
   putc((v)?('*'):('!'))
   $testno += 1
-  $linepos = ($linepos+1)%$linewidth
+  $linepos = ($linepos+1) % $linewidth
 end
 
 ################################################################################
@@ -187,7 +188,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()
@@ -197,7 +198,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()
@@ -207,7 +208,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)
@@ -236,7 +237,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()
@@ -246,7 +247,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()
@@ -256,7 +257,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)
@@ -330,7 +331,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()
@@ -340,7 +341,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!
@@ -368,7 +369,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()
@@ -378,7 +379,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
@@ -390,7 +391,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)
@@ -407,11 +408,12 @@ def reduce_test(a,b)
 end
 
 ################################################################################
-# expmod_test                                                                  #
+# mulmod_test                                                                  #
 ################################################################################
 
-def expmod_test(a,b,c)
+def mulmod_test(a,b,c)
   begin
+    printf("[testing] mulmod(%#x, %#x, %#x)\n",a,b,c) if $debug
     line = $sp.gets()
     line = "" if line==nil
     puts("DBG got: "+line) if $debug
@@ -419,7 +421,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()
@@ -429,7 +431,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()
@@ -439,7 +441,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
@@ -451,7 +453,72 @@ 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)
+    m = /[\s]*\([\s]*([+-]?[0-9a-fA-F]*)[\s]*\*[\s]*([+-]?[0-9a-fA-F]*)[\s]*\)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
+    puts("DBG: line did not match pattern (" + line + ")") if !m && $debug
+  end while ! m 
+  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.chomp!
+  if(a_== a && b_ == b && c_ == c && d_ == (a * b % c) )
+    $logfile.printf("[pass]: %s\n", line)
+    return true
+  else
+    $logfile.printf("[fail (%s%s%s%s)]: %s", (a == a_) ? '' : 'a', (b == b_) ? '' : 'b', (c_ == c) ? '' : 'c', (d_== (a * b % c)) ? '' : 'd',line)
+    $logfile.printf(" ; should (%s * %s) %% %s = %s\n", a.to_s(16), b.to_s(16), c.to_s(16), (a * b % c).to_s(16))
+    return false
+  end
+  return false
+end
+
+################################################################################
+# expmod_test                                                                  #
+################################################################################
+
+def expmod_test(a,b,c)
+  begin
+    printf("[testing] expmod(%#x, %#x, %#x)\n",a,b,c) if $debug
+    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)+" ")
+  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 c:[\s]*/.match(line)
+  $sp.print(c.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]+)\*\*([+-]?[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)
@@ -473,6 +540,7 @@ end
 ################################################################################
 
 def gcdext_test(a,b)
+  $logfile.printf("[testing] gcdext(%s, %s)\n", a.to_s(16), b.to_s(16))
   begin
     line = $sp.gets()
     line = "" if line==nil
@@ -481,7 +549,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()
@@ -491,7 +559,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
@@ -504,7 +572,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)
@@ -672,6 +740,24 @@ def run_test_mul(skip=0)
   end while length_a_B<4096/8
 end
 
+################################################################################
+# run_test_mul_word                                                                 #
+################################################################################
+
+def run_test_mul_word(skip=0)
+  length_a_B = skip+1
+  length_b_B = skip+1
+  begin
+    $size = length_a_B
+    (0..255).each do |i|
+      a = rand(256 ** length_a_B) 
+      v = mul_test(a, i)
+      screen_progress(v)   
+    end
+    length_a_B += 1
+  end while length_a_B < 4096 / 8
+end
+
 ################################################################################
 # run_test_square                                                              #
 ################################################################################
@@ -721,6 +807,66 @@ end
 ################################################################################
 
 def run_test_expmod(skip=0)
+  length_a_B = skip + 1
+  length_b_B = skip + 1
+  length_c_B = skip + 1
+  begin
+    $size = length_a_B
+    (0..16).each do |i|
+      a = rand(256 ** length_a_B)
+      b = rand(256 ** length_b_B) + 1
+      c = rand(256 ** length_c_B) + 1
+      v = expmod_test(a, b, c)
+      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 
+      c = rand(256 ** b_size) + 1
+      v = expmod_test(a, b, c)
+      screen_progress(v)      
+      end
+    length_a_B += 1
+    length_b_B += 1
+  end while length_a_B<4096/8
+end
+
+################################################################################
+# run_test_expmodmont                                                          #
+################################################################################
+
+def run_test_expmodmont(skip=0)
+  length_a_B = skip + 1
+  length_b_B = skip + 1
+  length_c_B = skip + 1
+  begin
+    $size = length_a_B
+    (0..16).each do |i|
+      a = rand(256 ** length_a_B)
+      b = rand(256 ** length_b_B) + 1
+      c = rand(256 ** length_c_B) / 2 * 2 +1
+      v = expmod_test(a, b, c)
+      screen_progress(v)
+      end
+    (0..16).each do |i|
+      b_size = rand(length_b_B + 10)
+      a = rand(256 ** length_a_B)
+      b = rand(256 ** b_size) + 1 
+      c = rand(256 ** b_size) / 2 * 2 +1
+      v = expmod_test(a, b, c)
+      screen_progress(v)      
+      end
+    length_a_B += 1
+    length_b_B += 1
+  end while length_a_B<4096/8
+end
+
+################################################################################
+# run_test_mulmod                                                              #
+################################################################################
+
+def run_test_mulmod(skip=0)
   length_a_B = skip+1
   length_b_B = skip+1
   length_c_B = skip+1
@@ -728,21 +874,26 @@ def run_test_expmod(skip=0)
     $size = length_a_B
     (0..16).each do |i|
       a = rand(256**length_a_B)
-      b = rand(256**length_b_B)+1
-      c = rand(256**length_c_B)+1
-      v = expmod_test(a, b, c)
+      b = rand(256**length_b_B)
+      c = (rand(256**length_c_B) / 2 * 2) + 1
+      a %= c
+      b %= c
+      v = mulmod_test(a, b, c)
       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 
-      c = rand(256**b_size)+1
-      v = expmod_test(a, b, c)
+      b = rand(256**b_size)
+      c = (rand(256**length_c_B) / 2 * 2) + 1
+      a %= c
+      b %= c
+      v = mulmod_test(a, b, c)
       screen_progress(v)      
       end
     length_a_B += 1
     length_b_B += 1
+    length_c_B += 1
   end while length_a_B<4096/8
 end
 
@@ -834,17 +985,21 @@ 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')
 printf("logfile: %s\n", logfilename)
-
+$logfile.sync = true
 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
@@ -852,18 +1007,24 @@ $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['M'] = proc {|x| run_test_mulmod(x) }
+tests['n'] = proc {|x| run_test_mul_word(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['E'] = proc {|x| run_test_expmodmont(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['M'] = 'mul-mont-test'
+init_str['n'] = 'mul-word-test'
 init_str['s'] = 'square-test'
 init_str['r'] = 'reduce-test'
 init_str['e'] = 'expmod-test'
+init_str['E'] = 'expmod-mont-test'
 init_str['g'] = 'gcdext-test'
 
 srand(0xdeadbeef)
@@ -879,7 +1040,7 @@ if opts['a']
     end  
   end
 else
-  'amsre'.each_char do |x|
+  'amsrMeE'.each_char do |x|
     if tests[x]
       puts init_str[x]
       init_system(init_str[x])
@@ -889,6 +1050,6 @@ else
     end  
   end
 end
-
+1
 
 $logile.close()