]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/bigint_test.rb
bug fix + docu
[avr-crypto-lib.git] / host / bigint_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 'ftools'
27 require 'date'
28 $buffer_size = 0
29 $conffile_check = Hash.new
30 $conffile_check.default = 0
31
32 ################################################################################
33 # readconfigfile                                                               #
34 ################################################################################
35
36 def readconfigfile(fname, conf)
37   return conf if $conffile_check[fname]==1
38   $conffile_check[fname]=1
39   section = "default"
40   if not File.exists?(fname)
41     return conf
42   end
43   file = File.open(fname, "r")
44   until file.eof
45     line = file.gets()
46         next if /[\s]*#/.match(line)
47         if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
48           section=m[1]
49           conf[m[1]] = Hash.new
50           next
51         end
52         next if not /=/.match(line)
53         m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
54         if m[1]=="include"
55           Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
56         else
57           conf[section][m[1]] = m[2]
58         end
59   end
60   file.close()
61   return conf
62 end
63
64 ################################################################################
65 # expmod                                                                       #
66 ################################################################################
67
68 def expmod(base, power, mod)
69   result = 1
70   while power > 0
71     result = (result * base) % mod if power & 1 == 1
72     base = (base * base) % mod
73     power >>= 1;
74   end
75   return result
76 end
77
78 ################################################################################
79 # gcdext                                                                 #
80 ################################################################################
81
82 def gcdext(x,y)
83   g=1
84   while(x&1==0 && y&1==0) do
85     x>>=1
86     y>>=1
87     g<<=1
88   end
89   u=x; v=y; a=1; b=0; c=0; d=1
90   begin
91     while(u&1==0) do
92       if(a%2==1 || b%2==1)
93         a += y
94         b -= x
95       end
96       u>>=1; a>>=1; b>>=1
97     end
98     while(v&1==0) do
99       if(c%2==1 || d%2==1)
100         c += y
101         d -= x
102       end
103       v>>=1; c>>=1; d>>=1;
104     end
105     if(u>=v)
106       u -= v; a-=c; b-=d
107     else
108       v -= u; c-=a; d-=b
109     end
110   end while(u!=0)
111   return[g*v, c, d]
112 end
113
114 ################################################################################
115 # reset_system                                                                 #
116 ################################################################################
117
118 def reset_system
119   $sp.print("exit\r")
120   sleep 0.1
121   $sp.print("exit\r")
122   sleep 0.1
123 end
124
125 ################################################################################
126 # init_system                                                                  #
127 ################################################################################
128
129 def init_system(test_prog)
130   begin
131     line = $sp.gets()
132   end while line!=nil
133   $sp.print("echo off \r")
134   print("DBG i: " + "echo off \r"+"\n") if $debug
135   sleep 0.1
136   $sp.print("#{test_prog}\r")
137   print("DBG i: " + "#{test_prog} \r"+"\n") if $debug
138   sleep 1
139 end
140
141 ################################################################################
142 # wait_for_prompt                                                                  #
143 ################################################################################
144
145 def wait_for_prompt(prompt)
146   prompt = /[\s]*#{prompt}[\s]*/ if(prompt.class == String)
147   start_time = Time.now.to_i
148   begin
149     line = $sp.gets()
150     puts("DBG got (#{__LINE__}): "+line) if $debug && line
151     line = "" if line==nil
152     if /^(Error:|Crypto-VS).*/.match(line)
153       puts line
154       return false
155     end
156     if (Time.now.to_i- start_time) > $max_timeout
157       return false
158     end
159   end while not m=prompt.match(line)
160   return m
161 end
162
163 ################################################################################
164 # screen_progress                                                              #
165 ################################################################################
166 def screen_progress(v)
167   if $linepos==0
168     printf("\n%5d [%04d]: ", $testno, $size)
169   end
170   putc((v)?('*'):('!'))
171   $testno += 1
172   $linepos = ($linepos+1)%$linewidth
173 end
174
175 ################################################################################
176 # add_test                                                                     #
177 ################################################################################
178
179 def add_test(a,b)
180   begin
181     line = $sp.gets()
182     line = "" if line==nil
183     puts("DBG got: "+line) if $debug
184     if /^Error:.*/.match(line)
185       puts line
186       return false
187     end
188   end while not /[\s]*enter a:[\s]*/.match(line)
189   $sp.print(a.to_s(16)+" ")
190   begin
191     line = $sp.gets()
192     line = "" if line==nil
193     puts("DBG got: "+line) if $debug
194     if /^Error:.*/.match(line)
195       puts line
196       return false
197     end
198   end while not /[\s]*enter b:[\s]*/.match(line)
199   $sp.print(b.to_s(16)+" ")
200   begin
201     line = $sp.gets()
202     line = "" if line==nil
203     puts("DBG got: "+line) if $debug
204     if /^Error:.*/.match(line)
205       puts line
206       return false
207     end
208   end while not m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
209   a_ = m[1].to_i(16)
210   b_ = m[2].to_i(16)
211   c_ = m[3].to_i(16)
212   line.chomp!
213   if(a_== a && b_ == b && c_ == (a+b))
214     $logfile.printf("[pass]: %s\n", line)
215     return true
216   else
217     $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
218     $logfile.printf(" ; should %s + %s = %s\n", a.to_s(16), b.to_s(16), (a+b).to_s(16))
219     return false
220   end
221   return false
222 end
223
224 ################################################################################
225 # mul_test                                                                     #
226 ################################################################################
227
228 def mul_test(a,b)
229   begin
230     line = $sp.gets()
231     line = "" if line==nil
232     puts("DBG got: "+line) if $debug
233     if /^Error:.*/.match(line)
234       puts line
235       return false
236     end
237   end while not /[\s]*enter a:[\s]*/.match(line)
238   $sp.print(a.to_s(16)+" ")
239   begin
240     line = $sp.gets()
241     line = "" if line==nil
242     puts("DBG got: "+line) if $debug
243     if /^Error:.*/.match(line)
244       puts line
245       return false
246     end
247   end while not /[\s]*enter b:[\s]*/.match(line)
248   $sp.print(b.to_s(16)+" ")
249   begin
250     line = $sp.gets()
251     line = "" if line==nil
252     puts("DBG got: "+line) if $debug
253     if /^Error:.*/.match(line)
254       puts line
255       return false
256     end
257   end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+\*[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
258   a_ = m[1].to_i(16)
259   b_ = m[2].to_i(16)
260   c_ = m[3].to_i(16)
261   line.chomp!
262   if(a_== a && b_ == b && c_ == (a*b))
263     $logfile.printf("[pass]: %s\n", line)
264     return true
265   else
266     $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a*b)?"":"c",line)
267     $logfile.printf(" ; should %s * %s = %s\n", a.to_s(16), b.to_s(16), (a*b).to_s(16))
268     return false
269   end
270   return false
271 end
272
273 ################################################################################
274 # add_scale_test                                                               #
275 ################################################################################
276 def add_scale_test_dummy(a, b, scale)
277   should = a + (b<<(8*scale))
278   printf("[dummy] %s + %s <<8*%04x = %s\n",a.to_s(16), b.to_s(16), scale, should.to_s(16))
279 end
280
281 def add_scale_test(a, b, scale)
282   m = wait_for_prompt("enter a:")
283   return false if !m 
284   puts("DBG put (#{__LINE__}): "+a.to_s(16)+" ") if $debug
285   $sp.print(a.to_s(16)+" ")
286   m = wait_for_prompt("enter b:")
287   return false if !m 
288   puts("DBG put (#{__LINE__}): "+b.to_s(16)+" ") if $debug
289   $sp.print(b.to_s(16)+" ")
290   m = wait_for_prompt("enter scale:")
291   return false if !m 
292   puts("DBG put (#{__LINE__}): "+scale.to_s(10)+" ") if $debug
293   $sp.print(scale.to_s(10)+"\r")
294   should = a + (b<<(8*scale))
295   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]+)/)
296   if !m 
297     $logfile.printf("[fail (CRASH)]:")
298     $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))
299     return false
300   end 
301   line = m[0]
302   a_ = m[1].to_i(16)
303   b_ = m[2].to_i(16)
304   s_ = m[3].to_i(16)
305   c_ = m[4].to_i(16)
306   line.chomp!
307   if(a_== a && b_ == b && s_ == scale && c_ == should )
308     $logfile.printf("[pass]: %s\n", line)
309     return true
310   else
311     $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (scale==s_)?"":"s",(c_==should)?"":"c",line)
312     $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))
313     return false
314   end
315   return false
316 end
317
318 ################################################################################
319 # square_test                                                                  #
320 ################################################################################
321
322 def square_test(a)
323   begin
324     line = $sp.gets()
325     line = "" if line==nil
326     puts("DBG got: "+line) if $debug
327     if /^Error:.*/.match(line)
328       puts line
329       return false
330     end
331   end while not /[\s]*enter a:[\s]*/.match(line)
332   $sp.print(a.to_s(16)+" ")
333   begin
334     line = $sp.gets()
335     line = "" if line==nil
336     puts("DBG got: "+line) if $debug
337     if /^Error:.*/.match(line)
338       puts line
339       return false
340     end
341   end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
342   a_ = m[1].to_i(16)
343   c_ = m[2].to_i(16)
344   line.chomp!
345   if(a_== a && c_ == (a**2))
346     $logfile.printf("[pass]: %s\n", line)
347     return true
348   else
349     $logfile.printf("[fail (%s%s)]: %s", (a==a_)?"":"a", (c_==a**2)?"":"c",line)
350     $logfile.printf(" ; should %s **2 = %s\n", a.to_s(16), (a**2).to_s(16))
351     return false
352   end
353   return false
354 end
355
356 ################################################################################
357 # reduce_test                                                                  #
358 ################################################################################
359
360 def reduce_test(a,b)
361   begin
362     line = $sp.gets()
363     line = "" if line==nil
364     puts("DBG got: "+line) if $debug
365     if /^Error:.*/.match(line)
366       puts line
367       return false
368     end
369   end while not /[\s]*enter a:[\s]*/.match(line)
370   $sp.print(a.to_s(16)+" ")
371   begin
372     line = $sp.gets()
373     line = "" if line==nil
374     puts("DBG got: "+line) if $debug
375     if /^Error:.*/.match(line)
376       puts line
377       return false
378     end
379   end while not /[\s]*enter b:[\s]*/.match(line)
380   $sp.print(b.to_s(16)+" ")
381   line=''
382   begin
383     line_tmp = $sp.gets()
384     line_tmp = '' if line_tmp==nil
385     line += line_tmp
386     puts("DBG got: "+line) if $debug
387     if /^Error:.*/.match(line)
388       puts line
389       return false
390     end
391   end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
392   a_ = m[1].to_i(16)
393   b_ = m[2].to_i(16)
394   c_ = m[3].to_i(16)
395   line.chomp!
396   if(a_== a && b_ == b && c_ == (a%b))
397     $logfile.printf("[pass]: %s\n", line)
398     return true
399   else
400     $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a%b)?"":"c",line)
401     $logfile.printf(" ; should %s %% %s = %s\n", a.to_s(16), b.to_s(16), (a%b).to_s(16))
402     return false
403   end
404   return false
405 end
406
407 ################################################################################
408 # expmod_test                                                                  #
409 ################################################################################
410
411 def expmod_test(a,b,c)
412   begin
413     line = $sp.gets()
414     line = "" if line==nil
415     puts("DBG got: "+line) if $debug
416     if /^Error:.*/.match(line)
417       puts line
418       return false
419     end
420   end while not /[\s]*enter a:[\s]*/.match(line)
421   $sp.print(a.to_s(16)+" ")
422   begin
423     line = $sp.gets()
424     line = "" if line==nil
425     puts("DBG got: "+line) if $debug
426     if /^Error:.*/.match(line)
427       puts line
428       return false
429     end
430   end while not /[\s]*enter b:[\s]*/.match(line)
431   $sp.print(b.to_s(16)+" ")
432   begin
433     line = $sp.gets()
434     line = "" if line==nil
435     puts("DBG got: "+line) if $debug
436     if /^Error:.*/.match(line)
437       puts line
438       return false
439     end
440   end while not /[\s]*enter c:[\s]*/.match(line)
441   $sp.print(c.to_s(16)+" ")
442   line=''
443   begin
444     line_tmp = $sp.gets()
445     line_tmp = '' if line_tmp==nil
446     line += line_tmp
447     puts("DBG got: "+line) if $debug
448     if /^Error:.*/.match(line)
449       puts line
450       return false
451     end
452   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)
453   a_ = m[1].to_i(16)
454   b_ = m[2].to_i(16)
455   c_ = m[3].to_i(16)
456   d_ = m[4].to_i(16)
457   line.chomp!
458   if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
459     $logfile.printf("[pass]: %s\n", line)
460     return true
461   else
462     $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', (c_==c)?'':'c', (d_==expmod(a,b,c))?'':'d',line)
463     $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))
464     return false
465   end
466   return false
467 end
468
469 ################################################################################
470 # gcdext_test                                                                  #
471 ################################################################################
472
473 def gcdext_test(a,b)
474   begin
475     line = $sp.gets()
476     line = "" if line==nil
477     puts("DBG got: "+line) if $debug
478     if /^Error:.*/.match(line)
479       puts line
480       return false
481     end
482   end while not /[\s]*enter a:[\s]*/.match(line)
483   $sp.print(a.to_s(16)+" ")
484   begin
485     line = $sp.gets()
486     line = "" if line==nil
487     puts("DBG got: "+line) if $debug
488     if /^Error:.*/.match(line)
489       puts line
490       return false
491     end
492   end while not /[\s]*enter b:[\s]*/.match(line)
493   $sp.print(b.to_s(16)+" ")
494   line=''
495   begin
496     line_tmp = $sp.gets()
497     line_tmp = '' if line_tmp==nil
498     line = ''  if line.end_with?('\n')
499     line += line_tmp
500     puts("DBG got: "+line) if $debug
501     if /^Error:.*/.match(line)
502       puts line
503       return false
504     end
505   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)
506   a_ = m[1].to_i(16)
507   b_ = m[2].to_i(16)
508   c_ = m[3].to_i(16)
509   d_ = m[4].to_i(16)
510   e_ = m[5].to_i(16)
511   line.chomp!
512   line.gsub!("\r",'')
513   line.gsub!("\n",'')
514   ref = gcdext(a,b)
515   if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
516     $logfile.printf("[pass]: %s\n", line)
517     return true
518   else
519     $logfile.printf("[fail (%s%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', 
520        (c_==ref[1])?'':'c', (d_==ref[2])?'':'d', (e_==ref[0])?'':'e', line)
521     $logfile.printf(" ; should gcdext( %s, %s) => a = %s; b = %s; gcd = %s\n",
522        a.to_s(16), b.to_s(16), ref[1].to_s(16), ref[2].to_s(16), ref[0].to_s(16))
523     return false
524   end
525   return false
526 end
527
528 ################################################################################
529 # run_test_add                                                                 #
530 ################################################################################
531
532 def run_test_add(skip=0)
533   length_a_B = skip+1
534   length_b_B = skip+1
535   begin
536     $size = length_a_B
537     (0..16).each do |i|
538       s_a = rand(2)*2-1
539       s_b = rand(2)*2-1
540       a = rand(256**length_a_B) * s_a
541       b = rand(256**length_a_B) * s_b
542       v = add_test(a, b)
543       screen_progress(v)
544       v = add_test(b, a)
545       screen_progress(v)
546     end
547     (0..16).each do |i|
548       s_a = rand(2)-1
549       s_b = rand(2)-1
550       b_size = rand(length_b_B+1)
551       a = rand(256**length_a_B) * s_a
552       b = rand(256**b_size) * s_b
553       v = add_test(a, b)
554       screen_progress(v)      
555       v = add_test(b, a)
556       screen_progress(v)
557
558     end
559     length_a_B += 1
560     length_b_B += 1
561   end while length_a_B<4096/8
562 end
563
564 ################################################################################
565 # run_test_add_scale                                                           #
566 ################################################################################
567
568 def run_test_add_scale(skip=0)
569   length_a_B = skip+1
570   length_b_B = skip+1
571   begin
572     $size = length_a_B
573     (0..4).each do |i|
574       scales = [0, 300]
575       16.times { scales << rand(301) }
576       scales.sort!
577       scales.each do |scale|
578         a = rand(256**length_a_B)
579         b = rand(256**length_a_B)
580         v = add_scale_test(a, b, scale)
581         screen_progress(v)
582         v = add_scale_test(b, a, scale)
583         screen_progress(v)
584       end
585     end
586     (0..4).each do |i|
587       scales = [0, 300]
588       16.times { scales << rand(301) }
589       scales.sort!
590       scales.each do |scale|
591         b_size = rand(length_b_B+1)+1
592         a = rand(256**length_a_B)
593         b = rand(256**b_size)
594         v = add_scale_test(a, b, scale)
595         screen_progress(v)      
596         v = add_scale_test(b, a, scale)
597         screen_progress(v)
598       end
599     end
600     length_a_B += 10
601     length_b_B += 10
602   end while length_a_B<4096/8
603 end
604
605 def run_test_add_scale_dummy(skip=0)
606   length_a_B = skip+1
607   length_b_B = skip+1
608   begin
609     $size = length_a_B
610     (0..4).each do |i|
611       scales = [0, 300]
612       16.times { scales << rand(301) }
613       scales.sort!
614       scales.each do |scale|
615         a = rand(256**length_a_B)
616         b = rand(256**length_a_B)
617         v = add_scale_test_dummy(a, b, scale)
618         v = add_scale_test_dummy(b, a, scale)
619       end
620     end
621     (0..4).each do |i|
622       scales = [0, 300]
623       16.times { scales << rand(301) }
624       scales.sort!
625       scales.each do |scale|
626         b_size = rand(length_b_B+1)
627         a = rand(256**length_a_B)
628         b = rand(256**b_size)
629         v = add_scale_test_dummy(a, b, scale)
630         v = add_scale_test_dummy(b, a, scale)
631       end
632     end
633     length_a_B += 10
634     length_b_B += 10
635   end while length_a_B<4096/8
636 end
637
638 ################################################################################
639 # run_test_mul                                                                 #
640 ################################################################################
641
642 def run_test_mul(skip=0)
643   length_a_B = skip+1
644   length_b_B = skip+1
645   begin
646     $size = length_a_B
647     (0..16).each do |i|
648       s_a = rand(2)*2-1
649       s_b = rand(2)*2-1
650       a = rand(256**length_a_B) * s_a
651       b = rand(256**length_a_B) * s_b
652       v = mul_test(a, b)
653       screen_progress(v)
654       v = mul_test(b, a)
655       screen_progress(v)
656     end
657     (0..16).each do |i|
658       s_a = rand(2)-1
659       s_b = rand(2)-1
660       b_size = rand(length_b_B+1)
661       a = rand(256**length_a_B) * s_a
662       b = rand(256**b_size) * s_b
663       v = mul_test(a, b)
664       screen_progress(v)      
665       v = mul_test(b, a)
666       screen_progress(v)
667     end
668     length_a_B += 1
669     length_b_B += 1
670   end while length_a_B<4096/8
671 end
672
673 ################################################################################
674 # run_test_square                                                              #
675 ################################################################################
676
677 def run_test_square(skip=0)
678   length_a_B = skip+1
679   begin
680     $size = length_a_B
681     (0..16).each do |i|
682       a = rand(256**length_a_B)
683       v = square_test(a)
684       screen_progress(v)
685     end
686     length_a_B += 1
687   end while length_a_B<4096/8
688 end
689
690 ################################################################################
691 # run_test_reduce                                                              #
692 ################################################################################
693
694 def run_test_reduce(skip=0)
695   length_a_B = skip+1
696   length_b_B = skip+1
697   begin
698     $size = length_a_B
699     (0..16).each do |i|
700       a = rand(256**length_a_B)
701       b = rand(256**length_a_B)+1
702       v = reduce_test(a, b)
703       screen_progress(v)
704       end
705     (0..16).each do |i|
706       b_size = rand(length_b_B+1)
707       a = rand(256**length_a_B)
708       b = rand(256**b_size)+1 
709       v = reduce_test(a, b)
710       screen_progress(v)      
711       end
712     length_a_B += 1
713     length_b_B += 1
714   end while length_a_B<4096/8
715 end
716
717 ################################################################################
718 # run_test_expmod                                                              #
719 ################################################################################
720
721 def run_test_expmod(skip=0)
722   length_a_B = skip+1
723   length_b_B = skip+1
724   length_c_B = skip+1
725   begin
726     $size = length_a_B
727     (0..16).each do |i|
728       a = rand(256**length_a_B)
729       b = rand(256**length_b_B)+1
730       c = rand(256**length_c_B)+1
731       v = expmod_test(a, b, c)
732       screen_progress(v)
733       end
734     (0..16).each do |i|
735       b_size = rand(length_b_B+1)
736       a = rand(256**length_a_B)
737       b = rand(256**b_size)+1 
738       c = rand(256**b_size)+1
739       v = expmod_test(a, b, c)
740       screen_progress(v)      
741       end
742     length_a_B += 1
743     length_b_B += 1
744   end while length_a_B<4096/8
745 end
746
747 ################################################################################
748 # run_test_gcdext                                                              #
749 ################################################################################
750
751 def run_test_gcdext(skip=0)
752   length_a_B = skip+1
753   length_b_B = skip+1
754   begin
755     $size = length_a_B
756     (0..16).each do |i|
757       a = rand(256**length_a_B)
758       b = rand(256**length_a_B)+1
759       v = gcdext_test(a, b)
760       $logfile.flush()
761       screen_progress(v)
762       end
763     (0..16).each do |i|
764       b_size = rand(length_b_B+1)
765       a = rand(256**length_a_B)
766       b = rand(256**b_size)+1 
767       v = gcdext_test(a, b)
768       $logfile.flush()
769       screen_progress(v)      
770       end
771     length_a_B += 1
772     length_b_B += 1
773   end while length_a_B<4096/8
774 end
775
776 def init_serialport(conf)
777   puts("serial port interface version: " + SerialPort::VERSION);
778   $linewidth = 64
779   $linepos = 0
780   $testno = 0
781   params = { "baud"       => conf["PORT"]["baud"].to_i,
782               "data_bits" => conf["PORT"]["databits"].to_i,
783               "stop_bits" => conf["PORT"]["stopbits"].to_i,
784               "parity"    => SerialPort::NONE }
785   params["paraty"] = SerialPort::ODD   if conf["PORT"]["paraty"].downcase == "odd"
786   params["paraty"] = SerialPort::EVEN  if conf["PORT"]["paraty"].downcase == "even"
787   params["paraty"] = SerialPort::MARK  if conf["PORT"]["paraty"].downcase == "mark"
788   params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
789   
790   puts("\nPort: "+conf["PORT"]["port"]+"@"    +
791                   params["baud"].to_s      +
792                   " "                      +
793                   params["data_bits"].to_s +
794                   conf["PORT"]["paraty"][0,1].upcase +
795                   params["stop_bits"].to_s +
796                   "\n")
797   
798   $sp = SerialPort.new(conf["PORT"]["port"], params)
799   
800   $sp.read_timeout=1000; # 5 minutes
801   $sp.flow_control = SerialPort::SOFT
802   
803   reset_system()
804 end
805
806 ################################################################################
807 # MAIN                                                                         #
808 ################################################################################
809
810 opts = Getopt::Std.getopts("s:f:i:a:hd")
811
812 conf = Hash.new
813 conf = readconfigfile("/etc/testport.conf", conf)
814 conf = readconfigfile("~/.testport.conf", conf)
815 conf = readconfigfile("testport.conf", conf)
816 conf = readconfigfile(opts["f"], conf) if opts["f"]
817
818 #puts conf.inspect
819 init_serialport(conf)
820
821   $max_timeout = 2 * 60
822
823 if opts['d']
824   $debug = true
825 end
826
827 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
828 if File.exists?(logfilename)
829   i=1
830   begin
831     logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
832     i+=1
833   end while(File.exists?(logfilename))
834   while(i>2) do
835     File.move(sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt'), 
836               sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt'), true)
837     i-=1
838   end
839     File.move(sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt'), 
840               sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt'), true)
841   logfilename = conf['PORT']['testlogbase']+'bigint.txt' 
842 end
843 $logfile = File.open(logfilename, 'w')
844 printf("logfile: %s\n", logfilename)
845
846 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
847 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
848 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
849
850 tests = Hash.new
851 tests['a'] = proc {|x| run_test_add(x) }
852 tests['m'] = proc {|x| run_test_mul(x) }
853 tests['x'] = proc {|x| run_test_add_scale(x) }
854 tests['s'] = proc {|x| run_test_square(x) }
855 tests['r'] = proc {|x| run_test_reduce(x) }
856 tests['e'] = proc {|x| run_test_expmod(x) }
857 tests['g'] = proc {|x| run_test_gcdext(x) }
858 init_str = Hash.new
859 init_str['a'] = 'add-test'
860 init_str['x'] = 'add-scale-test'
861 init_str['m'] = 'mul-test'
862 init_str['s'] = 'square-test'
863 init_str['r'] = 'reduce-test'
864 init_str['e'] = 'expmod-test'
865 init_str['g'] = 'gcdext-test'
866
867 srand(0xdeadbeef)
868
869 if opts['a']
870   opts['a'].each_char do |x|
871     if tests[x]
872       puts init_str[x]
873       init_system(init_str[x])
874       tests[x].call(opts['s']?opts['s'].to_i():0) 
875     else
876       puts "no test defiened for '#{x}'"
877     end  
878   end
879 else
880   'amsre'.each_char do |x|
881     if tests[x]
882       puts init_str[x]
883       init_system(init_str[x])
884       tests[x].call(opts['s']?opts['s'].to_i():0) 
885     else
886       puts "no test defiened for '#{x}'"
887     end  
888   end
889 end
890
891
892 $logile.close()