Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Oppen committed Jun 7, 2023
1 parent eefb615 commit e0581e1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 47 deletions.
6 changes: 3 additions & 3 deletions src/hint_processor/hint_processor_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub trait HintProcessor {
//(may contain other variables aside from those used by the hint)
reference_ids: &HashMap<String, usize>,
//List of all references (key corresponds to element of the previous dictionary)
references: &HashMap<usize, HintReference>,
references: &Vec<HintReference>,
) -> Result<Box<dyn Any>, VirtualMachineError> {
Ok(any_box!(HintProcessorData {
code: hint_code.to_string(),
Expand All @@ -52,7 +52,7 @@ pub trait HintProcessor {

fn get_ids_data(
reference_ids: &HashMap<String, usize>,
references: &HashMap<usize, HintReference>,
references: &Vec<HintReference>,
) -> Result<HashMap<String, HintReference>, VirtualMachineError> {
let mut ids_data = HashMap::<String, HintReference>::new();
for (path, ref_id) in reference_ids {
Expand All @@ -63,7 +63,7 @@ fn get_ids_data(
ids_data.insert(
name.to_string(),
references
.get(ref_id)
.get(*ref_id)
.ok_or(VirtualMachineError::Unexpected)?
.clone(),
);
Expand Down
4 changes: 2 additions & 2 deletions src/serde/deserialize_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ fn deserialize_scientific_notation(n: Number) -> Option<Felt252> {
}
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Default)]
pub struct ReferenceManager {
pub references: Vec<Reference>,
}
Expand Down Expand Up @@ -432,11 +432,11 @@ pub fn parse_program_json(
.debug_info
.map(|debug_info| debug_info.instruction_locations),
identifiers: program_json.identifiers,
reference_manager: Program::get_reference_list(&program_json.reference_manager),
};
Ok(Program {
shared_program_data: Arc::new(shared_program_data),
constants,
reference_manager: program_json.reference_manager,
builtins: program_json.builtins,
})
}
Expand Down
37 changes: 30 additions & 7 deletions src/types/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ use crate::stdlib::{collections::HashMap, prelude::*, sync::Arc};
#[cfg(feature = "cairo-1-hints")]
use crate::serde::deserialize_program::{ApTracking, FlowTrackingData};
use crate::{
hint_processor::hint_processor_definition::HintReference,
serde::deserialize_program::{
deserialize_and_parse_program, Attribute, BuiltinName, HintParams, Identifier,
InstructionLocation, ReferenceManager,
InstructionLocation, OffsetValue, ReferenceManager,
},
types::{
errors::program_errors::ProgramError, instruction::Register, relocatable::MaybeRelocatable,
},
types::{errors::program_errors::ProgramError, relocatable::MaybeRelocatable},
};
#[cfg(feature = "cairo-1-hints")]
use cairo_lang_starknet::casm_contract_class::CasmContractClass;
Expand Down Expand Up @@ -48,14 +51,14 @@ pub(crate) struct SharedProgramData {
pub(crate) error_message_attributes: Vec<Attribute>,
pub(crate) instruction_locations: Option<HashMap<usize, InstructionLocation>>,
pub(crate) identifiers: HashMap<String, Identifier>,
pub(crate) reference_manager: Vec<HintReference>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Program {
pub(crate) shared_program_data: Arc<SharedProgramData>,
pub(crate) constants: HashMap<String, Felt252>,
pub(crate) builtins: Vec<BuiltinName>,
pub(crate) reference_manager: ReferenceManager,
}

impl Program {
Expand Down Expand Up @@ -89,11 +92,11 @@ impl Program {
error_message_attributes,
instruction_locations,
identifiers,
reference_manager: Self::get_reference_list(&reference_manager),
};
Ok(Self {
shared_program_data: Arc::new(shared_program_data),
constants,
reference_manager,
builtins,
})
}
Expand Down Expand Up @@ -139,16 +142,36 @@ impl Program {
.iter()
.map(|(cairo_type, identifier)| (cairo_type.as_str(), identifier))
}

pub(crate) fn get_reference_list(reference_manager: &ReferenceManager) -> Vec<HintReference> {
reference_manager
.references
.iter()
.map(|r| {
HintReference {
offset1: r.value_address.offset1.clone(),
offset2: r.value_address.offset2.clone(),
dereference: r.value_address.dereference,
// only store `ap` tracking data if the reference is referred to it
ap_tracking_data: match (&r.value_address.offset1, &r.value_address.offset2) {
(OffsetValue::Reference(Register::AP, _, _), _)
| (_, OffsetValue::Reference(Register::AP, _, _)) => {
Some(r.ap_tracking_data.clone())
}
_ => None,
},
cairo_type: Some(r.value_address.value_type.clone()),
}
})
.collect()
}
}

impl Default for Program {
fn default() -> Self {
Self {
shared_program_data: Arc::new(SharedProgramData::default()),
constants: HashMap::new(),
reference_manager: ReferenceManager {
references: Vec::new(),
},
builtins: Vec::new(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/vm/errors/vm_exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ fn get_value_from_simple_reference(
) -> Option<MaybeRelocatable> {
let reference: HintReference = runner
.program
.shared_program_data
.reference_manager
.references
.get(ref_id)?
.clone()
.into();
Expand Down
39 changes: 5 additions & 34 deletions src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ use crate::{
use crate::{
hint_processor::hint_processor_definition::{HintProcessor, HintReference},
math_utils::safe_div_usize,
serde::deserialize_program::{BuiltinName, OffsetValue},
serde::deserialize_program::BuiltinName,
types::{
errors::{math_errors::MathError, program_errors::ProgramError},
exec_scope::ExecutionScopes,
instance_definitions::{
bitwise_instance_def::BitwiseInstanceDef, ec_op_instance_def::EcOpInstanceDef,
ecdsa_instance_def::EcdsaInstanceDef,
},
instruction::Register,
layout::CairoLayout,
program::Program,
relocatable::{relocate_address, relocate_value, MaybeRelocatable, Relocatable},
Expand Down Expand Up @@ -481,38 +480,10 @@ impl CairoRunner {
self.initial_fp
}

pub fn get_reference_list(&self) -> HashMap<usize, HintReference> {
let mut references = HashMap::<usize, HintReference>::new();

for (i, reference) in self.program.reference_manager.references.iter().enumerate() {
references.insert(
i,
HintReference {
offset1: reference.value_address.offset1.clone(),
offset2: reference.value_address.offset2.clone(),
dereference: reference.value_address.dereference,
// only store `ap` tracking data if the reference is referred to it
ap_tracking_data: match (
&reference.value_address.offset1,
&reference.value_address.offset2,
) {
(OffsetValue::Reference(Register::AP, _, _), _)
| (_, OffsetValue::Reference(Register::AP, _, _)) => {
Some(reference.ap_tracking_data.clone())
}
_ => None,
},
cairo_type: Some(reference.value_address.value_type.clone()),
},
);
}
references
}

/// Gets the data used by the HintProcessor to execute each hint
pub fn get_hint_data_dictionary(
&self,
references: &HashMap<usize, HintReference>,
references: &Vec<HintReference>,
hint_executor: &mut dyn HintProcessor,
) -> Result<HashMap<usize, Vec<Box<dyn Any>>>, VirtualMachineError> {
let mut hint_data_dictionary = HashMap::<usize, Vec<Box<dyn Any>>>::new();
Expand Down Expand Up @@ -558,7 +529,7 @@ impl CairoRunner {
vm: &mut VirtualMachine,
hint_processor: &mut dyn HintProcessor,
) -> Result<(), VirtualMachineError> {
let references = self.get_reference_list();
let references = &self.program.shared_program_data.reference_manager;
let hint_data_dictionary = self.get_hint_data_dictionary(&references, hint_processor)?;

#[cfg(feature = "hooks")]
Expand Down Expand Up @@ -590,8 +561,8 @@ impl CairoRunner {
vm: &mut VirtualMachine,
hint_processor: &mut dyn HintProcessor,
) -> Result<(), VirtualMachineError> {
let references = self.get_reference_list();
let hint_data_dictionary = self.get_hint_data_dictionary(&references, hint_processor)?;
let references = &self.program.shared_program_data.reference_manager;
let hint_data_dictionary = self.get_hint_data_dictionary(references, hint_processor)?;

for remaining_steps in (1..=steps).rev() {
if self.final_pc.as_ref() == Some(&vm.run_context.pc) {
Expand Down

0 comments on commit e0581e1

Please sign in to comment.