return conf
end
+################################################################################
+# expmod #
+################################################################################
+
+def expmod(base, power, mod)
+ result = 1
+ while power > 0
+ result = (result * base) % mod if power & 1 == 1
+ base = (base * base) % mod
+ power >>= 1;
+ end
+ return result
+end
+
+################################################################################
+# gcdext #
+################################################################################
+
+def gcdext(x,y)
+ g=1
+ while(x&1==0 && y&1==0) do
+ x>>=1
+ y>>=1
+ g<<=1
+ end
+ u=x; v=y; a=1; b=0; c=0; d=1
+ begin
+ while(u&1==0) do
+ if(a%2==1 || b%2==1)
+ a += y
+ b -= x
+ end
+ u>>=1; a>>=1; b>>=1
+ end
+ while(v&1==0) do
+ if(c%2==1 || d%2==1)
+ c += y
+ d -= x
+ end
+ v>>=1; c>>=1; d>>=1;
+ end
+ if(u>=v)
+ u -= v; a-=c; b-=d
+ else
+ v -= u; c-=a; d-=b
+ end
+ end while(u!=0)
+ return[g*v, c, d]
+end
+
################################################################################
# reset_system #
################################################################################
$logfile.printf("[pass]: %s\n", line)
return true
else
- $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
+ $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a*b)?"":"c",line)
$logfile.printf(" ; should %s * %s = %s\n", a.to_s(16), b.to_s(16), (a*b).to_s(16))
return false
end
return false
end
+################################################################################
+# add_scale_test #
+################################################################################
+
+def add_scale_test(a, b, scale)
+ 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 not /[\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 not /[\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 not /[\s]*enter scale:[\s]*/.match(line)
+ $sp.print(scale.to_s(16)+"\n")
+ 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 not m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\s]*<<8\*[\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)
+ s_ = m[3].to_i(16)
+ c_ = m[4].to_i(16)
+ line.chomp!
+ if(a_== a && b_ == b && c_ == (a+b))
+ $logfile.printf("[pass]: %s\n", line)
+ return true
+ else
+ $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
+ $logfile.printf(" ; should %s + %s = %s\n", a.to_s(16), b.to_s(16), (a+b).to_s(16))
+ return false
+ end
+ return false
+end
+
################################################################################
# square_test #
################################################################################
return false
end
+################################################################################
+# reduce_test #
+################################################################################
+
+def reduce_test(a,b)
+ 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 not /[\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 not /[\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 += line_tmp
+ puts("DBG got: "+line) if $debug
+ if /^Error:.*/.match(line)
+ puts line
+ return false
+ end
+ end while not m=/[\s]*([+-]?[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)
+ line.chomp!
+ if(a_== a && b_ == b && c_ == (a%b))
+ $logfile.printf("[pass]: %s\n", line)
+ return true
+ else
+ $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a%b)?"":"c",line)
+ $logfile.printf(" ; should %s %% %s = %s\n", a.to_s(16), b.to_s(16), (a%b).to_s(16))
+ return false
+ end
+ return false
+end
+
+################################################################################
+# expmod_test #
+################################################################################
+
+def expmod_test(a,b,c)
+ 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 not /[\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 not /[\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 not /[\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 not 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)
+ 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 not /[\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 not /[\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 not 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
+
################################################################################
# run_test_add #
################################################################################
end while length_a_B<4096/8
end
+################################################################################
+# run_test_reduce #
+################################################################################
+
+def run_test_reduce(skip=0)
+ length_a_B = skip+1
+ length_b_B = skip+1
+ begin
+ $size = length_a_B
+ (0..16).each do |i|
+ a = rand(256**length_a_B)
+ b = rand(256**length_a_B)+1
+ v = reduce_test(a, b)
+ screen_progress(v)
+ end
+ (0..16).each do |i|
+ b_size = rand(length_b_B+1)
+ a = rand(256**length_a_B)
+ b = rand(256**b_size)+1
+ v = reduce_test(a, b)
+ screen_progress(v)
+ end
+ length_a_B += 1
+ length_b_B += 1
+ end while length_a_B<4096/8
+end
+
+################################################################################
+# run_test_expmod #
+################################################################################
+
+def run_test_expmod(skip=0)
+ length_a_B = skip+1
+ length_b_B = skip+1
+ length_c_B = skip+1
+ begin
+ $size = length_a_B
+ (0..16).each do |i|
+ a = rand(256**length_a_B)
+ b = rand(256**length_b_B)+1
+ c = rand(256**length_c_B)+1
+ v = expmod_test(a, b, c)
+ screen_progress(v)
+ end
+ (0..16).each do |i|
+ b_size = rand(length_b_B+1)
+ a = rand(256**length_a_B)
+ b = rand(256**b_size)+1
+ c = rand(256**b_size)+1
+ v = expmod_test(a, b, c)
+ screen_progress(v)
+ end
+ length_a_B += 1
+ length_b_B += 1
+ end while length_a_B<4096/8
+end
+
+################################################################################
+# run_test_gcdext #
+################################################################################
+
+def run_test_gcdext(skip=0)
+ length_a_B = skip+1
+ length_b_B = skip+1
+ begin
+ $size = length_a_B
+ (0..16).each do |i|
+ a = rand(256**length_a_B)
+ b = rand(256**length_a_B)+1
+ v = gcdext_test(a, b)
+ $logfile.flush()
+ screen_progress(v)
+ end
+ (0..16).each do |i|
+ b_size = rand(length_b_B+1)
+ a = rand(256**length_a_B)
+ b = rand(256**b_size)+1
+ v = gcdext_test(a, b)
+ $logfile.flush()
+ screen_progress(v)
+ end
+ length_a_B += 1
+ length_b_B += 1
+ end while length_a_B<4096/8
+end
+
################################################################################
# MAIN #
################################################################################
tests['a'] = proc {|x| run_test_add(x) }
tests['m'] = proc {|x| run_test_mul(x) }
tests['s'] = proc {|x| run_test_square(x) }
+tests['r'] = proc {|x| run_test_reduce(x) }
+tests['e'] = proc {|x| run_test_expmod(x) }
+tests['g'] = proc {|x| run_test_gcdext(x) }
init_str = Hash.new
init_str['a'] = 'add-test'
init_str['m'] = 'mul-test'
init_str['s'] = 'square-test'
+init_str['r'] = 'reduce-test'
+init_str['e'] = 'expmod-test'
+init_str['g'] = 'gcdext-test'
srand(0xdeadbeef)
end
end
else
- 'ams'.each_char do |x|
+ 'amsre'.each_char do |x|
if tests[x]
puts init_str[x]
init_system(init_str[x])