mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-09 10:43:19 -05:00
[tests] remove bn2bin()
It's only called in one place.
This commit is contained in:
parent
3b9b38579c
commit
f950ec2520
1 changed files with 12 additions and 12 deletions
|
@ -9,29 +9,29 @@ endian format.
|
||||||
|
|
||||||
This file is copied from python-bitcoinlib.
|
This file is copied from python-bitcoinlib.
|
||||||
"""
|
"""
|
||||||
def bn2bin(v):
|
|
||||||
"""Convert a number to a byte array."""
|
|
||||||
s = bytearray()
|
|
||||||
bytes_len = (v.bit_length() + 7) // 8
|
|
||||||
for i in range(bytes_len, 0, -1):
|
|
||||||
s.append((v >> ((i - 1) * 8)) & 0xff)
|
|
||||||
return s
|
|
||||||
|
|
||||||
def bn2mpi(v):
|
def bn2mpi(v):
|
||||||
"""Convert number to MPI format, without the sign byte."""
|
"""Convert number to MPI format, without the sign byte."""
|
||||||
|
# The top bit is used to indicate the sign of the number. If there
|
||||||
|
# isn't a spare bit in the bit length, add an extension byte.
|
||||||
have_ext = False
|
have_ext = False
|
||||||
|
ext = bytearray()
|
||||||
if v.bit_length() > 0:
|
if v.bit_length() > 0:
|
||||||
have_ext = (v.bit_length() & 0x07) == 0
|
have_ext = (v.bit_length() & 0x07) == 0
|
||||||
|
ext.append(0)
|
||||||
|
|
||||||
|
# Is the number negative?
|
||||||
neg = False
|
neg = False
|
||||||
if v < 0:
|
if v < 0:
|
||||||
neg = True
|
neg = True
|
||||||
v = -v
|
v = -v
|
||||||
|
|
||||||
ext = bytearray()
|
# Convert the int to bytes
|
||||||
if have_ext:
|
v_bin = bytearray()
|
||||||
ext.append(0)
|
bytes_len = (v.bit_length() + 7) // 8
|
||||||
v_bin = bn2bin(v)
|
for i in range(bytes_len, 0, -1):
|
||||||
|
v_bin.append((v >> ((i - 1) * 8)) & 0xff)
|
||||||
|
|
||||||
|
# Add the sign bit if necessary
|
||||||
if neg:
|
if neg:
|
||||||
if have_ext:
|
if have_ext:
|
||||||
ext[0] |= 0x80
|
ext[0] |= 0x80
|
||||||
|
|
Loading…
Add table
Reference in a new issue