Skip to content

Commit 515fb67

Browse files
committed
fix formatting
1 parent 3ca7438 commit 515fb67

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/ecdsa/util.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@
1717
oid_ecPublicKey = (1, 2, 840, 10045, 2, 1)
1818
encoded_oid_ecPublicKey = der.encode_oid(*oid_ecPublicKey)
1919

20-
if sys.version > '3':
21-
entropy_to_bits = lambda ent_256: bin(int.from_bytes(ent_256, 'big'))[2:].zfill(len(ent_256)*8)
22-
else:
23-
entropy_to_bits = lambda ent_256: ''.join(bin(ord(x))[2:].zfill(8) for x in ent_256)
20+
if sys.version > '3':
21+
def entropy_to_bits(ent_256):
22+
"""Convert a bytestring to string of 0's and 1's"""
23+
return bin(int.from_bytes(ent_256, 'big'))[2:].zfill(len(ent_256)*8)
24+
else:
25+
def entropy_to_bits(ent_256):
26+
"""Convert a bytestring to string of 0's and 1's"""
27+
return ''.join(bin(ord(x))[2:].zfill(8) for x in ent_256)
28+
29+
2430
if sys.version < '2.7': #Can't add a method to a built-in type so we are stuck with this
25-
bit_length = lambda x: len(bin(x)) - 2
26-
else:
27-
bit_length = lambda x: x.bit_length() or 1
28-
29-
31+
def bit_length(x):
32+
return len(bin(x)) - 2
33+
else:
34+
def bit_length(x):
35+
return x.bit_length() or 1
3036

3137

3238
def orderlen(order):
@@ -35,7 +41,8 @@ def orderlen(order):
3541

3642
def randrange(order, entropy=None):
3743
"""Return a random integer k such that 1 <= k < order, uniformly
38-
distributed across that range. Worst case should be a mean of 2 loops at (2**k)+2.
44+
distributed across that range. Worst case should be a mean of 2 loops at
45+
(2**k)+2.
3946
4047
Note that this function is not declared to be forwards-compatible: we may
4148
change the behavior in future releases. The entropy= argument (which
@@ -47,14 +54,13 @@ def randrange(order, entropy=None):
4754
if entropy is None:
4855
entropy = os.urandom
4956
upper_2 = bit_length(order-2)
50-
upper_256 = upper_2//8 + 1
51-
while True: #I don't think this needs a counter with bit-wise randrange
52-
ent_256 = entropy(upper_256)
53-
ent_2=entropy_to_bits(ent_256)
54-
rand_num = int(ent_2[:upper_2], base=2) +1
55-
if 0 < rand_num < order: return rand_num
56-
else:continue
57-
57+
upper_256 = upper_2//8 + 1
58+
while True: # I don't think this needs a counter with bit-wise randrange
59+
ent_256 = entropy(upper_256)
60+
ent_2 = entropy_to_bits(ent_256)
61+
rand_num = int(ent_2[:upper_2], base=2) + 1
62+
if 0 < rand_num < order:
63+
return rand_num
5864

5965

6066
class PRNG:

0 commit comments

Comments
 (0)