]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/bigint_test.rb
added Khazad
[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) 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   $sp.print(a.to_s(16)+" ")
264   begin
265     line = $sp.gets()
266     line = "" if line==nil
267     puts("DBG got: "+line) if $debug
268     if /^Error:.*/.match(line)
269       puts line
270       return false
271     end
272   end while not /[\s]*enter b:[\s]*/.match(line)
273   $sp.print(b.to_s(16)+" ")
274   begin
275     line = $sp.gets()
276     line = "" if line==nil
277     puts("DBG got: "+line) if $debug
278     if /^Error:.*/.match(line)
279       puts line
280       return false
281     end
282   end while not /[\s]*enter scale:[\s]*/.match(line)
283   $sp.print(scale.to_s(16)+"\n")
284   begin
285     line = $sp.gets()
286     line = "" if line==nil
287     puts("DBG got: "+line) if $debug
288     if /^Error:.*/.match(line)
289       puts line
290       return false
291     end
292   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)
293   a_ = m[1].to_i(16)
294   b_ = m[2].to_i(16)
295   s_ = m[3].to_i(16)
296   c_ = m[4].to_i(16)
297   line.chomp!
298   if(a_== a && b_ == b && c_ == (a+b))
299     $logfile.printf("[pass]: %s\n", line)
300     return true
301   else
302     $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
303     $logfile.printf(" ; should %s + %s = %s\n", a.to_s(16), b.to_s(16), (a+b).to_s(16))
304     return false
305   end
306   return false
307 end
308
309 ################################################################################
310 # square_test                                                                  #
311 ################################################################################
312
313 def square_test(a)
314   begin
315     line = $sp.gets()
316     line = "" if line==nil
317     puts("DBG got: "+line) if $debug
318     if /^Error:.*/.match(line)
319       puts line
320       return false
321     end
322   end while not /[\s]*enter a:[\s]*/.match(line)
323   $sp.print(a.to_s(16)+" ")
324   begin
325     line = $sp.gets()
326     line = "" if line==nil
327     puts("DBG got: "+line) if $debug
328     if /^Error:.*/.match(line)
329       puts line
330       return false
331     end
332   end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
333   a_ = m[1].to_i(16)
334   c_ = m[2].to_i(16)
335   line.chomp!
336   if(a_== a && c_ == (a**2))
337     $logfile.printf("[pass]: %s\n", line)
338     return true
339   else
340     $logfile.printf("[fail (%s%s)]: %s", (a==a_)?"":"a", (c_==a**2)?"":"c",line)
341     $logfile.printf(" ; should %s **2 = %s\n", a.to_s(16), (a**2).to_s(16))
342     return false
343   end
344   return false
345 end
346
347 ################################################################################
348 # reduce_test                                                                  #
349 ################################################################################
350
351 def reduce_test(a,b)
352   begin
353     line = $sp.gets()
354     line = "" if line==nil
355     puts("DBG got: "+line) if $debug
356     if /^Error:.*/.match(line)
357       puts line
358       return false
359     end
360   end while not /[\s]*enter a:[\s]*/.match(line)
361   $sp.print(a.to_s(16)+" ")
362   begin
363     line = $sp.gets()
364     line = "" if line==nil
365     puts("DBG got: "+line) if $debug
366     if /^Error:.*/.match(line)
367       puts line
368       return false
369     end
370   end while not /[\s]*enter b:[\s]*/.match(line)
371   $sp.print(b.to_s(16)+" ")
372   line=''
373   begin
374     line_tmp = $sp.gets()
375     line_tmp = '' if line_tmp==nil
376     line += line_tmp
377     puts("DBG got: "+line) if $debug
378     if /^Error:.*/.match(line)
379       puts line
380       return false
381     end
382   end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
383   a_ = m[1].to_i(16)
384   b_ = m[2].to_i(16)
385   c_ = m[3].to_i(16)
386   line.chomp!
387   if(a_== a && b_ == b && c_ == (a%b))
388     $logfile.printf("[pass]: %s\n", line)
389     return true
390   else
391     $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a%b)?"":"c",line)
392     $logfile.printf(" ; should %s %% %s = %s\n", a.to_s(16), b.to_s(16), (a%b).to_s(16))
393     return false
394   end
395   return false
396 end
397
398 ################################################################################
399 # expmod_test                                                                  #
400 ################################################################################
401
402 def expmod_test(a,b,c)
403   begin
404     line = $sp.gets()
405     line = "" if line==nil
406     puts("DBG got: "+line) if $debug
407     if /^Error:.*/.match(line)
408       puts line
409       return false
410     end
411   end while not /[\s]*enter a:[\s]*/.match(line)
412   $sp.print(a.to_s(16)+" ")
413   begin
414     line = $sp.gets()
415     line = "" if line==nil
416     puts("DBG got: "+line) if $debug
417     if /^Error:.*/.match(line)
418       puts line
419       return false
420     end
421   end while not /[\s]*enter b:[\s]*/.match(line)
422   $sp.print(b.to_s(16)+" ")
423   begin
424     line = $sp.gets()
425     line = "" if line==nil
426     puts("DBG got: "+line) if $debug
427     if /^Error:.*/.match(line)
428       puts line
429       return false
430     end
431   end while not /[\s]*enter c:[\s]*/.match(line)
432   $sp.print(c.to_s(16)+" ")
433   line=''
434   begin
435     line_tmp = $sp.gets()
436     line_tmp = '' if line_tmp==nil
437     line += line_tmp
438     puts("DBG got: "+line) if $debug
439     if /^Error:.*/.match(line)
440       puts line
441       return false
442     end
443   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)
444   a_ = m[1].to_i(16)
445   b_ = m[2].to_i(16)
446   c_ = m[3].to_i(16)
447   d_ = m[4].to_i(16)
448   line.chomp!
449   if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
450     $logfile.printf("[pass]: %s\n", line)
451     return true
452   else
453     $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', (c_==c)?'':'c', (d_==expmod(a,b,c))?'':'d',line)
454     $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))
455     return false
456   end
457   return false
458 end
459
460 ################################################################################
461 # gcdext_test                                                                  #
462 ################################################################################
463
464 def gcdext_test(a,b)
465   begin
466     line = $sp.gets()
467     line = "" if line==nil
468     puts("DBG got: "+line) if $debug
469     if /^Error:.*/.match(line)
470       puts line
471       return false
472     end
473   end while not /[\s]*enter a:[\s]*/.match(line)
474   $sp.print(a.to_s(16)+" ")
475   begin
476     line = $sp.gets()
477     line = "" if line==nil
478     puts("DBG got: "+line) if $debug
479     if /^Error:.*/.match(line)
480       puts line
481       return false
482     end
483   end while not /[\s]*enter b:[\s]*/.match(line)
484   $sp.print(b.to_s(16)+" ")
485   line=''
486   begin
487     line_tmp = $sp.gets()
488     line_tmp = '' if line_tmp==nil
489     line = ''  if line.end_with?('\n')
490     line += line_tmp
491     puts("DBG got: "+line) if $debug
492     if /^Error:.*/.match(line)
493       puts line
494       return false
495     end
496   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)
497   a_ = m[1].to_i(16)
498   b_ = m[2].to_i(16)
499   c_ = m[3].to_i(16)
500   d_ = m[4].to_i(16)
501   e_ = m[5].to_i(16)
502   line.chomp!
503   line.gsub!("\r",'')
504   line.gsub!("\n",'')
505   ref = gcdext(a,b)
506   if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
507     $logfile.printf("[pass]: %s\n", line)
508     return true
509   else
510     $logfile.printf("[fail (%s%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', 
511        (c_==ref[1])?'':'c', (d_==ref[2])?'':'d', (e_==ref[0])?'':'e', line)
512     $logfile.printf(" ; should gcdext( %s, %s) => a = %s; b = %s; gcd = %s\n",
513        a.to_s(16), b.to_s(16), ref[1].to_s(16), ref[2].to_s(16), ref[0].to_s(16))
514     return false
515   end
516   return false
517 end
518
519 ################################################################################
520 # run_test_add                                                                 #
521 ################################################################################
522
523 def run_test_add(skip=0)
524   length_a_B = skip+1
525   length_b_B = skip+1
526   begin
527     $size = length_a_B
528     (0..16).each do |i|
529       s_a = rand(2)*2-1
530       s_b = rand(2)*2-1
531       a = rand(256**length_a_B) * s_a
532       b = rand(256**length_a_B) * s_b
533       v = add_test(a, b)
534       screen_progress(v)
535       v = add_test(b, a)
536       screen_progress(v)
537     end
538     (0..16).each do |i|
539       s_a = rand(2)-1
540       s_b = rand(2)-1
541       b_size = rand(length_b_B+1)
542       a = rand(256**length_a_B) * s_a
543       b = rand(256**b_size) * s_b
544       v = add_test(a, b)
545       screen_progress(v)      
546       v = add_test(b, a)
547       screen_progress(v)
548
549     end
550     length_a_B += 1
551     length_b_B += 1
552   end while length_a_B<4096/8
553 end
554
555 ################################################################################
556 # run_test_mul                                                                 #
557 ################################################################################
558
559 def run_test_mul(skip=0)
560   length_a_B = skip+1
561   length_b_B = skip+1
562   begin
563     $size = length_a_B
564     (0..16).each do |i|
565       s_a = rand(2)*2-1
566       s_b = rand(2)*2-1
567       a = rand(256**length_a_B) * s_a
568       b = rand(256**length_a_B) * s_b
569       v = mul_test(a, b)
570       screen_progress(v)
571       v = mul_test(b, a)
572       screen_progress(v)
573     end
574     (0..16).each do |i|
575       s_a = rand(2)-1
576       s_b = rand(2)-1
577       b_size = rand(length_b_B+1)
578       a = rand(256**length_a_B) * s_a
579       b = rand(256**b_size) * s_b
580       v = mul_test(a, b)
581       screen_progress(v)      
582       v = mul_test(b, a)
583       screen_progress(v)
584     end
585     length_a_B += 1
586     length_b_B += 1
587   end while length_a_B<4096/8
588 end
589
590 ################################################################################
591 # run_test_square                                                              #
592 ################################################################################
593
594 def run_test_square(skip=0)
595   length_a_B = skip+1
596   begin
597     $size = length_a_B
598     (0..16).each do |i|
599       a = rand(256**length_a_B)
600       v = square_test(a)
601       screen_progress(v)
602     end
603     length_a_B += 1
604   end while length_a_B<4096/8
605 end
606
607 ################################################################################
608 # run_test_reduce                                                              #
609 ################################################################################
610
611 def run_test_reduce(skip=0)
612   length_a_B = skip+1
613   length_b_B = skip+1
614   begin
615     $size = length_a_B
616     (0..16).each do |i|
617       a = rand(256**length_a_B)
618       b = rand(256**length_a_B)+1
619       v = reduce_test(a, b)
620       screen_progress(v)
621       end
622     (0..16).each do |i|
623       b_size = rand(length_b_B+1)
624       a = rand(256**length_a_B)
625       b = rand(256**b_size)+1 
626       v = reduce_test(a, b)
627       screen_progress(v)      
628       end
629     length_a_B += 1
630     length_b_B += 1
631   end while length_a_B<4096/8
632 end
633
634 ################################################################################
635 # run_test_expmod                                                              #
636 ################################################################################
637
638 def run_test_expmod(skip=0)
639   length_a_B = skip+1
640   length_b_B = skip+1
641   length_c_B = skip+1
642   begin
643     $size = length_a_B
644     (0..16).each do |i|
645       a = rand(256**length_a_B)
646       b = rand(256**length_b_B)+1
647       c = rand(256**length_c_B)+1
648       v = expmod_test(a, b, c)
649       screen_progress(v)
650       end
651     (0..16).each do |i|
652       b_size = rand(length_b_B+1)
653       a = rand(256**length_a_B)
654       b = rand(256**b_size)+1 
655       c = rand(256**b_size)+1
656       v = expmod_test(a, b, c)
657       screen_progress(v)      
658       end
659     length_a_B += 1
660     length_b_B += 1
661   end while length_a_B<4096/8
662 end
663
664 ################################################################################
665 # run_test_gcdext                                                              #
666 ################################################################################
667
668 def run_test_gcdext(skip=0)
669   length_a_B = skip+1
670   length_b_B = skip+1
671   begin
672     $size = length_a_B
673     (0..16).each do |i|
674       a = rand(256**length_a_B)
675       b = rand(256**length_a_B)+1
676       v = gcdext_test(a, b)
677       $logfile.flush()
678       screen_progress(v)
679       end
680     (0..16).each do |i|
681       b_size = rand(length_b_B+1)
682       a = rand(256**length_a_B)
683       b = rand(256**b_size)+1 
684       v = gcdext_test(a, b)
685       $logfile.flush()
686       screen_progress(v)      
687       end
688     length_a_B += 1
689     length_b_B += 1
690   end while length_a_B<4096/8
691 end
692
693 ################################################################################
694 # MAIN                                                                         #
695 ################################################################################
696
697 opts = Getopt::Std.getopts("s:f:i:a:hd")
698
699 conf = Hash.new
700 conf = readconfigfile("/etc/testport.conf", conf)
701 conf = readconfigfile("~/.testport.conf", conf)
702 conf = readconfigfile("testport.conf", conf)
703 conf = readconfigfile(opts["f"], conf) if opts["f"]
704
705 #puts conf.inspect
706
707 puts("serial port interface version: " + SerialPort::VERSION);
708 $linewidth = 64
709 $linepos = 0
710 $testno = 0
711 params = { "baud"       => conf["PORT"]["baud"].to_i,
712             "data_bits" => conf["PORT"]["databits"].to_i,
713             "stop_bits" => conf["PORT"]["stopbits"].to_i,
714             "parity"    => SerialPort::NONE }
715 params["paraty"] = SerialPort::ODD   if conf["PORT"]["paraty"].downcase == "odd"
716 params["paraty"] = SerialPort::EVEN  if conf["PORT"]["paraty"].downcase == "even"
717 params["paraty"] = SerialPort::MARK  if conf["PORT"]["paraty"].downcase == "mark"
718 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
719
720 puts("\nPort: "+conf["PORT"]["port"]+"@"    +
721                 params["baud"].to_s      +
722                 " "                      +
723                 params["data_bits"].to_s +
724                 conf["PORT"]["paraty"][0,1].upcase +
725                 params["stop_bits"].to_s +
726                 "\n")
727
728 $sp = SerialPort.new(conf["PORT"]["port"], params)
729
730 $sp.read_timeout=1000; # 5 minutes
731 $sp.flow_control = SerialPort::SOFT
732
733 reset_system()
734
735 if opts['d']
736   $debug = true
737 end
738
739 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
740 if File.exists?(logfilename)
741   i=1
742   begin
743     logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
744     i+=1
745   end while(File.exists?(logfilename))
746   while(i>2) do
747     File.move(sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt'), 
748               sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt'), true)
749     i-=1
750   end
751     File.move(sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt'), 
752               sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt'), true)
753   logfilename = conf['PORT']['testlogbase']+'bigint.txt' 
754 end
755 $logfile = File.open(logfilename, 'w')
756 printf("logfile: %s\n", logfilename)
757
758 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
759 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
760 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
761
762 tests = Hash.new
763 tests['a'] = proc {|x| run_test_add(x) }
764 tests['m'] = proc {|x| run_test_mul(x) }
765 tests['s'] = proc {|x| run_test_square(x) }
766 tests['r'] = proc {|x| run_test_reduce(x) }
767 tests['e'] = proc {|x| run_test_expmod(x) }
768 tests['g'] = proc {|x| run_test_gcdext(x) }
769 init_str = Hash.new
770 init_str['a'] = 'add-test'
771 init_str['m'] = 'mul-test'
772 init_str['s'] = 'square-test'
773 init_str['r'] = 'reduce-test'
774 init_str['e'] = 'expmod-test'
775 init_str['g'] = 'gcdext-test'
776
777 srand(0xdeadbeef)
778
779 if opts['a']
780   opts['a'].each_char do |x|
781     if tests[x]
782       puts init_str[x]
783       init_system(init_str[x])
784       tests[x].call(opts['s']?opts['s'].to_i():0) 
785     else
786       puts "no test defiened for '#{x}'"
787     end  
788   end
789 else
790   'amsre'.each_char do |x|
791     if tests[x]
792       puts init_str[x]
793       init_system(init_str[x])
794       tests[x].call(opts['s']?opts['s'].to_i():0) 
795     else
796       puts "no test defiened for '#{x}'"
797     end  
798   end
799 end
800
801
802 $logile.close()