4 This file is part of the AVR-Crypto-Lib.
5 Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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.
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.
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/>.
29 $conffile_check = Hash.new
30 $conffile_check.default = 0
40 ################################################################################
42 ################################################################################
44 def readconfigfile(fname, conf)
45 return conf if $conffile_check[fname]==1
46 $conffile_check[fname]=1
48 if ! File.exists?(fname)
51 file = File.open(fname, "r")
54 next if /[\s]*#/.match(line)
55 if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
60 next if ! /=/.match(line)
61 m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
63 Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
65 conf[section][m[1]] = m[2]
72 ################################################################################
74 ################################################################################
76 def expmod(base, power, mod)
79 result = (result * base) % mod if power & 1 == 1
80 base = (base * base) % mod
86 ################################################################################
88 ################################################################################
91 return [0, 0, 0] if(x == 0 || y == 0)
93 while(x&1==0 && y&1==0) do
98 u=x; v=y; a=1; b=0; c=0; d=1
123 ################################################################################
125 ################################################################################
134 ################################################################################
136 ################################################################################
138 def init_system(test_prog)
142 $sp.print("echo off \r")
143 print("DBG i: " + "echo off \r"+"\n") if $debug
145 $sp.print("#{test_prog}\r")
146 print("DBG i: " + "#{test_prog} \r"+"\n") if $debug
150 ################################################################################
152 ################################################################################
154 def wait_for_prompt(prompt)
155 prompt = /[\s]*#{prompt}[\s]*/ if(prompt.class == String)
156 start_time = Time.now.to_i
160 puts("DBG got (#{__LINE__}): "+line) if $debug && line
161 line = "" if line==nil
162 if /^(Error:|Crypto-VS).*/.match(line)
166 if (Time.now.to_i- start_time) > $max_timeout
170 end while ! m=prompt.match(acc)
174 ################################################################################
176 ################################################################################
177 def screen_progress(v)
179 printf("\n%5d [%04d]: ", $testno, $size)
181 putc((v)?('*'):('!'))
183 $linepos = ($linepos+1) % $linewidth
186 ################################################################################
188 ################################################################################
193 line = "" if line==nil
194 puts("DBG got: "+line) if $debug
195 if /^Error:.*/.match(line)
199 end while ! /[\s]*enter a:[\s]*/.match(line)
200 $sp.print(a.to_s(16)+" ")
203 line = "" if line==nil
204 puts("DBG got: "+line) if $debug
205 if /^Error:.*/.match(line)
209 end while ! /[\s]*enter b:[\s]*/.match(line)
210 $sp.print(b.to_s(16)+" ")
213 line = "" if line==nil
214 puts("DBG got: "+line) if $debug
215 if /^Error:.*/.match(line)
219 end while ! m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
224 if(a_== a && b_ == b && c_ == (a+b))
225 $logfile.printf("[pass (%d)]: %s\n", $testno, line)
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))
238 ################################################################################
240 ################################################################################
245 line = "" if line==nil
246 puts("DBG got: "+line) if $debug
247 if /^Error:.*/.match(line)
251 end while ! /[\s]*enter a:[\s]*/.match(line)
252 $sp.print(a.to_s(16)+" ")
255 line = "" if line==nil
256 puts("DBG got: "+line) if $debug
257 if /^Error:.*/.match(line)
261 end while ! /[\s]*enter b:[\s]*/.match(line)
262 $sp.print(b.to_s(16)+" ")
265 line = "" if line==nil
266 puts("DBG got: "+line) if $debug
267 if /^Error:.*/.match(line)
271 end while ! m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\-[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
276 if(a_== a && b_ == b && c_ == (a - b))
277 $logfile.printf("[pass (%d)]: %s\n", $testno, line)
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))
290 ################################################################################
292 ################################################################################
297 line = "" if line==nil
298 puts("DBG got: "+line) if $debug
299 if /^Error:.*/.match(line)
303 end while ! /[\s]*enter a:[\s]*/.match(line)
304 $sp.print(a.to_s(16)+" ")
307 line = "" if line==nil
308 puts("DBG got: "+line) if $debug
309 if /^Error:.*/.match(line)
313 end while ! /[\s]*enter b:[\s]*/.match(line)
314 $sp.print(b.to_s(16)+" ")
317 line = "" if line==nil
318 puts("DBG got: "+line) if $debug
319 if /^Error:.*/.match(line)
323 end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+\*[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
328 if(a_== a && b_ == b && c_ == (a*b))
329 $logfile.printf("[pass]: %s\n", line)
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))
339 ################################################################################
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))
347 def add_scale_test(a, b, scale)
348 m = wait_for_prompt("enter a:")
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:")
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:")
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]+)/)
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))
373 if(a_== a && b_ == b && s_ == scale && c_ == should )
374 $logfile.printf("[pass]: %s\n", line)
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))
384 ################################################################################
386 ################################################################################
391 line = "" if line==nil
392 puts("DBG got: "+line) if $debug
393 if /^Error:.*/.match(line)
397 end while ! /[\s]*enter a:[\s]*/.match(line)
398 $sp.print(a.to_s(16)+" ")
401 line = "" if line==nil
402 puts("DBG got: "+line) if $debug
403 if /^Error:.*/.match(line)
407 end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
411 if(a_== a && c_ == (a**2))
412 $logfile.printf("[pass]: %s\n", line)
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))
422 ################################################################################
424 ################################################################################
429 line = "" if line==nil
430 puts("DBG got: "+line) if $debug
431 if /^Error:.*/.match(line)
435 end while ! /[\s]*enter a:[\s]*/.match(line)
436 $sp.print(a.to_s(16)+" ")
439 line = "" if line==nil
440 puts("DBG got: "+line) if $debug
441 if /^Error:.*/.match(line)
445 end while ! /[\s]*enter b:[\s]*/.match(line)
446 $sp.print(b.to_s(16)+" ")
449 line_tmp = $sp.gets()
450 line_tmp = '' if line_tmp==nil
452 puts("DBG got: "+line) if $debug
453 if /^Error:.*/.match(line)
457 end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
462 if(a_== a && b_ == b && c_ == (a%b))
463 $logfile.printf("[pass (%d)]: %s\n", $testno, line)
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))
476 ################################################################################
478 ################################################################################
483 line = "" if line==nil
484 puts("DBG got: "+line) if $debug
485 if /^Error:.*/.match(line)
489 end while ! /[\s]*enter a:[\s]*/.match(line)
490 $sp.print(a.to_s(16) + " ")
493 line = "" if line == nil
494 puts("DBG got: " + line) if $debug
495 if /^Error:.*/.match(line)
499 end while ! /[\s]*enter b:[\s]*/.match(line)
500 $sp.print(b.to_s(16) + " ")
503 line_tmp = $sp.gets()
504 line_tmp = '' if line_tmp == nil
506 puts("DBG got: " + line) if $debug
507 if /^Error:.*/.match(line)
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)
517 if (a_== a && b_ == b && c_ == (a / b) && d_ == (a % b))
518 $logfile.printf("[pass (%d)]: %s\n", $testno, line)
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))
532 ################################################################################
534 ################################################################################
536 def mulmod_test(a,b,c)
538 printf("[testing] mulmod(%#x, %#x, %#x)\n",a,b,c) if $debug
540 line = "" if line==nil
541 puts("DBG got: "+line) if $debug
542 if /^Error:.*/.match(line)
546 end while ! /[\s]*enter a:[\s]*/.match(line)
547 $sp.print(a.to_s(16)+" ")
550 line = "" if line==nil
551 puts("DBG got: "+line) if $debug
552 if /^Error:.*/.match(line)
556 end while ! /[\s]*enter b:[\s]*/.match(line)
557 $sp.print(b.to_s(16)+" ")
560 line = "" if line==nil
561 puts("DBG got: "+line) if $debug
562 if /^Error:.*/.match(line)
566 end while ! /[\s]*enter c:[\s]*/.match(line)
567 $sp.print(c.to_s(16)+" ")
570 line_tmp = $sp.gets()
571 line_tmp = '' if line_tmp==nil
573 puts("DBG got: "+line) if $debug
574 if /^Error:.*/.match(line)
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
586 if(a_== a && b_ == b && c_ == c && d_ == (a * b % c) )
587 $logfile.printf("[pass]: %s\n", line)
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))
597 ################################################################################
599 ################################################################################
601 def expmod_test(a,b,c)
603 printf("[testing] expmod(%#x, %#x, %#x)\n",a,b,c) if $debug
605 line = "" if line==nil
606 puts("DBG got: "+line) if $debug
607 if /^Error:.*/.match(line)
611 end while ! /[\s]*enter a:[\s]*/.match(line)
612 $sp.print(a.to_s(16)+" ")
615 line = "" if line==nil
616 puts("DBG got: "+line) if $debug
617 if /^Error:.*/.match(line)
621 end while ! /[\s]*enter b:[\s]*/.match(line)
622 $sp.print(b.to_s(16)+" ")
625 line = "" if line==nil
626 puts("DBG got: "+line) if $debug
627 if /^Error:.*/.match(line)
631 end while ! /[\s]*enter c:[\s]*/.match(line)
632 $sp.print(c.to_s(16)+" ")
635 line_tmp = $sp.gets()
636 line_tmp = '' if line_tmp == nil
638 puts("DBG got: "+line) if $debug
639 if /^Error:/.match(line)
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)
649 if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
650 $logfile.printf("[pass]: %s\n", line)
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))
660 ################################################################################
662 ################################################################################
665 $logfile.printf("[testing] gcdext(%s, %s)\n", a.to_s(16), b.to_s(16))
668 line = "" if line==nil
669 puts("DBG got: "+line) if $debug
670 if /^Error:.*/.match(line)
674 end while ! /[\s]*enter a:[\s]*/.match(line)
675 $sp.print(a.to_s(16)+" ")
678 line = "" if line==nil
679 puts("DBG got: "+line) if $debug
680 if /^Error:.*/.match(line)
684 end while ! /[\s]*enter b:[\s]*/.match(line)
685 $sp.print(b.to_s(16)+" ")
688 line_tmp = $sp.gets()
689 line_tmp = '' if line_tmp==nil
690 line = '' if line.end_with?('\n')
692 puts("DBG got: "+line) if $debug
693 if /^Error:.*/.match(line)
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)
707 if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
708 $logfile.printf("[pass (%d)]: %s\n", $testno, line)
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))
724 ################################################################################
726 ################################################################################
728 def run_test_add(skip=0)
734 a = rand(256**length_a_B)
735 b = rand(256**length_a_B)
742 b_size = rand(length_b_B+1)
743 a = rand(256**length_a_B)
744 b = rand(256**b_size)
752 end while length_a_B<4096/8
755 ################################################################################
757 ################################################################################
759 def run_test_sub(skip = 0)
760 length_a_B = skip + 1
761 length_b_B = skip + 1
765 a = rand(256 ** length_a_B)
771 b_size = rand(length_b_B + 1)
772 a = rand(256 ** length_a_B)
773 b = rand(min(256 ** b_size, a + 1))
779 end while length_a_B < 4096 / 8
783 ################################################################################
784 # run_test_add_scale #
785 ################################################################################
787 def run_test_add_scale(skip=0)
794 16.times { scales << rand(301) }
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)
801 v = add_scale_test(b, a, scale)
807 16.times { scales << rand(301) }
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)
815 v = add_scale_test(b, a, scale)
821 end while length_a_B<4096/8
824 def run_test_add_scale_dummy(skip=0)
831 16.times { scales << rand(301) }
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)
842 16.times { scales << rand(301) }
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)
854 end while length_a_B<4096/8
857 ################################################################################
859 ################################################################################
861 def run_test_mul(skip=0)
869 a = rand(256**length_a_B) * s_a
870 b = rand(256**length_a_B) * s_b
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
889 end while length_a_B<4096/8
892 ################################################################################
893 # run_test_mul_word #
894 ################################################################################
896 def run_test_mul_word(skip=0)
902 a = rand(256 ** length_a_B)
907 end while length_a_B < 4096 / 8
910 ################################################################################
912 ################################################################################
914 def run_test_square(skip=0)
919 a = rand(256**length_a_B)
924 end while length_a_B<4096/8
927 ################################################################################
929 ################################################################################
931 def run_test_reduce(skip=0)
937 a = rand(256**length_a_B)
938 b = rand(256**length_a_B)+1
939 v = reduce_test(a, b)
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)
951 end while length_a_B < 4096 / 8
954 ################################################################################
956 ################################################################################
958 def run_test_div(skip=0)
959 length_a_B = skip + 1
960 length_b_B = skip + 1
964 a = rand(256 ** length_a_B)
965 b = rand(256 ** length_a_B) + 1
970 b_size = rand(length_b_B + 1)
971 a = rand(256 ** length_a_B)
972 b = rand(256 ** b_size) + 1
978 end while length_a_B < 4096 / 8
981 ################################################################################
983 ################################################################################
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
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)
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)
1008 end while length_a_B<4096/8
1011 ################################################################################
1012 # run_test_expmodmont #
1013 ################################################################################
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
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)
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)
1038 end while length_a_B<4096/8
1041 ################################################################################
1043 ################################################################################
1045 def run_test_mulmod(skip=0)
1052 a = rand(256**length_a_B)
1053 b = rand(256**length_b_B)
1054 c = (rand(256**length_c_B) / 2 * 2) + 1
1057 v = mulmod_test(a, b, c)
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
1067 v = mulmod_test(a, b, c)
1073 end while length_a_B<4096/8
1076 ################################################################################
1078 ################################################################################
1080 def run_test_gcdext(skip=0)
1086 a = rand(256**length_a_B)
1087 b = rand(256**length_a_B)+1
1088 v = gcdext_test(a, b)
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)
1102 end while length_a_B<4096/8
1105 def init_serialport(conf)
1106 puts("serial port interface version: " + SerialPort::VERSION);
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"
1119 puts("\nPort: "+conf["PORT"]["port"]+"@" +
1120 params["baud"].to_s +
1122 params["data_bits"].to_s +
1123 conf["PORT"]["paraty"][0,1].upcase +
1124 params["stop_bits"].to_s +
1127 $sp = SerialPort.new(conf["PORT"]["port"], params)
1129 $sp.read_timeout = 100; # 5 minutes
1130 $sp.flow_control = SerialPort::SOFT
1135 ################################################################################
1137 ################################################################################
1139 opts = Getopt::Std.getopts("r:s:f:i:a:hd")
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"]
1148 init_serialport(conf)
1150 $max_timeout = 5 * 60
1157 random_seed = opts['r'].to_i(0)
1159 random_seed = 0xdeadbeef
1162 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
1163 if File.exists?(logfilename)
1166 logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
1168 end while(File.exists?(logfilename))
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')
1173 # printf("%s -> %s\n", n1, n2)
1176 n1 = sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt')
1177 n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt')
1179 printf("%s -> %s\n", n1, n2)
1180 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
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)
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) }
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'
1219 opts['a'].each_char do |x|
1222 init_system(init_str[x])
1223 tests[x].call(opts['s']?opts['s'].to_i():0)
1225 puts "no test defined for '#{x}'"
1229 'amsrMeE'.each_char do |x|
1232 init_system(init_str[x])
1233 tests[x].call(opts['s']?opts['s'].to_i():0)
1235 puts "no test defined for '#{x}'"