Skip to content

Commit

Permalink
Add rlp.rlp_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementWalter committed Nov 13, 2024
1 parent ba7bd85 commit ee6512e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
17 changes: 13 additions & 4 deletions cairo/ethereum/rlp.cairo
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
12 changes: 11 additions & 1 deletion cairo/tests/ethereum/test_rlp.cairo
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
6 changes: 5 additions & 1 deletion cairo/tests/ethereum/test_rlp.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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)

0 comments on commit ee6512e

Please sign in to comment.