summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBarry Mead <barrymead@cox.net>2010-02-21 15:32:05 -0700
committerBarry Mead <barrymead@cox.net>2010-02-21 15:32:05 -0700
commite519b9b9edb284713645affeee0bfc4a41a30a62 (patch)
tree3f6d7048cb289243615b8802ee7434738f86d7f2
parent732207e9b4864a6167051aaff25635f0d2d726d0 (diff)
downloadrsa-e519b9b9edb284713645affeee0bfc4a41a30a62.tar.gz
Comment clarity in fast_exponentiation
-rw-r--r--rsa/__init__.py14
-rw-r--r--rsa/fastrsa.py12
2 files changed, 13 insertions, 13 deletions
diff --git a/rsa/__init__.py b/rsa/__init__.py
index ea6a9e5..6b66506 100644
--- a/rsa/__init__.py
+++ b/rsa/__init__.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 expoent 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
@@ -464,7 +464,7 @@ def chopstring(message, key, n, funcref):
msglen = len(message)
mbits = msglen * 8
- #Bit counts start at 1, bit numbers start at zero (so deduct 2)
+ #Set aside 2-bits so setting of safebit won't overflow modulo n.
nbits = bit_size(n) - 2 # leave room for safebit
nbytes = nbits / 8
blocks = msglen / nbytes
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