From ee6512ecec4a507b0240f87c639023188abc5375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Walter?= Date: Wed, 13 Nov 2024 18:09:04 +0100 Subject: [PATCH] Add rlp.rlp_hash --- cairo/ethereum/rlp.cairo | 17 +++++++++++++---- cairo/tests/ethereum/test_rlp.cairo | 12 +++++++++++- cairo/tests/ethereum/test_rlp.py | 6 +++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/cairo/ethereum/rlp.cairo b/cairo/ethereum/rlp.cairo index 196dc9e..e399a7a 100644 --- a/cairo/ethereum/rlp.cairo +++ b/cairo/ethereum/rlp.cairo @@ -1,10 +1,12 @@ -from starkware.cairo.common.math_cmp import is_le, is_not_zero -from starkware.cairo.common.alloc import alloc -from starkware.cairo.common.memcpy import memcpy from ethereum.base_types import Bytes, BytesStruct, TupleBytes, TupleBytesStruct +from ethereum.crypto.hash import keccak256, Hash32 from ethereum.utils.numeric import is_zero -from src.utils.bytes import felt_to_bytes, felt_to_bytes_little from src.utils.array import reverse +from src.utils.bytes import felt_to_bytes, felt_to_bytes_little +from starkware.cairo.common.alloc import alloc +from starkware.cairo.common.cairo_builtins import BitwiseBuiltin, KeccakBuiltin +from starkware.cairo.common.math_cmp import is_le, is_not_zero +from starkware.cairo.common.memcpy import memcpy func _encode_bytes{range_check_ptr}(dst: felt*, raw_bytes: Bytes) -> felt { alloc_locals; @@ -97,3 +99,10 @@ func encode_sequence{range_check_ptr}(raw_sequence: TupleBytes) -> Bytes { let encoded_bytes = Bytes(value); return encoded_bytes; } + +func rlp_hash{range_check_ptr, bitwise_ptr: BitwiseBuiltin*, keccak_ptr: KeccakBuiltin*}( + raw_bytes: Bytes +) -> Hash32 { + let encoded_bytes = encode_bytes(raw_bytes); + return keccak256(encoded_bytes); +} diff --git a/cairo/tests/ethereum/test_rlp.cairo b/cairo/tests/ethereum/test_rlp.cairo index ea2f943..fadd9aa 100644 --- a/cairo/tests/ethereum/test_rlp.cairo +++ b/cairo/tests/ethereum/test_rlp.cairo @@ -1,5 +1,7 @@ -from ethereum.rlp import encode_bytes, get_joined_encodings, encode_sequence +from ethereum.rlp import encode_bytes, get_joined_encodings, encode_sequence, rlp_hash +from starkware.cairo.common.cairo_builtins import BitwiseBuiltin, KeccakBuiltin from ethereum.base_types import Bytes, TupleBytes +from ethereum.crypto.hash import Hash32 func test_encode_bytes{range_check_ptr}() -> Bytes { tempvar raw_bytes: Bytes; @@ -21,3 +23,11 @@ func test_encode_sequence{range_check_ptr}() -> Bytes { let encoded_bytes = encode_sequence(raw_sequence); return encoded_bytes; } + +func test_rlp_hash{range_check_ptr, bitwise_ptr: BitwiseBuiltin*, keccak_ptr: KeccakBuiltin*}( + ) -> Hash32 { + tempvar raw_bytes: Bytes; + %{ memory[ap - 1] = gen_arg(program_input["raw_bytes"]) %} + let hash = rlp_hash(raw_bytes); + return hash; +} diff --git a/cairo/tests/ethereum/test_rlp.py b/cairo/tests/ethereum/test_rlp.py index 5e5362b..a6a1904 100644 --- a/cairo/tests/ethereum/test_rlp.py +++ b/cairo/tests/ethereum/test_rlp.py @@ -1,7 +1,7 @@ import hypothesis.strategies as st from hypothesis import given -from ethereum.rlp import encode_bytes, encode_sequence, get_joined_encodings +from ethereum.rlp import encode_bytes, encode_sequence, get_joined_encodings, rlp_hash class TestRlp: @@ -22,3 +22,7 @@ def test_encode_sequence(self, cairo_run, raw_sequence): assert encode_sequence(raw_sequence) == cairo_run( "test_encode_sequence", raw_sequence=raw_sequence ) + + @given(raw_bytes=st.binary()) + def test_rlp_hash(self, cairo_run, raw_bytes): + assert rlp_hash(raw_bytes) == cairo_run("test_rlp_hash", raw_bytes=raw_bytes)