Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: pad numbers to requested size before XOR-ing them #22

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/aleph_vrf/coordinator/vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def generate_final_vrf(
)

random_numbers_list.append(
int_to_bytes(int(vrf_publish_response.random_number))
int_to_bytes(int(vrf_publish_response.random_number), n=vrf_request.nb_bytes)
)

node_response = CRNVRFResponse(
Expand Down
13 changes: 8 additions & 5 deletions src/aleph_vrf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ def xor_all(x: List[bytes]) -> bytes:
return result


def int_to_bytes(x: int) -> bytes:
"""Converts an integer to bytes."""
return x.to_bytes((x.bit_length() + 7) // 8, "big")
def int_to_bytes(x: int, n: int = 0) -> bytes:
"""
Converts an integer to bytes.
If `n` is specified, pads the number to reach n bytes.
"""
return x.to_bytes(max((x.bit_length() + 7) // 8, n), "big")


def bytes_to_int(x: bytes) -> int:
Expand All @@ -30,9 +33,9 @@ def bytes_to_binary(x: bytes) -> str:
return "".join(format(b, "08b") for b in x)


def binary_to_bytes(s: str):
def binary_to_bytes(s: str, n: int = 0):
"""Converts binary string to bytes."""
return int(s, 2).to_bytes((len(s) + 7) // 8, byteorder="big")
return int(s, 2).to_bytes(max((len(s) + 7) // 8, n), byteorder="big")


def generate_nonce() -> Nonce:
Expand Down