Skip to content

Commit

Permalink
Clean up hint processing
Browse files Browse the repository at this point in the history
  • Loading branch information
fmkra committed Oct 25, 2024
1 parent 55e6ba0 commit fa2492f
Show file tree
Hide file tree
Showing 29 changed files with 277 additions and 318 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ num-bigint = "0.4.6"
num-traits = "0.2.19"
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
hex = "0.4.3"
starknet-crypto = "0.7.2"
starknet-crypto = "0.7.2"
linkme = "0.3.29"
1 change: 1 addition & 0 deletions cairo_vm_hints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ num-traits.workspace = true
tiny-keccak.workspace = true
hex.workspace = true
starknet-crypto.workspace = true
linkme.workspace = true
2 changes: 1 addition & 1 deletion cairo_vm_hints/src/hint_processor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::run_hint;
use crate::hints::run_hint;
use cairo_vm::{
hint_processor::{
builtin_hint_processor::builtin_hint_processor_definition::{
Expand Down
17 changes: 4 additions & 13 deletions cairo_vm_hints/src/hints/lib/bit_length.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::hints::{Hint, HINTS};
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_integer_from_var_name, insert_value_from_var_name,
Expand All @@ -6,6 +7,7 @@ use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use linkme::distributed_slice;
use std::collections::HashMap;

const HINT_BIT_LENGTH: &str = "ids.bit_length = ids.x.bit_length()";
Expand All @@ -28,16 +30,5 @@ fn hint_bit_length(
Ok(())
}

pub fn run_hint(
vm: &mut VirtualMachine,
exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
match hint_data.code.as_str() {
HINT_BIT_LENGTH => hint_bit_length(vm, exec_scope, hint_data, constants),
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
}
}
#[distributed_slice(HINTS)]
static _HINT_BIT_LENGTH: Hint = (HINT_BIT_LENGTH, hint_bit_length);
17 changes: 4 additions & 13 deletions cairo_vm_hints/src/hints/lib/block_header/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::hints::{Hint, HINTS};
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_integer_from_var_name, insert_value_into_ap,
Expand All @@ -6,6 +7,7 @@ use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use linkme::distributed_slice;
use std::cmp::Ordering;
use std::collections::HashMap;

Expand All @@ -31,16 +33,5 @@ fn hint_rlp_bigint_size(
Ok(())
}

pub fn run_hint(
vm: &mut VirtualMachine,
exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
match hint_data.code.as_str() {
HINT_RLP_BIGINT_SIZE => hint_rlp_bigint_size(vm, exec_scope, hint_data, constants),
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
}
}
#[distributed_slice(HINTS)]
static _HINT_RLP_BIGINT_SIZE: Hint = (HINT_RLP_BIGINT_SIZE, hint_rlp_bigint_size);
9 changes: 7 additions & 2 deletions cairo_vm_hints/src/hints/lib/mmr/bit_length.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::hints::{Hint, HINTS};
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_integer_from_var_name, insert_value_from_var_name,
Expand All @@ -6,11 +7,12 @@ use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use linkme::distributed_slice;
use std::collections::HashMap;

pub const MMR_BIT_LENGTH: &str = "ids.bit_length = ids.mmr_len.bit_length()";
const MMR_LEFT_CHILD: &str = "ids.bit_length = ids.mmr_len.bit_length()";

pub fn mmr_bit_length(
fn mmr_left_child(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
Expand All @@ -27,3 +29,6 @@ pub fn mmr_bit_length(

Ok(())
}

#[distributed_slice(HINTS)]
static _MMR_LEFT_CHILD: Hint = (MMR_LEFT_CHILD, mmr_left_child);
9 changes: 7 additions & 2 deletions cairo_vm_hints/src/hints/lib/mmr/left_child.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::hints::{Hint, HINTS};
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_integer_from_var_name, insert_value_from_var_name,
Expand All @@ -6,12 +7,13 @@ use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use linkme::distributed_slice;
use starknet_types_core::felt::Felt;
use std::collections::HashMap;

pub const MMR_LEFT_CHILD: &str = "ids.in_mmr = 1 if ids.left_child<=ids.mmr_len else 0";
const MMR_LEFT_CHILD: &str = "ids.in_mmr = 1 if ids.left_child<=ids.mmr_len else 0";

pub fn mmr_left_child(
fn mmr_left_child(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
Expand Down Expand Up @@ -41,3 +43,6 @@ pub fn mmr_left_child(

Ok(())
}

#[distributed_slice(HINTS)]
static _MMR_LEFT_CHILD: Hint = (MMR_LEFT_CHILD, mmr_left_child);
27 changes: 0 additions & 27 deletions cairo_vm_hints/src/hints/lib/mmr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,2 @@
use cairo_vm::{
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData,
types::exec_scope::ExecutionScopes,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
Felt252,
};
use std::collections::HashMap;

mod bit_length;
mod left_child;

pub fn run_hint(
vm: &mut VirtualMachine,
exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
match hint_data.code.as_str() {
bit_length::MMR_BIT_LENGTH => {
bit_length::mmr_bit_length(vm, exec_scope, hint_data, constants)
}
left_child::MMR_LEFT_CHILD => {
left_child::mmr_left_child(vm, exec_scope, hint_data, constants)
}
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
}
}
34 changes: 0 additions & 34 deletions cairo_vm_hints/src/hints/lib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,6 @@
use cairo_vm::{
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData,
types::exec_scope::ExecutionScopes,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
Felt252,
};
use std::collections::HashMap;

mod bit_length;
mod block_header;
mod mmr;
mod mpt;
mod rlp_little;
mod utils;

pub fn run_hint(
vm: &mut VirtualMachine,
exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let hints = [
bit_length::run_hint,
block_header::run_hint,
mmr::run_hint,
mpt::run_hint,
rlp_little::run_hint,
utils::run_hint,
];

for hint in hints.iter() {
let res = hint(vm, exec_scope, hint_data, constants);
if !matches!(res, Err(HintError::UnknownHint(_))) {
return res;
}
}
Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
))
}
28 changes: 12 additions & 16 deletions cairo_vm_hints/src/hints/lib/mpt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fn is_long_list(value: Felt252) -> bool {
FELT_248 <= value && value <= FELT_255
}

use crate::hints::{Hint, HINTS};
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_integer_from_var_name, insert_value_from_var_name,
Expand All @@ -42,6 +43,7 @@ use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use linkme::distributed_slice;
use std::collections::HashMap;

const HINT_LONG_SHORT_LIST: &str = "from tools.py.hints import is_short_list, is_long_list\nif is_short_list(ids.list_prefix):\n ids.long_short_list = 0\nelif is_long_list(ids.list_prefix):\n ids.long_short_list = 1\nelse:\n raise ValueError(f\"Invalid list prefix: {hex(ids.list_prefix)}. Not a recognized list type.\")";
Expand Down Expand Up @@ -81,6 +83,8 @@ fn hint_long_short_list(
)))),
}
}
#[distributed_slice(HINTS)]
static _HIT_LONG_SHORT_LIST: Hint = (HINT_LONG_SHORT_LIST, hint_long_short_list);

const HINT_FIRST_ITEM_TYPE: &str = "from tools.py.hints import is_single_byte, is_short_string\nif is_single_byte(ids.first_item_prefix):\n ids.first_item_type = 0\nelif is_short_string(ids.first_item_prefix):\n ids.first_item_type = 1\nelse:\n raise ValueError(f\"Unsupported first item prefix: {hex(ids.first_item_prefix)}.\")";

Expand Down Expand Up @@ -120,6 +124,9 @@ fn hint_first_item_type(
}
}

#[distributed_slice(HINTS)]
static _HINT_FIRST_ITEM_TYPE: Hint = (HINT_FIRST_ITEM_TYPE, hint_first_item_type);

const HINT_SECOND_ITEM_TYPE: &str = "from tools.py.hints import is_single_byte, is_short_string, is_long_string\nif is_single_byte(ids.second_item_prefix):\n ids.second_item_type = 0\nelif is_short_string(ids.second_item_prefix):\n ids.second_item_type = 1\nelif is_long_string(ids.second_item_prefix):\n ids.second_item_type = 2\nelse:\n raise ValueError(f\"Unsupported second item prefix: {hex(ids.second_item_prefix)}.\")";

fn hint_second_item_type(
Expand Down Expand Up @@ -165,6 +172,9 @@ fn hint_second_item_type(
}
}

#[distributed_slice(HINTS)]
static _HINT_SECOND_ITEM_TYPE: Hint = (HINT_SECOND_ITEM_TYPE, hint_second_item_type);

const HINT_ITEM_TYPE: &str = "from tools.py.hints import is_single_byte, is_short_string\nif is_single_byte(ids.item_prefix):\n ids.item_type = 0\nelif is_short_string(ids.item_prefix):\n ids.item_type = 1\nelse:\n raise ValueError(f\"Unsupported item prefix: {hex(ids.item_prefix)} for a branch node. Should be single byte or short string only.\")";

fn hint_item_type(
Expand Down Expand Up @@ -203,19 +213,5 @@ fn hint_item_type(
}
}

pub fn run_hint(
vm: &mut VirtualMachine,
exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
match hint_data.code.as_str() {
HINT_LONG_SHORT_LIST => hint_long_short_list(vm, exec_scope, hint_data, constants),
HINT_FIRST_ITEM_TYPE => hint_first_item_type(vm, exec_scope, hint_data, constants),
HINT_SECOND_ITEM_TYPE => hint_second_item_type(vm, exec_scope, hint_data, constants),
HINT_ITEM_TYPE => hint_item_type(vm, exec_scope, hint_data, constants),
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
}
}
#[distributed_slice(HINTS)]
static _HINT_ITEM_TYPE: Hint = (HINT_ITEM_TYPE, hint_item_type);
17 changes: 13 additions & 4 deletions cairo_vm_hints/src/hints/lib/rlp_little/assert.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::hints::{Hint, HINTS};
use crate::utils;
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use linkme::distributed_slice;
use std::collections::HashMap;

pub const HINT_EXPECTED_LEADING_ZEROES: &str = "assert ids.res == expected_leading_zeroes, f\"Expected {expected_leading_zeroes} but got {ids.res}\"";
const HINT_EXPECTED_LEADING_ZEROES: &str = "assert ids.res == expected_leading_zeroes, f\"Expected {expected_leading_zeroes} but got {ids.res}\"";

pub fn hint_expected_leading_zeroes(
fn hint_expected_leading_zeroes(
vm: &mut VirtualMachine,
exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
Expand All @@ -27,9 +29,13 @@ pub fn hint_expected_leading_zeroes(
}
}

pub const HINT_EXPECTED_NIBBLE: &str = "assert ids.extracted_nibble_at_pos == expected_nibble, f\"extracted_nibble_at_pos={ids.extracted_nibble_at_pos} expected_nibble={expected_nibble}\"";
#[distributed_slice(HINTS)]
static _HINT_EXPECTED_LEADING_ZEROS: Hint =
(HINT_EXPECTED_LEADING_ZEROES, hint_expected_leading_zeroes);

pub fn hint_expected_nibble(
const HINT_EXPECTED_NIBBLE: &str = "assert ids.extracted_nibble_at_pos == expected_nibble, f\"extracted_nibble_at_pos={ids.extracted_nibble_at_pos} expected_nibble={expected_nibble}\"";

fn hint_expected_nibble(
vm: &mut VirtualMachine,
exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
Expand All @@ -48,3 +54,6 @@ pub fn hint_expected_nibble(
Ok(())
}
}

#[distributed_slice(HINTS)]
static _HINT_EXPECTED_NIBBLE: Hint = (HINT_EXPECTED_NIBBLE, hint_expected_nibble);
9 changes: 7 additions & 2 deletions cairo_vm_hints/src/hints/lib/rlp_little/divmod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use crate::hints::{Hint, HINTS};
use crate::utils;
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::Relocatable;
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use linkme::distributed_slice;
use starknet_types_core::felt::NonZeroFelt;
use std::collections::HashMap;

pub const HINT_POW_CUT: &str =
const HINT_POW_CUT: &str =
"ids.q, ids.r = divmod(memory[ids.array + ids.start_word + ids.i], ids.pow_cut)";

pub fn hint_pow_cut(
fn hint_pow_cut(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
Expand All @@ -34,3 +36,6 @@ pub fn hint_pow_cut(
utils::write_value("q", q, vm, hint_data)?;
utils::write_value("r", r, vm, hint_data)
}

#[distributed_slice(HINTS)]
static _HINT_POW_CUT: Hint = (HINT_POW_CUT, hint_pow_cut);
Loading

0 comments on commit fa2492f

Please sign in to comment.