Skip to content
This repository was archived by the owner on Jan 9, 2025. It is now read-only.

[KGA-31] [KGA-130] [KGA-138] fix: felt_to_bytes_little loop stop condition #1573

Merged
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
20 changes: 19 additions & 1 deletion cairo_zero/tests/src/utils/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,28 @@ def test_should_raise_when_bytes_len_is_not_minimal(
"memory[ids.output] = res = (int(ids.value) % PRIME) % ids.base\nassert res < ids.bound, f'split_int(): Limb {res} is out of range.'",
f"if ids.value == {n} and ids.bytes_len == 0:\n memory[ids.output] = 0\nelse:\n memory[ids.output] = (int(ids.value) % PRIME) % ids.base",
),
cairo_error(message="bytes_len is not the minimal possible"),
cairo_error(
message=[
"bytes_len is not the minimal possible",
"Value is not empty",
]
),
):
cairo_run("test__felt_to_bytes_little", n=n)

def test_should_raise_finding_39_code4rena_2024_09(
self, cairo_program, cairo_run
):
with (
patch_hint(
cairo_program,
"memory[ids.output] = res = (int(ids.value) % PRIME) % ids.base\nassert res < ids.bound, f'split_int(): Limb {res} is out of range.'",
"memory[ids.output] = 2 if ids.bytes_len < 3 else (int(ids.value) % PRIME) % ids.base\nprint(f'[DEBUG] Byte value: {memory[ids.output]}')",
),
cairo_error(message="bytes_len is not the minimal possible"),
):
cairo_run("test__felt_to_bytes_little", n=3)

class TestFeltToBytes:
@given(n=integers(min_value=0, max_value=2**248 - 1))
def test_should_return_bytes(self, cairo_run, n):
Expand Down
6 changes: 5 additions & 1 deletion cairo_zero/utils/bytes.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.math import split_int, split_felt, assert_le_felt, assert_nn_le
from starkware.cairo.common.math_cmp import is_nn
from starkware.cairo.common.uint256 import Uint256
from starkware.cairo.common.memcpy import memcpy
from starkware.cairo.common.memset import memset
Expand Down Expand Up @@ -82,7 +83,9 @@ func felt_to_bytes_little{range_check_ptr}(dst: felt*, value: felt) -> felt {
let range_check_ptr = [ap - 3];
let value = [ap - 2];
let bytes_len = [ap - 1];
assert value = 0;
with_attr error_message("Value is not empty") {
assert value = 0;
}

let (pow256_address) = get_label_location(pow256_table);
if (bytes_len == 1) {
Expand All @@ -97,6 +100,7 @@ func felt_to_bytes_little{range_check_ptr}(dst: felt*, value: felt) -> felt {
let lower_bound = [ap - 1];
let upper_bound = pow256_address[bytes_len];
with_attr error_message("bytes_len is not the minimal possible") {
assert_le_felt(bytes_len, 31);
assert_le_felt(lower_bound, initial_value);
assert_le_felt(initial_value, upper_bound - 1);
}
Expand Down
7 changes: 6 additions & 1 deletion tests/utils/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ def cairo_error(message=None):
else:
error = re.search(r"Error message: (.*)", str(e.value))
error = error.group(1) if error else str(e.value)
assert str(message) in error, f"Expected {message}, got {error}"
if isinstance(message, list):
assert any(
str(msg) in error for msg in message
), f"Expected {message}, got {error}"
else:
assert str(message) in error, f"Expected {message}, got {error}"
finally:
pass
Loading