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

Use CairoArg enum instead of Any in CairoRunner::run_from_entrypoint #686

Merged
merged 82 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 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
9f54747
Use CairoArg enum instead of Any in CairoRunner::run_from_entrypoint
fmoletta Jan 4, 2023
7765b50
Fix tests
fmoletta Jan 4, 2023
b1559de
Add tests + remove unused func
fmoletta Jan 4, 2023
99367ff
use slice instead of vec
fmoletta Jan 4, 2023
6aba6c5
Update changelog
fmoletta Jan 4, 2023
f441575
Merge branch 'main' of github.com:lambdaclass/cairo-rs into run-from-…
fmoletta Jan 4, 2023
3a9266a
Add non-typed helpers for ids variables
fmoletta Jan 4, 2023
6555822
Swap BigInt for MaybeRelocatable in Dictionary
fmoletta Jan 4, 2023
e76a541
Merge branch 'main' of github.com:lambdaclass/cairo-rs into maybe-rel…
fmoletta Jan 4, 2023
9d62363
Update dict_hint_utils + dict macros in test_utils
fmoletta Jan 4, 2023
c8b514c
Fix tests
fmoletta Jan 4, 2023
29785c2
Clippy
fmoletta Jan 4, 2023
ee751fa
Add test for dict_write with relocatable
fmoletta Jan 4, 2023
510ae02
Add tests on hint_utils.rs
fmoletta Jan 4, 2023
6988b57
Add tetss on hint_processor_utils.rs
fmoletta Jan 4, 2023
3946f98
Add integration test for bug case
fmoletta Jan 4, 2023
68a4265
Update changelog
fmoletta Jan 4, 2023
a8c52bb
Merge branch 'main' of github.com:lambdaclass/cairo-rs into maybe-rel…
fmoletta Jan 6, 2023
0b34376
Fix eof
fmoletta Jan 6, 2023
55d5aa4
Merge branch 'maybe-rel-dict' into run-from-entrypoint-args
fmoletta Jan 6, 2023
9de2432
Remove debug print
fmoletta Jan 6, 2023
8131896
Merge branch 'main' of github.com:lambdaclass/cairo-rs into run-from-…
fmoletta Jan 6, 2023
1521e41
Merge branch 'main' of github.com:lambdaclass/cairo-rs into run-from-…
fmoletta Jan 17, 2023
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

#### Upcoming Changes

* Use CairoArg enum instead of Any in CairoRunner::run_from_entrypoint [#686](https://github.com/lambdaclass/cairo-rs/pull/686)
* Public Api changes:
* Remove `Result` from `MaybeRelocatable::mod_floor`, it now returns a `MaybeRelocatable`
* Add struct `CairoArg`
* Change `arg` argument of `CairoRunner::run_from_entrypoint` from `Vec<&dyn Any>` to `&[&CairoArg]`
* Remove argument `typed_args` from `CairoRunner::run_from_entrypoint`
* Remove no longer used method `gen_typed_arg` from `VirtualMachine` & `MemorySegmentManager`
* Add methods `MemorySegmentManager::gen_cairo_arg` & `MemorySegmentManager::write_simple_args` as typed counterparts to `MemorySegmentManager::gen_arg` & `MemorySegmentManager::write_arg`

#### [0.1.1] - 2023-01-11

* Add input file contents to traceback [#666](https://github.com/lambdaclass/cairo-rs/pull/666/files)
Expand Down
181 changes: 46 additions & 135 deletions src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
},
security::verify_secure_runner,
trace::get_perm_range_check_limits,
vm_memory::{memory::RelocateValue, memory_segments::gen_typed_args},
vm_memory::memory::RelocateValue,
{
runners::builtin_runner::{
BitwiseBuiltinRunner, BuiltinRunner, EcOpBuiltinRunner, HashBuiltinRunner,
Expand All @@ -44,6 +44,24 @@ use std::{

use super::builtin_runner::KeccakBuiltinRunner;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CairoArg {
Single(MaybeRelocatable),
Array(Vec<MaybeRelocatable>),
}

impl From<MaybeRelocatable> for CairoArg {
fn from(other: MaybeRelocatable) -> Self {
CairoArg::Single(other)
}
}

impl From<Vec<MaybeRelocatable>> for CairoArg {
fn from(other: Vec<MaybeRelocatable>) -> Self {
CairoArg::Array(other)
}
}

pub struct CairoRunner {
pub(crate) program: Program,
layout: CairoLayout,
Expand Down Expand Up @@ -937,28 +955,15 @@ impl CairoRunner {
pub fn run_from_entrypoint(
&mut self,
entrypoint: usize,
args: Vec<&dyn Any>,
typed_args: bool,
args: &[&CairoArg],
verify_secure: bool,
_apply_modulo_to_args: bool,
vm: &mut VirtualMachine,
hint_processor: &mut dyn HintProcessor,
) -> Result<(), VirtualMachineError> {
let stack = if typed_args {
if args.len() != 1 {
return Err(VirtualMachineError::InvalidArgCount(1, args.len()));
}

gen_typed_args(args)?
} else {
let mut stack = Vec::new();
for arg in args {
stack.push(vm.segments.gen_arg(arg, &mut vm.memory)?);
}

stack
};

let stack = args
.iter()
.map(|arg| vm.segments.gen_cairo_arg(arg, &mut vm.memory))
.collect::<Result<Vec<MaybeRelocatable>, VirtualMachineError>>()?;
let return_fp = vm.segments.add(&mut vm.memory);
let end = self.initialize_function_entrypoint(vm, entrypoint, stack, return_fp.into())?;

Expand Down Expand Up @@ -3333,116 +3338,6 @@ mod tests {
);
}

/// Test that the call to .run_from_entrypoint() with args.count() != 1 when
/// typed_args is true fails.
#[test]
fn run_from_entrypoint_typed_args_invalid_arg_count() {
let program =
Program::from_file(Path::new("cairo_programs/not_main.json"), Some("main")).unwrap();
let mut cairo_runner = cairo_runner!(program);
let mut vm = vm!();
let mut hint_processor = BuiltinHintProcessor::new_empty();

let entrypoint = program
.identifiers
.get("__main__.not_main")
.unwrap()
.pc
.unwrap();
assert_eq!(
cairo_runner.run_from_entrypoint(
entrypoint,
vec![],
true,
true,
true,
&mut vm,
&mut hint_processor,
),
Err(VirtualMachineError::InvalidArgCount(1, 0)),
);
assert_eq!(
cairo_runner.run_from_entrypoint(
entrypoint,
vec![&mayberelocatable!(0), &mayberelocatable!(1)],
true,
true,
true,
&mut vm,
&mut hint_processor,
),
Err(VirtualMachineError::InvalidArgCount(1, 2)),
);
}

/// Test that the call to .run_from_entrypoint() with args.count() == 1 when
/// typed_args is true succeeds.
#[test]
fn run_from_entrypoint_typed_args() {
let program =
Program::from_file(Path::new("cairo_programs/not_main.json"), Some("main")).unwrap();
let mut cairo_runner = cairo_runner!(program);
let mut vm = vm!();
let mut hint_processor = BuiltinHintProcessor::new_empty();

let entrypoint = program
.identifiers
.get("__main__.not_main")
.unwrap()
.pc
.unwrap();

vm.accessed_addresses = Some(Vec::new());
cairo_runner.initialize_builtins(&mut vm).unwrap();
cairo_runner.initialize_segments(&mut vm, None);
assert_eq!(
cairo_runner.run_from_entrypoint(
entrypoint,
vec![&mayberelocatable!(0)],
true,
true,
true,
&mut vm,
&mut hint_processor,
),
Ok(()),
);
}

/// Test that the call to .run_from_entrypoint() when typed_args is false
/// succeeds.
#[test]
fn run_from_entrypoint_untyped_args() {
let program =
Program::from_file(Path::new("cairo_programs/not_main.json"), Some("main")).unwrap();
let mut cairo_runner = cairo_runner!(program);
let mut vm = vm!();
let mut hint_processor = BuiltinHintProcessor::new_empty();

let entrypoint = program
.identifiers
.get("__main__.not_main")
.unwrap()
.pc
.unwrap();

vm.accessed_addresses = Some(Vec::new());
cairo_runner.initialize_builtins(&mut vm).unwrap();
cairo_runner.initialize_segments(&mut vm, None);
assert_eq!(
cairo_runner.run_from_entrypoint(
entrypoint,
vec![],
false,
true,
true,
&mut vm,
&mut hint_processor,
),
Ok(()),
);
}

#[test]
fn finalize_segments_run_not_ended() {
let program = program!();
Expand Down Expand Up @@ -4229,9 +4124,10 @@ mod tests {
assert_eq!(
cairo_runner.run_from_entrypoint(
main_entrypoint,
vec![&mayberelocatable!(2), &MaybeRelocatable::from((2, 0))], //range_check_ptr
false,
true,
&vec![
&mayberelocatable!(2).into(),
&MaybeRelocatable::from((2, 0)).into()
], //range_check_ptr
true,
&mut vm,
&mut hint_processor,
Expand All @@ -4257,14 +4153,29 @@ mod tests {
assert_eq!(
new_cairo_runner.run_from_entrypoint(
fib_entrypoint,
vec![&mayberelocatable!(2), &MaybeRelocatable::from((2, 0))],
false,
true,
&vec![
&mayberelocatable!(2).into(),
&MaybeRelocatable::from((2, 0)).into()
],
true,
&mut new_vm,
&mut hint_processor,
),
Ok(()),
);
}

#[test]
fn cairo_arg_from_single() {
let expected = CairoArg::Single(MaybeRelocatable::from((0, 0)));
let value = MaybeRelocatable::from((0, 0));
assert_eq!(expected, value.into())
}

#[test]
fn cairo_arg_from_array() {
let expected = CairoArg::Array(vec![MaybeRelocatable::from((0, 0))]);
let value = vec![MaybeRelocatable::from((0, 0))];
assert_eq!(expected, value.into())
}
}
53 changes: 0 additions & 53 deletions src/vm/vm_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ use felt::Felt;
use num_traits::{ToPrimitive, Zero};
use std::{any::Any, borrow::Cow, collections::HashMap, ops::Add};

use super::vm_memory::memory_segments::gen_typed_args;

const MAX_TRACEBACK_ENTRIES: u32 = 20;

#[derive(PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -963,13 +961,6 @@ impl VirtualMachine {
self.memory.add_relocation_rule(src_ptr, dst_ptr)
}

pub fn gen_typed_args(
&self,
args: Vec<&dyn Any>,
) -> Result<Vec<MaybeRelocatable>, VirtualMachineError> {
gen_typed_args(args)
}

pub fn gen_arg(&mut self, arg: &dyn Any) -> Result<MaybeRelocatable, VirtualMachineError> {
self.segments.gen_arg(arg, &mut self.memory)
}
Expand Down Expand Up @@ -3772,50 +3763,6 @@ mod tests {
);
}

/// Test that the call to .gen_arg() with any other argument returns a not
/// implemented error.
#[test]
fn gen_arg_not_implemented() {
let mut vm = vm!();

assert_eq!(vm.gen_arg(&""), Err(VirtualMachineError::NotImplemented),);
}

#[test]
fn gen_typed_args_empty() {
assert_eq!(gen_typed_args(vec![]), Ok(vec![]));
}

/// Test that the call to .gen_typed_args() with an unsupported vector
/// returns a not implemented error.
#[test]
fn gen_typed_args_not_implemented() {
assert_eq!(
gen_typed_args(vec![&0usize]),
Err(VirtualMachineError::NotImplemented),
);
}

/// Test that the call to .gen_typed_args() with a Vec<MaybeRelocatable>
/// with a relocatables returns the original contents.
#[test]
fn gen_typed_args_relocatable_slice() {
assert_eq!(
gen_typed_args(vec![&[
mayberelocatable!(0, 0),
mayberelocatable!(0, 1),
mayberelocatable!(0, 2),
]
.into_iter()
.collect::<Vec<MaybeRelocatable>>(),]),
Ok(vec![
mayberelocatable!(0, 0),
mayberelocatable!(0, 1),
mayberelocatable!(0, 2),
]),
);
}

/// Test that compute_effective_sizes() works as intended.
#[test]
fn compute_effective_sizes() {
Expand Down
Loading