Skip to content

Commit

Permalink
Mmr hints
Browse files Browse the repository at this point in the history
  • Loading branch information
fmkra committed Oct 23, 2024
1 parent 86acea5 commit 4291846
Show file tree
Hide file tree
Showing 11 changed files with 524 additions and 12 deletions.
54 changes: 51 additions & 3 deletions cairo_vm_hints/Cargo.lock

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

7 changes: 6 additions & 1 deletion cairo_vm_hints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ starknet-types-core = "0.1.7"
thiserror = "1.0.64"
clap = { version = "4.3.10", features = ["derive"] }
bincode = { version = "2.0.0-rc.3", default-features = false, features = ["serde"] }
rand = "0.8"
rand = "0.8"
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"
11 changes: 8 additions & 3 deletions cairo_vm_hints/TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ TODO

lib/block_header.cairo:36
lib/block_header.cairo:87
lib/mmr.cairo:144
lib/mmr.cairo:237
lib/mmr.cairo:282
lib/mpt:172
lib/mpt:214
Expand Down Expand Up @@ -39,7 +37,6 @@ lib/utils:cairo:565
lib/utils:cairo:601
lib/utils:cairo:640
tests/cairo_programs/bit_length.cairo:7
tests/cairo_programs/construct_mmr_test.cairo:59
tests/cairo_programs/dw_hack_test.cairo:27
tests/cairo_programs/dw_hack_test.cairo:38
tests/cairo_programs/dw_hack_test.cairo:58
Expand Down Expand Up @@ -74,10 +71,18 @@ tests/fuzzing/mpt.cairo:50
DONE

lib/mmr.cairo:102
lib/mmr.cairo:144
lib/mmr.cairo:237
lib/utils:cairo:305
lib/utils:cairo:333
tests/cairo_programs/construct_mmr_test.cairo:59
tests/cairo_programs/is_valid_mmr_size.cairo:10
tests/cairo_programs/is_valid_mmr_size.cairo:29
tests/cairo_programs/is_valid_mmr_size.cairo:48
tests/cairo_programs/is_valid_mmr_size.cairo:57
tests/cairo_programs/is_valid_mmr_size.cairo:72

DONE TEST

tests/cairo_programs/is_valid_mmr_size.cairo
tests/cairo_programs/bit_length.cairo
10 changes: 8 additions & 2 deletions cairo_vm_hints/src/hint_processor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::hints::{
get_bit_length::{get_bit_length, GET_BIT_LENGTH},
bit_length_mmr::{bit_length_mmr, BIT_LENGTH_MMR},
bit_length_x::{bit_length_x, BIT_LENGTH_X},
mmr_left_child::{mmr_left_child, MMR_LEFT_CHILD},
test_construct_mmr::{test_construct_mmr, TEST_CONSTRUCT_MMR},
test_is_valid_mmr_size_generate_random::{
test_is_valid_mmr_size_generate_random, TEST_IS_VALID_MMR_SIZE_GENERATE_RANDOM,
},
Expand Down Expand Up @@ -60,7 +63,10 @@ fn run_hint(
TEST_IS_VALID_MMR_SIZE_GENERATE_RANDOM => {
test_is_valid_mmr_size_generate_random(vm, exec_scope, hint_data, constants)
}
GET_BIT_LENGTH => get_bit_length(vm, exec_scope, hint_data, constants),
BIT_LENGTH_X => bit_length_x(vm, exec_scope, hint_data, constants),
BIT_LENGTH_MMR => bit_length_mmr(vm, exec_scope, hint_data, constants),
TEST_CONSTRUCT_MMR => test_construct_mmr(vm, exec_scope, hint_data, constants),
MMR_LEFT_CHILD => mmr_left_child(vm, exec_scope, hint_data, constants),
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
Expand Down
29 changes: 29 additions & 0 deletions cairo_vm_hints/src/hints/bit_length_mmr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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,
};
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 std::collections::HashMap;

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

pub fn bit_length_mmr(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let x = get_integer_from_var_name("mmr_len", vm, &hint_data.ids_data, &hint_data.ap_tracking)?;
insert_value_from_var_name(
"bit_length",
MaybeRelocatable::Int(x.bits().into()),
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?;

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use std::collections::HashMap;

pub const GET_BIT_LENGTH: &str = "ids.bit_length = ids.x.bit_length()";
pub const BIT_LENGTH_X: &str = "ids.bit_length = ids.x.bit_length()";

pub fn get_bit_length(
pub fn bit_length_x(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
Expand Down
43 changes: 43 additions & 0 deletions cairo_vm_hints/src/hints/mmr_left_child.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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,
};
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 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";

pub fn mmr_left_child(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let left_child = get_integer_from_var_name(
"left_child",
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?;
let mmr_len =
get_integer_from_var_name("mmr_len", vm, &hint_data.ids_data, &hint_data.ap_tracking)?;

let in_mmr = if left_child <= mmr_len {
Felt::ONE
} else {
Felt::ZERO
};
insert_value_from_var_name(
"in_mmr",
MaybeRelocatable::Int(in_mmr),
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?;

Ok(())
}
5 changes: 4 additions & 1 deletion cairo_vm_hints/src/hints/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
pub mod get_bit_length;
pub mod bit_length_mmr;
pub mod bit_length_x;
pub mod mmr_left_child;
pub mod test_construct_mmr;
pub mod test_is_valid_mmr_size_generate_random;
pub mod test_is_valid_mmr_size_generate_sequential;
pub mod test_is_valid_mmr_size_print_1;
Expand Down
Loading

0 comments on commit 4291846

Please sign in to comment.