Skip to content

Commit

Permalink
use 2-ary NAF for regular multiplication too
Browse files Browse the repository at this point in the history
  • Loading branch information
tomato42 committed Oct 18, 2019
1 parent 3fbbd15 commit 7755356
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/ecdsa/ellipticcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,20 @@ def _mul_precompute(self, other):
i *= 2
return result

def _naf(self, mult):
ret = []
while mult:
if mult % 2:
nd = mult % 4
if nd >= 2:
nd = nd - 4
ret += [nd]
mult -= nd
else:
ret += [0]
mult //= 2
return ret

def __mul__(self, other):
"""Multiply point by an integer."""
if self.__y == 0 or other == 0:
Expand All @@ -355,21 +369,15 @@ def __mul__(self, other):
#if self.__order:
# other = other % self.__order

def leftmost_bit(x):
assert x > 0
result = 1
while result <= x:
result = 2 * result
return result // 2

e = other
i = leftmost_bit(e)
self = self.scale()
result = self
while i > 1:
result = INFINITY
# since adding points when at least one of them is scaled
# is quicker, reverse the NAF order
for i in reversed(self._naf(other)):
result = result.double()
i = i // 2
if e & i != 0:
if i < 0:
result = result + (-self)
elif i > 0:
result = result + self
return result

Expand Down

0 comments on commit 7755356

Please sign in to comment.