]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/bigint2_test.rb
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / host / bigint2_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 def max(a, b)
33   return a > b ? a : b
34 end
35
36 def min(a, b)
37   return a < b ? a : b
38 end
39
40 ################################################################################
41 # readconfigfile                                                               #
42 ################################################################################
43
44 def readconfigfile(fname, conf)
45   return conf if $conffile_check[fname]==1
46   $conffile_check[fname]=1
47   section = "default"
48   if ! File.exists?(fname)
49     return conf
50   end
51   file = File.open(fname, "r")
52   until file.eof
53     line = file.gets()
54         next if /[\s]*#/.match(line)
55         if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
56           section=m[1]
57           conf[m[1]] = Hash.new
58           next
59         end
60         next if ! /=/.match(line)
61         m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
62         if m[1]=="include"
63           Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
64         else
65           conf[section][m[1]] = m[2]
66         end
67   end
68   file.close()
69   return conf
70 end
71
72 ################################################################################
73 # expmod                                                                       #
74 ################################################################################
75
76 def expmod(base, power, mod)
77   result = 1
78   while power > 0
79     result = (result * base) % mod if power & 1 == 1
80     base = (base * base) % mod
81     power >>= 1;
82   end
83   return result
84 end
85
86 ################################################################################
87 # gcdext                                                                 #
88 ################################################################################
89
90 def gcdext(x,y)
91   return  [0, 0, 0] if(x == 0 || y == 0)
92   g=1
93   while(x&1==0 && y&1==0) do
94     x>>=1
95     y>>=1
96     g<<=1
97   end
98   u=x; v=y; a=1; b=0; c=0; d=1
99   begin
100     while(u&1==0) do
101       if(a%2==1 || b%2==1)
102         a += y
103         b -= x
104       end
105       u>>=1; a>>=1; b>>=1
106     end
107     while(v&1==0) do
108       if(c%2==1 || d%2==1)
109         c += y
110         d -= x
111       end
112       v>>=1; c>>=1; d>>=1;
113     end
114     if(u>=v)
115       u -= v; a-=c; b-=d
116     else
117       v -= u; c-=a; d-=b
118     end
119   end while(u!=0)
120   return[g*v, c, d]
121 end
122
123 ################################################################################
124 # reset_system                                                                 #
125 ################################################################################
126
127 def reset_system
128   $sp.print("exit\r")
129   sleep 0.1
130   $sp.print("exit\r")
131   sleep 0.1
132 end
133
134 ################################################################################
135 # init_system                                                                  #
136 ################################################################################
137
138 def init_system(test_prog)
139   begin
140     line = $sp.gets()
141   end while line!=nil
142   $sp.print("echo off \r")
143   print("DBG i: " + "echo off \r"+"\n") if $debug
144   sleep 0.1
145   $sp.print("#{test_prog}\r")
146   print("DBG i: " + "#{test_prog} \r"+"\n") if $debug
147   sleep 1
148 end
149
150 ################################################################################
151 # wait_for_prompt                                                                  #
152 ################################################################################
153
154 def wait_for_prompt(prompt)
155   prompt = /[\s]*#{prompt}[\s]*/ if(prompt.class == String)
156   start_time = Time.now.to_i
157   acc = ''
158   begin
159     line = $sp.gets()
160     puts("DBG got (#{__LINE__}): "+line) if $debug && line
161     line = "" if line==nil
162     if /^(Error:|Crypto-VS).*/.match(line)
163       puts line
164       return false
165     end
166     if (Time.now.to_i- start_time) > $max_timeout
167       return false
168     end
169   acc += line
170   end while ! m=prompt.match(acc)
171   return m
172 end
173
174 ################################################################################
175 # screen_progress                                                              #
176 ################################################################################
177 def screen_progress(v)
178   if $linepos == 0
179     printf("\n%5d [%04d]: ", $testno, $size)
180   end
181   putc((v)?('*'):('!'))
182   $testno += 1
183   $linepos = ($linepos+1) % $linewidth
184 end
185
186 ################################################################################
187 # add_test                                                                     #
188 ################################################################################
189
190 def add_test(a,b)
191   begin
192     line = $sp.gets()
193     line = "" if line==nil
194     puts("DBG got: "+line) if $debug
195     if /^Error:.*/.match(line)
196       puts line
197       return false
198     end
199   end while ! /[\s]*enter a:[\s]*/.match(line)
200   $sp.print(a.to_s(16)+" ")
201   begin
202     line = $sp.gets()
203     line = "" if line==nil
204     puts("DBG got: "+line) if $debug
205     if /^Error:.*/.match(line)
206       puts line
207       return false
208     end
209   end while ! /[\s]*enter b:[\s]*/.match(line)
210   $sp.print(b.to_s(16)+" ")
211   begin
212     line = $sp.gets()
213     line = "" if line==nil
214     puts("DBG got: "+line) if $debug
215     if /^Error:.*/.match(line)
216       puts line
217       return false
218     end
219   end while ! m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
220   a_ = m[1].to_i(16)
221   b_ = m[2].to_i(16)
222   c_ = m[3].to_i(16)
223   line.chomp!
224   if(a_== a && b_ == b && c_ == (a+b))
225     $logfile.printf("[pass (%d)]: %s\n", $testno, line)
226     return true
227   else
228     $logfile.printf("[fail (%s%s%s) (%d)]: %s", 
229       (a == a_) ? "" : "a", 
230       (b == b_) ? "" : "b", 
231       (c_ == a + b) ?  "" : "c", $testno, line)
232     $logfile.printf(" ; should %s + %s = %s\n", a.to_s(16), b.to_s(16), (a+b).to_s(16))
233     return false
234   end
235   return false
236 end
237
238 ################################################################################
239 # sub_test                                                                     #
240 ################################################################################
241
242 def sub_test(a, b)
243   begin
244     line = $sp.gets()
245     line = "" if line==nil
246     puts("DBG got: "+line) if $debug
247     if /^Error:.*/.match(line)
248       puts line
249       return false
250     end
251   end while ! /[\s]*enter a:[\s]*/.match(line)
252   $sp.print(a.to_s(16)+" ")
253   begin
254     line = $sp.gets()
255     line = "" if line==nil
256     puts("DBG got: "+line) if $debug
257     if /^Error:.*/.match(line)
258       puts line
259       return false
260     end
261   end while ! /[\s]*enter b:[\s]*/.match(line)
262   $sp.print(b.to_s(16)+" ")
263   begin
264     line = $sp.gets()
265     line = "" if line==nil
266     puts("DBG got: "+line) if $debug
267     if /^Error:.*/.match(line)
268       puts line
269       return false
270     end
271   end while ! m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\-[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
272   a_ = m[1].to_i(16)
273   b_ = m[2].to_i(16)
274   c_ = m[3].to_i(16)
275   line.chomp!
276   if(a_== a && b_ == b && c_ == (a - b))
277     $logfile.printf("[pass (%d)]: %s\n", $testno, line)
278     return true
279   else
280     $logfile.printf("[fail (%s%s%s) (%d)]: %s", 
281       (a == a_) ? "" : "a", 
282       (b == b_) ? "" : "b", 
283       (c_ == a - b) ? "" : "c", $testno, line)
284     $logfile.printf(" ; should %s - %s = %s\n", a.to_s(16), b.to_s(16), (a - b).to_s(16))
285     return false
286   end
287   return false
288 end
289
290 ################################################################################
291 # mul_test                                                                     #
292 ################################################################################
293
294 def mul_test(a,b)
295   begin
296     line = $sp.gets()
297     line = "" if line==nil
298     puts("DBG got: "+line) if $debug
299     if /^Error:.*/.match(line)
300       puts line
301       return false
302     end
303   end while ! /[\s]*enter a:[\s]*/.match(line)
304   $sp.print(a.to_s(16)+" ")
305   begin
306     line = $sp.gets()
307     line = "" if line==nil
308     puts("DBG got: "+line) if $debug
309     if /^Error:.*/.match(line)
310       puts line
311       return false
312     end
313   end while ! /[\s]*enter b:[\s]*/.match(line)
314   $sp.print(b.to_s(16)+" ")
315   begin
316     line = $sp.gets()
317     line = "" if line==nil
318     puts("DBG got: "+line) if $debug
319     if /^Error:.*/.match(line)
320       puts line
321       return false
322     end
323   end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+\*[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
324   a_ = m[1].to_i(16)
325   b_ = m[2].to_i(16)
326   c_ = m[3].to_i(16)
327   line.chomp!
328   if(a_== a && b_ == b && c_ == (a*b))
329     $logfile.printf("[pass]: %s\n", line)
330     return true
331   else
332     $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a*b)?"":"c",line)
333     $logfile.printf(" ; should %s * %s = %s\n", a.to_s(16), b.to_s(16), (a*b).to_s(16))
334     return false
335   end
336   return false
337 end
338
339 ################################################################################
340 # add_scale_test                                                               #
341 ################################################################################
342 def add_scale_test_dummy(a, b, scale)
343   should = a + (b<<(8*scale))
344   printf("[dummy] %s + %s <<8*%04x = %s\n",a.to_s(16), b.to_s(16), scale, should.to_s(16))
345 end
346
347 def add_scale_test(a, b, scale)
348   m = wait_for_prompt("enter a:")
349   return false if !m 
350   puts("DBG put (#{__LINE__}): "+a.to_s(16)+" ") if $debug
351   $sp.print(a.to_s(16)+" ")
352   m = wait_for_prompt("enter b:")
353   return false if !m 
354   puts("DBG put (#{__LINE__}): "+b.to_s(16)+" ") if $debug
355   $sp.print(b.to_s(16)+" ")
356   m = wait_for_prompt("enter scale:")
357   return false if !m 
358   puts("DBG put (#{__LINE__}): "+scale.to_s(10)+" ") if $debug
359   $sp.print(scale.to_s(10)+"\r")
360   should = a + (b<<(8*scale))
361   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]+)/)
362   if !m 
363     $logfile.printf("[fail (CRASH)]:")
364     $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))
365     return false
366   end 
367   line = m[0]
368   a_ = m[1].to_i(16)
369   b_ = m[2].to_i(16)
370   s_ = m[3].to_i(16)
371   c_ = m[4].to_i(16)
372   line.chomp!
373   if(a_== a && b_ == b && s_ == scale && c_ == should )
374     $logfile.printf("[pass]: %s\n", line)
375     return true
376   else
377     $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (scale==s_)?"":"s",(c_==should)?"":"c",line)
378     $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))
379     return false
380   end
381   return false
382 end
383
384 ################################################################################
385 # square_test                                                                  #
386 ################################################################################
387
388 def square_test(a)
389   begin
390     line = $sp.gets()
391     line = "" if line==nil
392     puts("DBG got: "+line) if $debug
393     if /^Error:.*/.match(line)
394       puts line
395       return false
396     end
397   end while ! /[\s]*enter a:[\s]*/.match(line)
398   $sp.print(a.to_s(16)+" ")
399   begin
400     line = $sp.gets()
401     line = "" if line==nil
402     puts("DBG got: "+line) if $debug
403     if /^Error:.*/.match(line)
404       puts line
405       return false
406     end
407   end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
408   a_ = m[1].to_i(16)
409   c_ = m[2].to_i(16)
410   line.chomp!
411   if(a_== a && c_ == (a**2))
412     $logfile.printf("[pass]: %s\n", line)
413     return true
414   else
415     $logfile.printf("[fail (%s%s)]: %s", (a==a_)?"":"a", (c_==a**2)?"":"c",line)
416     $logfile.printf(" ; should %s **2 = %s\n", a.to_s(16), (a**2).to_s(16))
417     return false
418   end
419   return false
420 end
421
422 ################################################################################
423 # reduce_test                                                                  #
424 ################################################################################
425
426 def reduce_test(a,b)
427   begin
428     line = $sp.gets()
429     line = "" if line==nil
430     puts("DBG got: "+line) if $debug
431     if /^Error:.*/.match(line)
432       puts line
433       return false
434     end
435   end while ! /[\s]*enter a:[\s]*/.match(line)
436   $sp.print(a.to_s(16)+" ")
437   begin
438     line = $sp.gets()
439     line = "" if line==nil
440     puts("DBG got: "+line) if $debug
441     if /^Error:.*/.match(line)
442       puts line
443       return false
444     end
445   end while ! /[\s]*enter b:[\s]*/.match(line)
446   $sp.print(b.to_s(16)+" ")
447   line=''
448   begin
449     line_tmp = $sp.gets()
450     line_tmp = '' if line_tmp==nil
451     line += line_tmp
452     puts("DBG got: "+line) if $debug
453     if /^Error:.*/.match(line)
454       puts line
455       return false
456     end
457   end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
458   a_ = m[1].to_i(16)
459   b_ = m[2].to_i(16)
460   c_ = m[3].to_i(16)
461   line.strip!
462   if(a_== a && b_ == b && c_ == (a%b))
463     $logfile.printf("[pass (%d)]: %s\n", $testno, line)
464     return true
465   else
466     $logfile.printf("[fail (%s%s%s) (%d)]: %s", 
467       (a == a_) ? "" : "a", 
468       (b == b_) ? "" : "b", 
469       (c_ == a % b) ? "" : "c", $testno, line)
470     $logfile.printf(" ; should %s %% %s = %s\n", a.to_s(16), b.to_s(16), (a % b).to_s(16))
471     return false
472   end
473   return false
474 end
475
476 ################################################################################
477 # div_test                                                                  #
478 ################################################################################
479
480 def div_test(a, b)
481   begin
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   line = ''
502   begin
503     line_tmp = $sp.gets()
504     line_tmp = '' if line_tmp == nil
505     line += line_tmp
506     puts("DBG got: " + line) if $debug
507     if /^Error:.*/.match(line)
508       puts line
509       return false
510     end
511   end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+\/[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+);[\s]*R[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
512   a_ = m[1].to_i(16)
513   b_ = m[2].to_i(16)
514   c_ = m[3].to_i(16)
515   d_ = m[4].to_i(16)
516   line.strip!
517   if (a_== a && b_ == b && c_ == (a / b) && d_ == (a % b))
518     $logfile.printf("[pass (%d)]: %s\n", $testno, line)
519     return true
520   else
521     $logfile.printf("[fail (%s%s%s%s) (%d)]: %s", 
522       (a == a_) ? "" : "a", 
523       (b == b_) ? "" : "b", 
524       (c_ == a / b) ? "" : "c",
525       (d_ == a % b) ? "" : "d", $testno, line)
526     $logfile.printf(" ; should %s %% %s = %s; R = %s\n", a.to_s(16), b.to_s(16), (a / b).to_s(16), (a % b).to_s(16))
527     return false
528   end
529   return false
530 end
531
532 ################################################################################
533 # mulmod_test                                                                  #
534 ################################################################################
535
536 def mulmod_test(a,b,c)
537   begin
538     printf("[testing] mulmod(%#x, %#x, %#x)\n",a,b,c) if $debug
539     line = $sp.gets()
540     line = "" if line==nil
541     puts("DBG got: "+line) if $debug
542     if /^Error:.*/.match(line)
543       puts line
544       return false
545     end
546   end while ! /[\s]*enter a:[\s]*/.match(line)
547   $sp.print(a.to_s(16)+" ")
548   begin
549     line = $sp.gets()
550     line = "" if line==nil
551     puts("DBG got: "+line) if $debug
552     if /^Error:.*/.match(line)
553       puts line
554       return false
555     end
556   end while ! /[\s]*enter b:[\s]*/.match(line)
557   $sp.print(b.to_s(16)+" ")
558   begin
559     line = $sp.gets()
560     line = "" if line==nil
561     puts("DBG got: "+line) if $debug
562     if /^Error:.*/.match(line)
563       puts line
564       return false
565     end
566   end while ! /[\s]*enter c:[\s]*/.match(line)
567   $sp.print(c.to_s(16)+" ")
568   line=''
569   begin
570     line_tmp = $sp.gets()
571     line_tmp = '' if line_tmp==nil
572     line += line_tmp
573     puts("DBG got: "+line) if $debug
574     if /^Error:.*/.match(line)
575       puts line
576       return false
577     end
578     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)
579     puts("DBG: line did not match pattern (" + line + ")") if !m && $debug
580   end while ! m 
581   a_ = m[1].to_i(16)
582   b_ = m[2].to_i(16)
583   c_ = m[3].to_i(16)
584   d_ = m[4].to_i(16)
585   line.chomp!
586   if(a_== a && b_ == b && c_ == c && d_ == (a * b % c) )
587     $logfile.printf("[pass]: %s\n", line)
588     return true
589   else
590     $logfile.printf("[fail (%s%s%s%s)]: %s", (a == a_) ? '' : 'a', (b == b_) ? '' : 'b', (c_ == c) ? '' : 'c', (d_== (a * b % c)) ? '' : 'd',line)
591     $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))
592     return false
593   end
594   return false
595 end
596
597 ################################################################################
598 # expmod_test                                                                  #
599 ################################################################################
600
601 def expmod_test(a,b,c)
602   begin
603     printf("[testing] expmod(%#x, %#x, %#x)\n",a,b,c) if $debug
604     line = $sp.gets()
605     line = "" if line==nil
606     puts("DBG got: "+line) if $debug
607     if /^Error:.*/.match(line)
608       puts line
609       return false
610     end
611   end while ! /[\s]*enter a:[\s]*/.match(line)
612   $sp.print(a.to_s(16)+" ")
613   begin
614     line = $sp.gets()
615     line = "" if line==nil
616     puts("DBG got: "+line) if $debug
617     if /^Error:.*/.match(line)
618       puts line
619       return false
620     end
621   end while ! /[\s]*enter b:[\s]*/.match(line)
622   $sp.print(b.to_s(16)+" ")
623   begin
624     line = $sp.gets()
625     line = "" if line==nil
626     puts("DBG got: "+line) if $debug
627     if /^Error:.*/.match(line)
628       puts line
629       return false
630     end
631   end while ! /[\s]*enter c:[\s]*/.match(line)
632   $sp.print(c.to_s(16)+" ")
633   line=''
634   begin
635     line_tmp = $sp.gets()
636     line_tmp = '' if line_tmp == nil
637     line += line_tmp
638     puts("DBG got: "+line) if $debug
639     if /^Error:/.match(line)
640       puts line
641       return false
642     end
643   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)
644   a_ = m[1].to_i(16)
645   b_ = m[2].to_i(16)
646   c_ = m[3].to_i(16)
647   d_ = m[4].to_i(16)
648   line.chomp!
649   if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
650     $logfile.printf("[pass]: %s\n", line)
651     return true
652   else
653     $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', (c_==c)?'':'c', (d_==expmod(a,b,c))?'':'d',line)
654     $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))
655     return false
656   end
657   return false
658 end
659
660 ################################################################################
661 # gcdext_test                                                                  #
662 ################################################################################
663
664 def gcdext_test(a,b)
665   $logfile.printf("[testing] gcdext(%s, %s)\n", a.to_s(16), b.to_s(16))
666   begin
667     line = $sp.gets()
668     line = "" if line==nil
669     puts("DBG got: "+line) if $debug
670     if /^Error:.*/.match(line)
671       puts line
672       return false
673     end
674   end while ! /[\s]*enter a:[\s]*/.match(line)
675   $sp.print(a.to_s(16)+" ")
676   begin
677     line = $sp.gets()
678     line = "" if line==nil
679     puts("DBG got: "+line) if $debug
680     if /^Error:.*/.match(line)
681       puts line
682       return false
683     end
684   end while ! /[\s]*enter b:[\s]*/.match(line)
685   $sp.print(b.to_s(16)+" ")
686   line=''
687   begin
688     line_tmp = $sp.gets()
689     line_tmp = '' if line_tmp==nil
690     line = ''  if line.end_with?('\n')
691     line += line_tmp
692     puts("DBG got: "+line) if $debug
693     if /^Error:.*/.match(line)
694       puts line
695       return false
696     end
697   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)
698   a_ = m[1].to_i(16)
699   b_ = m[2].to_i(16)
700   c_ = m[3].to_i(16)
701   d_ = m[4].to_i(16)
702   e_ = m[5].to_i(16)
703   line.chomp!
704   line.gsub!("\r",'')
705   line.gsub!("\n",'')
706   ref = gcdext(a,b)
707   if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
708     $logfile.printf("[pass (%d)]: %s\n", $testno, line)
709     return true
710   else
711     $logfile.printf("[fail (%s%s%s%s%s) (%d)]: %s", 
712       (a == a_) ? '' : 'a', 
713       (b == b_) ? '' : 'b', 
714       (c_ == ref[1]) ? '' : 'c', 
715       (d_ == ref[2]) ? '' : 'd', 
716       (e_ == ref[0]) ? '' : 'e', $testno, line)
717     $logfile.printf(" ; should gcdext( %s, %s) => a = %s; b = %s; gcd = %s\n",
718        a.to_s(16), b.to_s(16), ref[1].to_s(16), ref[2].to_s(16), ref[0].to_s(16))
719     return false
720   end
721   return false
722 end
723
724 ################################################################################
725 # run_test_add                                                                 #
726 ################################################################################
727
728 def run_test_add(skip=0)
729   length_a_B = skip+1
730   length_b_B = skip+1
731   begin
732     $size = length_a_B
733     (0..16).each do |i|
734       a = rand(256**length_a_B)
735       b = rand(256**length_a_B)
736       v = add_test(a, b)
737       screen_progress(v)
738       v = add_test(b, a)
739       screen_progress(v)
740     end
741     (0..16).each do |i|
742       b_size = rand(length_b_B+1)
743       a = rand(256**length_a_B)
744       b = rand(256**b_size)
745       v = add_test(a, b)
746       screen_progress(v)      
747       v = add_test(b, a)
748       screen_progress(v)
749     end
750     length_a_B += 1
751     length_b_B += 1
752   end while length_a_B<4096/8
753 end
754
755 ################################################################################
756 # run_test_sub                                                                 #
757 ################################################################################
758
759 def run_test_sub(skip = 0)
760   length_a_B = skip + 1
761   length_b_B = skip + 1
762   begin
763     $size = length_a_B
764     (0..16).each do |i|
765       a = rand(256 ** length_a_B)
766       b = rand(a + 1)
767       v = sub_test(a, b)
768       screen_progress(v)
769     end
770     (0..16).each do |i|
771       b_size = rand(length_b_B + 1)
772       a = rand(256 ** length_a_B)
773       b = rand(min(256 ** b_size, a + 1))
774       v = sub_test(a, b)
775       screen_progress(v)
776     end
777     length_a_B += 1
778     length_b_B += 1
779   end while length_a_B < 4096 / 8
780 end
781
782
783 ################################################################################
784 # run_test_add_scale                                                           #
785 ################################################################################
786
787 def run_test_add_scale(skip=0)
788   length_a_B = skip+1
789   length_b_B = skip+1
790   begin
791     $size = length_a_B
792     (0..4).each do |i|
793       scales = [0, 300]
794       16.times { scales << rand(301) }
795       scales.sort!
796       scales.each do |scale|
797         a = rand(256**length_a_B)
798         b = rand(256**length_a_B)
799         v = add_scale_test(a, b, scale)
800         screen_progress(v)
801         v = add_scale_test(b, a, scale)
802         screen_progress(v)
803       end
804     end
805     (0..4).each do |i|
806       scales = [0, 300]
807       16.times { scales << rand(301) }
808       scales.sort!
809       scales.each do |scale|
810         b_size = rand(length_b_B+1)+1
811         a = rand(256**length_a_B)
812         b = rand(256**b_size)
813         v = add_scale_test(a, b, scale)
814         screen_progress(v)      
815         v = add_scale_test(b, a, scale)
816         screen_progress(v)
817       end
818     end
819     length_a_B += 10
820     length_b_B += 10
821   end while length_a_B<4096/8
822 end
823
824 def run_test_add_scale_dummy(skip=0)
825   length_a_B = skip+1
826   length_b_B = skip+1
827   begin
828     $size = length_a_B
829     (0..4).each do |i|
830       scales = [0, 300]
831       16.times { scales << rand(301) }
832       scales.sort!
833       scales.each do |scale|
834         a = rand(256**length_a_B)
835         b = rand(256**length_a_B)
836         v = add_scale_test_dummy(a, b, scale)
837         v = add_scale_test_dummy(b, a, scale)
838       end
839     end
840     (0..4).each do |i|
841       scales = [0, 300]
842       16.times { scales << rand(301) }
843       scales.sort!
844       scales.each do |scale|
845         b_size = rand(length_b_B+1)
846         a = rand(256**length_a_B)
847         b = rand(256**b_size)
848         v = add_scale_test_dummy(a, b, scale)
849         v = add_scale_test_dummy(b, a, scale)
850       end
851     end
852     length_a_B += 10
853     length_b_B += 10
854   end while length_a_B<4096/8
855 end
856
857 ################################################################################
858 # run_test_mul                                                                 #
859 ################################################################################
860
861 def run_test_mul(skip=0)
862   length_a_B = skip+1
863   length_b_B = skip+1
864   begin
865     $size = length_a_B
866     (0..16).each do |i|
867       s_a = rand(2)*2-1
868       s_b = rand(2)*2-1
869       a = rand(256**length_a_B) * s_a
870       b = rand(256**length_a_B) * s_b
871       v = mul_test(a, b)
872       screen_progress(v)
873       v = mul_test(b, a)
874       screen_progress(v)
875     end
876     (0..16).each do |i|
877       s_a = rand(2)-1
878       s_b = rand(2)-1
879       b_size = rand(length_b_B+1)
880       a = rand(256**length_a_B) * s_a
881       b = rand(256**b_size) * s_b
882       v = mul_test(a, b)
883       screen_progress(v)      
884       v = mul_test(b, a)
885       screen_progress(v)
886     end
887     length_a_B += 1
888     length_b_B += 1
889   end while length_a_B<4096/8
890 end
891
892 ################################################################################
893 # run_test_mul_word                                                                 #
894 ################################################################################
895
896 def run_test_mul_word(skip=0)
897   length_a_B = skip+1
898   length_b_B = skip+1
899   begin
900     $size = length_a_B
901     (0..255).each do |i|
902       a = rand(256 ** length_a_B) 
903       v = mul_test(a, i)
904       screen_progress(v)   
905     end
906     length_a_B += 1
907   end while length_a_B < 4096 / 8
908 end
909
910 ################################################################################
911 # run_test_square                                                              #
912 ################################################################################
913
914 def run_test_square(skip=0)
915   length_a_B = skip+1
916   begin
917     $size = length_a_B
918     (0..16).each do |i|
919       a = rand(256**length_a_B)
920       v = square_test(a)
921       screen_progress(v)
922     end
923     length_a_B += 1
924   end while length_a_B<4096/8
925 end
926
927 ################################################################################
928 # run_test_reduce                                                              #
929 ################################################################################
930
931 def run_test_reduce(skip=0)
932   length_a_B = skip+1
933   length_b_B = skip+1
934   begin
935     $size = length_a_B
936     (0..16).each do |i|
937       a = rand(256**length_a_B)
938       b = rand(256**length_a_B)+1
939       v = reduce_test(a, b)
940       screen_progress(v)
941       end
942     (0..16).each do |i|
943       b_size = rand(length_b_B+1)
944       a = rand(256**length_a_B)
945       b = rand(256**b_size)+1 
946       v = reduce_test(a, b)
947       screen_progress(v)      
948       end
949     length_a_B += 1
950     length_b_B += 1
951   end while length_a_B < 4096 / 8
952 end
953
954 ################################################################################
955 # run_test_div                                                              #
956 ################################################################################
957
958 def run_test_div(skip=0)
959   length_a_B = skip + 1
960   length_b_B = skip + 1
961   begin
962     $size = length_a_B
963     (0..16).each do |i|
964       a = rand(256 ** length_a_B)
965       b = rand(256 ** length_a_B) + 1
966       v = div_test(a, b)
967       screen_progress(v)
968       end
969     (0..16).each do |i|
970       b_size = rand(length_b_B + 1)
971       a = rand(256 ** length_a_B)
972       b = rand(256 ** b_size) + 1 
973       v = div_test(a, b)
974       screen_progress(v)      
975       end
976     length_a_B += 1
977     length_b_B += 1
978   end while length_a_B < 4096 / 8
979 end
980
981 ################################################################################
982 # run_test_expmod                                                              #
983 ################################################################################
984
985 def run_test_expmod(skip=0)
986   length_a_B = skip + 1
987   length_b_B = skip + 1
988   length_c_B = skip + 1
989   begin
990     $size = length_a_B
991     (0..16).each do |i|
992       a = rand(256 ** length_a_B)
993       b = rand(256 ** length_b_B) + 1
994       c = rand(256 ** length_c_B) + 1
995       v = expmod_test(a, b, c)
996       screen_progress(v)
997       end
998     (0..16).each do |i|
999       b_size = rand(length_b_B+1)
1000       a = rand(256 ** length_a_B)
1001       b = rand(256 ** b_size) + 1 
1002       c = rand(256 ** b_size) + 1
1003       v = expmod_test(a, b, c)
1004       screen_progress(v)      
1005       end
1006     length_a_B += 1
1007     length_b_B += 1
1008   end while length_a_B<4096/8
1009 end
1010
1011 ################################################################################
1012 # run_test_expmodmont                                                          #
1013 ################################################################################
1014
1015 def run_test_expmodmont(skip=0)
1016   length_a_B = skip + 1
1017   length_b_B = skip + 1
1018   length_c_B = skip + 1
1019   begin
1020     $size = length_a_B
1021     (0..16).each do |i|
1022       a = rand(256 ** length_a_B)
1023       b = rand(256 ** length_b_B) + 1
1024       c = rand(256 ** length_c_B) / 2 * 2 +1
1025       v = expmod_test(a, b, c)
1026       screen_progress(v)
1027       end
1028     (0..16).each do |i|
1029       b_size = rand(length_b_B + 10)
1030       a = rand(256 ** length_a_B)
1031       b = rand(256 ** b_size) + 1 
1032       c = rand(256 ** b_size) / 2 * 2 +1
1033       v = expmod_test(a, b, c)
1034       screen_progress(v)      
1035       end
1036     length_a_B += 1
1037     length_b_B += 1
1038   end while length_a_B<4096/8
1039 end
1040
1041 ################################################################################
1042 # run_test_mulmod                                                              #
1043 ################################################################################
1044
1045 def run_test_mulmod(skip=0)
1046   length_a_B = skip+1
1047   length_b_B = skip+1
1048   length_c_B = skip+1
1049   begin
1050     $size = length_a_B
1051     (0..16).each do |i|
1052       a = rand(256**length_a_B)
1053       b = rand(256**length_b_B)
1054       c = (rand(256**length_c_B) / 2 * 2) + 1
1055       a %= c
1056       b %= c
1057       v = mulmod_test(a, b, c)
1058       screen_progress(v)
1059       end
1060     (0..16).each do |i|
1061       b_size = rand(length_b_B+1)
1062       a = rand(256**length_a_B)
1063       b = rand(256**b_size)
1064       c = (rand(256**length_c_B) / 2 * 2) + 1
1065       a %= c
1066       b %= c
1067       v = mulmod_test(a, b, c)
1068       screen_progress(v)      
1069       end
1070     length_a_B += 1
1071     length_b_B += 1
1072     length_c_B += 1
1073   end while length_a_B<4096/8
1074 end
1075
1076 ################################################################################
1077 # run_test_gcdext                                                              #
1078 ################################################################################
1079
1080 def run_test_gcdext(skip=0)
1081   length_a_B = skip+1
1082   length_b_B = skip+1
1083   begin
1084     $size = length_a_B
1085     (0..16).each do |i|
1086       a = rand(256**length_a_B)
1087       b = rand(256**length_a_B)+1
1088       v = gcdext_test(a, b)
1089       $logfile.flush()
1090       screen_progress(v)
1091       end
1092     (0..16).each do |i|
1093       b_size = rand(length_b_B+1)
1094       a = rand(256**length_a_B)
1095       b = rand(256**b_size)+1 
1096       v = gcdext_test(a, b)
1097       $logfile.flush()
1098       screen_progress(v)      
1099       end
1100     length_a_B += 1
1101     length_b_B += 1
1102   end while length_a_B<4096/8
1103 end
1104
1105 def init_serialport(conf)
1106   puts("serial port interface version: " + SerialPort::VERSION);
1107   $linewidth = 64
1108   $linepos = 0
1109   $testno = 0
1110   params = { "baud"       => conf["PORT"]["baud"].to_i,
1111               "data_bits" => conf["PORT"]["databits"].to_i,
1112               "stop_bits" => conf["PORT"]["stopbits"].to_i,
1113               "parity"    => SerialPort::NONE }
1114   params["paraty"] = SerialPort::ODD   if conf["PORT"]["paraty"].downcase == "odd"
1115   params["paraty"] = SerialPort::EVEN  if conf["PORT"]["paraty"].downcase == "even"
1116   params["paraty"] = SerialPort::MARK  if conf["PORT"]["paraty"].downcase == "mark"
1117   params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
1118   
1119   puts("\nPort: "+conf["PORT"]["port"]+"@"    +
1120                   params["baud"].to_s      +
1121                   " "                      +
1122                   params["data_bits"].to_s +
1123                   conf["PORT"]["paraty"][0,1].upcase +
1124                   params["stop_bits"].to_s +
1125                   "\n")
1126   
1127   $sp = SerialPort.new(conf["PORT"]["port"], params)
1128   
1129   $sp.read_timeout = 100; # 5 minutes
1130   $sp.flow_control = SerialPort::SOFT
1131   
1132   reset_system()
1133 end
1134
1135 ################################################################################
1136 # MAIN                                                                         #
1137 ################################################################################
1138
1139 opts = Getopt::Std.getopts("r:s:f:i:a:hd")
1140
1141 conf = Hash.new
1142 conf = readconfigfile("/etc/testport.conf", conf)
1143 conf = readconfigfile("~/.testport.conf", conf)
1144 conf = readconfigfile("testport.conf", conf)
1145 conf = readconfigfile(opts["f"], conf) if opts["f"]
1146
1147 #puts conf.inspect
1148 init_serialport(conf)
1149
1150   $max_timeout = 5 * 60
1151
1152 if opts['d']
1153   $debug = true
1154 end
1155
1156 if opts['r']
1157   random_seed = opts['r'].to_i(0)
1158 else
1159   random_seed = 0xdeadbeef
1160 end
1161
1162 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
1163 if File.exists?(logfilename)
1164   i=1
1165   begin
1166     logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
1167     i+=1
1168   end while(File.exists?(logfilename))
1169   while(i>2) do
1170     n1 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt')
1171     n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt')
1172     File.rename(n1, n2)
1173 #    printf("%s -> %s\n", n1, n2) 
1174     i-=1
1175   end
1176   n1 = sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt')
1177   n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt')
1178   File.rename(n1, n2) 
1179   printf("%s -> %s\n", n1, n2)  
1180   logfilename = conf['PORT']['testlogbase']+'bigint.txt' 
1181 end
1182 $logfile = File.open(logfilename, 'w')
1183 printf("logfile: %s\n", logfilename)
1184 $logfile.sync = true
1185 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
1186 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
1187 $logfile.printf("seed = 0x%X\n", random_seed)
1188
1189 tests = Hash.new
1190 tests['a'] = proc {|x| run_test_add(x) }
1191 tests['b'] = proc {|x| run_test_sub(x) }
1192 tests['d'] = proc {|x| run_test_div(x) }
1193 tests['m'] = proc {|x| run_test_mul(x) }
1194 tests['M'] = proc {|x| run_test_mulmod(x) }
1195 tests['n'] = proc {|x| run_test_mul_word(x) }
1196 tests['x'] = proc {|x| run_test_add_scale(x) }
1197 tests['s'] = proc {|x| run_test_square(x) }
1198 tests['r'] = proc {|x| run_test_reduce(x) }
1199 tests['e'] = proc {|x| run_test_expmod(x) }
1200 tests['E'] = proc {|x| run_test_expmodmont(x) }
1201 tests['g'] = proc {|x| run_test_gcdext(x) }
1202 init_str = Hash.new
1203 init_str['a'] = 'add-test'
1204 init_str['b'] = 'sub-test'
1205 init_str['d'] = 'div-test'
1206 init_str['x'] = 'add-scale-test'
1207 init_str['m'] = 'mul-test'
1208 init_str['M'] = 'mul-mont-test'
1209 init_str['n'] = 'mul-word-test'
1210 init_str['s'] = 'square-test'
1211 init_str['r'] = 'reduce-test'
1212 init_str['e'] = 'expmod-test'
1213 init_str['E'] = 'expmod-mont-test'
1214 init_str['g'] = 'gcdext-test'
1215
1216 srand(random_seed)
1217
1218 if opts['a']
1219   opts['a'].each_char do |x|
1220     if tests[x]
1221       puts init_str[x]
1222       init_system(init_str[x])
1223       tests[x].call(opts['s']?opts['s'].to_i():0) 
1224     else
1225       puts "no test defined for '#{x}'"
1226     end  
1227   end
1228 else
1229   'amsrMeE'.each_char do |x|
1230     if tests[x]
1231       puts init_str[x]
1232       init_system(init_str[x])
1233       tests[x].call(opts['s']?opts['s'].to_i():0) 
1234     else
1235       puts "no test defined for '#{x}'"
1236     end  
1237   end
1238 end
1239 1
1240
1241 $logfile.close()