Skip to content

Commit

Permalink
feat: add os hints tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GMKrieger committed Nov 8, 2024
1 parent b147c82 commit 8dc4454
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 12 deletions.
122 changes: 122 additions & 0 deletions crates/starknet-os/src/hints/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,125 @@ pub fn set_ap_to_new_block_hash(

Ok(())
}

#[cfg(test)]
mod tests {
use std::collections::HashMap;

use cairo_vm::hint_processor::hint_processor_definition::HintReference;
use cairo_vm::serde::deserialize_program::ApTracking;
use cairo_vm::types::errors::math_errors::MathError;
use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::Relocatable;
use cairo_vm::vm::vm_core::VirtualMachine;
use cairo_vm::Felt252;
use rstest::{fixture, rstest};

use super::*;
use crate::utils::get_variable_from_root_exec_scope;

#[fixture]
fn os_input() -> StarknetOsInput {
StarknetOsInput {
new_block_hash: Felt252::from(3),
prev_block_hash: Felt252::from(1),
full_output: true,
..Default::default()
}
}

#[rstest]
fn test_write_full_output_to_mem(os_input: StarknetOsInput) {
let mut vm = VirtualMachine::new(false);
vm.add_memory_segment();
vm.add_memory_segment();
vm.set_fp(1);

let ap_tracking = ApTracking::new();
let constants = HashMap::new();
let ids_data = HashMap::from([(vars::ids::BIT.to_string(), HintReference::new_simple(-1))]);

let result = Felt252::from(os_input.full_output);

let mut exec_scopes: ExecutionScopes = Default::default();
exec_scopes.insert_value(vars::scopes::OS_INPUT, Rc::new(os_input));

write_full_output_to_mem(&mut vm, &mut exec_scopes, &ids_data, &ap_tracking, &constants)
.expect("Hint should succeed");

// Rust can't infer the type, so we need to assign it manually here
let fp: Result<Relocatable, MathError> = vm.get_fp() + 19;

assert_eq!(vm.get_integer(fp.unwrap()).unwrap().into_owned(), result);
}

#[rstest]
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_configure_kzg_manager() {
let mut vm = VirtualMachine::new(false);
vm.add_memory_segment();
vm.set_fp(1);

let ap_tracking = ApTracking::new();
let constants = HashMap::new();
let ids_data = HashMap::from([(vars::ids::BIT.to_string(), HintReference::new_simple(-1))]);

let mut exec_scopes: ExecutionScopes = Default::default();

configure_kzg_manager(&mut vm, &mut exec_scopes, &ids_data, &ap_tracking, &constants)
.expect("Hint should succeed");

// Rust can't infer the type, so we need to assign it manually here
let result: bool =
get_variable_from_root_exec_scope(&exec_scopes, vars::scopes::SERIALIZE_DATA_AVAILABILITY_CREATE_PAGES)
.unwrap();

assert!(result)
}

#[rstest]
fn test_set_ap_to_prev_block_hash(os_input: StarknetOsInput) {
let mut vm = VirtualMachine::new(false);
vm.add_memory_segment();
vm.add_memory_segment();
vm.set_ap(1);
vm.set_fp(1);

let ap_tracking = ApTracking::new();
let constants = HashMap::new();
let ids_data = HashMap::from([(vars::ids::BIT.to_string(), HintReference::new_simple(-1))]);

let result = os_input.prev_block_hash;

let mut exec_scopes: ExecutionScopes = Default::default();
exec_scopes.insert_value(vars::scopes::OS_INPUT, Rc::new(os_input));

set_ap_to_prev_block_hash(&mut vm, &mut exec_scopes, &ids_data, &ap_tracking, &constants)
.expect("Hint should succeed");

assert_eq!(vm.get_integer(vm.get_ap()).unwrap().into_owned(), result);
}

#[rstest]
fn test_set_ap_to_new_block_hash(os_input: StarknetOsInput) {
let mut vm = VirtualMachine::new(false);
vm.add_memory_segment();
vm.add_memory_segment();
vm.set_ap(1);
vm.set_fp(1);

let ap_tracking = ApTracking::new();
let constants = HashMap::new();
let ids_data = HashMap::from([(vars::ids::BIT.to_string(), HintReference::new_simple(-1))]);

let result = os_input.new_block_hash;

let mut exec_scopes: ExecutionScopes = Default::default();
exec_scopes.insert_value(vars::scopes::OS_INPUT, Rc::new(os_input));

set_ap_to_new_block_hash(&mut vm, &mut exec_scopes, &ids_data, &ap_tracking, &constants)
.expect("Hint should succeed");

assert_eq!(vm.get_integer(vm.get_ap()).unwrap().into_owned(), result);
}
}
12 changes: 1 addition & 11 deletions crates/starknet-os/src/hints/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,18 +385,8 @@ mod tests {
tree_height: 251_usize,
commitment_facts: Default::default(),
},
deprecated_compiled_classes: Default::default(),
compiled_classes: Default::default(),
contracts: Default::default(),
contract_address_to_class_hash: Default::default(),
class_hash_to_compiled_class_hash: Default::default(),
general_config: Default::default(),
transactions: Default::default(),
declared_class_hash_to_component_hashes: Default::default(),
new_block_hash: Default::default(),
prev_block_hash: Default::default(),
compiled_class_visited_pcs: Default::default(),
full_output: false,
..Default::default()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/starknet-os/src/io/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::starknet::business_logic::fact_state::contract_state_objects::Contrac
use crate::starknet::starknet_storage::CommitmentInfo;
use crate::utils::Felt252HexNoPrefix;

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, Default)]
pub struct StarknetOsInput {
pub contract_state_commitment_info: CommitmentInfo,
pub contract_class_commitment_info: CommitmentInfo,
Expand Down

0 comments on commit 8dc4454

Please sign in to comment.