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

Add new error type HintError #676

Merged
merged 59 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
c47e701
Start get_traceback_entries + add convenience methos
fmoletta Dec 22, 2022
51219ca
Add fn is_call_instruction
fmoletta Dec 22, 2022
3b276db
add code
fmoletta Dec 22, 2022
05f0bdd
Merge branch 'main' of github.com:lambdaclass/cairo-rs into traceback
fmoletta Dec 22, 2022
c89eb3f
Refactor code
fmoletta Dec 22, 2022
f02db3e
Clippy
fmoletta Dec 22, 2022
05813bd
Add get_traceback method
fmoletta Dec 22, 2022
58992c3
Fix get_error_attr_value
fmoletta Dec 22, 2022
b381624
Add traceback to VmException
fmoletta Dec 22, 2022
936eadc
Make traceback non-optional
fmoletta Dec 22, 2022
e72dff5
Add tests for is_call_instruction
fmoletta Dec 22, 2022
93eae26
Add traceback to error display
fmoletta Dec 22, 2022
67ccc39
Add test + fix logic for get_traceback_entries
fmoletta Dec 22, 2022
ba410b0
Code refactor
fmoletta Dec 22, 2022
acf3299
Add one more test for get_traceback_entries
fmoletta Dec 22, 2022
bc66d0f
Fix string format + add test for get_traceback
fmoletta Dec 22, 2022
b3952cb
Improve fn
fmoletta Dec 27, 2022
c2fa8a0
Add reference to is_call_instruction signature
fmoletta Dec 27, 2022
88e4b52
Add reference to immediate in decode_instruction + remove clone
fmoletta Dec 27, 2022
7a03cec
Merge branch 'main' of github.com:lambdaclass/cairo-rs into traceback
fmoletta Dec 27, 2022
371dc43
Fix hint_processor mutability in tests
fmoletta Dec 27, 2022
46a7202
Add Location::get_location_marks
fmoletta Dec 27, 2022
fd0b9c6
Fix method to_string_with_contents
fmoletta Dec 27, 2022
5e753bb
Fix string format
fmoletta Dec 27, 2022
09ee596
Fix string format
fmoletta Dec 28, 2022
9eac613
Update traceback tests
fmoletta Dec 28, 2022
3612b01
Add tests for Location::to_string_with_contents()
fmoletta Dec 28, 2022
f56122d
Fix intermediate string format
fmoletta Dec 28, 2022
00ccd31
Fix test
fmoletta Dec 28, 2022
2ed2287
Add tests for Location::get_location_marks()
fmoletta Dec 28, 2022
839cbf1
Update VmException display
fmoletta Dec 28, 2022
8937c87
Fix string format
fmoletta Dec 28, 2022
747bb77
Fix string format
fmoletta Dec 28, 2022
1dbe083
Merge branch 'main' of github.com:lambdaclass/cairo-rs into input_fil…
fmoletta Dec 28, 2022
5f78c47
Remove debug print
fmoletta Dec 28, 2022
542e06e
Fix Display
fmoletta Dec 28, 2022
d33f6e1
Implement Display for MaybeRelocatable
fmoletta Dec 28, 2022
2fdef4a
Add real-case test for VmException Display
fmoletta Dec 28, 2022
9845fc5
Remove debug format from erros containing MaybeRelocatable and Reloca…
fmoletta Dec 28, 2022
8b1e11c
Add tests for display implementation
fmoletta Dec 28, 2022
f46e49a
Update Changelog
fmoletta Dec 28, 2022
e9ecfe4
Clippy
fmoletta Dec 28, 2022
40d06cb
Remove unnecessary &
fmoletta Dec 29, 2022
c7b5cfe
Add hint location to InstructionLocation
fmoletta Dec 29, 2022
160fe32
Use InstructionLocation instead of Location in insruction_locations f…
fmoletta Dec 29, 2022
bd6111a
Add hint location logic to get_location
fmoletta Dec 29, 2022
a9045fa
Add rought version of VirtualMachineError::Hint
fmoletta Dec 29, 2022
085d6eb
Add test for error display on HintError
fmoletta Dec 29, 2022
2909f1b
Add test for get_location with hint_index
fmoletta Dec 29, 2022
5542e3f
Start refactor
fmoletta Dec 29, 2022
4a9e6dd
Update changelog
fmoletta Dec 29, 2022
7f6e049
Merge branch 'hint-location' into hint-error
fmoletta Dec 29, 2022
68f7384
Finnish changing hint fns to HintError
fmoletta Dec 29, 2022
94aa1f1
Update custom hint example
fmoletta Dec 29, 2022
c683b6d
Fix changelog format
fmoletta Dec 29, 2022
31fdb1e
Add changelog entry for this PR
fmoletta Dec 29, 2022
3695a11
Merge branch 'hint-location' into hint-error
fmoletta Dec 29, 2022
6b142e7
Merge branch 'main' of github.com:lambdaclass/cairo-rs into hint-error
fmoletta Jan 2, 2023
d17c74d
Merge branch 'main' of github.com:lambdaclass/cairo-rs into hint-error
fmoletta Jan 3, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@

* 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.
* Make the `compile_hint` implementation which was in the `BuiltinHintProcessor` the default implementation in the trait.

* Add new error type `HintError` [#676](https://github.com/lambdaclass/cairo-rs/pull/676)
* Public Api changes:
* `HintProcessor::execute_hint()` now returns a `HintError` instead of a `VirtualMachineError`
* helper functions on `hint_processor_utils.rs` now return a `HintError`
72 changes: 42 additions & 30 deletions custom_hint_example/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions custom_hint_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use cairo_rs::hint_processor::builtin_hint_processor::hint_utils::get_integer_fr
use cairo_rs::hint_processor::hint_processor_definition::HintReference;
use cairo_rs::types::exec_scope::ExecutionScopes;
use cairo_rs::serde::deserialize_program::ApTracking;
use cairo_rs::vm::{errors::vm_errors::VirtualMachineError, vm_core::VirtualMachine};
use cairo_rs::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use num_bigint::BigInt;
use std::collections::HashMap;
use std::path::Path;
use std::rc::Rc;

// Create the function that implements the custom hint
fn print_a_hint(
Expand All @@ -18,7 +19,7 @@ fn print_a_hint(
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, BigInt>,
) -> Result<(), VirtualMachineError> {
) -> Result<(), HintError> {
let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?;
println!("{}", a);
Ok(())
Expand All @@ -32,14 +33,16 @@ fn main() {
let mut hint_processor = BuiltinHintProcessor::new_empty();

//Add the custom hint, together with the Python code
hint_processor.add_hint(String::from("print(ids.a)"), hint);
hint_processor.add_hint(String::from("print(ids.a)"), Rc::new(hint));

//Run the cairo program
cairo_run(
Path::new("custom_hint.json"),
"main",
false,
false,
"all",
false,
&mut hint_processor,
)
.expect("Couldn't run program");
Expand Down
45 changes: 23 additions & 22 deletions src/hint_processor/builtin_hint_processor/blake2s_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::hint_processor::builtin_hint_processor::hint_utils::{
};
use crate::hint_processor::hint_processor_definition::HintReference;
use crate::hint_processor::hint_processor_utils::bigint_to_u32;
use crate::vm::errors::hint_errors::HintError;
use crate::vm::vm_core::VirtualMachine;
use num_traits::ToPrimitive;
use std::borrow::Cow;
Expand All @@ -18,14 +19,14 @@ use num_bigint::BigInt;

fn get_fixed_size_u32_array<const T: usize>(
h_range: &Vec<Cow<BigInt>>,
) -> Result<[u32; T], VirtualMachineError> {
) -> Result<[u32; T], HintError> {
let mut u32_vec = Vec::<u32>::with_capacity(h_range.len());
for num in h_range {
u32_vec.push(num.to_u32().ok_or(VirtualMachineError::BigintToU32Fail)?);
u32_vec.push(num.to_u32().ok_or(HintError::BigintToU32Fail)?);
}
u32_vec
.try_into()
.map_err(|_| VirtualMachineError::FixedSizeArrayFail(T))
.map_err(|_| HintError::FixedSizeArrayFail(T))
}

fn get_maybe_relocatable_array_from_u32(array: &Vec<u32>) -> Vec<MaybeRelocatable> {
Expand All @@ -44,10 +45,7 @@ Computes the blake2s compress function and fills the value in the right position
output_ptr should point to the middle of an instance, right after initial_state, message, t, f,
which should all have a value at this point, and right before the output portion which will be
written by this function.*/
fn compute_blake2s_func(
vm: &mut VirtualMachine,
output_rel: Relocatable,
) -> Result<(), VirtualMachineError> {
fn compute_blake2s_func(vm: &mut VirtualMachine, output_rel: Relocatable) -> Result<(), HintError> {
let h = get_fixed_size_u32_array::<8>(&vm.get_integer_range(&(output_rel.sub(26)?), 8)?)?;
let message =
get_fixed_size_u32_array::<16>(&vm.get_integer_range(&(output_rel.sub(18)?), 16)?)?;
Expand All @@ -69,7 +67,7 @@ pub fn compute_blake2s(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), VirtualMachineError> {
) -> Result<(), HintError> {
let output = get_ptr_from_var_name("output", vm, ids_data, ap_tracking)?;
compute_blake2s_func(vm, output)
}
Expand Down Expand Up @@ -100,7 +98,7 @@ pub fn finalize_blake2s(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), VirtualMachineError> {
) -> Result<(), HintError> {
const N_PACKED_INSTANCES: usize = 7;
let blake2s_ptr_end = get_ptr_from_var_name("blake2s_ptr_end", vm, ids_data, ap_tracking)?;
let message: [u32; 16] = [0; 16];
Expand Down Expand Up @@ -132,7 +130,7 @@ pub fn blake2s_add_uint256(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), VirtualMachineError> {
) -> Result<(), HintError> {
//Get variables from ids
let data_ptr = get_ptr_from_var_name("data", vm, ids_data, ap_tracking)?;
let low_addr = get_relocatable_from_var_name("low", vm, ids_data, ap_tracking)?;
Expand Down Expand Up @@ -179,7 +177,7 @@ pub fn blake2s_add_uint256_bigend(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), VirtualMachineError> {
) -> Result<(), HintError> {
//Get variables from ids
let data_ptr = get_ptr_from_var_name("data", vm, ids_data, ap_tracking)?;
let low_addr = get_relocatable_from_var_name("low", vm, ids_data, ap_tracking)?;
Expand Down Expand Up @@ -225,6 +223,7 @@ mod tests {
use crate::relocatable;
use crate::types::exec_scope::ExecutionScopes;
use crate::utils::test_utils::*;
use crate::vm::errors::hint_errors::HintError;
use crate::vm::vm_core::VirtualMachine;
use crate::vm::vm_memory::memory::Memory;
use crate::{bigint, vm::errors::memory_errors::MemoryError};
Expand All @@ -247,7 +246,9 @@ mod tests {
//Execute the hint
assert_eq!(
run_hint!(vm, ids_data, hint_code),
Err(VirtualMachineError::CantSubOffset(5, 26))
Err(HintError::Internal(VirtualMachineError::CantSubOffset(
5, 26
)))
);
}

Expand All @@ -266,9 +267,9 @@ mod tests {
//Execute the hint
assert_eq!(
run_hint!(vm, ids_data, hint_code),
Err(VirtualMachineError::ExpectedInteger(
Err(HintError::Internal(VirtualMachineError::ExpectedInteger(
MaybeRelocatable::from((2, 0))
))
)))
);
}

Expand All @@ -286,8 +287,8 @@ mod tests {
//Execute the hint
assert_eq!(
run_hint!(vm, ids_data, hint_code),
Err(VirtualMachineError::ExpectedRelocatable(
MaybeRelocatable::from((1, 0))
Err(HintError::Internal(
VirtualMachineError::ExpectedRelocatable(MaybeRelocatable::from((1, 0)))
))
);
}
Expand Down Expand Up @@ -316,7 +317,7 @@ mod tests {
//Execute the hint
assert_eq!(
run_hint!(vm, ids_data, hint_code),
Err(VirtualMachineError::BigintToU32Fail)
Err(HintError::BigintToU32Fail)
);
}

Expand All @@ -334,9 +335,9 @@ mod tests {
//Execute the hint
assert_eq!(
run_hint!(vm, ids_data, hint_code),
Err(VirtualMachineError::ExpectedInteger(
Err(HintError::Internal(VirtualMachineError::ExpectedInteger(
MaybeRelocatable::from((2, 0))
))
)))
);
}

Expand Down Expand Up @@ -398,13 +399,13 @@ mod tests {
//Execute the hint
assert_eq!(
run_hint!(vm, ids_data, hint_code),
Err(VirtualMachineError::MemoryError(
Err(HintError::Internal(VirtualMachineError::MemoryError(
MemoryError::InconsistentMemory(
MaybeRelocatable::from((2, 0)),
MaybeRelocatable::from((2, 0)),
MaybeRelocatable::from(bigint!(1795745351))
)
))
)))
);
}

Expand All @@ -418,7 +419,7 @@ mod tests {
//Execute the hint
assert_eq!(
run_hint!(vm, HashMap::new(), hint_code),
Err(VirtualMachineError::FailedToGetIds)
Err(HintError::FailedToGetIds)
);
}

Expand Down