]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/bigint2_test.rb
first publication of bigint2-dev
[avr-crypto-lib.git] / host / bigint2_test.rb
1 #!/usr/bin/ruby
2 # bigint_test.rb
3 =begin
4     This file is part of the AVR-Crypto-Lib.
5     Copyright (C) 2008, 2009  Daniel Otte (daniel.otte@rub.de)
6
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 =end
20
21 $debug = true
22 $debug = false
23 require 'rubygems'
24 require 'serialport'
25 require 'getopt/std'
26 require 'fileutils'
27 require 'date'
28 $buffer_size = 0
29 $conffile_check = Hash.new
30 $conffile_check.default = 0
31
32 def max(a, b)
33   return a > b ? a : b
34 end
35
36 def min(a, b)
37   return a < b ? a : b
38 end
39
40 ################################################################################
41 # readconfigfile                                                               #
42 ################################################################################
43
44 def readconfigfile(fname, conf)
45   return conf if $conffile_check[fname]==1
46   $conffile_check[fname]=1
47   section = "default"
48   if ! File.exists?(fname)
49     return conf
50   end
51   file = File.open(fname, "r")
52   until file.eof
53     line = file.gets()
54         next if /[\s]*#/.match(line)
55         if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
56           section=m[1]
57           conf[m[1]] = Hash.new
58           next
59         end
60         next if ! /=/.match(line)
61         m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
62         if m[1]=="include"
63           Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
64         else
65           conf[section][m[1]] = m[2]
66         end
67   end
68   file.close()
69   return conf
70 end
71
72 ################################################################################
73 # expmod                                                                       #
74 ################################################################################
75
76 def expmod(base, power, mod)
77   result = 1
78   while power > 0
79     result = (result * base) % mod if power & 1 == 1
80     base = (base * base) % mod
81     power >>= 1;
82   end
83   return result
84 end
85
86 ################################################################################
87 # gcdext                                                                 #
88 ################################################################################
89
90 def gcdext(x,y)
91   return  [0, 0, 0] if(x == 0 || y == 0)
92   g=1
93   while(x&1==0 && y&1==0) do
94     x>>=1
95     y>>=1
96     g<<=1
97   end
98   u=x; v=y; a=1; b=0; c=0; d=1
99   begin
100     while(u&1==0) do
101       if(a%2==1 || b%2==1)
102         a += y
103         b -= x
104       end
105       u>>=1; a>>=1; b>>=1
106     end
107     while(v&1==0) do
108       if(c%2==1 || d%2==1)
109         c += y
110         d -= x
111       end
112       v>>=1; c>>=1; d>>=1;
113     end
114     if(u>=v)
115       u -= v; a-=c; b-=d
116     else
117       v -= u; c-=a; d-=b
118     end
119   end while(u!=0)
120   return[g*v, c, d]
121 end
122
123 ################################################################################
124 # reset_system                                                                 #
125 ################################################################################
126
127 def reset_system
128   $sp.print("exit\r")
129   sleep 0.1
130   $sp.print("exit\r")
131   sleep 0.1
132 end
133
134 ################################################################################
135 # init_system                                                                  #
136 ################################################################################
137
138 def init_system(test_prog)
139   begin
140     line = $sp.gets()
141   end while line!=nil
142   $sp.print("echo off \r")
143   print("DBG i: " + "echo off \r"+"\n") if $debug
144   sleep 0.1
145   $sp.print("#{test_prog}\r")
146   print("DBG i: " + "#{test_prog} \r"+"\n") if $debug
147   sleep 1
148 end
149
150 ################################################################################
151 # wait_for_prompt                                                                  #
152 ################################################################################
153
154 def wait_for_prompt(prompt)
155   prompt = /[\s]*#{prompt}[\s]*/ if(prompt.class == String)
156   start_time = Time.now.to_i
157   acc = ''
158   begin
159     line = $sp.gets()
160     puts("DBG got (#{__LINE__}): "+line) if $debug && line
161     line = "" if line==nil
162     if /^(Error:|Crypto-VS).*/.match(line)
163       puts line
164       return false
165     end
166     if (Time.now.to_i- start_time) > $max_timeout
167       return false
168     end
169   acc += line
170   end while ! m=prompt.match(acc)
171   return m
172 end
173
174 ################################################################################
175 # screen_progress                                                              #
176 ################################################################################
177 def screen_progress(v)
178   if $linepos == 0
179     printf("\n%5d [%04d]: ", $testno, $size)
180   end
181   putc((v)?('*'):('!'))
182   $testno += 1
183   $linepos = ($linepos+1) % $linewidth
184 end
185
186 ################################################################################
187 # add_test                                                                     #
188 ################################################################################
189
190 def add_test(a,b)
191   begin
192     line = $sp.gets()
193     line = "" if line==nil
194     puts("DBG got: "+line) if $debug
195     if /^Error:.*/.match(line)
196       puts line
197       return false
198     end
199   end while ! /[\s]*enter a:[\s]*/.match(line)
200   $sp.print(a.to_s(16)+" ")
201   begin
202     line = $sp.gets()
203     line = "" if line==nil
204     puts("DBG got: "+line) if $debug
205     if /^Error:.*/.match(line)
206       puts line
207       return false
208     end
209   end while ! /[\s]*enter b:[\s]*/.match(line)
210   $sp.print(b.to_s(16)+" ")
211   begin
212     line = $sp.gets()
213     line = "" if line==nil
214     puts("DBG got: "+line) if $debug
215     if /^Error:.*/.match(line)
216       puts line
217       return false
218     end
219   end while ! m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
220   a_ = m[1].to_i(16)
221   b_ = m[2].to_i(16)
222   c_ = m[3].to_i(16)
223   line.chomp!
224   if(a_== a && b_ == b && c_ == (a+b))
225     $logfile.printf("[pass (%d)]: %s\n", $testno, line)
226     return true
227   else
228     $logfile.printf("[fail (%s%s%s) (%d)]: %s", 
229       (a == a_) ? "" : "a", 
230       (b == b_) ? "" : "b", 
231       (c_ == a + b) ?  "" : "c", $testno, line)
232     $logfile.printf(" ; should %s + %s = %s\n", a.to_s(16), b.to_s(16), (a+b).to_s(16))
233     return false
234   end
235   return false
236 end
237
238 ################################################################################
239 # sub_test                                                                     #
240 ################################################################################
241
242 def sub_test(a, b)
243   begin
244     line = $sp.gets()
245     line = "" if line==nil
246     puts("DBG got: "+line) if $debug
247     if /^Error:.*/.match(line)
248       puts line
249       return false
250     end
251   end while ! /[\s]*enter a:[\s]*/.match(line)
252   $sp.print(a.to_s(16)+" ")
253   begin
254     line = $sp.gets()
255     line = "" if line==nil
256     puts("DBG got: "+line) if $debug
257     if /^Error:.*/.match(line)
258       puts line
259       return false
260     end
261   end while ! /[\s]*enter b:[\s]*/.match(line)
262   $sp.print(b.to_s(16)+" ")
263   begin
264     line = $sp.gets()
265     line = "" if line==nil
266     puts("DBG got: "+line) if $debug
267     if /^Error:.*/.match(line)
268       puts line
269       return false
270     end
271   end while ! m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\-[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
272   a_ = m[1].to_i(16)
273   b_ = m[2].to_i(16)
274   c_ = m[3].to_i(16)
275   line.chomp!
276   if(a_== a && b_ == b && c_ == (a - b))
277     $logfile.printf("[pass (%d)]: %s\n", $testno, line)
278     return true
279   else
280     $logfile.printf("[fail (%s%s%s) (%d)]: %s", 
281       (a == a_) ? "" : "a", 
282       (b == b_) ? "" : "b", 
283       (c_ == a - b) ? "" : "c", $testno, line)
284     $logfile.printf(" ; should %s - %s = %s\n", a.to_s(16), b.to_s(16), (a - b).to_s(16))
285     return false
286   end
287   return false
288 end
289
290 ################################################################################
291 # mul_test                                                                     #
292 ################################################################################
293
294 def mul_test(a,b)
295   begin
296     line = $sp.gets()
297     line = "" if line==nil
298     puts("DBG got: "+line) if $debug
299     if /^Error:.*/.match(line)
300       puts line
301       return false
302     end
303   end while ! /[\s]*enter a:[\s]*/.match(line)
304   $sp.print(a.to_s(16)+" ")
305   begin
306     line = $sp.gets()
307     line = "" if line==nil
308     puts("DBG got: "+line) if $debug
309     if /^Error:.*/.match(line)
310       puts line
311       return false
312     end
313   end while ! /[\s]*enter b:[\s]*/.match(line)
314   $sp.print(b.to_s(16)+" ")
315   begin
316     line = $sp.gets()
317     line = "" if line==nil
318     puts("DBG got: "+line) if $debug
319     if /^Error:.*/.match(line)
320       puts line
321       return false
322     end
323   end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+\*[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
324   a_ = m[1].to_i(16)
325   b_ = m[2].to_i(16)
326   c_ = m[3].to_i(16)
327   line.chomp!
328   if(a_== a && b_ == b && c_ == (a*b))
329     $logfile.printf("[pass]: %s\n", line)
330     return true
331   else
332     $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a*b)?"":"c",line)
333     $logfile.printf(" ; should %s * %s = %s\n", a.to_s(16), b.to_s(16), (a*b).to_s(16))
334     return false
335   end
336   return false
337 end
338
339 ################################################################################
340 # add_scale_test                                                               #
341 ################################################################################
342 def add_scale_test_dummy(a, b, scale)
343   should = a + (b<<(8*scale))
344   printf("[dummy] %s + %s <<8*%04x = %s\n",a.to_s(16), b.to_s(16), scale, should.to_s(16))
345 end
346
347 def add_scale_test(a, b, scale)
348   m = wait_for_prompt("enter a:")
349   return false if !m 
350   puts("DBG put (#{__LINE__}): "+a.to_s(16)+" ") if $debug
351   $sp.print(a.to_s(16)+" ")
352   m = wait_for_prompt("enter b:")
353   return false if !m 
354   puts("DBG put (#{__LINE__}): "+b.to_s(16)+" ") if $debug
355   $sp.print(b.to_s(16)+" ")
356   m = wait_for_prompt("enter scale:")
357   return false if !m 
358   puts("DBG put (#{__LINE__}): "+scale.to_s(10)+" ") if $debug
359   $sp.print(scale.to_s(10)+"\r")
360   should = a + (b<<(8*scale))
361   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]+)/)
362   if !m 
363     $logfile.printf("[fail (CRASH)]:")
364     $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))
365     return false
366   end 
367   line = m[0]
368   a_ = m[1].to_i(16)
369   b_ = m[2].to_i(16)
370   s_ = m[3].to_i(16)
371   c_ = m[4].to_i(16)
372   line.chomp!
373   if(a_== a && b_ == b && s_ == scale && c_ == should )
374     $logfile.printf("[pass]: %s\n", line)
375     return true
376   else
377     $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (scale==s_)?"":"s",(c_==should)?"":"c",line)
378     $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))
379     return false
380   end
381   return false
382 end
383
384 ################################################################################
385 # square_test                                                                  #
386 ################################################################################
387
388 def square_test(a)
389   begin
390     line = $sp.gets()
391     line = "" if line==nil
392     puts("DBG got: "+line) if $debug
393     if /^Error:.*/.match(line)
394       puts line
395       return false
396     end
397   end while ! /[\s]*enter a:[\s]*/.match(line)
398   $sp.print(a.to_s(16)+" ")
399   begin
400     line = $sp.gets()
401     line = "" if line==nil
402     puts("DBG got: "+line) if $debug
403     if /^Error:.*/.match(line)
404       puts line
405       return false
406     end
407   end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
408   a_ = m[1].to_i(16)
409   c_ = m[2].to_i(16)
410   line.chomp!
411   if(a_== a && c_ == (a**2))
412     $logfile.printf("[pass]: %s\n", line)
413     return true
414   else
415     $logfile.printf("[fail (%s%s)]: %s", (a==a_)?"":"a", (c_==a**2)?"":"c",line)
416     $logfile.printf(" ; should %s **2 = %s\n", a.to_s(16), (a**2).to_s(16))
417     return false
418   end
419   return false
420 end
421
422 ################################################################################
423 # reduce_test                                                                  #
424 ################################################################################
425
426 def reduce_test(a,b)
427   begin
428     line = $sp.gets()
429     line = "" if line==nil
430     puts("DBG got: "+line) if $debug
431     if /^Error:.*/.match(line)
432       puts line
433       return false
434     end
435   end while ! /[\s]*enter a:[\s]*/.match(line)
436   $sp.print(a.to_s(16)+" ")
437   begin
438     line = $sp.gets()
439     line = "" if line==nil
440     puts("DBG got: "+line) if $debug
441     if /^Error:.*/.match(line)
442       puts line
443       return false
444     end
445   end while ! /[\s]*enter b:[\s]*/.match(line)
446   $sp.print(b.to_s(16)+" ")
447   line=''
448   begin
449     line_tmp = $sp.gets()
450     line_tmp = '' if line_tmp==nil
451     line += line_tmp
452     puts("DBG got: "+line) if $debug
453     if /^Error:.*/.match(line)
454       puts line
455       return false
456     end
457   end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
458   a_ = m[1].to_i(16)
459   b_ = m[2].to_i(16)
460   c_ = m[3].to_i(16)
461   line.strip!
462   if(a_== a && b_ == b && c_ == (a%b))
463     $logfile.printf("[pass (%d)]: %s\n", $testno, line)
464     return true
465   else
466     $logfile.printf("[fail (%s%s%s) (%d)]: %s", 
467       (a == a_) ? "" : "a", 
468       (b == b_) ? "" : "b", 
469       (c_ == a % b) ? "" : "c", $testno, line)
470     $logfile.printf(" ; should %s %% %s = %s\n", a.to_s(16), b.to_s(16), (a % b).to_s(16))
471     return false
472   end
473   return false
474 end
475
476 ################################################################################
477 # mulmod_test                                                                  #
478 ################################################################################
479
480 def mulmod_test(a,b,c)
481   begin
482     printf("[testing] mulmod(%#x, %#x, %#x)\n",a,b,c) if $debug
483     line = $sp.gets()
484     line = "" if line==nil
485     puts("DBG got: "+line) if $debug
486     if /^Error:.*/.match(line)
487       puts line
488       return false
489     end
490   end while ! /[\s]*enter a:[\s]*/.match(line)
491   $sp.print(a.to_s(16)+" ")
492   begin
493     line = $sp.gets()
494     line = "" if line==nil
495     puts("DBG got: "+line) if $debug
496     if /^Error:.*/.match(line)
497       puts line
498       return false
499     end
500   end while ! /[\s]*enter b:[\s]*/.match(line)
501   $sp.print(b.to_s(16)+" ")
502   begin
503     line = $sp.gets()
504     line = "" if line==nil
505     puts("DBG got: "+line) if $debug
506     if /^Error:.*/.match(line)
507       puts line
508       return false
509     end
510   end while ! /[\s]*enter c:[\s]*/.match(line)
511   $sp.print(c.to_s(16)+" ")
512   line=''
513   begin
514     line_tmp = $sp.gets()
515     line_tmp = '' if line_tmp==nil
516     line += line_tmp
517     puts("DBG got: "+line) if $debug
518     if /^Error:.*/.match(line)
519       puts line
520       return false
521     end
522     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)
523     puts("DBG: line did not match pattern (" + line + ")") if !m && $debug
524   end while ! m 
525   a_ = m[1].to_i(16)
526   b_ = m[2].to_i(16)
527   c_ = m[3].to_i(16)
528   d_ = m[4].to_i(16)
529   line.chomp!
530   if(a_== a && b_ == b && c_ == c && d_ == (a * b % c) )
531     $logfile.printf("[pass]: %s\n", line)
532     return true
533   else
534     $logfile.printf("[fail (%s%s%s%s)]: %s", (a == a_) ? '' : 'a', (b == b_) ? '' : 'b', (c_ == c) ? '' : 'c', (d_== (a * b % c)) ? '' : 'd',line)
535     $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))
536     return false
537   end
538   return false
539 end
540
541 ################################################################################
542 # expmod_test                                                                  #
543 ################################################################################
544
545 def expmod_test(a,b,c)
546   begin
547     printf("[testing] expmod(%#x, %#x, %#x)\n",a,b,c) if $debug
548     line = $sp.gets()
549     line = "" if line==nil
550     puts("DBG got: "+line) if $debug
551     if /^Error:.*/.match(line)
552       puts line
553       return false
554     end
555   end while ! /[\s]*enter a:[\s]*/.match(line)
556   $sp.print(a.to_s(16)+" ")
557   begin
558     line = $sp.gets()
559     line = "" if line==nil
560     puts("DBG got: "+line) if $debug
561     if /^Error:.*/.match(line)
562       puts line
563       return false
564     end
565   end while ! /[\s]*enter b:[\s]*/.match(line)
566   $sp.print(b.to_s(16)+" ")
567   begin
568     line = $sp.gets()
569     line = "" if line==nil
570     puts("DBG got: "+line) if $debug
571     if /^Error:.*/.match(line)
572       puts line
573       return false
574     end
575   end while ! /[\s]*enter c:[\s]*/.match(line)
576   $sp.print(c.to_s(16)+" ")
577   line=''
578   begin
579     line_tmp = $sp.gets()
580     line_tmp = '' if line_tmp == nil
581     line += line_tmp
582     puts("DBG got: "+line) if $debug
583     if /^Error:/.match(line)
584       puts line
585       return false
586     end
587   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)
588   a_ = m[1].to_i(16)
589   b_ = m[2].to_i(16)
590   c_ = m[3].to_i(16)
591   d_ = m[4].to_i(16)
592   line.chomp!
593   if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
594     $logfile.printf("[pass]: %s\n", line)
595     return true
596   else
597     $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', (c_==c)?'':'c', (d_==expmod(a,b,c))?'':'d',line)
598     $logfile.printf(" ; should %s**%s %% %s = %s\n", a.to_s(16), b.to_s(16), c.to_s(16), expmod(a,b,c).to_s(16))
599     return false
600   end
601   return false
602 end
603
604 ################################################################################
605 # gcdext_test                                                                  #
606 ################################################################################
607
608 def gcdext_test(a,b)
609   $logfile.printf("[testing] gcdext(%s, %s)\n", a.to_s(16), b.to_s(16))
610   begin
611     line = $sp.gets()
612     line = "" if line==nil
613     puts("DBG got: "+line) if $debug
614     if /^Error:.*/.match(line)
615       puts line
616       return false
617     end
618   end while ! /[\s]*enter a:[\s]*/.match(line)
619   $sp.print(a.to_s(16)+" ")
620   begin
621     line = $sp.gets()
622     line = "" if line==nil
623     puts("DBG got: "+line) if $debug
624     if /^Error:.*/.match(line)
625       puts line
626       return false
627     end
628   end while ! /[\s]*enter b:[\s]*/.match(line)
629   $sp.print(b.to_s(16)+" ")
630   line=''
631   begin
632     line_tmp = $sp.gets()
633     line_tmp = '' if line_tmp==nil
634     line = ''  if line.end_with?('\n')
635     line += line_tmp
636     puts("DBG got: "+line) if $debug
637     if /^Error:.*/.match(line)
638       puts line
639       return false
640     end
641   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)
642   a_ = m[1].to_i(16)
643   b_ = m[2].to_i(16)
644   c_ = m[3].to_i(16)
645   d_ = m[4].to_i(16)
646   e_ = m[5].to_i(16)
647   line.chomp!
648   line.gsub!("\r",'')
649   line.gsub!("\n",'')
650   ref = gcdext(a,b)
651   if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
652     $logfile.printf("[pass (%d)]: %s\n", $testno, line)
653     return true
654   else
655     $logfile.printf("[fail (%s%s%s%s%s) (%d)]: %s", 
656       (a == a_) ? '' : 'a', 
657       (b == b_) ? '' : 'b', 
658       (c_ == ref[1]) ? '' : 'c', 
659       (d_ == ref[2]) ? '' : 'd', 
660       (e_ == ref[0]) ? '' : 'e', $testno, line)
661     $logfile.printf(" ; should gcdext( %s, %s) => a = %s; b = %s; gcd = %s\n",
662        a.to_s(16), b.to_s(16), ref[1].to_s(16), ref[2].to_s(16), ref[0].to_s(16))
663     return false
664   end
665   return false
666 end
667
668 ################################################################################
669 # run_test_add                                                                 #
670 ################################################################################
671
672 def run_test_add(skip=0)
673   length_a_B = skip+1
674   length_b_B = skip+1
675   begin
676     $size = length_a_B
677     (0..16).each do |i|
678       a = rand(256**length_a_B)
679       b = rand(256**length_a_B)
680       v = add_test(a, b)
681       screen_progress(v)
682       v = add_test(b, a)
683       screen_progress(v)
684     end
685     (0..16).each do |i|
686       b_size = rand(length_b_B+1)
687       a = rand(256**length_a_B)
688       b = rand(256**b_size)
689       v = add_test(a, b)
690       screen_progress(v)      
691       v = add_test(b, a)
692       screen_progress(v)
693     end
694     length_a_B += 1
695     length_b_B += 1
696   end while length_a_B<4096/8
697 end
698
699 ################################################################################
700 # run_test_sub                                                                 #
701 ################################################################################
702
703 def run_test_sub(skip = 0)
704   length_a_B = skip + 1
705   length_b_B = skip + 1
706   begin
707     $size = length_a_B
708     (0..16).each do |i|
709       a = rand(256 ** length_a_B)
710       b = rand(a + 1)
711       v = sub_test(a, b)
712       screen_progress(v)
713     end
714     (0..16).each do |i|
715       b_size = rand(length_b_B + 1)
716       a = rand(256 ** length_a_B)
717       b = rand(min(256 ** b_size, a + 1))
718       v = sub_test(a, b)
719       screen_progress(v)
720     end
721     length_a_B += 1
722     length_b_B += 1
723   end while length_a_B < 4096 / 8
724 end
725
726
727 ################################################################################
728 # run_test_add_scale                                                           #
729 ################################################################################
730
731 def run_test_add_scale(skip=0)
732   length_a_B = skip+1
733   length_b_B = skip+1
734   begin
735     $size = length_a_B
736     (0..4).each do |i|
737       scales = [0, 300]
738       16.times { scales << rand(301) }
739       scales.sort!
740       scales.each do |scale|
741         a = rand(256**length_a_B)
742         b = rand(256**length_a_B)
743         v = add_scale_test(a, b, scale)
744         screen_progress(v)
745         v = add_scale_test(b, a, scale)
746         screen_progress(v)
747       end
748     end
749     (0..4).each do |i|
750       scales = [0, 300]
751       16.times { scales << rand(301) }
752       scales.sort!
753       scales.each do |scale|
754         b_size = rand(length_b_B+1)+1
755         a = rand(256**length_a_B)
756         b = rand(256**b_size)
757         v = add_scale_test(a, b, scale)
758         screen_progress(v)      
759         v = add_scale_test(b, a, scale)
760         screen_progress(v)
761       end
762     end
763     length_a_B += 10
764     length_b_B += 10
765   end while length_a_B<4096/8
766 end
767
768 def run_test_add_scale_dummy(skip=0)
769   length_a_B = skip+1
770   length_b_B = skip+1
771   begin
772     $size = length_a_B
773     (0..4).each do |i|
774       scales = [0, 300]
775       16.times { scales << rand(301) }
776       scales.sort!
777       scales.each do |scale|
778         a = rand(256**length_a_B)
779         b = rand(256**length_a_B)
780         v = add_scale_test_dummy(a, b, scale)
781         v = add_scale_test_dummy(b, a, scale)
782       end
783     end
784     (0..4).each do |i|
785       scales = [0, 300]
786       16.times { scales << rand(301) }
787       scales.sort!
788       scales.each do |scale|
789         b_size = rand(length_b_B+1)
790         a = rand(256**length_a_B)
791         b = rand(256**b_size)
792         v = add_scale_test_dummy(a, b, scale)
793         v = add_scale_test_dummy(b, a, scale)
794       end
795     end
796     length_a_B += 10
797     length_b_B += 10
798   end while length_a_B<4096/8
799 end
800
801 ################################################################################
802 # run_test_mul                                                                 #
803 ################################################################################
804
805 def run_test_mul(skip=0)
806   length_a_B = skip+1
807   length_b_B = skip+1
808   begin
809     $size = length_a_B
810     (0..16).each do |i|
811       s_a = rand(2)*2-1
812       s_b = rand(2)*2-1
813       a = rand(256**length_a_B) * s_a
814       b = rand(256**length_a_B) * s_b
815       v = mul_test(a, b)
816       screen_progress(v)
817       v = mul_test(b, a)
818       screen_progress(v)
819     end
820     (0..16).each do |i|
821       s_a = rand(2)-1
822       s_b = rand(2)-1
823       b_size = rand(length_b_B+1)
824       a = rand(256**length_a_B) * s_a
825       b = rand(256**b_size) * s_b
826       v = mul_test(a, b)
827       screen_progress(v)      
828       v = mul_test(b, a)
829       screen_progress(v)
830     end
831     length_a_B += 1
832     length_b_B += 1
833   end while length_a_B<4096/8
834 end
835
836 ################################################################################
837 # run_test_mul_word                                                                 #
838 ################################################################################
839
840 def run_test_mul_word(skip=0)
841   length_a_B = skip+1
842   length_b_B = skip+1
843   begin
844     $size = length_a_B
845     (0..255).each do |i|
846       a = rand(256 ** length_a_B) 
847       v = mul_test(a, i)
848       screen_progress(v)   
849     end
850     length_a_B += 1
851   end while length_a_B < 4096 / 8
852 end
853
854 ################################################################################
855 # run_test_square                                                              #
856 ################################################################################
857
858 def run_test_square(skip=0)
859   length_a_B = skip+1
860   begin
861     $size = length_a_B
862     (0..16).each do |i|
863       a = rand(256**length_a_B)
864       v = square_test(a)
865       screen_progress(v)
866     end
867     length_a_B += 1
868   end while length_a_B<4096/8
869 end
870
871 ################################################################################
872 # run_test_reduce                                                              #
873 ################################################################################
874
875 def run_test_reduce(skip=0)
876   length_a_B = skip+1
877   length_b_B = skip+1
878   begin
879     $size = length_a_B
880     (0..16).each do |i|
881       a = rand(256**length_a_B)
882       b = rand(256**length_a_B)+1
883       v = reduce_test(a, b)
884       screen_progress(v)
885       end
886     (0..16).each do |i|
887       b_size = rand(length_b_B+1)
888       a = rand(256**length_a_B)
889       b = rand(256**b_size)+1 
890       v = reduce_test(a, b)
891       screen_progress(v)      
892       end
893     length_a_B += 1
894     length_b_B += 1
895   end while length_a_B<4096/8
896 end
897
898 ################################################################################
899 # run_test_expmod                                                              #
900 ################################################################################
901
902 def run_test_expmod(skip=0)
903   length_a_B = skip + 1
904   length_b_B = skip + 1
905   length_c_B = skip + 1
906   begin
907     $size = length_a_B
908     (0..16).each do |i|
909       a = rand(256 ** length_a_B)
910       b = rand(256 ** length_b_B) + 1
911       c = rand(256 ** length_c_B) + 1
912       v = expmod_test(a, b, c)
913       screen_progress(v)
914       end
915     (0..16).each do |i|
916       b_size = rand(length_b_B+1)
917       a = rand(256 ** length_a_B)
918       b = rand(256 ** b_size) + 1 
919       c = rand(256 ** b_size) + 1
920       v = expmod_test(a, b, c)
921       screen_progress(v)      
922       end
923     length_a_B += 1
924     length_b_B += 1
925   end while length_a_B<4096/8
926 end
927
928 ################################################################################
929 # run_test_expmodmont                                                          #
930 ################################################################################
931
932 def run_test_expmodmont(skip=0)
933   length_a_B = skip + 1
934   length_b_B = skip + 1
935   length_c_B = skip + 1
936   begin
937     $size = length_a_B
938     (0..16).each do |i|
939       a = rand(256 ** length_a_B)
940       b = rand(256 ** length_b_B) + 1
941       c = rand(256 ** length_c_B) / 2 * 2 +1
942       v = expmod_test(a, b, c)
943       screen_progress(v)
944       end
945     (0..16).each do |i|
946       b_size = rand(length_b_B + 10)
947       a = rand(256 ** length_a_B)
948       b = rand(256 ** b_size) + 1 
949       c = rand(256 ** b_size) / 2 * 2 +1
950       v = expmod_test(a, b, c)
951       screen_progress(v)      
952       end
953     length_a_B += 1
954     length_b_B += 1
955   end while length_a_B<4096/8
956 end
957
958 ################################################################################
959 # run_test_mulmod                                                              #
960 ################################################################################
961
962 def run_test_mulmod(skip=0)
963   length_a_B = skip+1
964   length_b_B = skip+1
965   length_c_B = skip+1
966   begin
967     $size = length_a_B
968     (0..16).each do |i|
969       a = rand(256**length_a_B)
970       b = rand(256**length_b_B)
971       c = (rand(256**length_c_B) / 2 * 2) + 1
972       a %= c
973       b %= c
974       v = mulmod_test(a, b, c)
975       screen_progress(v)
976       end
977     (0..16).each do |i|
978       b_size = rand(length_b_B+1)
979       a = rand(256**length_a_B)
980       b = rand(256**b_size)
981       c = (rand(256**length_c_B) / 2 * 2) + 1
982       a %= c
983       b %= c
984       v = mulmod_test(a, b, c)
985       screen_progress(v)      
986       end
987     length_a_B += 1
988     length_b_B += 1
989     length_c_B += 1
990   end while length_a_B<4096/8
991 end
992
993 ################################################################################
994 # run_test_gcdext                                                              #
995 ################################################################################
996
997 def run_test_gcdext(skip=0)
998   length_a_B = skip+1
999   length_b_B = skip+1
1000   begin
1001     $size = length_a_B
1002     (0..16).each do |i|
1003       a = rand(256**length_a_B)
1004       b = rand(256**length_a_B)+1
1005       v = gcdext_test(a, b)
1006       $logfile.flush()
1007       screen_progress(v)
1008       end
1009     (0..16).each do |i|
1010       b_size = rand(length_b_B+1)
1011       a = rand(256**length_a_B)
1012       b = rand(256**b_size)+1 
1013       v = gcdext_test(a, b)
1014       $logfile.flush()
1015       screen_progress(v)      
1016       end
1017     length_a_B += 1
1018     length_b_B += 1
1019   end while length_a_B<4096/8
1020 end
1021
1022 def init_serialport(conf)
1023   puts("serial port interface version: " + SerialPort::VERSION);
1024   $linewidth = 64
1025   $linepos = 0
1026   $testno = 0
1027   params = { "baud"       => conf["PORT"]["baud"].to_i,
1028               "data_bits" => conf["PORT"]["databits"].to_i,
1029               "stop_bits" => conf["PORT"]["stopbits"].to_i,
1030               "parity"    => SerialPort::NONE }
1031   params["paraty"] = SerialPort::ODD   if conf["PORT"]["paraty"].downcase == "odd"
1032   params["paraty"] = SerialPort::EVEN  if conf["PORT"]["paraty"].downcase == "even"
1033   params["paraty"] = SerialPort::MARK  if conf["PORT"]["paraty"].downcase == "mark"
1034   params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
1035   
1036   puts("\nPort: "+conf["PORT"]["port"]+"@"    +
1037                   params["baud"].to_s      +
1038                   " "                      +
1039                   params["data_bits"].to_s +
1040                   conf["PORT"]["paraty"][0,1].upcase +
1041                   params["stop_bits"].to_s +
1042                   "\n")
1043   
1044   $sp = SerialPort.new(conf["PORT"]["port"], params)
1045   
1046   $sp.read_timeout = 100; # 5 minutes
1047   $sp.flow_control = SerialPort::SOFT
1048   
1049   reset_system()
1050 end
1051
1052 ################################################################################
1053 # MAIN                                                                         #
1054 ################################################################################
1055
1056 opts = Getopt::Std.getopts("r:s:f:i:a:hd")
1057
1058 conf = Hash.new
1059 conf = readconfigfile("/etc/testport.conf", conf)
1060 conf = readconfigfile("~/.testport.conf", conf)
1061 conf = readconfigfile("testport.conf", conf)
1062 conf = readconfigfile(opts["f"], conf) if opts["f"]
1063
1064 #puts conf.inspect
1065 init_serialport(conf)
1066
1067   $max_timeout = 5 * 60
1068
1069 if opts['d']
1070   $debug = true
1071 end
1072
1073 if opts['r']
1074   random_seed = opts['r'].to_i(0)
1075 else
1076   random_seed = 0xdeadbeef
1077 end
1078
1079 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
1080 if File.exists?(logfilename)
1081   i=1
1082   begin
1083     logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
1084     i+=1
1085   end while(File.exists?(logfilename))
1086   while(i>2) do
1087     n1 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt')
1088     n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt')
1089     File.rename(n1, n2)
1090 #    printf("%s -> %s\n", n1, n2) 
1091     i-=1
1092   end
1093   n1 = sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt')
1094   n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt')
1095   File.rename(n1, n2) 
1096   printf("%s -> %s\n", n1, n2)  
1097   logfilename = conf['PORT']['testlogbase']+'bigint.txt' 
1098 end
1099 $logfile = File.open(logfilename, 'w')
1100 printf("logfile: %s\n", logfilename)
1101 $logfile.sync = true
1102 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
1103 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
1104 $logfile.printf("seed = 0x%X\n", random_seed)
1105
1106 tests = Hash.new
1107 tests['a'] = proc {|x| run_test_add(x) }
1108 tests['b'] = proc {|x| run_test_sub(x) }
1109 tests['m'] = proc {|x| run_test_mul(x) }
1110 tests['M'] = proc {|x| run_test_mulmod(x) }
1111 tests['n'] = proc {|x| run_test_mul_word(x) }
1112 tests['x'] = proc {|x| run_test_add_scale(x) }
1113 tests['s'] = proc {|x| run_test_square(x) }
1114 tests['r'] = proc {|x| run_test_reduce(x) }
1115 tests['e'] = proc {|x| run_test_expmod(x) }
1116 tests['E'] = proc {|x| run_test_expmodmont(x) }
1117 tests['g'] = proc {|x| run_test_gcdext(x) }
1118 init_str = Hash.new
1119 init_str['a'] = 'add-test'
1120 init_str['b'] = 'sub-test'
1121 init_str['x'] = 'add-scale-test'
1122 init_str['m'] = 'mul-test'
1123 init_str['M'] = 'mul-mont-test'
1124 init_str['n'] = 'mul-word-test'
1125 init_str['s'] = 'square-test'
1126 init_str['r'] = 'reduce-test'
1127 init_str['e'] = 'expmod-test'
1128 init_str['E'] = 'expmod-mont-test'
1129 init_str['g'] = 'gcdext-test'
1130
1131 srand(random_seed)
1132
1133 if opts['a']
1134   opts['a'].each_char do |x|
1135     if tests[x]
1136       puts init_str[x]
1137       init_system(init_str[x])
1138       tests[x].call(opts['s']?opts['s'].to_i():0) 
1139     else
1140       puts "no test defined for '#{x}'"
1141     end  
1142   end
1143 else
1144   'amsrMeE'.each_char do |x|
1145     if tests[x]
1146       puts init_str[x]
1147       init_system(init_str[x])
1148       tests[x].call(opts['s']?opts['s'].to_i():0) 
1149     else
1150       puts "no test defined for '#{x}'"
1151     end  
1152   end
1153 end
1154 1
1155
1156 $logile.close()