mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-10 11:57:28 -03:00
Merge #17319: Tests: remove bignum module
3ed772d221
[tests] remove bignum.py (John Newbery)f950ec2520
[tests] remove bn2bin() (John Newbery)3b9b38579c
[tests] remove bn_bytes() function (John Newbery)a760aa14a9
[tests] remove mpi2vch() function (John Newbery)9a60bef50d
[tests] don't encode the integer size in bignum (John Newbery)1dc68aee66
[tests] add function comments to bignum (John Newbery)f31fc0e92e
[tests] fix flake8 warnings in script.py and bignum.py (John Newbery) Pull request description: Only one function is imported in script.py. Just move that function to script.py and remove the bignum.py module. Remove unused functionality and fix some flake8 warnings along the way. Top commit has no ACKs. Tree-SHA512: 015f543ab545b5d5451896e2751d9c19334d9155b03faacd2023781e89833a2440f7f28741e9a8ac49badd9cdc012cbb6e038cdcdebeefaf9cb9d461c0689157
This commit is contained in:
commit
ad04f0d8a5
3 changed files with 173 additions and 197 deletions
|
@ -134,9 +134,6 @@ Utilities for manipulating transaction scripts (originally from python-bitcoinli
|
|||
#### [key.py](test_framework/key.py)
|
||||
Test-only secp256k1 elliptic curve implementation
|
||||
|
||||
#### [bignum.py](test_framework/bignum.py)
|
||||
Helpers for script.py
|
||||
|
||||
#### [blocktools.py](test_framework/blocktools.py)
|
||||
Helper functions for creating blocks and transactions.
|
||||
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
#
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
"""Big number routines.
|
||||
|
||||
This file is copied from python-bitcoinlib.
|
||||
"""
|
||||
|
||||
import struct
|
||||
|
||||
|
||||
# generic big endian MPI format
|
||||
|
||||
def bn_bytes(v, have_ext=False):
|
||||
ext = 0
|
||||
if have_ext:
|
||||
ext = 1
|
||||
return ((v.bit_length()+7)//8) + ext
|
||||
|
||||
def bn2bin(v):
|
||||
s = bytearray()
|
||||
i = bn_bytes(v)
|
||||
while i > 0:
|
||||
s.append((v >> ((i-1) * 8)) & 0xff)
|
||||
i -= 1
|
||||
return s
|
||||
|
||||
def bn2mpi(v):
|
||||
have_ext = False
|
||||
if v.bit_length() > 0:
|
||||
have_ext = (v.bit_length() & 0x07) == 0
|
||||
|
||||
neg = False
|
||||
if v < 0:
|
||||
neg = True
|
||||
v = -v
|
||||
|
||||
s = struct.pack(b">I", bn_bytes(v, have_ext))
|
||||
ext = bytearray()
|
||||
if have_ext:
|
||||
ext.append(0)
|
||||
v_bin = bn2bin(v)
|
||||
if neg:
|
||||
if have_ext:
|
||||
ext[0] |= 0x80
|
||||
else:
|
||||
v_bin[0] |= 0x80
|
||||
return s + ext + v_bin
|
||||
|
||||
# bitcoin-specific little endian format, with implicit size
|
||||
def mpi2vch(s):
|
||||
r = s[4:] # strip size
|
||||
r = r[::-1] # reverse string, converting BE->LE
|
||||
return r
|
||||
|
||||
def bn2vch(v):
|
||||
return bytes(mpi2vch(bn2mpi(v)))
|
|
@ -6,21 +6,59 @@
|
|||
|
||||
This file is modified from python-bitcoinlib.
|
||||
"""
|
||||
|
||||
from .messages import CTransaction, CTxOut, sha256, hash256, uint256_from_str, ser_uint256, ser_string
|
||||
|
||||
import hashlib
|
||||
import struct
|
||||
|
||||
from .bignum import bn2vch
|
||||
from .messages import (
|
||||
CTransaction,
|
||||
CTxOut,
|
||||
hash256,
|
||||
ser_string,
|
||||
ser_uint256,
|
||||
sha256,
|
||||
uint256_from_str,
|
||||
)
|
||||
|
||||
MAX_SCRIPT_ELEMENT_SIZE = 520
|
||||
|
||||
OPCODE_NAMES = {}
|
||||
|
||||
def hash160(s):
|
||||
return hashlib.new('ripemd160', sha256(s)).digest()
|
||||
|
||||
def bn2vch(v):
|
||||
"""Convert number to bitcoin-specific little endian format."""
|
||||
# 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
|
||||
ext = bytearray()
|
||||
if v.bit_length() > 0:
|
||||
have_ext = (v.bit_length() & 0x07) == 0
|
||||
ext.append(0)
|
||||
|
||||
# Is the number negative?
|
||||
neg = False
|
||||
if v < 0:
|
||||
neg = True
|
||||
v = -v
|
||||
|
||||
# Convert the int to bytes
|
||||
v_bin = bytearray()
|
||||
bytes_len = (v.bit_length() + 7) // 8
|
||||
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 have_ext:
|
||||
ext[0] |= 0x80
|
||||
else:
|
||||
v_bin[0] |= 0x80
|
||||
|
||||
v_bytes = ext + v_bin
|
||||
# Reverse bytes ordering for LE
|
||||
v_bytes.reverse()
|
||||
|
||||
return bytes(v_bytes)
|
||||
|
||||
_opcode_instances = []
|
||||
class CScriptOp(int):
|
||||
|
@ -506,7 +544,6 @@ class CScript(bytes):
|
|||
else:
|
||||
assert False # shouldn't happen
|
||||
|
||||
|
||||
data = bytes(self[i:i + datasize])
|
||||
|
||||
# Check for truncation
|
||||
|
|
Loading…
Reference in a new issue