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