Skip to content

Commit

Permalink
[KGA-31] fix: felt_to_bytes_little loop stop condition (#1573)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

<!-- Give an estimate of the time you spent on this PR in terms of work
days.
Did you spend 0.5 days on this PR or rather 2 days?  -->

Time spent on this PR:

## Pull request type

<!-- Please try to limit your pull request to one type,
submit multiple pull requests if needed. -->

Please check the type of change your PR introduces:

- [x] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying,
or link to a relevant issue. -->

Resolves #1563

## What is the new behavior?

<!-- Please describe the behavior or changes that are being added by
this PR. -->

The loop will stop at 31 bytes

<!-- Reviewable:start -->
- - -
This change is [<img src="https://reviewable.io/review_button.svg"
height="34" align="absmiddle"
alt="Reviewable"/>](https://reviewable.io/reviews/kkrt-labs/kakarot/1573)
<!-- Reviewable:end -->
  • Loading branch information
obatirou authored Nov 4, 2024
1 parent fc8addc commit f599f71
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
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

0 comments on commit f599f71

Please sign in to comment.