Skip to content

Commit

Permalink
Convert comba script to Python3
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed Apr 8, 2018
1 parent 8b4f531 commit c921a1b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/lib/math/mp/mp_comba.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Comba Multiplication and Squaring
*
* This file was automatically generated by ./src/scripts/comba.py on 2016-01-01
* This file was automatically generated by ./src/scripts/comba.py on 2018-04-08
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
Expand Down
52 changes: 26 additions & 26 deletions src/scripts/comba.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python2
#!/usr/bin/python3

import sys
import datetime
Expand All @@ -12,10 +12,10 @@ def comba_indexes(N):

indexes = []

for i in xrange(0, 2*N):
for i in range(0, 2*N):
x = []

for j in xrange(max(0, i-N+1), min(N, i+1)):
for j in range(max(0, i-N+1), min(N, i+1)):
x += [(j,i-j)]
indexes += [sorted(x)]

Expand All @@ -25,10 +25,10 @@ def comba_sqr_indexes(N):

indexes = []

for i in xrange(0, 2*N):
for i in range(0, 2*N):
x = []

for j in xrange(max(0, i-N+1), min(N, i+1)):
for j in range(max(0, i-N+1), min(N, i+1)):
if j < i-j:
x += [(j,i-j)]
else:
Expand All @@ -46,14 +46,14 @@ def comba_multiply_code(N):

for (i,idx) in zip(range(0, len(indexes)), indexes):
for pair in idx:
print " word3_muladd(&%s, &%s, &%s, x[%2d], y[%2d]);" % (w2, w1, w0, pair[0], pair[1])
print(" word3_muladd(&%s, &%s, &%s, x[%2d], y[%2d]);" % (w2, w1, w0, pair[0], pair[1]))

if i < 2*N-2:
print " z[%2d] = %s; %s = 0;\n" % (i, w0, w0)
print(" z[%2d] = %s; %s = 0;\n" % (i, w0, w0))
else:
print " z[%2d] = %s;" % (i, w0)
print(" z[%2d] = %s;" % (i, w0))
(w0,w1,w2) = (w1,w2,w0)
#print "z[%2d] = w0; w0 = w1; w1 = w2; w2 = 0;" % (i)
#print("z[%2d] = w0; w0 = w1; w1 = w2; w2 = 0;" % (i))

def comba_square_code(N):
indexes = comba_sqr_indexes(N)
Expand All @@ -65,22 +65,22 @@ def comba_square_code(N):
for (rnd,idx) in zip(range(0, len(indexes)), indexes):
for (i,pair) in zip(range(0, len(idx)), idx):
if pair[0] == pair[1]:
print " word3_muladd (&%s, &%s, &%s, x[%2d], x[%2d]);" % (w2, w1, w0, pair[0], pair[1])
print(" word3_muladd (&%s, &%s, &%s, x[%2d], x[%2d]);" % (w2, w1, w0, pair[0], pair[1]))
elif i % 2 == 0:
print " word3_muladd_2(&%s, &%s, &%s, x[%2d], x[%2d]);" % (w2, w1, w0, pair[0], pair[1])
print(" word3_muladd_2(&%s, &%s, &%s, x[%2d], x[%2d]);" % (w2, w1, w0, pair[0], pair[1]))

if rnd < 2*N-2:
print " z[%2d] = %s; %s = 0;\n" % (rnd, w0, w0)
print(" z[%2d] = %s; %s = 0;\n" % (rnd, w0, w0))
else:
print " z[%2d] = %s;" % (rnd, w0)
print(" z[%2d] = %s;" % (rnd, w0))

(w0,w1,w2) = (w1,w2,w0)

def main(args = None):
if args is None:
args = sys.argv

print """/*
print("""/*
* Comba Multiplication and Squaring
*
* This file was automatically generated by %s on %s
Expand All @@ -92,28 +92,28 @@ def main(args = None):
#include <botan/internal/mp_asmi.h>
namespace Botan {
""" % (sys.argv[0], datetime.date.today().strftime("%Y-%m-%d"))
""" % (sys.argv[0], datetime.date.today().strftime("%Y-%m-%d")))

for n in [4,6,8,9,16]:
print "/*\n* Comba %dx%d Squaring\n*/" % (n, n)
print "void bigint_comba_sqr%d(word z[%d], const word x[%d])" % (n, 2*n, n)
print " {"
print " word w2 = 0, w1 = 0, w0 = 0;\n"
print("/*\n* Comba %dx%d Squaring\n*/" % (n, n))
print("void bigint_comba_sqr%d(word z[%d], const word x[%d])" % (n, 2*n, n))
print(" {")
print(" word w2 = 0, w1 = 0, w0 = 0;\n")

comba_square_code(n)

print " }\n"
print(" }\n")

print "/*\n* Comba %dx%d Multiplication\n*/" % (n, n)
print "void bigint_comba_mul%d(word z[%d], const word x[%d], const word y[%d])" % (n, 2*n, n, n)
print " {"
print " word w2 = 0, w1 = 0, w0 = 0;\n"
print("/*\n* Comba %dx%d Multiplication\n*/" % (n, n))
print("void bigint_comba_mul%d(word z[%d], const word x[%d], const word y[%d])" % (n, 2*n, n, n))
print(" {")
print(" word w2 = 0, w1 = 0, w0 = 0;\n")

comba_multiply_code(n)

print " }\n"
print(" }\n")

print "}"
print("}")

if __name__ == '__main__':
sys.exit(main())

0 comments on commit c921a1b

Please sign in to comment.