summaryrefslogtreecommitdiff
path: root/rsa/fastrsa.py
diff options
context:
space:
mode:
Diffstat (limited to 'rsa/fastrsa.py')
-rw-r--r--rsa/fastrsa.py12
1 files changed, 6 insertions, 6 deletions
diff --git a/rsa/fastrsa.py b/rsa/fastrsa.py
index 1437da7..3a26437 100644
--- a/rsa/fastrsa.py
+++ b/rsa/fastrsa.py
@@ -171,16 +171,16 @@ def fast_exponentiation(a, e, n):
"""Calculates r = a^e mod n
"""
#Single loop version is faster and uses less memory
- #MSB is always 1 so skip testing it and start with result = a
+ #MSB is always 1 so skip testing it and start with next exponent bit.
msbe = bit_size(e) - 2 #Find MSB-1 of exponent
test = long(1 << msbe) #Isolate each exponent bit with test value
a %= n #Throw away any overflow modulo n
result = a #Start with result = a (skip MSB test)
- while test != 0:
- if e & test != 0: #If exponent bit 1 square and mult by a
- result = (result * result * a) % n
- else: #If exponent bit 0 just square
- result = (result * result) % n
+ while test != 0: #Repeat til all exponent bits have been tested
+ if e & test != 0: #If exponent bit is 1, square and mult by a
+ result = (result * result * a) % n #Then reduce modulo n
+ else: #If exponent bit is 0, just square
+ result = (result * result) % n #Then reduce modulo n
test >>= 1 #Move to next exponent bit
return result