From d963259a8d35fd1c5eba9cd32bd6943914013653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Walter?= Date: Tue, 12 Nov 2024 19:03:02 +0100 Subject: [PATCH] Add keccak256 hash function --- cairo/ethereum/crypto/hash.cairo | 19 ++++++++++++++++++- cairo/tests/ethereum/crypto/test_hash.cairo | 11 +++++++++++ cairo/tests/ethereum/crypto/test_hash.py | 12 ++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 cairo/tests/ethereum/crypto/test_hash.cairo create mode 100644 cairo/tests/ethereum/crypto/test_hash.py diff --git a/cairo/ethereum/crypto/hash.cairo b/cairo/ethereum/crypto/hash.cairo index 4dc72a3b9..f396efd56 100644 --- a/cairo/ethereum/crypto/hash.cairo +++ b/cairo/ethereum/crypto/hash.cairo @@ -1,3 +1,20 @@ -from ethereum.base_types import Bytes32 +from ethereum.base_types import Bytes32, Bytes, Uint256 +from src.utils.bytes import bytes_to_bytes8_little_endian +from starkware.cairo.common.builtin_keccak.keccak import keccak_bigend +from starkware.cairo.common.cairo_builtins import BitwiseBuiltin, KeccakBuiltin +from starkware.cairo.common.alloc import alloc using Hash32 = Bytes32; + +func keccak256{range_check_ptr, bitwise_ptr: BitwiseBuiltin*, keccak_ptr: KeccakBuiltin*}( + buffer: Bytes +) -> Hash32 { + alloc_locals; + let (local dst: felt*) = alloc(); + bytes_to_bytes8_little_endian(dst, buffer.value.len, buffer.value.data); + + let (code_hash) = keccak_bigend(dst, buffer.value.len); + tempvar value = new Uint256(low=code_hash.low, high=code_hash.high); + tempvar hash = Hash32(value=value); + return hash; +} diff --git a/cairo/tests/ethereum/crypto/test_hash.cairo b/cairo/tests/ethereum/crypto/test_hash.cairo new file mode 100644 index 000000000..ab3ed9839 --- /dev/null +++ b/cairo/tests/ethereum/crypto/test_hash.cairo @@ -0,0 +1,11 @@ +from ethereum.crypto.hash import keccak256, Hash32 +from ethereum.base_types import Bytes +from starkware.cairo.common.cairo_builtins import KeccakBuiltin, BitwiseBuiltin + +func test_keccak256{range_check_ptr, bitwise_ptr: BitwiseBuiltin*, keccak_ptr: KeccakBuiltin*}( + ) -> Hash32 { + tempvar buffer: Bytes; + %{ memory[ap - 1] = gen_arg(program_input["buffer"]) %} + let hash = keccak256(buffer); + return hash; +} diff --git a/cairo/tests/ethereum/crypto/test_hash.py b/cairo/tests/ethereum/crypto/test_hash.py new file mode 100644 index 000000000..e4b35732e --- /dev/null +++ b/cairo/tests/ethereum/crypto/test_hash.py @@ -0,0 +1,12 @@ +import hypothesis.strategies as st +from hypothesis import given + +from ethereum.crypto.hash import keccak256 + + +class TestHash: + + class TestKeccak256: + @given(buffer=st.binary(min_size=0, max_size=1000)) + def test_keccak256(self, cairo_run, buffer): + assert keccak256(buffer) == cairo_run("test_keccak256", buffer=buffer)