-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hint): support hint #68 for keccak_finalize
Version from whitelist `starknet/security/whitelists/cairo_keccak.json`, differs from the current one in bounds for `_block_size`, `1000` instead of `10`.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
cairo_programs/bad_programs/cairo_finalize_keccak_block_size_too_big.cairo
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,62 @@ | ||
%builtins range_check bitwise | ||
|
||
from starkware.cairo.common.alloc import alloc | ||
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin | ||
from starkware.cairo.common.cairo_keccak.keccak import _finalize_keccak_inner, cairo_keccak, KECCAK_STATE_SIZE_FELTS | ||
from starkware.cairo.common.math import unsigned_div_rem | ||
from starkware.cairo.common.uint256 import Uint256 | ||
|
||
const BLOCK_SIZE = 0x800000000000011000000000000000000000000000000000000000000000000; | ||
|
||
// Verifies that the results of cairo_keccak() are valid. For optimization, this can be called only | ||
// once after all the keccak calculations are completed. | ||
// Version copied from starknet/security/whitelists/cairo_keccak.json | ||
func finalize_keccak{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}( | ||
keccak_ptr_start: felt*, keccak_ptr_end: felt* | ||
) { | ||
alloc_locals; | ||
|
||
tempvar n = (keccak_ptr_end - keccak_ptr_start) / (2 * KECCAK_STATE_SIZE_FELTS); | ||
if (n == 0) { | ||
return (); | ||
} | ||
|
||
%{ | ||
# Add dummy pairs of input and output. | ||
_keccak_state_size_felts = int(ids.KECCAK_STATE_SIZE_FELTS) | ||
_block_size = int(ids.BLOCK_SIZE) | ||
assert 0 <= _keccak_state_size_felts < 100 | ||
assert 0 <= _block_size < 1000 | ||
inp = [0] * _keccak_state_size_felts | ||
padding = (inp + keccak_func(inp)) * _block_size | ||
segments.write_arg(ids.keccak_ptr_end, padding) | ||
%} | ||
|
||
// Compute the amount of blocks (rounded up). | ||
let (local q, r) = unsigned_div_rem(n + BLOCK_SIZE - 1, BLOCK_SIZE); | ||
_finalize_keccak_inner(keccak_ptr_start, n=q); | ||
return (); | ||
} | ||
|
||
func main{range_check_ptr: felt, bitwise_ptr: BitwiseBuiltin*}() { | ||
alloc_locals; | ||
|
||
let (keccak_ptr: felt*) = alloc(); | ||
let keccak_ptr_start = keccak_ptr; | ||
|
||
let (inputs: felt*) = alloc(); | ||
|
||
assert inputs[0] = 8031924123371070792; | ||
assert inputs[1] = 560229490; | ||
|
||
let n_bytes = 16; | ||
|
||
let (res: Uint256) = cairo_keccak{keccak_ptr=keccak_ptr}(inputs=inputs, n_bytes=n_bytes); | ||
|
||
assert res.low = 293431514620200399776069983710520819074; | ||
assert res.high = 317109767021952548743448767588473366791; | ||
|
||
finalize_keccak(keccak_ptr_start=keccak_ptr_start, keccak_ptr_end=keccak_ptr); | ||
|
||
return (); | ||
} |