Skip to content

Commit 9a60bef

Browse files
committed
[tests] don't encode the integer size in bignum
We just throw it away whenever we use the result so don't add it.
1 parent 1dc68ae commit 9a60bef

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

test/functional/test_framework/bignum.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@
99
1010
This file is copied from python-bitcoinlib.
1111
"""
12-
import struct
13-
14-
def bn_bytes(v, have_ext=False):
12+
def bn_bytes(v):
1513
"""Return number of bytes in integer representation of v."""
16-
ext = 0
17-
if have_ext:
18-
ext = 1
19-
return (v.bit_length() + 7) // 8 + ext
14+
return (v.bit_length() + 7) // 8
2015

2116
def bn2bin(v):
2217
"""Convert a number to a byte array."""
@@ -28,7 +23,7 @@ def bn2bin(v):
2823
return s
2924

3025
def bn2mpi(v):
31-
"""Convert number to MPI format."""
26+
"""Convert number to MPI format, without the sign byte."""
3227
have_ext = False
3328
if v.bit_length() > 0:
3429
have_ext = (v.bit_length() & 0x07) == 0
@@ -38,7 +33,6 @@ def bn2mpi(v):
3833
neg = True
3934
v = -v
4035

41-
s = struct.pack(b">I", bn_bytes(v, have_ext))
4236
ext = bytearray()
4337
if have_ext:
4438
ext.append(0)
@@ -48,13 +42,11 @@ def bn2mpi(v):
4842
ext[0] |= 0x80
4943
else:
5044
v_bin[0] |= 0x80
51-
return s + ext + v_bin
45+
return ext + v_bin
5246

5347
def mpi2vch(s):
54-
"""Convert MPI format to bitcoin-specific little endian format, with implicit size."""
55-
r = s[4:] # strip size
56-
r = r[::-1] # reverse string, converting BE->LE
57-
return r
48+
"""Convert MPI format to bitcoin-specific little endian format."""
49+
return s[::-1] # reverse string, converting BE->LE
5850

5951
def bn2vch(v):
6052
"""Convert number to bitcoin-specific little endian format."""

0 commit comments

Comments
 (0)