File tree Expand file tree Collapse file tree 1 file changed +12
-12
lines changed
test/functional/test_framework Expand file tree Collapse file tree 1 file changed +12
-12
lines changed Original file line number Diff line number Diff line change 9
9
10
10
This file is copied from python-bitcoinlib.
11
11
"""
12
- def bn2bin (v ):
13
- """Convert a number to a byte array."""
14
- s = bytearray ()
15
- bytes_len = (v .bit_length () + 7 ) // 8
16
- for i in range (bytes_len , 0 , - 1 ):
17
- s .append ((v >> ((i - 1 ) * 8 )) & 0xff )
18
- return s
19
-
20
12
def bn2mpi (v ):
21
13
"""Convert number to MPI format, without the sign byte."""
14
+ # The top bit is used to indicate the sign of the number. If there
15
+ # isn't a spare bit in the bit length, add an extension byte.
22
16
have_ext = False
17
+ ext = bytearray ()
23
18
if v .bit_length () > 0 :
24
19
have_ext = (v .bit_length () & 0x07 ) == 0
20
+ ext .append (0 )
25
21
22
+ # Is the number negative?
26
23
neg = False
27
24
if v < 0 :
28
25
neg = True
29
26
v = - v
30
27
31
- ext = bytearray ()
32
- if have_ext :
33
- ext .append (0 )
34
- v_bin = bn2bin (v )
28
+ # Convert the int to bytes
29
+ v_bin = bytearray ()
30
+ bytes_len = (v .bit_length () + 7 ) // 8
31
+ for i in range (bytes_len , 0 , - 1 ):
32
+ v_bin .append ((v >> ((i - 1 ) * 8 )) & 0xff )
33
+
34
+ # Add the sign bit if necessary
35
35
if neg :
36
36
if have_ext :
37
37
ext [0 ] |= 0x80
You can’t perform that action at this time.
0 commit comments