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