Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/bignum.py
Show All 22 Lines | def bn2bin(v): | ||||
s = bytearray() | s = bytearray() | ||||
i = bn_bytes(v) | i = bn_bytes(v) | ||||
while i > 0: | while i > 0: | ||||
s.append((v >> ((i - 1) * 8)) & 0xff) | s.append((v >> ((i - 1) * 8)) & 0xff) | ||||
i -= 1 | i -= 1 | ||||
return s | return s | ||||
def bin2bn(s): | |||||
l = 0 | |||||
for ch in s: | |||||
l = (l << 8) | ch | |||||
return l | |||||
def bn2mpi(v): | def bn2mpi(v): | ||||
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 | ||||
neg = False | neg = False | ||||
if v < 0: | if v < 0: | ||||
neg = True | neg = True | ||||
v = -v | v = -v | ||||
s = struct.pack(b">I", bn_bytes(v, have_ext)) | 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) | ||||
v_bin = bn2bin(v) | v_bin = bn2bin(v) | ||||
if neg: | if neg: | ||||
if have_ext: | if have_ext: | ||||
ext[0] |= 0x80 | ext[0] |= 0x80 | ||||
else: | else: | ||||
v_bin[0] |= 0x80 | v_bin[0] |= 0x80 | ||||
return s + ext + v_bin | 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 | # bitcoin-specific little endian format, with implicit size | ||||
def mpi2vch(s): | def mpi2vch(s): | ||||
r = s[4:] # strip size | r = s[4:] # strip size | ||||
r = r[::-1] # reverse string, converting BE->LE | r = r[::-1] # reverse string, converting BE->LE | ||||
return r | return r | ||||
def bn2vch(v): | def bn2vch(v): | ||||
return bytes(mpi2vch(bn2mpi(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)) |