Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 38 additions & 17 deletions docs/hint_processor/builtin_hint_processor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, HintReference>,
ap_tracking: &ApTracking,
constants: &HashMap<String, BigInt>,
) -> Result<(), VirtualMachineError> {
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
// Your implementation
}

Expand All @@ -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<String, HintReference>,
ap_tracking: &ApTracking,
constants: &HashMap<String, BigInt>,
) -> Result<(), VirtualMachineError> {
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?;
println!("{}", a);
Ok(())
Expand All @@ -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<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. 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<String, Rc<HintFunc>> = 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::<u8>::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");
Expand Down Expand Up @@ -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()));
}
```