diff options
author | MarcoFalke <falke.marco@gmail.com> | 2017-03-21 00:10:53 +0100 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2017-03-21 00:11:14 +0100 |
commit | 3192e5278abca7c1f3b4a2a7f77a0ce941c73985 (patch) | |
tree | 826cfa504915d204ce8f6a414d50ed0ffbf7003f /test/functional/test_framework/bignum.py | |
parent | 0c17afcbe73ec546bc63aea34639fa0f4ea45c37 (diff) | |
parent | 63d66ba20a634b54f6d5e8b051fb4a106f2cef6c (diff) |
Merge #9956: Reorganise qa directory
63d66ba Move src/test/bitcoin-util-test.py to test/util/bitcoin-util-test.py (John Newbery)
5b0bff4 Rename --enable-extended-rpc-tests to --enable-extended-functional-tests (John Newbery)
a9bd622 Rename test/pull-tester/rpc-tests.py to test/functional/test_runner.py (John Newbery)
c28ee91 Rename rpc-tests directory to functional (John Newbery)
00902c4 Rename qa directory to test (John Newbery)
Tree-SHA512: ee7125c0c647d81590177beef2c8852c4ef76fdcf888096d9d4d360562a01d8d3b453345c3040487b2a043935bd1e7e80018f34462d6e02262bedbe23edcc576
Diffstat (limited to 'test/functional/test_framework/bignum.py')
-rw-r--r-- | test/functional/test_framework/bignum.py | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/test/functional/test_framework/bignum.py b/test/functional/test_framework/bignum.py new file mode 100644 index 0000000000..024611da6e --- /dev/null +++ b/test/functional/test_framework/bignum.py @@ -0,0 +1,97 @@ +#!/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 bin2bn(s): + l = 0 + for ch in s: + l = (l << 8) | ch + return l + +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 + +def mpi2bn(s): + if len(s) < 4: + return None + s_size = bytes(s[:4]) + v_len = struct.unpack(b">I", s_size)[0] + if len(s) != (v_len + 4): + return None + if v_len == 0: + return 0 + + v_str = bytearray(s[4:]) + neg = False + i = v_str[0] + if i & 0x80: + neg = True + i &= ~0x80 + v_str[0] = i + + v = bin2bn(v_str) + + if neg: + return -v + return v + +# 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))) + +def vch2mpi(s): + r = struct.pack(b">I", len(s)) # size + r += s[::-1] # reverse string, converting LE->BE + return r + +def vch2bn(s): + return mpi2bn(vch2mpi(s)) + |