0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-13 11:25:02 -05:00

[tests] don't encode the integer size in bignum

We just throw it away whenever we use the
result so don't add it.
This commit is contained in:
John Newbery 2019-10-30 10:25:23 -04:00
parent 1dc68aee66
commit 9a60bef50d

View file

@ -9,14 +9,9 @@ endian format.
This file is copied from python-bitcoinlib. This file is copied from python-bitcoinlib.
""" """
import struct def bn_bytes(v):
def bn_bytes(v, have_ext=False):
"""Return number of bytes in integer representation of v.""" """Return number of bytes in integer representation of v."""
ext = 0 return (v.bit_length() + 7) // 8
if have_ext:
ext = 1
return (v.bit_length() + 7) // 8 + ext
def bn2bin(v): def bn2bin(v):
"""Convert a number to a byte array.""" """Convert a number to a byte array."""
@ -28,7 +23,7 @@ def bn2bin(v):
return s return s
def bn2mpi(v): def bn2mpi(v):
"""Convert number to MPI format.""" """Convert number to MPI format, without the sign byte."""
have_ext = False have_ext = False
if v.bit_length() > 0: if v.bit_length() > 0:
have_ext = (v.bit_length() & 0x07) == 0 have_ext = (v.bit_length() & 0x07) == 0
@ -38,7 +33,6 @@ def bn2mpi(v):
neg = True neg = True
v = -v v = -v
s = struct.pack(b">I", bn_bytes(v, have_ext))
ext = bytearray() ext = bytearray()
if have_ext: if have_ext:
ext.append(0) ext.append(0)
@ -48,13 +42,11 @@ def bn2mpi(v):
ext[0] |= 0x80 ext[0] |= 0x80
else: else:
v_bin[0] |= 0x80 v_bin[0] |= 0x80
return s + ext + v_bin return ext + v_bin
def mpi2vch(s): def mpi2vch(s):
"""Convert MPI format to bitcoin-specific little endian format, with implicit size.""" """Convert MPI format to bitcoin-specific little endian format."""
r = s[4:] # strip size return s[::-1] # reverse string, converting BE->LE
r = r[::-1] # reverse string, converting BE->LE
return r
def bn2vch(v): def bn2vch(v):
"""Convert number to bitcoin-specific little endian format.""" """Convert number to bitcoin-specific little endian format."""