+################################################################################
+# expmod_test #
+################################################################################
+
+def expmod_test(a,b,c)
+ begin
+ printf("[testing] expmod(%#x, %#x, %#x)\n",a,b,c) if $debug
+ line = $sp.gets()
+ line = "" if line==nil
+ puts("DBG got: "+line) if $debug
+ if /^Error:.*/.match(line)
+ puts line
+ return false
+ end
+ end while ! /[\s]*enter a:[\s]*/.match(line)
+ $sp.print(a.to_s(16)+" ")
+ begin
+ line = $sp.gets()
+ line = "" if line==nil
+ puts("DBG got: "+line) if $debug
+ if /^Error:.*/.match(line)
+ puts line
+ return false
+ end
+ end while ! /[\s]*enter b:[\s]*/.match(line)
+ $sp.print(b.to_s(16)+" ")
+ begin
+ line = $sp.gets()
+ line = "" if line==nil
+ puts("DBG got: "+line) if $debug
+ if /^Error:.*/.match(line)
+ puts line
+ return false
+ end
+ end while ! /[\s]*enter c:[\s]*/.match(line)
+ $sp.print(c.to_s(16)+" ")
+ line=''
+ begin
+ line_tmp = $sp.gets()
+ line_tmp = '' if line_tmp==nil
+ line += line_tmp
+ puts("DBG got: "+line) if $debug
+ if /^Error:.*/.match(line)
+ puts line
+ return false
+ end
+ 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)
+ a_ = m[1].to_i(16)
+ b_ = m[2].to_i(16)
+ c_ = m[3].to_i(16)
+ d_ = m[4].to_i(16)
+ line.chomp!
+ if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
+ $logfile.printf("[pass]: %s\n", line)
+ return true
+ else
+ $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', (c_==c)?'':'c', (d_==expmod(a,b,c))?'':'d',line)
+ $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))
+ return false
+ end
+ return false
+end
+
+################################################################################
+# gcdext_test #
+################################################################################
+
+def gcdext_test(a,b)
+ $logfile.printf("[testing] gcdext(%s, %s)\n", a.to_s(16), b.to_s(16))
+ begin
+ line = $sp.gets()
+ line = "" if line==nil
+ puts("DBG got: "+line) if $debug
+ if /^Error:.*/.match(line)
+ puts line
+ return false
+ end
+ end while ! /[\s]*enter a:[\s]*/.match(line)
+ $sp.print(a.to_s(16)+" ")
+ begin
+ line = $sp.gets()
+ line = "" if line==nil
+ puts("DBG got: "+line) if $debug
+ if /^Error:.*/.match(line)
+ puts line
+ return false
+ end
+ end while ! /[\s]*enter b:[\s]*/.match(line)
+ $sp.print(b.to_s(16)+" ")
+ line=''
+ begin
+ line_tmp = $sp.gets()
+ line_tmp = '' if line_tmp==nil
+ line = '' if line.end_with?('\n')
+ line += line_tmp
+ puts("DBG got: "+line) if $debug
+ if /^Error:.*/.match(line)
+ puts line
+ return false
+ end
+ 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)
+ a_ = m[1].to_i(16)
+ b_ = m[2].to_i(16)
+ c_ = m[3].to_i(16)
+ d_ = m[4].to_i(16)
+ e_ = m[5].to_i(16)
+ line.chomp!
+ line.gsub!("\r",'')
+ line.gsub!("\n",'')
+ ref = gcdext(a,b)
+ if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
+ $logfile.printf("[pass]: %s\n", line)
+ return true
+ else
+ $logfile.printf("[fail (%s%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b',
+ (c_==ref[1])?'':'c', (d_==ref[2])?'':'d', (e_==ref[0])?'':'e', line)
+ $logfile.printf(" ; should gcdext( %s, %s) => a = %s; b = %s; gcd = %s\n",
+ a.to_s(16), b.to_s(16), ref[1].to_s(16), ref[2].to_s(16), ref[0].to_s(16))
+ return false
+ end
+ return false
+end
+