Skip to content

Commit

Permalink
feat: Add alternative hint code for hint on _block_permutation used…
Browse files Browse the repository at this point in the history
… by 0.10.3 whitelist (lambdaclass#958)

* Add alternative hint string

* Fix whitelist name

* Add integration test

* Add changelog entry

* Remove old file

* Fix broken tests
  • Loading branch information
fmoletta authored and kariy committed Jun 23, 2023
1 parent 86ce1ad commit f80a1f4
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

#### Upcoming Changes

* Add alternative hint code for hint on _block_permutation used by 0.10.3 whitelist [#958](https://github.com/lambdaclass/cairo-rs/pull/958)

`BuiltinHintProcessor` now supports the following hint:

```python
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)
```

* Implement hints on uint384 lib (Part 1) [#960](https://github.com/lambdaclass/cairo-rs/pull/960)

`BuiltinHintProcessor` now supports the following hints:
Expand Down Expand Up @@ -104,6 +118,7 @@
ids.root.d1 = root_split[1]
ids.root.d2 = root_split[2]
```

* Implement hint on `uint256_mul_div_mod`[#957](https://github.com/lambdaclass/cairo-rs/pull/957)

`BuiltinHintProcessor` now supports the following hint:
Expand Down
88 changes: 88 additions & 0 deletions cairo_programs/_keccak_alternative_hint.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
%builtins output range_check bitwise

from starkware.cairo.common.cairo_keccak.keccak import (
_prepare_block,
KECCAK_FULL_RATE_IN_BYTES,
KECCAK_FULL_RATE_IN_WORDS,
KECCAK_STATE_SIZE_FELTS,
)
from starkware.cairo.common.math import assert_nn_le
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.serialize import serialize_word

func _keccak{range_check_ptr, bitwise_ptr: BitwiseBuiltin*, keccak_ptr: felt*}(
inputs: felt*, n_bytes: felt, state: felt*
) -> (output: felt*) {
alloc_locals;
if (nondet %{ ids.n_bytes >= ids.KECCAK_FULL_RATE_IN_BYTES %} != 0) {
_prepare_block(inputs=inputs, n_bytes=KECCAK_FULL_RATE_IN_BYTES, state=state);
_block_permutation();

return _keccak(
inputs=inputs + KECCAK_FULL_RATE_IN_WORDS,
n_bytes=n_bytes - KECCAK_FULL_RATE_IN_BYTES,
state=keccak_ptr - KECCAK_STATE_SIZE_FELTS,
);
}

assert_nn_le(n_bytes, KECCAK_FULL_RATE_IN_BYTES - 1);

_prepare_block(inputs=inputs, n_bytes=n_bytes, state=state);
_block_permutation();

return (output=keccak_ptr - KECCAK_STATE_SIZE_FELTS);
}

func _block_permutation{keccak_ptr: felt*}() {
%{
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)
%}
let keccak_ptr = keccak_ptr + KECCAK_STATE_SIZE_FELTS;

return ();
}

func fill_array(array: felt*, base: felt, array_length: felt, iterator: felt) {
if (iterator == array_length) {
return ();
}

assert array[iterator] = base;

return fill_array(array, base, array_length, iterator + 1);
}

func main{output_ptr: felt*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() {
alloc_locals;

let (output: felt*) = alloc();
let keccak_output = output;

let (inputs: felt*) = alloc();
let inputs_start = inputs;
fill_array(inputs, 9, 3, 0);

let (state: felt*) = alloc();
let state_start = state;
fill_array(state, 5, 25, 0);

let n_bytes = 24;

let (res: felt*) = _keccak{keccak_ptr=keccak_output}(
inputs=inputs_start, n_bytes=n_bytes, state=state_start
);

serialize_word(res[0]);
serialize_word(res[1]);
serialize_word(res[2]);
serialize_word(res[4]);

return ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl HintProcessor for BuiltinHintProcessor {
constants,
)
}
hint_code::BLOCK_PERMUTATION => {
hint_code::BLOCK_PERMUTATION | hint_code::BLOCK_PERMUTATION_WHITELIST => {
block_permutation(vm, &hint_data.ids_data, &hint_data.ap_tracking, constants)
}
hint_code::CAIRO_KECCAK_FINALIZE => {
Expand Down
10 changes: 10 additions & 0 deletions src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,16 @@ 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(crate) const BLOCK_PERMUTATION_WHITELIST: &str = r#"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(crate) const CAIRO_KECCAK_FINALIZE: &str = r#"# Add dummy pairs of input and output.
_keccak_state_size_felts = int(ids.KECCAK_STATE_SIZE_FELTS)
_block_size = int(ids.BLOCK_SIZE)
Expand Down
7 changes: 7 additions & 0 deletions src/tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,13 @@ fn cairo_run_is_quad_residue_test() {
run_program_simple(program_data.as_slice());
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn cairo_run_keccak_alternative_hint() {
let program_data = include_bytes!("../../cairo_programs/_keccak_alternative_hint.json");
run_program_simple(program_data.as_slice());
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn cairo_run_uint384() {
Expand Down

0 comments on commit f80a1f4

Please sign in to comment.