Skip to content

Instantly share code, notes, and snippets.

@mt-caret
Created June 27, 2019 01:04
Show Gist options
  • Save mt-caret/49d61d22a7577c4e8095f4fc92f86a22 to your computer and use it in GitHub Desktop.
Save mt-caret/49d61d22a7577c4e8095f4fc92f86a22 to your computer and use it in GitHub Desktop.
import numpy as np
from u256 import u256
max_value = 2**256 - 1
min_value = 0
mask = 2**32 - 1
o = u256()
def encode(n):
assert n <= max_value and n >= min_value
ret = []
for i in range(8):
ret.append((n >> (i * 32)) & mask)
return ret
def decode(xs):
assert len(xs) == 8
ret = 0
for i in range(8):
assert xs[i] <= mask and xs[i] >= 0
ret |= xs[i] << (i * 32)
return ret
def to_fut(xs):
return np.array(xs, dtype=np.uint32)
def of_fut(xs):
if 'get' in dir(xs):
return xs.get().tolist()
else:
return xs.tolist()
def call1(f, a):
arg = encode(a)
res = of_fut(f(to_fut(arg)))
print("input", arg, decode(arg))
print("output", res, decode(res))
def test_pack_unpack_roundtrip(a):
assert call1(o.pack_unpack_roundtrip, a) == a
if __name__ == '__main__':
test_pack_unpack_roundtrip(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment