Skip to content

Commit

Permalink
feat: cairo keccak hints + integration tests (keep-starknet-strange#472)
Browse files Browse the repository at this point in the history
* temp files

* tmp

* temp

* cairo keccak fix + integration test

* fix review
  • Loading branch information
StringNick authored Mar 17, 2024
1 parent 7627888 commit 5b451a9
Show file tree
Hide file tree
Showing 10 changed files with 730 additions and 29 deletions.
71 changes: 70 additions & 1 deletion src/hint_processor/builtin_hint_codes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,10 @@ pub const MEMSET_CONTINUE_LOOP =
\\ids.continue_loop = 1 if n > 0 else 0
;

pub const MEMCPY_CONTINUE_COPYING = "n -= 1 ids.continue_copying = 1 if n > 0 else 0";
pub const MEMCPY_CONTINUE_COPYING =
\\n -= 1
\\ids.continue_copying = 1 if n > 0 else 0
;

pub const DEFAULT_DICT_NEW =
\\if '__dict_manager' not in globals():
Expand Down Expand Up @@ -510,3 +513,69 @@ pub const DICT_SQUASH_COPY_DICT =
\\ 'initial_dict': dict(__dict_manager.get_dict(ids.dict_accesses_end)),
\\})
;

pub const BLOCK_PERMUTATION =
\\from starkware.cairo.common.keccak_utils.keccak_utils import keccak_func
\\_keccak_state_size_felts = int(ids.KECCAK_STATE_SIZE_FELTS)
\\assert 0 <= _keccak_state_size_felts < 100
\\
\\output_values = keccak_func(memory.get_range(
\\ ids.keccak_ptr - _keccak_state_size_felts, _keccak_state_size_felts))
\\segments.write_arg(ids.keccak_ptr, output_values)
;

// The 0.10.3 whitelist uses this variant (instead of the one used by the common library), but both hints have the same behaviour
// We should check for future refactors that may discard one of the variants
pub const BLOCK_PERMUTATION_WHITELIST_V1 =
\\from starkware.cairo.common.cairo_keccak.keccak_utils import keccak_func
\\_keccak_state_size_felts = int(ids.KECCAK_STATE_SIZE_FELTS)
\\assert 0 <= _keccak_state_size_felts < 100
\\
\\output_values = keccak_func(memory.get_range(
\\ ids.keccak_ptr - _keccak_state_size_felts, _keccak_state_size_felts))
\\segments.write_arg(ids.keccak_ptr, output_values)
;

pub const BLOCK_PERMUTATION_WHITELIST_V2 =
\\from starkware.cairo.common.cairo_keccak.keccak_utils import keccak_func
\\_keccak_state_size_felts = int(ids.KECCAK_STATE_SIZE_FELTS)
\\assert 0 <= _keccak_state_size_felts < 100
\\output_values = keccak_func(memory.get_range(
\\ ids.keccak_ptr_start, _keccak_state_size_felts))
\\segments.write_arg(ids.output, output_values)
;

pub const KECCAK_WRITE_ARGS =
\\segments.write_arg(ids.inputs, [ids.low % 2 ** 64, ids.low // 2 ** 64])
\\segments.write_arg(ids.inputs + 2, [ids.high % 2 ** 64, ids.high // 2 ** 64])
;

pub const COMPARE_BYTES_IN_WORD_NONDET =
"memory[ap] = to_felt_or_relocatable(ids.n_bytes < ids.BYTES_IN_WORD)";

pub const COMPARE_KECCAK_FULL_RATE_IN_BYTES_NONDET =
"memory[ap] = to_felt_or_relocatable(ids.n_bytes >= ids.KECCAK_FULL_RATE_IN_BYTES)";

pub const CAIRO_KECCAK_INPUT_IS_FULL_WORD = "ids.full_word = int(ids.n_bytes >= 8)";

pub const CAIRO_KECCAK_FINALIZE_V1 =
\\# 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 < 10
\\inp = [0] * _keccak_state_size_felts
\\padding = (inp + keccak_func(inp)) * _block_size
\\segments.write_arg(ids.keccak_ptr_end, padding)
;

pub const CAIRO_KECCAK_FINALIZE_V2 =
\\# 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)
;
Loading

0 comments on commit 5b451a9

Please sign in to comment.