-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
UNSAFE_KECCAK_FINALIZE
hint (#271)
* Implement UnsafeKeccak * Update dependencies * Add unit test * Fix hash * Add unit tests * Add integration test * Add missing file * Add constant + GetStructFieldRelocatable + start hint * Add test file * Add MemorySegmentManager.GetFeltRange * Progress * Finish hint * Add unit test * Add integration test
- Loading branch information
Showing
7 changed files
with
155 additions
and
5 deletions.
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
%builtins output | ||
|
||
from starkware.cairo.common.alloc import alloc | ||
from starkware.cairo.common.serialize import serialize_word | ||
from starkware.cairo.common.keccak import unsafe_keccak_finalize, KeccakState | ||
from starkware.cairo.common.uint256 import Uint256 | ||
|
||
func main{output_ptr: felt*}() { | ||
alloc_locals; | ||
|
||
let (data: felt*) = alloc(); | ||
|
||
assert data[0] = 0; | ||
assert data[1] = 1; | ||
assert data[2] = 2; | ||
|
||
let keccak_state = KeccakState(start_ptr=data, end_ptr=data + 2); | ||
|
||
let res: Uint256 = unsafe_keccak_finalize(keccak_state); | ||
|
||
assert res.low = 17219183504112405672555532996650339574; | ||
assert res.high = 235346966651632113557018504892503714354; | ||
|
||
serialize_word(res.low); | ||
serialize_word(res.high); | ||
|
||
return (); | ||
} |
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,5 @@ | ||
package hint_codes | ||
|
||
const UNSAFE_KECCAK = "from eth_hash.auto import keccak\n\ndata, length = ids.data, ids.length\n\nif '__keccak_max_size' in globals():\n assert length <= __keccak_max_size, \\\n f'unsafe_keccak() can only be used with length<={__keccak_max_size}. ' \\\n f'Got: length={length}.'\n\nkeccak_input = bytearray()\nfor word_i, byte_i in enumerate(range(0, length, 16)):\n word = memory[data + word_i]\n n_bytes = min(16, length - byte_i)\n assert 0 <= word < 2 ** (8 * n_bytes)\n keccak_input += word.to_bytes(n_bytes, 'big')\n\nhashed = keccak(keccak_input)\nids.high = int.from_bytes(hashed[:16], 'big')\nids.low = int.from_bytes(hashed[16:32], 'big')" | ||
|
||
const UNSAFE_KECCAK_FINALIZE = "from eth_hash.auto import keccak\nkeccak_input = bytearray()\nn_elms = ids.keccak_state.end_ptr - ids.keccak_state.start_ptr\nfor word in memory.get_range(ids.keccak_state.start_ptr, n_elms):\n keccak_input += word.to_bytes(16, 'big')\nhashed = keccak(keccak_input)\nids.high = int.from_bytes(hashed[:16], 'big')\nids.low = int.from_bytes(hashed[16:32], 'big')" |
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
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
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
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
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