Skip to content

Commit

Permalink
add py utils
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime committed May 6, 2024
1 parent cc4ced3 commit 4b4def1
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tools/py/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

def split_128(a):
"""Takes in value, returns uint256-ish tuple."""
assert (
0 <= a.bit_length() <= 256
), f"split_128 input exceeds maximum bit length of 256, got {a.bit_length()}"
return (a & ((1 << 128) - 1), a >> 128)


Expand All @@ -21,6 +24,17 @@ def reverse_endian_256(x: int):
return int.from_bytes(x.to_bytes(32, "big"), "little")


def parse_int_to_bytes(x: int) -> bytes:
"""
Convert an integer to a bytes object.
If the number of bytes is odd, left pad with one leading zero.
"""
hex_str = hex(x)[2:]
if len(hex_str) % 2 == 1:
hex_str = "0" + hex_str
return bytes.fromhex(hex_str)


def count_trailing_zero_bytes_from_int(number: int) -> int:
"""
Counts the number of trailing zero bytes in the hexadecimal representation of an integer.
Expand Down

0 comments on commit 4b4def1

Please sign in to comment.