Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default implementation of compile_hint #680

Merged
merged 4 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
* `Program.instruction_locations` type changed from `Option<HashMap<usize, Location>>` to `Option<HashMap<usize, InstructionLocation>>`
* `VirtualMachineError`s produced by `HintProcessor::execute_hint()` will be wrapped in a `VirtualMachineError::Hint` error containing their hint_index
* `get_location()` now receives an an optional usize value `hint_index`, used to obtain hint locations

* Default implementation of compile_hint [#680](https://github.com/lambdaclass/cairo-rs/pull/680)
* Internal changes:
* Make the `compile_hint` implementation which was in the `BuiltinHintProcessor` the default implementation in the trait.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::any_box;
use crate::hint_processor::builtin_hint_processor::blake2s_utils::{
blake2s_add_uint256, blake2s_add_uint256_bigend, compute_blake2s, finalize_blake2s,
};
Expand Down Expand Up @@ -438,41 +437,6 @@ impl HintProcessor for BuiltinHintProcessor {
code => Err(VirtualMachineError::UnknownHint(code.to_string())),
}
}

fn compile_hint(
&self,
code: &str,
ap_tracking: &ApTracking,
reference_ids: &HashMap<String, usize>,
references: &HashMap<usize, HintReference>,
) -> Result<Box<dyn Any>, VirtualMachineError> {
Ok(any_box!(HintProcessorData {
code: code.to_string(),
ap_tracking: ap_tracking.clone(),
ids_data: get_ids_data(reference_ids, references)?,
}))
}
}

fn get_ids_data(
reference_ids: &HashMap<String, usize>,
references: &HashMap<usize, HintReference>,
) -> Result<HashMap<String, HintReference>, VirtualMachineError> {
let mut ids_data = HashMap::<String, HintReference>::new();
for (path, ref_id) in reference_ids {
let name = path
.rsplit('.')
.next()
.ok_or(VirtualMachineError::FailedToGetIds)?;
ids_data.insert(
name.to_string(),
references
.get(ref_id)
.ok_or(VirtualMachineError::FailedToGetIds)?
.clone(),
);
}
Ok(ids_data)
}

#[cfg(test)]
Expand Down
32 changes: 31 additions & 1 deletion src/hint_processor/hint_processor_definition.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::any_box;
use crate::serde::deserialize_program::ApTracking;
use crate::serde::deserialize_program::OffsetValue;
use crate::types::exec_scope::ExecutionScopes;
Expand All @@ -8,6 +9,8 @@ use num_bigint::BigInt;
use std::any::Any;
use std::collections::HashMap;

use super::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;

pub trait HintProcessor {
//Executes the hint which's data is provided by a dynamic structure previously created by compile_hint
fn execute_hint(
Expand Down Expand Up @@ -36,7 +39,34 @@ pub trait HintProcessor {
reference_ids: &HashMap<String, usize>,
//List of all references (key corresponds to element of the previous dictionary)
references: &HashMap<usize, HintReference>,
) -> Result<Box<dyn Any>, VirtualMachineError>;
) -> Result<Box<dyn Any>, VirtualMachineError> {
Ok(any_box!(HintProcessorData {
code: hint_code.to_string(),
ap_tracking: ap_tracking_data.clone(),
ids_data: get_ids_data(reference_ids, references)?,
}))
}
}

fn get_ids_data(
reference_ids: &HashMap<String, usize>,
references: &HashMap<usize, HintReference>,
) -> Result<HashMap<String, HintReference>, VirtualMachineError> {
let mut ids_data = HashMap::<String, HintReference>::new();
for (path, ref_id) in reference_ids {
let name = path
.rsplit('.')
.next()
.ok_or(VirtualMachineError::FailedToGetIds)?;
ids_data.insert(
name.to_string(),
references
.get(ref_id)
.ok_or(VirtualMachineError::FailedToGetIds)?
.clone(),
);
}
Ok(ids_data)
}

#[derive(Debug, PartialEq, Clone)]
Expand Down