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

Commit

Permalink
Add range_checks and corresponding test
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementWalter committed Sep 3, 2024
1 parent 674eb6c commit e8b9f0c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/utils/rlp.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ namespace RLP {
let (rlp_type, offset, len) = decode_type(data);
local remaining_data_len = data_len - len - offset;
with_attr error_message("RLP data too short for declared length") {
assert [range_check_ptr] = offset;
assert [range_check_ptr + 1] = len;
assert [range_check_ptr + 2] = offset + len;
let range_check_ptr = range_check_ptr + 3;
assert_nn(remaining_data_len);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/src/utils/test_rlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from hypothesis import given
from hypothesis.strategies import binary, lists, recursive
from rlp import codec, decode, encode
from starkware.cairo.lang.cairo_constants import DEFAULT_PRIME

from tests.utils.constants import TRANSACTIONS
from tests.utils.errors import cairo_error
Expand Down Expand Up @@ -40,6 +41,12 @@ def test_should_raise_when_malicious_prover_fills_data(self, cairo_run, data):
data=list(encode(data)),
)

def test_should_raise_when_decoded_params_overflow(self, cairo_run):
size = bytes.fromhex(f"{DEFAULT_PRIME - 1:064x}")
data = [len(size) + 0xF7] + list(size)
with cairo_error("RLP data too short for declared length"):
cairo_run("test__decode_raw", data=data)

class TestDecode:
@given(data=recursive(binary(), lists))
def test_should_match_decode_reference_implementation(self, cairo_run, data):
Expand Down

0 comments on commit e8b9f0c

Please sign in to comment.