Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use cairo1Helper sha256 #1409

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/kakarot/interfaces/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,9 @@ namespace ICairo1Helpers {
msg_hash: Uint256, r: Uint256, s: Uint256, x: Uint256, y: Uint256
) -> (is_valid: felt) {
}

func compute_sha256_u32_array(
input_len: felt, input: felt*, last_input_word: felt, last_input_num_bytes: felt
) -> (sha256_u32_array_len: felt, sha256_u32_array: felt*) {
}
}
434 changes: 17 additions & 417 deletions src/kakarot/precompiles/sha256.cairo

Large diffs are not rendered by default.

288 changes: 0 additions & 288 deletions src/utils/sha_256/packed_sha256.cairo

This file was deleted.

15 changes: 9 additions & 6 deletions src/utils/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -771,24 +771,27 @@ namespace Helpers {
}

// @notice transform multiple bytes into words of 32 bits (big endian)
// @dev the input data must have length in multiples of 4
// @param data_len The length of the bytes
// @param data The pointer to the bytes array
// @param n_len used for recursion, set to 0
// @param n used for recursion, set to pointer
// @return n_len the resulting array length
// @return n the resulting array
// @return last the last word
// @return last_num_bytes the number of bytes in the last word
func bytes_to_bytes4_array{range_check_ptr}(
data_len: felt, data: felt*, n_len: felt, n: felt*
) -> (n_len: felt, n: felt*) {
) -> (n_len: felt, n: felt*, last: felt, last_num_bytes: felt) {
obatirou marked this conversation as resolved.
Show resolved Hide resolved
alloc_locals;
if (data_len == 0) {
return (n_len=n_len, n=n);
return (n_len=n_len, n=n, last=0, last_num_bytes=0);
}

let (_, r) = unsigned_div_rem(data_len, 4);
with_attr error_message("data length must be multiple of 4") {
assert r = 0;
let (q, r) = unsigned_div_rem(data_len, 4);
if (q == 0 and r != 0) {
let res = bytes_to_felt(r, data);
assert n[n_len] = res;
return (n_len=n_len, n=n, last=res, last_num_bytes=r);
}

// Load sequence of 4 bytes into a single 32-bit word (big endian)
Expand Down
2 changes: 1 addition & 1 deletion tests/src/kakarot/precompiles/test_sha256.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@pytest.mark.SHA256
class TestSHA256:
@pytest.mark.slow
@given(message_bytes=binary(min_size=1, max_size=56))
@given(message_bytes=binary(min_size=1, max_size=2**128 - 1))
@settings(max_examples=10)
def test_sha256_should_return_correct_hash(self, cairo_run, message_bytes):
# Hash with SHA256
Expand Down
18 changes: 8 additions & 10 deletions tests/src/utils/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,24 @@ func test__bytes_to_uint256{range_check_ptr}() -> Uint256 {
return res;
}

func test__bytes_to_bytes4_array{range_check_ptr}() {
func test__bytes_to_bytes4_array{range_check_ptr}() -> (felt*, felt, felt) {
alloc_locals;
// Given
let (data) = alloc();
let (expected) = alloc();
local data_len: felt;

%{
segments.write_arg(ids.data, program_input["data"])
segments.write_arg(ids.expected, program_input["expected"])
ids.data_len = len(program_input["data"])
%}

// When
let (tmp: felt*) = alloc();
let (_, result: felt*) = Helpers.bytes_to_bytes4_array(12, data, 0, tmp);

// Then
assert expected[0] = result[0];
assert expected[1] = result[1];
assert expected[2] = result[2];
let (_, result: felt*, last: felt, last_num_bytes: felt) = Helpers.bytes_to_bytes4_array(
data_len, data, 0, tmp
);

return ();
return (result, last, last_num_bytes);
}

func test__bytes4_array_to_bytes{range_check_ptr}() {
Expand Down
Loading
Loading