Skip to content

Commit

Permalink
Select builtins hint #1: enter scope (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
odesenfans committed Jan 3, 2024
1 parent 7281eb7 commit e1891bf
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod fact_topologies;
pub(crate) mod inner_select_builtins;
mod program_hash;
mod program_loader;
pub(crate) mod select_builtins;
pub(crate) mod simple_bootloader_hints;
pub(crate) mod types;
pub(crate) mod vars;
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use num_traits::ToPrimitive;
use std::any::Any;
use std::collections::HashMap;

use crate::hint_processor::builtin_hint_processor::bootloader::vars;
use crate::hint_processor::builtin_hint_processor::hint_utils::get_integer_from_var_name;
use crate::hint_processor::hint_processor_definition::HintReference;
use crate::serde::deserialize_program::ApTracking;
use crate::types::errors::math_errors::MathError;
use crate::types::exec_scope::ExecutionScopes;
use crate::vm::errors::hint_errors::HintError;
use crate::vm::vm_core::VirtualMachine;

/// Implements
/// %{ vm_enter_scope({'n_selected_builtins': ids.n_selected_builtins}) %}
pub fn select_builtins_enter_scope(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
let n_selected_builtins =
get_integer_from_var_name(vars::N_SELECTED_BUILTINS, vm, ids_data, ap_tracking)?
.into_owned();
let n_selected_builtins =
n_selected_builtins
.to_usize()
.ok_or(MathError::Felt252ToUsizeConversion(Box::new(
n_selected_builtins,
)))?;

exec_scopes.enter_scope(HashMap::from([(
vars::N_SELECTED_BUILTINS.to_string(),
Box::new(n_selected_builtins) as Box<dyn Any>,
)]));

Ok(())
}

#[cfg(test)]
mod tests {
use crate::hint_processor::hint_processor_definition::HintReference;
use crate::types::exec_scope::ExecutionScopes;
use crate::utils::test_utils::*;
use crate::vm::vm_core::VirtualMachine;

use super::*;

#[test]
fn test_select_builtins_enter_scope() {
let mut vm = vm!();
// Set n_selected_builtins to 7
vm.run_context.fp = 1;
vm.segments = segments![((1, 0), 7)];
let ids_data = ids_data![vars::N_SELECTED_BUILTINS];
let n_selected_builtins = 7usize;

let ap_tracking = ApTracking::default();
let mut exec_scopes = ExecutionScopes::new();

select_builtins_enter_scope(&mut vm, &mut exec_scopes, &ids_data, &ap_tracking)
.expect("Hint failed unexpectedly");

// Check that we entered a new scope
assert_eq!(exec_scopes.data.len(), 2);
assert_eq!(exec_scopes.data[1].len(), 1);

let n_selected_builtins_var: usize = exec_scopes.get(vars::N_SELECTED_BUILTINS).unwrap();

assert_eq!(n_selected_builtins_var, n_selected_builtins);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::hint_processor::builtin_hint_processor::bootloader::execute_task_hint
write_return_builtins_hint,
};
use crate::hint_processor::builtin_hint_processor::bootloader::inner_select_builtins::select_builtin;
use crate::hint_processor::builtin_hint_processor::bootloader::select_builtins::select_builtins_enter_scope;
use crate::hint_processor::builtin_hint_processor::bootloader::simple_bootloader_hints::{
divide_num_by_2, prepare_task_range_checks, set_ap_to_zero, set_current_task,
set_tasks_variable,
Expand Down Expand Up @@ -921,6 +922,12 @@ impl HintProcessorLogic for BuiltinHintProcessor {
hint_code::INNER_SELECT_BUILTINS_SELECT_BUILTIN => {
select_builtin(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::SELECT_BUILTINS_ENTER_SCOPE => select_builtins_enter_scope(
vm,
exec_scopes,
&hint_data.ids_data,
&hint_data.ap_tracking,
),
#[cfg(feature = "skip_next_instruction_hint")]
hint_code::SKIP_NEXT_INSTRUCTION => skip_next_instruction(vm),
#[cfg(feature = "print")]
Expand Down
3 changes: 3 additions & 0 deletions vm/src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1627,3 +1627,6 @@ ids.select_builtin = int(
n_selected_builtins > 0 and memory[ids.selected_encodings] == memory[ids.all_encodings])
if ids.select_builtin:
n_selected_builtins = n_selected_builtins - 1";

pub const SELECT_BUILTINS_ENTER_SCOPE: &str =
"vm_enter_scope({'n_selected_builtins': ids.n_selected_builtins})";

0 comments on commit e1891bf

Please sign in to comment.