Skip to content

Commit

Permalink
minor improvement: python3 speed for randrange by ~2.5x
Browse files Browse the repository at this point in the history
  • Loading branch information
pik committed Jan 28, 2015
1 parent 5c6f73f commit df0059f
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ecdsa/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
encoded_oid_ecPublicKey = der.encode_oid(*oid_ecPublicKey)

if sys.version > '3':
entropy_to_bits = lambda ent_256: ''.join(bin(x)[2:].zfill(8) for x in ent_256)
entropy_to_bits = lambda ent_256: bin(int.from_bytes(ent_256, 'big'))[2:].zfill(len(ent_256)*8)
else:
entropy_to_bits = lambda ent_256: ''.join(bin(ord(x))[2:].zfill(8) for x in ent_256)
if sys.version < '2.7': #Can't add a method to a built-in type so we are stuck with this
Expand All @@ -41,7 +41,7 @@ def randrange(order, entropy=None):
if entropy is None:
entropy = os.urandom
upper_2 = bit_length(order-2)
upper_256 = int(upper_2/8 + 1);
upper_256 = upper_2//8 + 1
while True: #I don't think this needs a counter with bit-wise randrange
ent_256 = entropy(upper_256)
ent_2=entropy_to_bits(ent_256)
Expand Down

0 comments on commit df0059f

Please sign in to comment.