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(hints): "full word" from cairo_keccak.json #1058

Merged
merged 3 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

#### Upcoming Changes

* Implement hint for `starkware.cairo.common.cairo_keccak.keccak._copy_inputs` as described by whitelist `starknet/security/whitelists/cairo_keccak.json` [#1058](https://github.com/lambdaclass/cairo-rs/pull/1058)

`BuiltinHintProcessor` now supports the following hint:

```python
%{ ids.full_word = int(ids.n_bytes >= 8) %}
```

* Add missing hint on vrf.json lib [#1052](https://github.com/lambdaclass/cairo-rs/pull/1052):

`BuiltinHintProcessor` now supports the following hint:
Expand Down
14 changes: 14 additions & 0 deletions cairo_programs/_keccak_alternative_hint.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ func run_cairo_keccak{output_ptr: felt*, range_check_ptr, bitwise_ptr: BitwiseBu
_prepare_block{keccak_ptr=output_ptr}(inputs=inputs, n_bytes=n_bytes, state=state);
_block_permutation_cairo_keccak{keccak_ptr=output_ptr}();

local full_word: felt;
%{ ids.full_word = int(ids.n_bytes >= 8) %}
assert full_word = 1;

let n_bytes = 8;
local full_word: felt;
%{ ids.full_word = int(ids.n_bytes >= 8) %}
assert full_word = 1;

let n_bytes = 7;
local full_word: felt;
%{ ids.full_word = int(ids.n_bytes >= 8) %}
assert full_word = 0;

return ();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
},
cairo_keccak::keccak_hints::{
block_permutation_v1, block_permutation_v2, cairo_keccak_finalize_v1,
cairo_keccak_finalize_v2, compare_bytes_in_word_nondet,
cairo_keccak_finalize_v2, cairo_keccak_is_full_word, compare_bytes_in_word_nondet,
compare_keccak_full_rate_in_bytes_nondet, keccak_write_args,
},
dict_hint_utils::{
Expand Down Expand Up @@ -550,6 +550,9 @@ impl HintProcessor for BuiltinHintProcessor {
hint_code::SHA256_FINALIZE => {
sha256_finalize(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::CAIRO_KECCAK_INPUT_IS_FULL_WORD => {
cairo_keccak_is_full_word(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::COMPARE_KECCAK_FULL_RATE_IN_BYTES_NONDET => {
compare_keccak_full_rate_in_bytes_nondet(
vm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::{
felt::Felt252,
hint_processor::{
builtin_hint_processor::hint_utils::{
get_integer_from_var_name, get_ptr_from_var_name, insert_value_into_ap,
get_integer_from_var_name, get_ptr_from_var_name, insert_value_from_var_name,
insert_value_into_ap,
},
hint_processor_definition::HintReference,
},
Expand Down Expand Up @@ -181,6 +182,24 @@ pub(crate) fn block_permutation_v1(
Ok(())
}

/*
Implements hint:
%{
ids.full_word = int(ids.n_bytes >= 8)
%}
*/
pub(crate) fn cairo_keccak_is_full_word(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
let n_bytes = get_integer_from_var_name("n_bytes", vm, ids_data, ap_tracking)?
.to_usize()
.unwrap_or(8); // Hack: if it doesn't fit `usize` then it's >= 8
let full_word = Felt252::from((n_bytes >= 8) as usize);
insert_value_from_var_name("full_word", &full_word, vm, ids_data, ap_tracking)
}

/*
Implements hint:
%{
Expand Down Expand Up @@ -362,10 +381,30 @@ mod tests {
vm::vm_core::VirtualMachine,
};
use assert_matches::assert_matches;
use rstest::*;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

#[rstest]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[case(0, 0)]
#[case(1, 0)]
#[case(7, 0)]
#[case(8, 1)]
#[case(16, 1)]
#[case(usize::MAX as i128, 1)]
#[case(i128::MAX, 1)]
fn cairo_keccak_is_full_word(#[case] n_bytes: i128, #[case] full_bytes: usize) {
let hint_code = "ids.full_word = int(ids.n_bytes >= 8)";
let mut vm = vm_with_range_check!();
vm.segments = segments![((1, 1), n_bytes)];
vm.run_context.fp = 2;
let ids_data = ids_data!["full_word", "n_bytes"];
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
check_memory![vm.segments.memory, ((1, 0), full_bytes)];
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn keccak_write_args_valid_test() {
Expand Down
2 changes: 2 additions & 0 deletions src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,8 @@ output_values = keccak_func(memory.get_range(
ids.keccak_ptr_start, _keccak_state_size_felts))
segments.write_arg(ids.output, output_values)"#;

pub const CAIRO_KECCAK_INPUT_IS_FULL_WORD: &str = r#"ids.full_word = int(ids.n_bytes >= 8)"#;

pub const CAIRO_KECCAK_FINALIZE_V1: &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
Loading