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 5, 2024
1 parent 20b57b9 commit 828f348
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 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,135 @@ 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 {
contract_state_commitment_info: Default::default(),
contract_class_commitment_info: 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: Felt252::from(3),
prev_block_hash: Felt252::from(1),
compiled_class_visited_pcs: Default::default(),
full_output: true,
}
}

#[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);
}
}

0 comments on commit 828f348

Please sign in to comment.