]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/bigint_test.rb
fixing E-Mail-Address & Copyright
[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) 2006-2015 Daniel Otte (bg@nerilex.org)
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 # mulmod_test                                                                  #
412 ################################################################################
413
414 def mulmod_test(a,b,c)
415   begin
416     printf("[testing] mulmod(%#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     m = /[\s]*\([\s]*([+-]?[0-9a-fA-F]*)[\s]*\*[\s]*([+-]?[0-9a-fA-F]*)[\s]*\)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
457     puts("DBG: line did not match pattern (" + line + ")") if !m && $debug
458   end while ! m 
459   a_ = m[1].to_i(16)
460   b_ = m[2].to_i(16)
461   c_ = m[3].to_i(16)
462   d_ = m[4].to_i(16)
463   line.chomp!
464   if(a_== a && b_ == b && c_ == c && d_ == (a * b % c) )
465     $logfile.printf("[pass]: %s\n", line)
466     return true
467   else
468     $logfile.printf("[fail (%s%s%s%s)]: %s", (a == a_) ? '' : 'a', (b == b_) ? '' : 'b', (c_ == c) ? '' : 'c', (d_== (a * b % c)) ? '' : 'd',line)
469     $logfile.printf(" ; should (%s * %s) %% %s = %s\n", a.to_s(16), b.to_s(16), c.to_s(16), (a * b % c).to_s(16))
470     return false
471   end
472   return false
473 end
474
475 ################################################################################
476 # expmod_test                                                                  #
477 ################################################################################
478
479 def expmod_test(a,b,c)
480   begin
481     printf("[testing] expmod(%#x, %#x, %#x)\n",a,b,c) if $debug
482     line = $sp.gets()
483     line = "" if line==nil
484     puts("DBG got: "+line) if $debug
485     if /^Error:.*/.match(line)
486       puts line
487       return false
488     end
489   end while ! /[\s]*enter a:[\s]*/.match(line)
490   $sp.print(a.to_s(16)+" ")
491   begin
492     line = $sp.gets()
493     line = "" if line==nil
494     puts("DBG got: "+line) if $debug
495     if /^Error:.*/.match(line)
496       puts line
497       return false
498     end
499   end while ! /[\s]*enter b:[\s]*/.match(line)
500   $sp.print(b.to_s(16)+" ")
501   begin
502     line = $sp.gets()
503     line = "" if line==nil
504     puts("DBG got: "+line) if $debug
505     if /^Error:.*/.match(line)
506       puts line
507       return false
508     end
509   end while ! /[\s]*enter c:[\s]*/.match(line)
510   $sp.print(c.to_s(16)+" ")
511   line=''
512   begin
513     line_tmp = $sp.gets()
514     line_tmp = '' if line_tmp == nil
515     line += line_tmp
516     puts("DBG got: "+line) if $debug
517     if /^Error:/.match(line)
518       puts line
519       return false
520     end
521   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)
522   a_ = m[1].to_i(16)
523   b_ = m[2].to_i(16)
524   c_ = m[3].to_i(16)
525   d_ = m[4].to_i(16)
526   line.chomp!
527   if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
528     $logfile.printf("[pass]: %s\n", line)
529     return true
530   else
531     $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', (c_==c)?'':'c', (d_==expmod(a,b,c))?'':'d',line)
532     $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))
533     return false
534   end
535   return false
536 end
537
538 ################################################################################
539 # gcdext_test                                                                  #
540 ################################################################################
541
542 def gcdext_test(a,b)
543   $logfile.printf("[testing] gcdext(%s, %s)\n", a.to_s(16), b.to_s(16))
544   begin
545     line = $sp.gets()
546     line = "" if line==nil
547     puts("DBG got: "+line) if $debug
548     if /^Error:.*/.match(line)
549       puts line
550       return false
551     end
552   end while ! /[\s]*enter a:[\s]*/.match(line)
553   $sp.print(a.to_s(16)+" ")
554   begin
555     line = $sp.gets()
556     line = "" if line==nil
557     puts("DBG got: "+line) if $debug
558     if /^Error:.*/.match(line)
559       puts line
560       return false
561     end
562   end while ! /[\s]*enter b:[\s]*/.match(line)
563   $sp.print(b.to_s(16)+" ")
564   line=''
565   begin
566     line_tmp = $sp.gets()
567     line_tmp = '' if line_tmp==nil
568     line = ''  if line.end_with?('\n')
569     line += line_tmp
570     puts("DBG got: "+line) if $debug
571     if /^Error:.*/.match(line)
572       puts line
573       return false
574     end
575   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)
576   a_ = m[1].to_i(16)
577   b_ = m[2].to_i(16)
578   c_ = m[3].to_i(16)
579   d_ = m[4].to_i(16)
580   e_ = m[5].to_i(16)
581   line.chomp!
582   line.gsub!("\r",'')
583   line.gsub!("\n",'')
584   ref = gcdext(a,b)
585   if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
586     $logfile.printf("[pass]: %s\n", line)
587     return true
588   else
589     $logfile.printf("[fail (%s%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', 
590        (c_==ref[1])?'':'c', (d_==ref[2])?'':'d', (e_==ref[0])?'':'e', line)
591     $logfile.printf(" ; should gcdext( %s, %s) => a = %s; b = %s; gcd = %s\n",
592        a.to_s(16), b.to_s(16), ref[1].to_s(16), ref[2].to_s(16), ref[0].to_s(16))
593     return false
594   end
595   return false
596 end
597
598 ################################################################################
599 # run_test_add                                                                 #
600 ################################################################################
601
602 def run_test_add(skip=0)
603   length_a_B = skip+1
604   length_b_B = skip+1
605   begin
606     $size = length_a_B
607     (0..16).each do |i|
608       s_a = rand(2)*2-1
609       s_b = rand(2)*2-1
610       a = rand(256**length_a_B) * s_a
611       b = rand(256**length_a_B) * s_b
612       v = add_test(a, b)
613       screen_progress(v)
614       v = add_test(b, a)
615       screen_progress(v)
616     end
617     (0..16).each do |i|
618       s_a = rand(2)-1
619       s_b = rand(2)-1
620       b_size = rand(length_b_B+1)
621       a = rand(256**length_a_B) * s_a
622       b = rand(256**b_size) * s_b
623       v = add_test(a, b)
624       screen_progress(v)      
625       v = add_test(b, a)
626       screen_progress(v)
627
628     end
629     length_a_B += 1
630     length_b_B += 1
631   end while length_a_B<4096/8
632 end
633
634 ################################################################################
635 # run_test_add_scale                                                           #
636 ################################################################################
637
638 def run_test_add_scale(skip=0)
639   length_a_B = skip+1
640   length_b_B = skip+1
641   begin
642     $size = length_a_B
643     (0..4).each do |i|
644       scales = [0, 300]
645       16.times { scales << rand(301) }
646       scales.sort!
647       scales.each do |scale|
648         a = rand(256**length_a_B)
649         b = rand(256**length_a_B)
650         v = add_scale_test(a, b, scale)
651         screen_progress(v)
652         v = add_scale_test(b, a, scale)
653         screen_progress(v)
654       end
655     end
656     (0..4).each do |i|
657       scales = [0, 300]
658       16.times { scales << rand(301) }
659       scales.sort!
660       scales.each do |scale|
661         b_size = rand(length_b_B+1)+1
662         a = rand(256**length_a_B)
663         b = rand(256**b_size)
664         v = add_scale_test(a, b, scale)
665         screen_progress(v)      
666         v = add_scale_test(b, a, scale)
667         screen_progress(v)
668       end
669     end
670     length_a_B += 10
671     length_b_B += 10
672   end while length_a_B<4096/8
673 end
674
675 def run_test_add_scale_dummy(skip=0)
676   length_a_B = skip+1
677   length_b_B = skip+1
678   begin
679     $size = length_a_B
680     (0..4).each do |i|
681       scales = [0, 300]
682       16.times { scales << rand(301) }
683       scales.sort!
684       scales.each do |scale|
685         a = rand(256**length_a_B)
686         b = rand(256**length_a_B)
687         v = add_scale_test_dummy(a, b, scale)
688         v = add_scale_test_dummy(b, a, scale)
689       end
690     end
691     (0..4).each do |i|
692       scales = [0, 300]
693       16.times { scales << rand(301) }
694       scales.sort!
695       scales.each do |scale|
696         b_size = rand(length_b_B+1)
697         a = rand(256**length_a_B)
698         b = rand(256**b_size)
699         v = add_scale_test_dummy(a, b, scale)
700         v = add_scale_test_dummy(b, a, scale)
701       end
702     end
703     length_a_B += 10
704     length_b_B += 10
705   end while length_a_B<4096/8
706 end
707
708 ################################################################################
709 # run_test_mul                                                                 #
710 ################################################################################
711
712 def run_test_mul(skip=0)
713   length_a_B = skip+1
714   length_b_B = skip+1
715   begin
716     $size = length_a_B
717     (0..16).each do |i|
718       s_a = rand(2)*2-1
719       s_b = rand(2)*2-1
720       a = rand(256**length_a_B) * s_a
721       b = rand(256**length_a_B) * s_b
722       v = mul_test(a, b)
723       screen_progress(v)
724       v = mul_test(b, a)
725       screen_progress(v)
726     end
727     (0..16).each do |i|
728       s_a = rand(2)-1
729       s_b = rand(2)-1
730       b_size = rand(length_b_B+1)
731       a = rand(256**length_a_B) * s_a
732       b = rand(256**b_size) * s_b
733       v = mul_test(a, b)
734       screen_progress(v)      
735       v = mul_test(b, a)
736       screen_progress(v)
737     end
738     length_a_B += 1
739     length_b_B += 1
740   end while length_a_B<4096/8
741 end
742
743 ################################################################################
744 # run_test_mul_word                                                                 #
745 ################################################################################
746
747 def run_test_mul_word(skip=0)
748   length_a_B = skip+1
749   length_b_B = skip+1
750   begin
751     $size = length_a_B
752     (0..255).each do |i|
753       a = rand(256 ** length_a_B) 
754       v = mul_test(a, i)
755       screen_progress(v)   
756     end
757     length_a_B += 1
758   end while length_a_B < 4096 / 8
759 end
760
761 ################################################################################
762 # run_test_square                                                              #
763 ################################################################################
764
765 def run_test_square(skip=0)
766   length_a_B = skip+1
767   begin
768     $size = length_a_B
769     (0..16).each do |i|
770       a = rand(256**length_a_B)
771       v = square_test(a)
772       screen_progress(v)
773     end
774     length_a_B += 1
775   end while length_a_B<4096/8
776 end
777
778 ################################################################################
779 # run_test_reduce                                                              #
780 ################################################################################
781
782 def run_test_reduce(skip=0)
783   length_a_B = skip+1
784   length_b_B = skip+1
785   begin
786     $size = length_a_B
787     (0..16).each do |i|
788       a = rand(256**length_a_B)
789       b = rand(256**length_a_B)+1
790       v = reduce_test(a, b)
791       screen_progress(v)
792       end
793     (0..16).each do |i|
794       b_size = rand(length_b_B+1)
795       a = rand(256**length_a_B)
796       b = rand(256**b_size)+1 
797       v = reduce_test(a, b)
798       screen_progress(v)      
799       end
800     length_a_B += 1
801     length_b_B += 1
802   end while length_a_B<4096/8
803 end
804
805 ################################################################################
806 # run_test_expmod                                                              #
807 ################################################################################
808
809 def run_test_expmod(skip=0)
810   length_a_B = skip + 1
811   length_b_B = skip + 1
812   length_c_B = skip + 1
813   begin
814     $size = length_a_B
815     (0..16).each do |i|
816       a = rand(256 ** length_a_B)
817       b = rand(256 ** length_b_B) + 1
818       c = rand(256 ** length_c_B) + 1
819       v = expmod_test(a, b, c)
820       screen_progress(v)
821       end
822     (0..16).each do |i|
823       b_size = rand(length_b_B+1)
824       a = rand(256 ** length_a_B)
825       b = rand(256 ** b_size) + 1 
826       c = rand(256 ** b_size) + 1
827       v = expmod_test(a, b, c)
828       screen_progress(v)      
829       end
830     length_a_B += 1
831     length_b_B += 1
832   end while length_a_B<4096/8
833 end
834
835 ################################################################################
836 # run_test_expmodmont                                                          #
837 ################################################################################
838
839 def run_test_expmodmont(skip=0)
840   length_a_B = skip + 1
841   length_b_B = skip + 1
842   length_c_B = skip + 1
843   begin
844     $size = length_a_B
845     (0..16).each do |i|
846       a = rand(256 ** length_a_B)
847       b = rand(256 ** length_b_B) + 1
848       c = rand(256 ** length_c_B) / 2 * 2 +1
849       v = expmod_test(a, b, c)
850       screen_progress(v)
851       end
852     (0..16).each do |i|
853       b_size = rand(length_b_B + 10)
854       a = rand(256 ** length_a_B)
855       b = rand(256 ** b_size) + 1 
856       c = rand(256 ** b_size) / 2 * 2 +1
857       v = expmod_test(a, b, c)
858       screen_progress(v)      
859       end
860     length_a_B += 1
861     length_b_B += 1
862   end while length_a_B<4096/8
863 end
864
865 ################################################################################
866 # run_test_mulmod                                                              #
867 ################################################################################
868
869 def run_test_mulmod(skip=0)
870   length_a_B = skip+1
871   length_b_B = skip+1
872   length_c_B = skip+1
873   begin
874     $size = length_a_B
875     (0..16).each do |i|
876       a = rand(256**length_a_B)
877       b = rand(256**length_b_B)
878       c = (rand(256**length_c_B) / 2 * 2) + 1
879       a %= c
880       b %= c
881       v = mulmod_test(a, b, c)
882       screen_progress(v)
883       end
884     (0..16).each do |i|
885       b_size = rand(length_b_B+1)
886       a = rand(256**length_a_B)
887       b = rand(256**b_size)
888       c = (rand(256**length_c_B) / 2 * 2) + 1
889       a %= c
890       b %= c
891       v = mulmod_test(a, b, c)
892       screen_progress(v)      
893       end
894     length_a_B += 1
895     length_b_B += 1
896     length_c_B += 1
897   end while length_a_B<4096/8
898 end
899
900 ################################################################################
901 # run_test_gcdext                                                              #
902 ################################################################################
903
904 def run_test_gcdext(skip=0)
905   length_a_B = skip+1
906   length_b_B = skip+1
907   begin
908     $size = length_a_B
909     (0..16).each do |i|
910       a = rand(256**length_a_B)
911       b = rand(256**length_a_B)+1
912       v = gcdext_test(a, b)
913       $logfile.flush()
914       screen_progress(v)
915       end
916     (0..16).each do |i|
917       b_size = rand(length_b_B+1)
918       a = rand(256**length_a_B)
919       b = rand(256**b_size)+1 
920       v = gcdext_test(a, b)
921       $logfile.flush()
922       screen_progress(v)      
923       end
924     length_a_B += 1
925     length_b_B += 1
926   end while length_a_B<4096/8
927 end
928
929 def init_serialport(conf)
930   puts("serial port interface version: " + SerialPort::VERSION);
931   $linewidth = 64
932   $linepos = 0
933   $testno = 0
934   params = { "baud"       => conf["PORT"]["baud"].to_i,
935               "data_bits" => conf["PORT"]["databits"].to_i,
936               "stop_bits" => conf["PORT"]["stopbits"].to_i,
937               "parity"    => SerialPort::NONE }
938   params["paraty"] = SerialPort::ODD   if conf["PORT"]["paraty"].downcase == "odd"
939   params["paraty"] = SerialPort::EVEN  if conf["PORT"]["paraty"].downcase == "even"
940   params["paraty"] = SerialPort::MARK  if conf["PORT"]["paraty"].downcase == "mark"
941   params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
942   
943   puts("\nPort: "+conf["PORT"]["port"]+"@"    +
944                   params["baud"].to_s      +
945                   " "                      +
946                   params["data_bits"].to_s +
947                   conf["PORT"]["paraty"][0,1].upcase +
948                   params["stop_bits"].to_s +
949                   "\n")
950   
951   $sp = SerialPort.new(conf["PORT"]["port"], params)
952   
953   $sp.read_timeout=1000; # 5 minutes
954   $sp.flow_control = SerialPort::SOFT
955   
956   reset_system()
957 end
958
959 ################################################################################
960 # MAIN                                                                         #
961 ################################################################################
962
963 opts = Getopt::Std.getopts("s:f:i:a:hd")
964
965 conf = Hash.new
966 conf = readconfigfile("/etc/testport.conf", conf)
967 conf = readconfigfile("~/.testport.conf", conf)
968 conf = readconfigfile("testport.conf", conf)
969 conf = readconfigfile(opts["f"], conf) if opts["f"]
970
971 #puts conf.inspect
972 init_serialport(conf)
973
974   $max_timeout = 5 * 60
975
976 if opts['d']
977   $debug = true
978 end
979
980 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
981 if File.exists?(logfilename)
982   i=1
983   begin
984     logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
985     i+=1
986   end while(File.exists?(logfilename))
987   while(i>2) do
988     n1 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt')
989     n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt')
990     File.rename(n1, n2)
991 #    printf("%s -> %s\n", n1, n2) 
992     i-=1
993   end
994   n1 = sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt')
995   n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt')
996   File.rename(n1, n2) 
997   printf("%s -> %s\n", n1, n2)  
998   logfilename = conf['PORT']['testlogbase']+'bigint.txt' 
999 end
1000 $logfile = File.open(logfilename, 'w')
1001 printf("logfile: %s\n", logfilename)
1002 $logfile.sync = true
1003 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
1004 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
1005 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
1006
1007 tests = Hash.new
1008 tests['a'] = proc {|x| run_test_add(x) }
1009 tests['m'] = proc {|x| run_test_mul(x) }
1010 tests['M'] = proc {|x| run_test_mulmod(x) }
1011 tests['n'] = proc {|x| run_test_mul_word(x) }
1012 tests['x'] = proc {|x| run_test_add_scale(x) }
1013 tests['s'] = proc {|x| run_test_square(x) }
1014 tests['r'] = proc {|x| run_test_reduce(x) }
1015 tests['e'] = proc {|x| run_test_expmod(x) }
1016 tests['E'] = proc {|x| run_test_expmodmont(x) }
1017 tests['g'] = proc {|x| run_test_gcdext(x) }
1018 init_str = Hash.new
1019 init_str['a'] = 'add-test'
1020 init_str['x'] = 'add-scale-test'
1021 init_str['m'] = 'mul-test'
1022 init_str['M'] = 'mul-mont-test'
1023 init_str['n'] = 'mul-word-test'
1024 init_str['s'] = 'square-test'
1025 init_str['r'] = 'reduce-test'
1026 init_str['e'] = 'expmod-test'
1027 init_str['E'] = 'expmod-mont-test'
1028 init_str['g'] = 'gcdext-test'
1029
1030 srand(0xdeadbeef)
1031
1032 if opts['a']
1033   opts['a'].each_char do |x|
1034     if tests[x]
1035       puts init_str[x]
1036       init_system(init_str[x])
1037       tests[x].call(opts['s']?opts['s'].to_i():0) 
1038     else
1039       puts "no test defiened for '#{x}'"
1040     end  
1041   end
1042 else
1043   'amsrMeE'.each_char do |x|
1044     if tests[x]
1045       puts init_str[x]
1046       init_system(init_str[x])
1047       tests[x].call(opts['s']?opts['s'].to_i():0) 
1048     else
1049       puts "no test defiened for '#{x}'"
1050     end  
1051   end
1052 end
1053 1
1054
1055 $logile.close()