diff --git a/CHANGELOG.md b/CHANGELOG.md index dfdb3468f7..927f5d4132 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* docs: align BuiltinHintProcessor documentation with current implementation [#2274](https://github.com/lambdaclass/cairo-vm/pull/2274) + #### [3.0.0] - 2025-11-19 #### [3.0.0-rc.5] - 2025-11-14 diff --git a/docs/hint_processor/builtin_hint_processor/README.md b/docs/hint_processor/builtin_hint_processor/README.md index a551da3b7e..ed78e47db5 100644 --- a/docs/hint_processor/builtin_hint_processor/README.md +++ b/docs/hint_processor/builtin_hint_processor/README.md @@ -27,12 +27,12 @@ For this step, you will have to code your hint implementation as a Rust function The hint implementation must also follow a specific structure in terms of variable input and output: ```rust fn hint_func( - vm: &mut VM, + vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, - constants: &HashMap, -) -> Result<(), VirtualMachineError> { + constants: &HashMap, +) -> Result<(), HintError> { // Your implementation } @@ -43,12 +43,12 @@ For example, this function implements the hint "print(ids.a)": ```rust fn print_a_hint( - vm: &mut VM, + vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, ap_tracking: &ApTracking, - constants: &HashMap, -) -> Result<(), VirtualMachineError> { + constants: &HashMap, +) -> Result<(), HintError> { let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?; println!("{}", a); Ok(()) @@ -61,22 +61,43 @@ let hint = HintFunc(Box::new(print_a_hint)); Import the BuiltinHintProcessor from cairo-vm, instantiate it using the `new_empty()` method and the add your custom hint implementation using the method `add_hint` ```rust use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor +use std::rc::Rc; let mut hint_processor = BuiltinHintProcessor::new_empty(); -hint_processor.add_hint(String::from("print(ids.a)"), hint); +hint_processor.add_hint(String::from("print(ids.a)"), Rc::new(hint)); +``` +You can also create a dictionary of `Rc` and use the method `new()` to create a `BuiltinHintProcessor` with a preset dictionary of functions instead of using `add_hint()` for each custom hint. Note that `new()` also requires a `RunResources` instance: +```rust +use cairo_vm::vm::runners::cairo_runner::RunResources; +use std::collections::HashMap; +use std::rc::Rc; + +let mut map: HashMap> = HashMap::new(); +map.insert(String::from("print(ids.a)"), Rc::new(hint)); +let hint_processor = BuiltinHintProcessor::new(map, RunResources::default()); ``` -You can also create a dictionary of HintFunc and use the method `new()` to create a BuiltinHintProcessor with a preset dictionary of functions instead of using `add_hint()` for each custom hint. #### Step 4: Run your cairo program using BuiltinHintProcessor extended with your hint Import the function cairo_run from cairo-vm, and run your compiled program ```rust -use cairo_vm::cairo_run::cairo_run; +use cairo_vm::cairo_run::{cairo_run, CairoRunConfig}; +use cairo_vm::types::layout_name::LayoutName; +use std::fs::File; +use std::io::{BufReader, Read}; +use std::path::Path; + +let file = File::open(Path::new("custom_hint.json")).expect("Couldn't load file"); +let mut reader = BufReader::new(file); +let mut buffer = Vec::::new(); +reader.read_to_end(&mut buffer).expect("Couldn't read file"); + cairo_run( - Path::new("custom_hint.json"), - "main", - false, - false, + &buffer, + &CairoRunConfig { + layout: LayoutName::all_cairo, + ..Default::default() + }, &mut hint_processor, ) .expect("Couldn't run program"); @@ -125,11 +146,11 @@ There are also some helpers that dont depend on the hint processor used that can You can also find plenty of example implementations in the [builtin hint processor folder](../../../vm/src/hint_processor/builtin_hint_processor). ### Error Handling -This API uses VirtualMachineError as error return type for hint functions, while its not possible to add error types to VirtualMachineError, you can use VirtualMachineError::CustomHint which receives a string and prints an error message with the format: "Hint Error: [your message]". -For example, if we want our hint to return an error if ids.a is less than 0 we could write: +This API uses `HintError` as error return type for hint functions. To return a custom error, use `HintError::CustomHint`, which receives a string and prints an error message with the format: "Hint Error: [your message]". +For example, if we want our hint to return an error if `ids.a` is less than 0 we could write: ```rust -if (get_integer_from_var_name("a", vm, ids_data, ap_tracking)? < 0){ - return Err(VirtualMachineError::CustomHint(String::from("a < 0"))) +if get_integer_from_var_name("a", vm, ids_data, ap_tracking)? < 0 { + return Err(HintError::CustomHint("a < 0".into())); } ```