Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/ecdsa/numbertheory.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,23 @@ def modular_exp(base, exponent, modulus): # pragma: no cover
raise NegativeExponentError(
"Negative exponents (%d) not allowed" % exponent
)
return pow(base, exponent, modulus)

if (type(exponent)==int):
return fast_modular_exponentiation(base, exponent, modulus)
else:
return pow(base, exponent, modulus)

def fast_modular_exponentiation(base, exponent, modulas):

"""Log(N) computation required to find the final Exponent"""
binary = bin(exponent)[-1:1:-1]
l = {}
ans = 1
for i in range(len(binary)):
l[i] = base
base = (base**2)%modulas
if binary[i]=='1':
ans *= l[i]%modulas
return ans%modulas

def polynomial_reduce_mod(poly, polymod, p):
"""Reduce poly by polymod, integer arithmetic modulo p.
Expand Down