]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/bigint_test.rb
some bigint stuff in ASM
[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       (0..300).each do |scale|
568         a = rand(256**length_a_B)
569         b = rand(256**length_a_B)
570         v = add_scale_test(a, b, scale)
571         screen_progress(v)
572         v = add_scale_test(b, a, scale)
573         screen_progress(v)
574       end
575     end
576     (0..16).each do |i|
577       (0..300).each do |scale|
578         b_size = rand(length_b_B+1)
579         a = rand(256**length_a_B)
580         b = rand(256**b_size)
581         v = add_scale_test(a, b, scale)
582         screen_progress(v)      
583         v = add_scale_test(b, a, scale)
584         screen_progress(v)
585       end
586     end
587     length_a_B += 1
588     length_b_B += 1
589   end while length_a_B<4096/8
590 end
591
592 ################################################################################
593 # run_test_mul                                                                 #
594 ################################################################################
595
596 def run_test_mul(skip=0)
597   length_a_B = skip+1
598   length_b_B = skip+1
599   begin
600     $size = length_a_B
601     (0..16).each do |i|
602       s_a = rand(2)*2-1
603       s_b = rand(2)*2-1
604       a = rand(256**length_a_B) * s_a
605       b = rand(256**length_a_B) * s_b
606       v = mul_test(a, b)
607       screen_progress(v)
608       v = mul_test(b, a)
609       screen_progress(v)
610     end
611     (0..16).each do |i|
612       s_a = rand(2)-1
613       s_b = rand(2)-1
614       b_size = rand(length_b_B+1)
615       a = rand(256**length_a_B) * s_a
616       b = rand(256**b_size) * s_b
617       v = mul_test(a, b)
618       screen_progress(v)      
619       v = mul_test(b, a)
620       screen_progress(v)
621     end
622     length_a_B += 1
623     length_b_B += 1
624   end while length_a_B<4096/8
625 end
626
627 ################################################################################
628 # run_test_square                                                              #
629 ################################################################################
630
631 def run_test_square(skip=0)
632   length_a_B = skip+1
633   begin
634     $size = length_a_B
635     (0..16).each do |i|
636       a = rand(256**length_a_B)
637       v = square_test(a)
638       screen_progress(v)
639     end
640     length_a_B += 1
641   end while length_a_B<4096/8
642 end
643
644 ################################################################################
645 # run_test_reduce                                                              #
646 ################################################################################
647
648 def run_test_reduce(skip=0)
649   length_a_B = skip+1
650   length_b_B = skip+1
651   begin
652     $size = length_a_B
653     (0..16).each do |i|
654       a = rand(256**length_a_B)
655       b = rand(256**length_a_B)+1
656       v = reduce_test(a, b)
657       screen_progress(v)
658       end
659     (0..16).each do |i|
660       b_size = rand(length_b_B+1)
661       a = rand(256**length_a_B)
662       b = rand(256**b_size)+1 
663       v = reduce_test(a, b)
664       screen_progress(v)      
665       end
666     length_a_B += 1
667     length_b_B += 1
668   end while length_a_B<4096/8
669 end
670
671 ################################################################################
672 # run_test_expmod                                                              #
673 ################################################################################
674
675 def run_test_expmod(skip=0)
676   length_a_B = skip+1
677   length_b_B = skip+1
678   length_c_B = skip+1
679   begin
680     $size = length_a_B
681     (0..16).each do |i|
682       a = rand(256**length_a_B)
683       b = rand(256**length_b_B)+1
684       c = rand(256**length_c_B)+1
685       v = expmod_test(a, b, c)
686       screen_progress(v)
687       end
688     (0..16).each do |i|
689       b_size = rand(length_b_B+1)
690       a = rand(256**length_a_B)
691       b = rand(256**b_size)+1 
692       c = rand(256**b_size)+1
693       v = expmod_test(a, b, c)
694       screen_progress(v)      
695       end
696     length_a_B += 1
697     length_b_B += 1
698   end while length_a_B<4096/8
699 end
700
701 ################################################################################
702 # run_test_gcdext                                                              #
703 ################################################################################
704
705 def run_test_gcdext(skip=0)
706   length_a_B = skip+1
707   length_b_B = skip+1
708   begin
709     $size = length_a_B
710     (0..16).each do |i|
711       a = rand(256**length_a_B)
712       b = rand(256**length_a_B)+1
713       v = gcdext_test(a, b)
714       $logfile.flush()
715       screen_progress(v)
716       end
717     (0..16).each do |i|
718       b_size = rand(length_b_B+1)
719       a = rand(256**length_a_B)
720       b = rand(256**b_size)+1 
721       v = gcdext_test(a, b)
722       $logfile.flush()
723       screen_progress(v)      
724       end
725     length_a_B += 1
726     length_b_B += 1
727   end while length_a_B<4096/8
728 end
729
730 ################################################################################
731 # MAIN                                                                         #
732 ################################################################################
733
734 opts = Getopt::Std.getopts("s:f:i:a:hd")
735
736 conf = Hash.new
737 conf = readconfigfile("/etc/testport.conf", conf)
738 conf = readconfigfile("~/.testport.conf", conf)
739 conf = readconfigfile("testport.conf", conf)
740 conf = readconfigfile(opts["f"], conf) if opts["f"]
741
742 #puts conf.inspect
743
744 puts("serial port interface version: " + SerialPort::VERSION);
745 $linewidth = 64
746 $linepos = 0
747 $testno = 0
748 params = { "baud"       => conf["PORT"]["baud"].to_i,
749             "data_bits" => conf["PORT"]["databits"].to_i,
750             "stop_bits" => conf["PORT"]["stopbits"].to_i,
751             "parity"    => SerialPort::NONE }
752 params["paraty"] = SerialPort::ODD   if conf["PORT"]["paraty"].downcase == "odd"
753 params["paraty"] = SerialPort::EVEN  if conf["PORT"]["paraty"].downcase == "even"
754 params["paraty"] = SerialPort::MARK  if conf["PORT"]["paraty"].downcase == "mark"
755 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
756
757 puts("\nPort: "+conf["PORT"]["port"]+"@"    +
758                 params["baud"].to_s      +
759                 " "                      +
760                 params["data_bits"].to_s +
761                 conf["PORT"]["paraty"][0,1].upcase +
762                 params["stop_bits"].to_s +
763                 "\n")
764
765 $sp = SerialPort.new(conf["PORT"]["port"], params)
766
767 $sp.read_timeout=1000; # 5 minutes
768 $sp.flow_control = SerialPort::SOFT
769
770 reset_system()
771
772 if opts['d']
773   $debug = true
774 end
775
776 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
777 if File.exists?(logfilename)
778   i=1
779   begin
780     logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
781     i+=1
782   end while(File.exists?(logfilename))
783   while(i>2) do
784     File.move(sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt'), 
785               sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt'), true)
786     i-=1
787   end
788     File.move(sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt'), 
789               sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt'), true)
790   logfilename = conf['PORT']['testlogbase']+'bigint.txt' 
791 end
792 $logfile = File.open(logfilename, 'w')
793 printf("logfile: %s\n", logfilename)
794
795 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
796 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
797 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
798
799 tests = Hash.new
800 tests['a'] = proc {|x| run_test_add(x) }
801 tests['m'] = proc {|x| run_test_mul(x) }
802 tests['x'] = proc {|x| run_test_add_scale(x) }
803 tests['s'] = proc {|x| run_test_square(x) }
804 tests['r'] = proc {|x| run_test_reduce(x) }
805 tests['e'] = proc {|x| run_test_expmod(x) }
806 tests['g'] = proc {|x| run_test_gcdext(x) }
807 init_str = Hash.new
808 init_str['a'] = 'add-test'
809 init_str['x'] = 'add-scale-test'
810 init_str['m'] = 'mul-test'
811 init_str['s'] = 'square-test'
812 init_str['r'] = 'reduce-test'
813 init_str['e'] = 'expmod-test'
814 init_str['g'] = 'gcdext-test'
815
816 srand(0xdeadbeef)
817
818 if opts['a']
819   opts['a'].each_char do |x|
820     if tests[x]
821       puts init_str[x]
822       init_system(init_str[x])
823       tests[x].call(opts['s']?opts['s'].to_i():0) 
824     else
825       puts "no test defiened for '#{x}'"
826     end  
827   end
828 else
829   'amsre'.each_char do |x|
830     if tests[x]
831       puts init_str[x]
832       init_system(init_str[x])
833       tests[x].call(opts['s']?opts['s'].to_i():0) 
834     else
835       puts "no test defiened for '#{x}'"
836     end  
837   end
838 end
839
840
841 $logile.close()