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