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