aboutsummaryrefslogtreecommitdiffstats
path: root/src/scripts
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-11-04 22:34:48 +0000
committerlloyd <[email protected]>2014-11-04 22:34:48 +0000
commit3c3c28d9bf8dca1d46fb25204b038643a9ec7d5d (patch)
treeee048445fef18957f9c46a7ee36cde80ca3992b5 /src/scripts
parentc05e81c5d12de651dee8b752a0bd709ffed45785 (diff)
Add the script used to generate mp_comba.cpp
Diffstat (limited to 'src/scripts')
-rwxr-xr-xsrc/scripts/comba.py116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/scripts/comba.py b/src/scripts/comba.py
new file mode 100755
index 000000000..03cf2f2ed
--- /dev/null
+++ b/src/scripts/comba.py
@@ -0,0 +1,116 @@
+#!/usr/bin/python
+
+import sys
+
+# Used to generate src/lib/math/mp/mp_comba.cpp
+
+def comba_indexes(N):
+
+ indexes = []
+
+ for i in xrange(0, 2*N):
+ x = []
+
+ for j in xrange(max(0, i-N+1), min(N, i+1)):
+ x += [(j,i-j)]
+ indexes += [sorted(x)]
+
+ return indexes
+
+def comba_sqr_indexes(N):
+
+ indexes = []
+
+ for i in xrange(0, 2*N):
+ x = []
+
+ for j in xrange(max(0, i-N+1), min(N, i+1)):
+ if j < i-j:
+ x += [(j,i-j)]
+ else:
+ x += [(i-j,j)]
+ indexes += [sorted(x)]
+
+ return indexes
+
+def comba_multiply_code(N):
+ indexes = comba_indexes(N)
+
+ w2 = 'w2'
+ w1 = 'w1'
+ w0 = 'w0'
+
+ 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])
+
+ if i < 2*N-2:
+ print " z[%2d] = %s; %s = 0;\n" % (i, w0, w0)
+ else:
+ print " z[%2d] = %s;" % (i, w0)
+ (w0,w1,w2) = (w1,w2,w0)
+ #print "z[%2d] = w0; w0 = w1; w1 = w2; w2 = 0;" % (i)
+
+def comba_square_code(N):
+ indexes = comba_sqr_indexes(N)
+
+ w2 = 'w2'
+ w1 = 'w1'
+ w0 = 'w0'
+
+ 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])
+ elif i % 2 == 0:
+ 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)
+ else:
+ print " z[%2d] = %s;" % (rnd, w0)
+
+ (w0,w1,w2) = (w1,w2,w0)
+
+def main(args = None):
+ if args is None:
+ args = sys.argv
+
+ print """/*
+* Comba Multiplication and Squaring
+* (C) 1999-2007,2011 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/internal/mp_core.h>
+#include <botan/internal/mp_asmi.h>
+
+namespace Botan {
+
+extern "C" {
+"""
+
+ for n in [4,6,8,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"
+
+ comba_square_code(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"
+
+ comba_multiply_code(n)
+
+ print " }\n"
+
+ print "}\n\n}"
+
+if __name__ == '__main__':
+ sys.exit(main())