3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 """Big number routines.
7 This file is copied from python-bitcoinlib.
13 # generic big endian MPI format
15 def bn_bytes(v
, have_ext
=False):
19 return ((v
.bit_length()+7)//8) + ext
25 s
.append((v
>> ((i
-1) * 8)) & 0xff)
31 if v
.bit_length() > 0:
32 have_ext
= (v
.bit_length() & 0x07) == 0
39 s
= struct
.pack(b
">I", bn_bytes(v
, have_ext
))
49 return s
+ ext
+ v_bin
51 # bitcoin-specific little endian format, with implicit size
53 r
= s
[4:] # strip size
54 r
= r
[::-1] # reverse string, converting BE->LE
58 return bytes(mpi2vch(bn2mpi(v
)))