-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement bit length and is_valid_mmr_size test hints
- Loading branch information
Showing
9 changed files
with
246 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 15 additions & 7 deletions
22
cairo_vm_hints/src/hints/print_var.rs → cairo_vm_hints/src/hints/get_bit_length.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +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; | ||
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 PRINT_VAR: &str = "print(ids.x)"; | ||
pub const GET_BIT_LENGTH: &str = "ids.bit_length = ids.x.bit_length()"; | ||
|
||
pub fn print_var( | ||
pub fn get_bit_length( | ||
vm: &mut VirtualMachine, | ||
_exec_scope: &mut ExecutionScopes, | ||
hint_data: &HintProcessorData, | ||
_constants: &HashMap<String, Felt252>, | ||
) -> Result<(), HintError> { | ||
let ids_data = &hint_data.ids_data; | ||
let ap_tracking = &hint_data.ap_tracking; | ||
let a = get_integer_from_var_name("x", vm, ids_data, ap_tracking)?; | ||
println!("printing {}", a); | ||
let x = get_integer_from_var_name("x", 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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
pub mod print_var; | ||
pub mod get_bit_length; | ||
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; | ||
pub mod test_is_valid_mmr_size_print_2; |
84 changes: 84 additions & 0 deletions
84
cairo_vm_hints/src/hints/test_is_valid_mmr_size_generate_random.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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, get_ptr_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 rand::{thread_rng, Rng}; | ||
use starknet_types_core::felt::Felt; | ||
use std::collections::HashMap; | ||
|
||
fn is_valid_mmr_size(mut mmr_size: u64) -> bool { | ||
if mmr_size == 0 { | ||
return false; | ||
} | ||
let max_height = Felt::from(mmr_size).bits() as u32; | ||
for height in (0..=max_height).rev() { | ||
let node_count = 2u64.pow(height + 1) - 1; | ||
if node_count <= mmr_size { | ||
mmr_size -= node_count; | ||
} | ||
} | ||
mmr_size == 0 | ||
} | ||
|
||
pub const TEST_IS_VALID_MMR_SIZE_GENERATE_RANDOM: &str = "from tools.py.mmr import is_valid_mmr_size | ||
import random | ||
print(f\"Testing is_valid_mmr_size against python implementation with {ids.num_sizes} random sizes in [0, 20000000)...\") | ||
sizes_to_test = random.sample(range(0, 20000000), ids.num_sizes) | ||
expected_output = [is_valid_mmr_size(size) for size in sizes_to_test] | ||
segments.write_arg(ids.expected_output, expected_output) | ||
segments.write_arg(ids.input_array, sizes_to_test)"; | ||
|
||
pub fn test_is_valid_mmr_size_generate_random( | ||
vm: &mut VirtualMachine, | ||
_exec_scope: &mut ExecutionScopes, | ||
hint_data: &HintProcessorData, | ||
_constants: &HashMap<String, Felt252>, | ||
) -> Result<(), HintError> { | ||
// let ids_data = &hint_data.ids_data; | ||
// let ap_tracking = &hint_data.ap_tracking; | ||
// let a = get_integer_from_var_name("x", vm, ids_data, ap_tracking)?; | ||
// vm.segments.write_arg(vm.seg, arg) | ||
let num_sizes: u64 = | ||
get_integer_from_var_name("num_sizes", vm, &hint_data.ids_data, &hint_data.ap_tracking)? | ||
.try_into() | ||
.unwrap(); | ||
|
||
println!( | ||
"Testing is_valid_mmr_size against python implementation with {} random sizes in [0, 20000000)...", | ||
num_sizes | ||
); | ||
|
||
let mut rng = thread_rng(); | ||
let mut input_array = vec![]; | ||
let mut expected_output = vec![]; | ||
for _ in 0..num_sizes { | ||
let x = rng.gen_range(0..20000000); | ||
input_array.push(MaybeRelocatable::Int(x.into())); | ||
let y = is_valid_mmr_size(x); | ||
expected_output.push(MaybeRelocatable::Int(y.into())); | ||
} | ||
|
||
let expected_output_ptr = get_ptr_from_var_name( | ||
"expected_output", | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let input_array_ptr = get_ptr_from_var_name( | ||
"input_array", | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
vm.segments.load_data(input_array_ptr, &input_array)?; | ||
vm.segments | ||
.load_data(expected_output_ptr, &expected_output)?; | ||
|
||
Ok(()) | ||
} |
77 changes: 77 additions & 0 deletions
77
cairo_vm_hints/src/hints/test_is_valid_mmr_size_generate_sequential.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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, get_ptr_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, HashSet}; | ||
|
||
pub const TEST_IS_VALID_MMR_SIZE_GENERATE_SEQUENTIAL: &str = "print(f\"Testing is_valid_mmr_size by creating the mmr for all sizes in [0, {ids.num_elems})...\") | ||
from tools.py.mmr import MMR | ||
mmr = MMR() | ||
valid_mmr_sizes = set() | ||
for i in range(ids.num_elems): | ||
mmr.add(i) | ||
valid_mmr_sizes.add(len(mmr.pos_hash)) | ||
expected_output = [size in valid_mmr_sizes for size in range(0, len(mmr.pos_hash) + 1)] | ||
for out, inp in zip(expected_output, range(0, len(mmr.pos_hash) + 1)): | ||
print(out, inp) | ||
segments.write_arg(ids.expected_output, expected_output) | ||
segments.write_arg(ids.input_array, list(range(0, len(mmr.pos_hash) + 1)))"; | ||
|
||
pub fn test_is_valid_mmr_size_generate_sequential( | ||
vm: &mut VirtualMachine, | ||
_exec_scope: &mut ExecutionScopes, | ||
hint_data: &HintProcessorData, | ||
_constants: &HashMap<String, Felt252>, | ||
) -> Result<(), HintError> { | ||
// let ids_data = &hint_data.ids_data; | ||
// let ap_tracking = &hint_data.ap_tracking; | ||
// let a = get_integer_from_var_name("x", vm, ids_data, ap_tracking)?; | ||
// vm.segments.write_arg(vm.seg, arg) | ||
let num_elems: u64 = | ||
get_integer_from_var_name("num_elems", vm, &hint_data.ids_data, &hint_data.ap_tracking)? | ||
.try_into() | ||
.unwrap(); | ||
|
||
println!( | ||
"Testing is_valid_mmr_size by creating the mmr for all sizes in [0, {})...", | ||
num_elems | ||
); | ||
|
||
let mut valid_mmr_sizes = HashSet::new(); | ||
let mut mmr_size = 0; | ||
for leaf_count in 1..=num_elems { | ||
mmr_size = 2 * leaf_count - (leaf_count.count_ones() as u64); | ||
valid_mmr_sizes.insert(mmr_size); | ||
} | ||
let expected_output = (0..=mmr_size) | ||
.map(|i| MaybeRelocatable::Int(valid_mmr_sizes.contains(&i).into())) | ||
.collect::<Vec<_>>(); | ||
let input_array = (0..=mmr_size) | ||
.map(|x| MaybeRelocatable::Int(x.into())) | ||
.collect::<Vec<_>>(); | ||
|
||
let expected_output_ptr = get_ptr_from_var_name( | ||
"expected_output", | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let input_array_ptr = get_ptr_from_var_name( | ||
"input_array", | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
vm.segments.load_data(input_array_ptr, &input_array)?; | ||
vm.segments | ||
.load_data(expected_output_ptr, &expected_output)?; | ||
|
||
Ok(()) | ||
} |
17 changes: 17 additions & 0 deletions
17
cairo_vm_hints/src/hints/test_is_valid_mmr_size_print_1.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData; | ||
use cairo_vm::types::exec_scope::ExecutionScopes; | ||
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}; | ||
use cairo_vm::Felt252; | ||
use std::collections::HashMap; | ||
|
||
pub const TEST_IS_VALID_MMR_SIZE_PRINT_1: &str = "print('\\n')"; | ||
|
||
pub fn test_is_valid_mmr_size_print_1( | ||
_vm: &mut VirtualMachine, | ||
_exec_scope: &mut ExecutionScopes, | ||
_hint_data: &HintProcessorData, | ||
_constants: &HashMap<String, Felt252>, | ||
) -> Result<(), HintError> { | ||
println!("\n"); | ||
Ok(()) | ||
} |
17 changes: 17 additions & 0 deletions
17
cairo_vm_hints/src/hints/test_is_valid_mmr_size_print_2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData; | ||
use cairo_vm::types::exec_scope::ExecutionScopes; | ||
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}; | ||
use cairo_vm::Felt252; | ||
use std::collections::HashMap; | ||
|
||
pub const TEST_IS_VALID_MMR_SIZE_PRINT_2: &str = "print(f\"\\tPass!\\n\\n\")"; | ||
|
||
pub fn test_is_valid_mmr_size_print_2( | ||
_vm: &mut VirtualMachine, | ||
_exec_scope: &mut ExecutionScopes, | ||
_hint_data: &HintProcessorData, | ||
_constants: &HashMap<String, Felt252>, | ||
) -> Result<(), HintError> { | ||
println!("\tPass!\n\n"); | ||
Ok(()) | ||
} |