-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3636393
commit d963259
Showing
3 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |