I need a short addition chain for a number $n>2^{100}$ in order to implement a fast exponentiation. The memory footprint is not important.
Finding an optimal chain for a large number $n$ is difficult (and considered by some to be an NP-complete problem). However, many heuristics to find good but suboptimal chains have been proposed in literature.
Is there any library to search for short chains, or do I have to implement something by myself? (All the tools I found so far only seem to work for small numbers.)
Many thanks!
Update:
I implemented the binary approach, but the resulting chain was rather long.
I then implemented the algorithm based on continued fractions described on pages 161-162 in the "Handbook of Elliptic and Hyperelliptic Curve Cryptography" by Cohen et al., and the resulting chain was already 40% shorter. I will just use this chain for my implementation. Here is the corresponding source code:
from math import *
def add(v,j):
return v+[v[len(v)-1]+j]
def cross(v,w):
r=v
vs=v[len(v)-1]
for y in w[1:]:
r=r+[y*vs]
return r
def is2l(x):
l=log2(x)
if 2**l==x:
return l
return -1
def log2(x):
l=0
while 2**(l+1)<=x:
l=l+1
return l
def minchain(n):
l=is2l(n)
if l!=-1:
a=[]
for i in range(l+1):
a+=[2**i]
return a
if n==3:
return [1,2,3]
return chain(n,n/int(2**ceil(log2(n)/2.0)))
def chain(n,k):
# print "chain(",n,",",k,")"
q=n/k
r=n%k
if r==0:
return cross(minchain(k),minchain(q))
else:
return add(cross(chain(k,r),minchain(q)),r)
def sequence(c):
for i in range(len(c)):
a=c[i]
if a==1:
print "a["+str(i)+"]=x"
else:
for j in range(i*i):
if (j%i)<(j/i) and c[j%i]+c[j/i]==a:
print "a["+str(i)+"]=a["+str(j%i)+"]*a["+str(j/i)+"]"
if (j%i)==(j/i) and c[j%i]+c[j/i]==a:
print "a["+str(i)+"]=square(a["+str(j%i)+"])"
sequence(minchain(2**127-1))
- Probably the more complex (e.g. backtracking or genetic) algorithms described in the literature will find even shorter chains. However, I will not implement these myself.