diff --git a/Cargo.lock b/Cargo.lock index f01f4fb3..fbb33a5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,11 +130,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" [[package]] -name = "cairo-rs" +name = "cairo-felt" version = "0.1.0" -source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=8e3541768cf8a01b4b8e50e427cef19cae56c9e2#8e3541768cf8a01b4b8e50e427cef19cae56c9e2" +source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=7f8c2fb5ddebfdfe557c79f0072483f550e9e0e3#7f8c2fb5ddebfdfe557c79f0072483f550e9e0e3" +dependencies = [ + "lazy_static", + "num-bigint", + "num-integer", + "num-traits", + "serde", +] + +[[package]] +name = "cairo-rs-py" +version = "0.1.0" +dependencies = [ + "cairo-felt", + "cairo-vm", + "lazy_static", + "num-bigint", + "pyo3", + "rusty-hook", +] + +[[package]] +name = "cairo-vm" +version = "0.1.1" +source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=7f8c2fb5ddebfdfe557c79f0072483f550e9e0e3#7f8c2fb5ddebfdfe557c79f0072483f550e9e0e3" dependencies = [ "bincode", + "cairo-felt", "clap", "generic-array", "hex", @@ -156,17 +181,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "cairo-rs-py" -version = "0.1.0" -dependencies = [ - "cairo-rs", - "lazy_static", - "num-bigint", - "pyo3", - "rusty-hook", -] - [[package]] name = "cc" version = "1.0.77" @@ -576,7 +590,7 @@ dependencies = [ [[package]] name = "parse-hyperlinks" version = "0.23.4" -source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=8e3541768cf8a01b4b8e50e427cef19cae56c9e2#8e3541768cf8a01b4b8e50e427cef19cae56c9e2" +source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=7f8c2fb5ddebfdfe557c79f0072483f550e9e0e3#7f8c2fb5ddebfdfe557c79f0072483f550e9e0e3" dependencies = [ "nom", ] diff --git a/Cargo.toml b/Cargo.toml index 3232be07..a560cf61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,8 @@ edition = "2021" [dependencies] pyo3 = { version = "0.16.5", features = ["num-bigint"] } -cairo-rs = { git = "https://github.com/lambdaclass/cairo-rs.git", rev = "8e3541768cf8a01b4b8e50e427cef19cae56c9e2" } +cairo-vm = { git = "https://github.com/lambdaclass/cairo-rs.git", rev = "7f8c2fb5ddebfdfe557c79f0072483f550e9e0e3" } +cairo-felt = { git = "https://github.com/lambdaclass/cairo-rs.git", rev = "7f8c2fb5ddebfdfe557c79f0072483f550e9e0e3" } num-bigint = "0.4" lazy_static = "1.4.0" diff --git a/src/cairo_runner.rs b/src/cairo_runner.rs index b28b5194..5984dcfe 100644 --- a/src/cairo_runner.rs +++ b/src/cairo_runner.rs @@ -6,8 +6,7 @@ use crate::{ utils::to_py_error, vm_core::PyVM, }; -use cairo_rs::{ - bigint, +use cairo_vm::{ cairo_run::write_output, hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, serde::deserialize_program::Member, @@ -26,7 +25,6 @@ use cairo_rs::{ security::verify_secure_runner, }, }; -use num_bigint::BigInt; use pyo3::{ exceptions::{PyNotImplementedError, PyTypeError, PyValueError}, prelude::*, @@ -79,7 +77,7 @@ impl PyCairoRunner { Ok(PyCairoRunner { inner: cairo_runner, - pyvm: PyVM::new(program.prime, true, program.error_message_attributes), + pyvm: PyVM::new(true), hint_processor: BuiltinHintProcessor::new_empty(), hint_locals: HashMap::new(), struct_types: Rc::new(struct_types), @@ -148,7 +146,7 @@ impl PyCairoRunner { .ok_or(CairoRunError::Trace(TraceError::TraceNotEnabled)) .map_err(to_py_error)?; - match cairo_rs::cairo_run::write_binary_trace(relocated_trace, &trace_path) { + match cairo_vm::cairo_run::write_binary_trace(relocated_trace, &trace_path) { Ok(()) => (), Err(_e) => { return Err(CairoRunError::Runner(RunnerError::WriteFail)).map_err(to_py_error) @@ -158,7 +156,7 @@ impl PyCairoRunner { if let Some(memory_path) = memory_file { let memory_path = PathBuf::from(memory_path); - cairo_rs::cairo_run::write_binary_memory(&self.inner.relocated_memory, &memory_path) + cairo_vm::cairo_run::write_binary_memory(&self.inner.relocated_memory, &memory_path) .map_err(|_| to_py_error(CairoRunError::Runner(RunnerError::WriteFail)))?; } @@ -352,6 +350,7 @@ impl PyCairoRunner { .to_object(py)) } + #[allow(unused)] #[allow(clippy::too_many_arguments)] pub fn run_from_entrypoint( &mut self, @@ -384,7 +383,6 @@ impl PyCairoRunner { } self.static_locals = static_locals; - let apply_modulo_to_args = apply_modulo_to_args.unwrap_or(true); let entrypoint = if let Ok(x) = entrypoint.extract::() { x @@ -402,14 +400,7 @@ impl PyCairoRunner { let mut stack = Vec::new(); for arg in args.extract::>(py)? { let arg: MaybeRelocatable = arg.extract::()?.into(); - if apply_modulo_to_args { - let arg = arg - .mod_floor(self.pyvm.vm.borrow().get_prime()) - .map_err(to_py_error)?; - stack.push(arg) - } else { - stack.push(arg) - } + stack.push(arg) } stack } else { @@ -428,15 +419,10 @@ impl PyCairoRunner { let processed_args: Vec<&dyn Any> = processed_args.iter().map(|x| x.as_any()).collect(); let mut stack = Vec::new(); for arg in processed_args { - let prime = match apply_modulo_to_args { - true => Some(self.pyvm.vm.borrow().get_prime().clone()), - false => None, - }; - stack.push( (*self.pyvm.vm) .borrow_mut() - .gen_arg(arg, prime.as_ref()) + .gen_arg(arg) .map_err(to_py_error)?, ); } @@ -444,7 +430,7 @@ impl PyCairoRunner { stack }; - let return_fp = MaybeRelocatable::from(bigint!(0)); + let return_fp = MaybeRelocatable::from(0); let end = self .inner @@ -519,15 +505,7 @@ impl PyCairoRunner { )?; segment_ptr } - _ => { - let mut value: MaybeRelocatable = arg.extract::(py)?.into(); - if apply_modulo_to_args { - value = value - .mod_floor(self.pyvm.vm.borrow().get_prime()) - .map_err(to_py_error)?; - } - value - } + _ => arg.extract::(py)?.into(), }) .to_object(py), ) @@ -555,7 +533,7 @@ impl PyCairoRunner { (*self.pyvm.vm) .borrow_mut() - .load_data(&ptr, data) + .load_data(&ptr, &data) .map(|x| PyMaybeRelocatable::from(x).to_object(py)) .map_err(to_py_error) } @@ -657,7 +635,7 @@ impl PyCairoRunner { let pc = self.pyvm.vm.borrow().get_pc().offset; let instruction_location = get_location(pc, &self.inner, self.pyvm.failed_hint_index) .map(InstructionLocation::from); - let error_attribute = get_error_attr_value(pc, &self.inner); + let error_attribute = get_error_attr_value(pc, &self.inner, &self.pyvm.vm.borrow()); let traceback = get_traceback(&self.pyvm.vm.borrow(), &self.inner); VmException::new_err(( PyRelocatable::from((0, pc)), @@ -708,9 +686,10 @@ impl PyExecutionResources { #[cfg(test)] mod test { use super::*; + use crate::biguint; use crate::relocatable::PyMaybeRelocatable::RelocatableValue; - use cairo_rs::bigint; - use num_bigint::BigInt; + use cairo_felt::{Felt, NewFelt}; + use num_bigint::BigUint; use std::env::temp_dir; use std::fs; @@ -744,9 +723,7 @@ mod test { impl MyIterator { #[getter] pub fn __annotations__(&self) -> PyResult { - Ok(Annotations { - 0: self.types.clone(), - }) + Ok(Annotations(self.types.clone())) } pub fn __iter__(slf: PyRef) -> PyRef { slf @@ -784,7 +761,7 @@ mod test { #[pymethods] impl TypeFelt { fn __repr__(&self) -> String { - format!("TypeFelt") + "TypeFelt".to_string() } } @@ -796,7 +773,7 @@ mod test { #[pymethods] impl TypePointer { fn __repr__(&self) -> String { - format!("TypePointer") + "TypePointer".to_string() } } @@ -808,7 +785,7 @@ mod test { #[pymethods] impl TypeStruct { fn __repr__(&self) -> String { - format!("TypeStruct") + "TypeStruct".to_string() } } } @@ -1324,8 +1301,8 @@ mod test { let args = MyIterator { iter: Box::new( vec![ - PyMaybeRelocatable::from(bigint!(0)).to_object(py), - PyMaybeRelocatable::from(bigint!(2)).to_object(py), + PyMaybeRelocatable::from(biguint!(0_u32)).to_object(py), + PyMaybeRelocatable::from(biguint!(2_u32)).to_object(py), ] .into_iter(), ), @@ -1442,7 +1419,7 @@ mod test { runner .initialize_function_runner() .expect("Failed to initialize function runner"); - let pc_before_run = runner.pyvm.vm.borrow().get_pc().clone(); + let pc_before_run = *runner.pyvm.vm.borrow().get_pc(); Python::with_gil(|py| { let result = runner.run_from_entrypoint( @@ -1460,7 +1437,7 @@ mod test { assert!(format!("{:?}", result).contains("Execution reached the end of the program.")); }); - let pc_after_run = runner.pyvm.vm.borrow().get_pc().clone(); + let pc_after_run = *runner.pyvm.vm.borrow().get_pc(); // As the run_resurces provide 0 steps, no steps should have been run // To check this, we check that the pc hasnt changed after "running" the vm @@ -1612,7 +1589,7 @@ mod test { let args = MyIterator { iter: Box::new( - vec![PyMaybeRelocatable::from(bigint!(value)).to_object(*py)].into_iter(), + vec![PyMaybeRelocatable::from(biguint!(value)).to_object(*py)].into_iter(), ), types: vec![PyType::TypeFelt], }; @@ -1653,12 +1630,12 @@ mod test { Python::with_gil(|py| { let array = vec![ - PyMaybeRelocatable::from(bigint!(1)).to_object(py), - PyMaybeRelocatable::from(bigint!(2)).to_object(py), - PyMaybeRelocatable::from(bigint!(4)).to_object(py), - PyMaybeRelocatable::from(bigint!(8)).to_object(py), + PyMaybeRelocatable::from(biguint!(1_u32)).to_object(py), + PyMaybeRelocatable::from(biguint!(2_u32)).to_object(py), + PyMaybeRelocatable::from(biguint!(4_u32)).to_object(py), + PyMaybeRelocatable::from(biguint!(8_u32)).to_object(py), ]; - let size = PyMaybeRelocatable::from(bigint!(array.len())); + let size = PyMaybeRelocatable::from(biguint!(array.len())); let args = vec![array.into_py(py), size.to_object(py)]; let result = runner.run_from_entrypoint( py, @@ -1682,7 +1659,7 @@ mod test { .first() .expect("there's no return value") .into(); - assert_eq!(return_value, MaybeRelocatable::Int(bigint!(15))); + assert_eq!(return_value, MaybeRelocatable::from(15)); }); } @@ -1694,23 +1671,19 @@ mod test { (*runner.pyvm.get_vm()).borrow_mut().add_memory_segment(); runner - .insert(&(0, 0).into(), PyMaybeRelocatable::Int(bigint!(3))) + .insert(&(0, 0).into(), PyMaybeRelocatable::Int(biguint!(3_u32))) .unwrap(); runner - .insert(&(0, 1).into(), PyMaybeRelocatable::Int(bigint!(4))) + .insert(&(0, 1).into(), PyMaybeRelocatable::Int(biguint!(4_u32))) .unwrap(); runner - .insert(&(0, 2).into(), PyMaybeRelocatable::Int(bigint!(5))) + .insert(&(0, 2).into(), PyMaybeRelocatable::Int(biguint!(5_u32))) .unwrap(); assert_eq!( (*runner.pyvm.get_vm()) .borrow() .get_continuous_range(&(0, 0).into(), 3), - Ok(vec![ - bigint!(3).into(), - bigint!(4).into(), - bigint!(5).into(), - ]), + Ok(vec![3.into(), 4.into(), 5.into(),]), ) } @@ -1723,19 +1696,19 @@ mod test { (*runner.pyvm.get_vm()).borrow_mut().add_memory_segment(); runner - .insert(&(0, 0).into(), PyMaybeRelocatable::Int(bigint!(3))) + .insert(&(0, 0).into(), PyMaybeRelocatable::Int(biguint!(3_u32))) .unwrap(); runner - .insert(&(0, 1).into(), PyMaybeRelocatable::Int(bigint!(4))) + .insert(&(0, 1).into(), PyMaybeRelocatable::Int(biguint!(4_u32))) .unwrap(); runner - .insert(&(0, 0).into(), PyMaybeRelocatable::Int(bigint!(5))) + .insert(&(0, 0).into(), PyMaybeRelocatable::Int(biguint!(5_u32))) .expect_err("insertion succeeded when it should've failed"); assert_eq!( (*runner.pyvm.get_vm()) .borrow() .get_continuous_range(&(0, 0).into(), 2), - Ok(vec![bigint!(3).into(), bigint!(4).into(),]), + Ok(vec![3.into(), 4.into(),]), ); } @@ -1896,7 +1869,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(1), + &Felt::new(1), ); assert_eq!( vm_ref @@ -1905,7 +1878,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(2), + &Felt::new(2), ); let relocatable = vm_ref @@ -1913,8 +1886,7 @@ mod test { .unwrap() .unwrap() .get_relocatable() - .unwrap() - .clone(); + .unwrap(); assert_eq!( vm_ref @@ -1923,7 +1895,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(3), + &Felt::new(3), ); assert_eq!( vm_ref @@ -1932,7 +1904,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(4), + &Felt::new(4), ); assert!(vm_ref.get_maybe(&(&relocatable + 2)).unwrap().is_none()); @@ -1941,8 +1913,7 @@ mod test { .unwrap() .unwrap() .get_relocatable() - .unwrap() - .clone(); + .unwrap(); assert_eq!( vm_ref @@ -1951,7 +1922,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(5), + &Felt::new(5), ); assert_eq!( vm_ref @@ -1960,7 +1931,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(6), + &Felt::new(6), ); assert!(vm_ref.get_maybe(&(&relocatable + 2)).unwrap().is_none()); @@ -2074,7 +2045,7 @@ mod test { .get(py, &ap) .unwrap() .map(|x| MaybeRelocatable::from(x.extract::(py).unwrap())), - Some(MaybeRelocatable::Int(bigint!(144))), + Some(MaybeRelocatable::from(144)), ); }); } @@ -2097,13 +2068,7 @@ mod test { let ptr = vm.add_memory_segment(); vm.load_data( &(&ptr).into(), - vec![ - bigint!(1).into(), - bigint!(2).into(), - bigint!(3).into(), - bigint!(4).into(), - bigint!(5).into(), - ], + &vec![1.into(), 2.into(), 3.into(), 4.into(), 5.into()], ) .unwrap(); @@ -2119,13 +2084,7 @@ mod test { .into_iter() .map(MaybeRelocatable::from) .collect::>(), - vec![ - bigint!(1).into(), - bigint!(2).into(), - bigint!(3).into(), - bigint!(4).into(), - bigint!(5).into(), - ], + vec![1.into(), 2.into(), 3.into(), 4.into(), 5.into(),], ); }); } @@ -2161,11 +2120,8 @@ mod test { let mut vm = (*runner.pyvm.vm).borrow_mut(); // Check that the segment exists by writing to it. - vm.insert_value( - &Relocatable::from((0, 0)), - MaybeRelocatable::Int(bigint!(42)), - ) - .expect("memory insert failed"); + vm.insert_value(&Relocatable::from((0, 0)), MaybeRelocatable::from(42)) + .expect("memory insert failed"); }); } @@ -2179,8 +2135,8 @@ mod test { let arg = MyIterator { iter: Box::new( vec![ - PyMaybeRelocatable::from(bigint!(0)).to_object(py), - PyMaybeRelocatable::from(bigint!(2)).to_object(py), + PyMaybeRelocatable::from(biguint!(0_u32)).to_object(py), + PyMaybeRelocatable::from(biguint!(2_u32)).to_object(py), ] .into_iter(), ), @@ -2193,8 +2149,8 @@ mod test { assert_eq!( stack, vec![ - PyMaybeRelocatable::from(bigint!(0)), - PyMaybeRelocatable::from(bigint!(2)), + PyMaybeRelocatable::from(biguint!(0_u32)), + PyMaybeRelocatable::from(biguint!(2_u32)), ] ); }) @@ -2255,14 +2211,14 @@ mod test { runner .get(py, &addr) .expect("Could not get value") - .map(|x| x.extract::(py)) + .map(|x| x.extract::(py)) .transpose() - .expect("Could not convert value to a BigInt") + .expect("Could not convert value to a biguint") }; - assert_eq!(get_value(&segment, 0), Some(bigint!(1))); - assert_eq!(get_value(&segment, 1), Some(bigint!(2))); - assert_eq!(get_value(&segment, 2), Some(bigint!(3))); - assert_eq!(get_value(&segment, 3), Some(bigint!(4))); + assert_eq!(get_value(&segment, 0), Some(biguint!(1_u32))); + assert_eq!(get_value(&segment, 1), Some(biguint!(2_u32))); + assert_eq!(get_value(&segment, 2), Some(biguint!(3_u32))); + assert_eq!(get_value(&segment, 3), Some(biguint!(4_u32))); assert_eq!(get_value(&segment, 4), None); }); } @@ -2342,8 +2298,8 @@ mod test { let arg = MyIterator { iter: Box::new( vec![ - PyMaybeRelocatable::from(bigint!(0)).to_object(py), - PyMaybeRelocatable::from(bigint!(2)).to_object(py), + PyMaybeRelocatable::from(biguint!(0_u32)).to_object(py), + PyMaybeRelocatable::from(biguint!(2_u32)).to_object(py), ] .into_iter(), ), @@ -2364,7 +2320,7 @@ mod test { Python::with_gil(|py| { let segment = runner.add_segment(); - let set_value = |addr: &PyRelocatable, offset, value: BigInt| { + let set_value = |addr: &PyRelocatable, offset, value: BigUint| { let addr = addr.__add__(offset); memory .__setitem__(&addr, PyMaybeRelocatable::Int(value)) @@ -2375,20 +2331,20 @@ mod test { memory .__getitem__(&addr, py) .expect("Could not get value from memory.") - .map(|x| x.extract::(py)) + .map(|x| x.extract::(py)) .transpose() - .expect("Could not convert value to a BigInt") + .expect("Could not convert value to a biguint") }; - set_value(&segment, 0, bigint!(1)); - set_value(&segment, 1, bigint!(2)); - set_value(&segment, 2, bigint!(3)); - set_value(&segment, 3, bigint!(4)); + set_value(&segment, 0, biguint!(1_u32)); + set_value(&segment, 1, biguint!(2_u32)); + set_value(&segment, 2, biguint!(3_u32)); + set_value(&segment, 3, biguint!(4_u32)); - assert_eq!(get_value(&segment, 0), Some(bigint!(1))); - assert_eq!(get_value(&segment, 1), Some(bigint!(2))); - assert_eq!(get_value(&segment, 2), Some(bigint!(3))); - assert_eq!(get_value(&segment, 3), Some(bigint!(4))); + assert_eq!(get_value(&segment, 0), Some(biguint!(1_u32))); + assert_eq!(get_value(&segment, 1), Some(biguint!(2_u32))); + assert_eq!(get_value(&segment, 2), Some(biguint!(3_u32))); + assert_eq!(get_value(&segment, 3), Some(biguint!(4_u32))); assert_eq!(get_value(&segment, 4), None); }); } @@ -2412,7 +2368,7 @@ mod test { Python::with_gil(|py| { let segment = runner.add_segment(); - let set_value = |addr: &PyRelocatable, offset, value: BigInt| { + let set_value = |addr: &PyRelocatable, offset, value: BigUint| { let addr = addr.__add__(offset); memory .__setitem__(&addr, PyMaybeRelocatable::Int(value)) @@ -2423,20 +2379,20 @@ mod test { memory .__getitem__(&addr, py) .expect("Could not get value from memory.") - .map(|x| x.extract::(py)) + .map(|x| x.extract::(py)) .transpose() - .expect("Could not convert value to a BigInt") + .expect("Could not convert value to a biguint") }; - set_value(&segment, 0, bigint!(1)); - set_value(&segment, 1, bigint!(2)); - set_value(&segment, 2, bigint!(3)); - set_value(&segment, 3, bigint!(4)); + set_value(&segment, 0, biguint!(1_u32)); + set_value(&segment, 1, biguint!(2_u32)); + set_value(&segment, 2, biguint!(3_u32)); + set_value(&segment, 3, biguint!(4_u32)); - assert_eq!(get_value(&segment, 0), Some(bigint!(1))); - assert_eq!(get_value(&segment, 1), Some(bigint!(2))); - assert_eq!(get_value(&segment, 2), Some(bigint!(3))); - assert_eq!(get_value(&segment, 3), Some(bigint!(4))); + assert_eq!(get_value(&segment, 0), Some(biguint!(1_u32))); + assert_eq!(get_value(&segment, 1), Some(biguint!(2_u32))); + assert_eq!(get_value(&segment, 2), Some(biguint!(3_u32))); + assert_eq!(get_value(&segment, 3), Some(biguint!(4_u32))); assert_eq!(get_value(&segment, 4), None); }); } @@ -2602,8 +2558,8 @@ mod test { PyMaybeRelocatable::Int(value) => Ok(value), PyMaybeRelocatable::RelocatableValue(r) => Err(r), }; - let expected = bigint!(144); - assert_eq!(result, Ok(&expected) as Result<&BigInt, &PyRelocatable>); + let expected = biguint!(144_u32); + assert_eq!(result, Ok(&expected) as Result<&BigUint, &PyRelocatable>); }); } diff --git a/src/ecdsa.rs b/src/ecdsa.rs index b7a77b8a..fd6ec28b 100644 --- a/src/ecdsa.rs +++ b/src/ecdsa.rs @@ -1,11 +1,12 @@ +use num_bigint::BigUint; use std::collections::HashMap; -use cairo_rs::{ +use cairo_vm::{ types::relocatable::Relocatable, vm::{errors::vm_errors::VirtualMachineError, runners::builtin_runner::SignatureBuiltinRunner}, }; -use num_bigint::BigInt; +use cairo_felt::Felt; use pyo3::prelude::*; use crate::relocatable::PyRelocatable; @@ -13,7 +14,7 @@ use crate::relocatable::PyRelocatable; #[pyclass(name = "Signature")] #[derive(Clone, Debug, PartialEq, Eq)] pub struct PySignature { - signatures: HashMap, + signatures: HashMap, } #[pymethods] @@ -25,8 +26,9 @@ impl PySignature { } } - pub fn add_signature(&mut self, address: PyRelocatable, pair: (BigInt, BigInt)) { - self.signatures.insert(address, pair); + pub fn add_signature(&mut self, address: PyRelocatable, pair: (BigUint, BigUint)) { + self.signatures + .insert(address, (pair.0.into(), pair.1.into())); } } @@ -59,10 +61,8 @@ impl ToPyObject for PySignature { #[cfg(test)] mod test { use super::*; - use crate::relocatable::PyRelocatable; - use num_bigint::{BigInt, Sign}; - use crate::cairo_runner::PyCairoRunner; + use crate::relocatable::PyRelocatable; use std::fs; @@ -79,8 +79,8 @@ mod test { }; let numbers = ( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), + BigUint::new(vec![1, 0, 0, 0, 0, 0, 17, 134217728]), + BigUint::new(vec![1, 0, 0, 0, 0, 0, 17, 134217728]), ); let mut signature = PySignature::new(); @@ -96,8 +96,8 @@ mod test { }; let numbers = ( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 13421772]), - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 13421772]), + BigUint::new(vec![1, 0, 0, 0, 0, 0, 17, 13421772]), + BigUint::new(vec![1, 0, 0, 0, 0, 0, 17, 13421772]), ); let mut signature = PySignature::new(); @@ -133,8 +133,8 @@ mod test { }; let numbers = ( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), + BigUint::new(vec![1, 0, 0, 0, 0, 0, 17, 134217728]), + BigUint::new(vec![1, 0, 0, 0, 0, 0, 17, 134217728]), ); let mut signature = PySignature::new(); diff --git a/src/ids.rs b/src/ids.rs index 90b711c1..506b04a8 100644 --- a/src/ids.rs +++ b/src/ids.rs @@ -1,5 +1,6 @@ use crate::utils::const_path_to_const_name; -use num_bigint::BigInt; +use cairo_felt::{Felt, FeltOps}; +use num_bigint::BigUint; use pyo3::exceptions::PyValueError; use std::{ cell::RefCell, @@ -7,16 +8,16 @@ use std::{ rc::Rc, }; -use cairo_rs::serde::deserialize_program::OffsetValue; -use cairo_rs::{ +use cairo_vm::{ hint_processor::{ hint_processor_definition::HintReference, - hint_processor_utils::compute_addr_from_reference as cairo_rs_compute_addr_from_reference, + hint_processor_utils::compute_addr_from_reference as cairo_vm_compute_addr_from_reference, }, serde::deserialize_program::{ApTracking, Member}, types::relocatable::Relocatable, - vm::{errors::vm_errors::VirtualMachineError, vm_core::VirtualMachine}, + vm::vm_core::VirtualMachine, }; +use cairo_vm::{serde::deserialize_program::OffsetValue, vm::errors::hint_errors::HintError}; use pyo3::{ exceptions::PyAttributeError, pyclass, pymethods, IntoPy, PyObject, PyResult, Python, ToPyObject, @@ -33,7 +34,7 @@ pub struct PyIds { vm: Rc>, references: HashMap, ap_tracking: ApTracking, - constants: HashMap, + constants: HashMap, struct_types: Rc>>, } @@ -148,7 +149,7 @@ impl PyIds { vm: &PyVM, references: &HashMap, ap_tracking: &ApTracking, - constants: &HashMap, + constants: &HashMap, struct_types: Rc>>, ) -> PyIds { PyIds { @@ -244,7 +245,7 @@ pub fn get_value_from_reference( ) -> PyResult { // //First handle case on only immediate if let OffsetValue::Immediate(num) = &hint_reference.offset1 { - return Ok(PyMaybeRelocatable::from(num)); + return Ok(PyMaybeRelocatable::from(num.to_biguint())); } //Then calculate address let var_addr = compute_addr_from_reference(hint_reference, vm, ap_tracking)?; @@ -256,7 +257,7 @@ pub fn get_value_from_reference( }; value - .ok_or_else(|| PyValueError::new_err(VirtualMachineError::FailedToGetIds.to_string())) + .ok_or_else(|| PyValueError::new_err(HintError::FailedToGetIds.to_string())) .map(PyMaybeRelocatable::from) } @@ -268,17 +269,14 @@ pub fn compute_addr_from_reference( //ApTracking of the Hint itself hint_ap_tracking: &ApTracking, ) -> PyResult { - cairo_rs_compute_addr_from_reference(hint_reference, vm, hint_ap_tracking) + cairo_vm_compute_addr_from_reference(hint_reference, vm, hint_ap_tracking) .map_err(|err| PyValueError::new_err(err.to_string())) } #[cfg(test)] mod tests { use crate::{memory::PyMemory, relocatable::PyRelocatable}; - use cairo_rs::{ - bigint, - types::{instruction::Register, relocatable::MaybeRelocatable}, - }; - use num_bigint::{BigInt, Sign}; + use cairo_felt::NewFelt; + use cairo_vm::types::{instruction::Register, relocatable::MaybeRelocatable}; use pyo3::{types::PyDict, PyCell}; use super::*; @@ -309,11 +307,7 @@ mod tests { #[test] fn ids_get_test() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -323,15 +317,12 @@ mod tests { //Create constants let mut constants = HashMap::new(); - constants.insert(String::from("CONST"), bigint!(3)); + constants.insert(String::from("CONST"), Felt::new(3)); //Insert ids.a into memory vm.vm .borrow_mut() - .insert_value( - &Relocatable::from((1, 1)), - &MaybeRelocatable::from(bigint!(2)), - ) + .insert_value(&Relocatable::from((1, 1)), &MaybeRelocatable::from(2)) .unwrap(); let memory = PyMemory::new(&vm); @@ -366,11 +357,11 @@ memory[fp+2] = ids.CONST //Check ids.a is now at memory[fp] assert_eq!( vm.vm.borrow().get_maybe(&Relocatable::from((1, 0))), - Ok(Some(MaybeRelocatable::from(bigint!(2)))) + Ok(Some(MaybeRelocatable::from(2))) ); assert_eq!( vm.vm.borrow().get_maybe(&Relocatable::from((1, 2))), - Ok(Some(MaybeRelocatable::from(bigint!(3)))) + Ok(Some(MaybeRelocatable::from(3))) ); }); } @@ -378,11 +369,7 @@ memory[fp+2] = ids.CONST #[test] fn ids_get_simple_struct() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -405,10 +392,7 @@ memory[fp+2] = ids.CONST //Insert ids.a.x into memory vm.vm .borrow_mut() - .insert_value( - &Relocatable::from((1, 0)), - &MaybeRelocatable::from(bigint!(55)), - ) + .insert_value(&Relocatable::from((1, 0)), &MaybeRelocatable::from(55)) .unwrap(); //Insert ids.a.ptr into memory @@ -450,7 +434,7 @@ memory[fp + 2] = ids.SimpleStruct.SIZE //Check ids.a.x is now at memory[fp] assert_eq!( vm.vm.borrow().get_maybe(&Relocatable::from((1, 0))), - Ok(Some(MaybeRelocatable::from(bigint!(55)))) + Ok(Some(MaybeRelocatable::from(Felt::new(55)))) ); //Check ids.a.ptr is now at memory[fp + 1] assert_eq!( @@ -460,7 +444,7 @@ memory[fp + 2] = ids.SimpleStruct.SIZE //Check ids.SimpleStruct.SIZE is now at memory[fp + 2] assert_eq!( vm.vm.borrow().get_maybe(&Relocatable::from((1, 2))), - Ok(Some(MaybeRelocatable::from(bigint!(2)))) + Ok(Some(MaybeRelocatable::from(Felt::new(2)))) ); //ids.a.y does not exist @@ -475,11 +459,7 @@ memory[fp + 2] = ids.SimpleStruct.SIZE #[test] fn ids_get_nested_struct() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -526,10 +506,7 @@ memory[fp + 2] = ids.SimpleStruct.SIZE //Insert ids.ns.x into memory vm.vm .borrow_mut() - .insert_value( - &Relocatable::from((1, 0)), - &MaybeRelocatable::from(bigint!(55)), - ) + .insert_value(&Relocatable::from((1, 0)), &MaybeRelocatable::from(55)) .unwrap(); //Insert ids.ns.ptr into memory @@ -571,7 +548,7 @@ memory[fp + 1] = ids.ns.struct.address_ //Check ids.Struct.SIZE is now at memory[fp] assert_eq!( vm.vm.borrow().get_maybe(&Relocatable::from((1, 3))), - Ok(Some(MaybeRelocatable::from(bigint!(0)))) + Ok(Some(MaybeRelocatable::from(0))) ); //Check that address of ids.ns.struct is now at memory[fp + 1] assert_eq!( @@ -584,11 +561,7 @@ memory[fp + 1] = ids.ns.struct.address_ #[test] fn ids_get_from_pointer() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..3 { vm.vm.borrow_mut().add_memory_segment(); } @@ -647,11 +620,7 @@ assert ids.ssp_x_ptr == 5 #[test] fn ids_failed_get_test() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -691,11 +660,7 @@ assert ids.ssp_x_ptr == 5 #[test] fn ids_set_test() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -705,14 +670,11 @@ assert ids.ssp_x_ptr == 5 //Create constants let mut constants = HashMap::new(); - constants.insert(String::from("CONST"), bigint!(3)); + constants.insert(String::from("CONST"), Felt::new(3)); vm.vm .borrow_mut() - .insert_value( - &Relocatable::from((1, 0)), - &MaybeRelocatable::from(bigint!(2)), - ) + .insert_value(&Relocatable::from((1, 0)), &MaybeRelocatable::from(2)) .unwrap(); let memory = PyMemory::new(&vm); @@ -745,7 +707,7 @@ assert ids.ssp_x_ptr == 5 //Check ids.a now contains memory[fp] assert_eq!( vm.vm.borrow().get_maybe(&Relocatable::from((1, 1))), - Ok(Some(MaybeRelocatable::from(bigint!(2)))) + Ok(Some(MaybeRelocatable::from(2))) ); //ids.b does not exist @@ -763,11 +725,7 @@ assert ids.ssp_x_ptr == 5 #[test] fn ids_set_struct_attribute() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -822,7 +780,7 @@ ids.struct.ptr = ids.fp //Check ids.struct.x now contains 5 assert_eq!( vm.vm.borrow().get_maybe(&Relocatable::from((1, 0))), - Ok(Some(MaybeRelocatable::from(bigint!(5)))) + Ok(Some(MaybeRelocatable::from(5))) ); //Check ids.struct.x now contains fp's address assert_eq!( @@ -842,11 +800,7 @@ ids.struct.ptr = ids.fp #[test] fn ids_ap_tracked_ref() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -921,7 +875,7 @@ memory[fp] = ids.ok_ref //Check ids.a is now at memory[fp] assert_eq!( vm.vm.borrow().get_maybe(&Relocatable::from((1, 0))), - Ok(Some(MaybeRelocatable::from(bigint!(5)))) + Ok(Some(MaybeRelocatable::from(5))) ); let code = r"ids.bad_ref"; @@ -931,15 +885,14 @@ memory[fp] = ids.ok_ref assert!(py_result .unwrap_err() .to_string() - .contains(&VirtualMachineError::InvalidTrackingGroup(1, 0).to_string())); + .contains(&HintError::InvalidTrackingGroup(1, 0).to_string())); let code = r"ids.none_ref"; let py_result = py.run(code, Some(globals), None); assert!(py_result.unwrap_err().to_string().contains( - &PyValueError::new_err(VirtualMachineError::NoneApTrackingData.to_string()) - .to_string() + &PyValueError::new_err(HintError::NoneApTrackingData.to_string()).to_string() )); }); } @@ -947,11 +900,7 @@ memory[fp] = ids.ok_ref #[test] fn ids_no_register_ref() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -962,8 +911,8 @@ memory[fp] = ids.ok_ref references.insert( String::from("imm_ref"), HintReference { - offset1: OffsetValue::Immediate(bigint!(imm)), - offset2: OffsetValue::Immediate(bigint!(0)), + offset1: OffsetValue::Immediate(Felt::from(imm)), + offset2: OffsetValue::Immediate(Felt::from(0)), dereference: true, ap_tracking_data: None, cairo_type: None, @@ -1010,7 +959,7 @@ memory[fp] = ids.ok_ref //Check ids.a is now at memory[fp] assert_eq!( vm.vm.borrow().get_maybe(&Relocatable::from((1, 0))), - Ok(Some(MaybeRelocatable::from(bigint!(imm)))) + Ok(Some(MaybeRelocatable::from(imm))) ); let code = r"ids.no_reg_ref"; @@ -1020,18 +969,14 @@ memory[fp] = ids.ok_ref assert!(py_result .unwrap_err() .to_string() - .contains(&VirtualMachineError::NoRegisterInReference.to_string())); + .contains(&HintError::NoRegisterInReference.to_string())); }); } #[test] fn ids_reference_with_immediate() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } diff --git a/src/instruction_location.rs b/src/instruction_location.rs index 700a43b3..4caa7ecb 100644 --- a/src/instruction_location.rs +++ b/src/instruction_location.rs @@ -1,4 +1,4 @@ -use cairo_rs::serde::deserialize_program::{InputFile, Location}; +use cairo_vm::serde::deserialize_program::{InputFile, Location}; use pyo3::prelude::*; #[pyclass] diff --git a/src/memory.rs b/src/memory.rs index 3006cef1..2fc5ae77 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -3,16 +3,17 @@ use crate::{ utils::to_py_error, vm_core::PyVM, }; -use cairo_rs::{ +use cairo_felt::FeltOps; +use cairo_vm::{ types::relocatable::{MaybeRelocatable, Relocatable}, vm::vm_core::VirtualMachine, }; -use num_bigint::BigInt; +use num_bigint::BigUint; use pyo3::{ exceptions::{PyTypeError, PyValueError}, prelude::*, }; -use std::{borrow::Cow, cell::RefCell, rc::Rc}; +use std::{cell::RefCell, rc::Rc}; const MEMORY_GET_ERROR_MSG: &str = "Failed to get value from Cairo memory"; const MEMORY_SET_ERROR_MSG: &str = "Failed to set value to Cairo memory"; @@ -86,38 +87,34 @@ impl PyMemory { } /// Return a continuous section of memory as a vector of integers. - pub fn get_range_as_ints(&self, addr: PyRelocatable, size: usize) -> PyResult> { + pub fn get_range_as_ints(&self, addr: PyRelocatable, size: usize) -> PyResult> { Ok(self .vm .borrow() .get_integer_range(&Relocatable::from(&addr), size) .map_err(to_py_error)? .into_iter() - .map(Cow::into_owned) + .map(|num| num.into_owned().to_biguint()) .collect()) } } #[cfg(test)] mod test { + use crate::biguint; use crate::relocatable::PyMaybeRelocatable; use crate::relocatable::PyMaybeRelocatable::RelocatableValue; use crate::vm_core::PyVM; use crate::{memory::PyMemory, relocatable::PyRelocatable}; - use cairo_rs::bigint; - use cairo_rs::types::relocatable::{MaybeRelocatable, Relocatable}; - use num_bigint::{BigInt, Sign}; + use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable}; + use num_bigint::BigUint; use pyo3::PyCell; use pyo3::{types::PyDict, Python}; #[test] fn memory_insert_test() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -143,11 +140,7 @@ mod test { #[test] fn memory_insert_ocuppied_address_error_test() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -177,11 +170,7 @@ memory[ap] = 3 #[test] fn memory_get_test() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -214,11 +203,7 @@ assert memory[ap] == fp #[test] fn get_range() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); @@ -230,15 +215,15 @@ assert memory[ap] == fp vm.vm .borrow_mut() - .insert_value(&Relocatable::from((0, 0)), bigint!(2345108766317314046_u64)) + .insert_value(&Relocatable::from((0, 0)), 2345108766317314046) .unwrap(); vm.vm .borrow_mut() - .insert_value(&Relocatable::from((1, 0)), &Relocatable::from((2, 0))) + .insert_value(&Relocatable::from((1, 0)), Relocatable::from((2, 0))) .unwrap(); vm.vm .borrow_mut() - .insert_value(&Relocatable::from((1, 1)), &Relocatable::from((3, 0))) + .insert_value(&Relocatable::from((1, 1)), Relocatable::from((3, 0))) .unwrap(); let maybe_relocatable = MaybeRelocatable::from((1, 0)); @@ -270,11 +255,7 @@ assert memory[ap] == fp #[test] fn get_range_with_gap() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); @@ -286,15 +267,15 @@ assert memory[ap] == fp vm.vm .borrow_mut() - .insert_value(&Relocatable::from((0, 0)), bigint!(2345108766317314046_u64)) + .insert_value(&Relocatable::from((0, 0)), 2345108766317314046) .unwrap(); vm.vm .borrow_mut() - .insert_value(&Relocatable::from((1, 0)), &Relocatable::from((2, 0))) + .insert_value(&Relocatable::from((1, 0)), Relocatable::from((2, 0))) .unwrap(); vm.vm .borrow_mut() - .insert_value(&Relocatable::from((1, 2)), &Relocatable::from((3, 0))) + .insert_value(&Relocatable::from((1, 2)), Relocatable::from((3, 0))) .unwrap(); let maybe_relocatable = MaybeRelocatable::from((1, 0)); @@ -314,11 +295,7 @@ assert memory[ap] == fp // Test that get_range_as_ints() works as intended. #[test] fn get_range_as_ints() { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let memory = PyMemory::new(&vm); let addr = { @@ -327,12 +304,7 @@ assert memory[ap] == fp vm.load_data( &MaybeRelocatable::from(&addr), - vec![ - bigint!(1).into(), - bigint!(2).into(), - bigint!(3).into(), - bigint!(4).into(), - ], + &vec![1.into(), 2.into(), 3.into(), 4.into()], ) .expect("memory insertion failed"); @@ -343,18 +315,19 @@ assert memory[ap] == fp memory .get_range_as_ints(addr.into(), 4) .expect("get_range_as_ints() failed"), - vec![bigint!(1), bigint!(2), bigint!(3), bigint!(4)], + vec![ + biguint!(1_u32), + biguint!(2_u32), + biguint!(3_u32), + biguint!(4_u32) + ], ); } // Test that get_range_as_ints() fails when not all values are integers. #[test] fn get_range_as_ints_mixed() { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let memory = PyMemory::new(&vm); let addr = { @@ -363,11 +336,11 @@ assert memory[ap] == fp vm.load_data( &MaybeRelocatable::from(&addr), - vec![ - bigint!(1).into(), - bigint!(2).into(), + &vec![ + 1.into(), + 2.into(), MaybeRelocatable::RelocatableValue((1, 2).into()), - bigint!(4).into(), + 4.into(), ], ) .expect("memory insertion failed"); @@ -384,11 +357,7 @@ assert memory[ap] == fp // segments. #[test] fn get_range_as_ints_incomplete() { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let memory = PyMemory::new(&vm); let addr = { @@ -397,12 +366,7 @@ assert memory[ap] == fp vm.load_data( &MaybeRelocatable::from(&addr), - vec![ - bigint!(1).into(), - bigint!(2).into(), - bigint!(3).into(), - bigint!(4).into(), - ], + &vec![1.into(), 2.into(), 3.into(), 4.into()], ) .expect("memory insertion failed"); diff --git a/src/memory_segments.rs b/src/memory_segments.rs index 8a934b01..4b03e8ba 100644 --- a/src/memory_segments.rs +++ b/src/memory_segments.rs @@ -4,7 +4,7 @@ use crate::{ utils::to_py_error, vm_core::PyVM, }; -use cairo_rs::{types::relocatable::MaybeRelocatable, vm::vm_core::VirtualMachine}; +use cairo_vm::{types::relocatable::MaybeRelocatable, vm::vm_core::VirtualMachine}; use pyo3::{prelude::*, types::PyIterator}; use std::{cell::RefCell, rc::Rc}; @@ -50,15 +50,7 @@ impl PySegmentManager { )?; segment_ptr } - _ => { - let mut value: MaybeRelocatable = arg.extract::(py)?.into(); - if apply_modulo_to_args { - value = value - .mod_floor(self.vm.borrow().get_prime()) - .map_err(to_py_error)?; - } - value - } + _ => arg.extract::(py)?.into(), }) .to_object(py), ) @@ -75,7 +67,7 @@ impl PySegmentManager { let ptr: MaybeRelocatable = ptr.into(); let arg_iter = PyIterator::from_object(py, &arg)?; - let mut data = Vec::new(); + let mut data = Vec::::new(); for value in arg_iter { data.push( self.gen_arg(py, value?.to_object(py), apply_modulo_to_args)? @@ -86,7 +78,7 @@ impl PySegmentManager { self.vm .borrow_mut() - .load_data(&ptr, data) + .load_data(&ptr, &data) .map(|x| PyMaybeRelocatable::from(x).to_object(py)) .map_err(to_py_error) } @@ -106,17 +98,13 @@ impl PySegmentManager { mod test { use super::PySegmentManager; use crate::{memory::PyMemory, relocatable::PyMaybeRelocatable, vm_core::PyVM}; - use cairo_rs::{bigint, types::relocatable::Relocatable}; - use num_bigint::{BigInt, Sign}; + use cairo_felt::{Felt, NewFelt}; + use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable}; use pyo3::{Python, ToPyObject}; #[test] fn add_segment_test() { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let segments = PySegmentManager::new(&vm, PyMemory::new(&vm)); assert!(segments.add().is_ok()); } @@ -124,11 +112,7 @@ mod test { #[test] fn write_arg_test() { Python::with_gil(|py| { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let segments = PySegmentManager::new(&vm, PyMemory::new(&vm)); let ptr = segments.add().unwrap(); @@ -153,7 +137,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(1), + &Felt::new(1), ); assert_eq!( vm_ref @@ -162,7 +146,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(2), + &Felt::new(2), ); let relocatable = vm_ref @@ -170,8 +154,7 @@ mod test { .unwrap() .unwrap() .get_relocatable() - .unwrap() - .clone(); + .unwrap(); assert_eq!( vm_ref @@ -180,7 +163,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(3), + &Felt::new(3), ); assert_eq!( vm_ref @@ -189,7 +172,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(4), + &Felt::new(4), ); assert!(vm_ref.get_maybe(&(&relocatable + 2)).unwrap().is_none()); @@ -198,8 +181,7 @@ mod test { .unwrap() .unwrap() .get_relocatable() - .unwrap() - .clone(); + .unwrap(); assert_eq!( vm_ref @@ -208,7 +190,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(5), + &Felt::new(5), ); assert_eq!( vm_ref @@ -217,7 +199,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(6), + &Felt::new(6), ); assert!(vm_ref.get_maybe(&(&relocatable + 2)).unwrap().is_none()); @@ -230,11 +212,7 @@ mod test { #[test] fn add_temp_segment_test() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let mut vm = PyVM::new(false); let memory = PyMemory::new(&vm); let mut segments = PySegmentManager::new(&mut vm, memory); assert!(segments.add_temp_segment().is_ok()); @@ -242,11 +220,7 @@ mod test { #[test] fn get_segment_used_size() { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let memory = PyMemory::new(&vm); let segments = PySegmentManager::new(&vm, memory); @@ -257,11 +231,11 @@ mod test { .borrow_mut() .load_data( &Relocatable::from(&segment).into(), - vec![ - bigint!(1).into(), - bigint!(2).into(), - bigint!(3).into(), - bigint!(4).into(), + &vec![ + MaybeRelocatable::from(1), + MaybeRelocatable::from(2), + MaybeRelocatable::from(3), + MaybeRelocatable::from(4), ], ) .is_ok()); diff --git a/src/range_check.rs b/src/range_check.rs index 5714b9ec..c3a648a7 100644 --- a/src/range_check.rs +++ b/src/range_check.rs @@ -1,26 +1,30 @@ -use cairo_rs::vm::{ +use cairo_vm::vm::{ errors::vm_errors::VirtualMachineError, runners::builtin_runner::RangeCheckBuiltinRunner, }; -use num_bigint::BigInt; +use cairo_felt::FeltOps; +use num_bigint::BigUint; use pyo3::prelude::*; #[pyclass(name = "RangeCheck")] #[derive(Clone, Debug, PartialEq, Eq)] pub struct PyRangeCheck { #[pyo3(get)] - bound: BigInt, + bound: Option, } #[pymethods] impl PyRangeCheck { #[new] - pub fn new(value: BigInt) -> Self { + pub fn new(value: Option) -> Self { Self { bound: value } } pub fn __repr__(&self) -> String { - format!("Bound: {}", self.bound) + match self.bound { + Some(ref bound) => format!("Bound: {}", bound), + None => String::from("None"), + } } } @@ -28,7 +32,7 @@ impl From> for PyRangeChec fn from(val: Result<&RangeCheckBuiltinRunner, VirtualMachineError>) -> Self { match val { Ok(range_check_builtin) => PyRangeCheck::from(range_check_builtin), - Err(_err) => PyRangeCheck::new(BigInt::from(0)), + Err(_err) => PyRangeCheck::new(None), } } } @@ -36,7 +40,7 @@ impl From> for PyRangeChec impl From<&RangeCheckBuiltinRunner> for PyRangeCheck { fn from(val: &RangeCheckBuiltinRunner) -> Self { Self { - bound: val._bound.clone(), + bound: val._bound.as_ref().map(|num| num.to_biguint()), } } } @@ -49,30 +53,28 @@ impl ToPyObject for PyRangeCheck { #[cfg(test)] mod test { + use crate::biguint; + use num_bigint::BigUint; + use super::PyRangeCheck; use super::*; - use cairo_rs::{ - bigint, - vm::{ - errors::vm_errors::VirtualMachineError, - runners::builtin_runner::RangeCheckBuiltinRunner, - }, + use cairo_vm::vm::{ + errors::vm_errors::VirtualMachineError, runners::builtin_runner::RangeCheckBuiltinRunner, }; - use num_bigint::BigInt; use pyo3::ToPyObject; #[test] fn py_range_check_new() { - let value = bigint!(12); - let new_py_range_check = PyRangeCheck::new(value.clone()); + let value = biguint!(12_u32); + let new_py_range_check = PyRangeCheck::new(Some(value.clone())); - assert_eq!(new_py_range_check, PyRangeCheck { bound: value }); + assert_eq!(new_py_range_check, PyRangeCheck { bound: Some(value) }); } #[test] fn py_range_check_repr() { - let value = bigint!(12); - let new_py_range_check = PyRangeCheck::new(value); + let value = biguint!(12_u32); + let new_py_range_check = PyRangeCheck::new(Some(value)); assert_eq!(new_py_range_check.__repr__(), String::from("Bound: 12")); } @@ -80,14 +82,14 @@ mod test { #[test] fn py_range_check_from_result_ok() { let value = 12; - let bound = bigint!(1usize << 16).pow(value); + let bound = biguint!(1usize << 16).pow(value); let range_check_builtin = RangeCheckBuiltinRunner::new(value, value, true); let result_with_range_check_builtin: Result<&RangeCheckBuiltinRunner, VirtualMachineError> = Ok(&range_check_builtin); assert_eq!( PyRangeCheck::from(result_with_range_check_builtin), - PyRangeCheck::new(bound) + PyRangeCheck::new(Some(bound)) ); } @@ -98,26 +100,26 @@ mod test { assert_eq!( PyRangeCheck::from(result_with_range_check_builtin), - PyRangeCheck::new(BigInt::from(0)) + PyRangeCheck::new(None) ); } #[test] fn py_range_check_from_range_check_builtin_runner() { let value = 12; - let bound = bigint!(1usize << 16).pow(value); + let bound = biguint!(1usize << 16).pow(value); let range_check_builtin = RangeCheckBuiltinRunner::new(value, value, true); assert_eq!( PyRangeCheck::from(&range_check_builtin), - PyRangeCheck::new(bound) + PyRangeCheck::new(Some(bound)) ); } #[test] fn py_range_check_to_py_object() { - let value = bigint!(12); - let new_py_range_check = PyRangeCheck::new(value.clone()); + let value = biguint!(12_u32); + let new_py_range_check = PyRangeCheck::new(Some(value.clone())); Python::with_gil(|py| { let py_object = new_py_range_check @@ -125,7 +127,7 @@ mod test { .extract::(py) .unwrap(); - assert_eq!(py_object, PyRangeCheck::new(value)); + assert_eq!(py_object, PyRangeCheck::new(Some(value))); }); } } diff --git a/src/relocatable.rs b/src/relocatable.rs index b7780f66..f110e5ad 100644 --- a/src/relocatable.rs +++ b/src/relocatable.rs @@ -1,19 +1,20 @@ -use cairo_rs::{ - bigint, - hint_processor::hint_processor_utils::bigint_to_usize, +use crate::{ + biguint, + utils::{biguint_to_usize, to_py_error}, +}; +use cairo_felt::FeltOps; +use cairo_vm::{ types::relocatable::{MaybeRelocatable, Relocatable}, vm::errors::vm_errors::VirtualMachineError, }; -use num_bigint::BigInt; +use num_bigint::BigUint; use pyo3::{exceptions::PyArithmeticError, prelude::*, pyclass::CompareOp}; -use crate::utils::to_py_error; - const PYRELOCATABLE_COMPARE_ERROR: &str = "Cannot compare Relocatables of different segments"; #[derive(FromPyObject, Debug, Clone, PartialEq, Eq)] pub enum PyMaybeRelocatable { - Int(BigInt), + Int(BigUint), RelocatableValue(PyRelocatable), } @@ -48,7 +49,7 @@ impl PyRelocatable { PyMaybeRelocatable::Int(value) => { Ok(PyMaybeRelocatable::RelocatableValue(PyRelocatable { segment_index: self.segment_index, - offset: self.offset - bigint_to_usize(&value).unwrap(), + offset: self.offset - biguint_to_usize(&value).unwrap(), }) .to_object(py)) } @@ -56,7 +57,7 @@ impl PyRelocatable { if self.segment_index != address.segment_index { return Err(VirtualMachineError::DiffIndexSub).map_err(to_py_error)?; } - Ok(PyMaybeRelocatable::Int(bigint!(self.offset - address.offset)).to_object(py)) + Ok(PyMaybeRelocatable::Int(biguint!(self.offset - address.offset)).to_object(py)) } } } @@ -111,7 +112,7 @@ impl From for MaybeRelocatable { PyMaybeRelocatable::RelocatableValue(rel) => MaybeRelocatable::RelocatableValue( Relocatable::from((rel.segment_index, rel.offset)), ), - PyMaybeRelocatable::Int(num) => MaybeRelocatable::Int(num), + PyMaybeRelocatable::Int(num) => MaybeRelocatable::Int(num.into()), } } } @@ -122,7 +123,7 @@ impl From<&PyMaybeRelocatable> for MaybeRelocatable { PyMaybeRelocatable::RelocatableValue(rel) => MaybeRelocatable::RelocatableValue( Relocatable::from((rel.segment_index, rel.offset)), ), - PyMaybeRelocatable::Int(num) => MaybeRelocatable::Int(num.clone()), + PyMaybeRelocatable::Int(num) => MaybeRelocatable::Int(num.into()), } } } @@ -133,7 +134,7 @@ impl From for PyMaybeRelocatable { MaybeRelocatable::RelocatableValue(rel) => PyMaybeRelocatable::RelocatableValue( PyRelocatable::new((rel.segment_index, rel.offset)), ), - MaybeRelocatable::Int(num) => PyMaybeRelocatable::Int(num), + MaybeRelocatable::Int(num) => PyMaybeRelocatable::Int(num.to_biguint()), } } } @@ -144,7 +145,7 @@ impl From<&MaybeRelocatable> for PyMaybeRelocatable { MaybeRelocatable::RelocatableValue(rel) => PyMaybeRelocatable::RelocatableValue( PyRelocatable::new((rel.segment_index, rel.offset)), ), - MaybeRelocatable::Int(num) => PyMaybeRelocatable::Int(num.clone()), + MaybeRelocatable::Int(num) => PyMaybeRelocatable::Int(num.to_biguint()), } } } @@ -188,27 +189,27 @@ impl From for PyMaybeRelocatable { } } -impl From<&BigInt> for PyMaybeRelocatable { - fn from(val: &BigInt) -> Self { +impl From<&BigUint> for PyMaybeRelocatable { + fn from(val: &BigUint) -> Self { PyMaybeRelocatable::Int(val.clone()) } } -impl From for PyMaybeRelocatable { - fn from(val: BigInt) -> Self { +impl From for PyMaybeRelocatable { + fn from(val: BigUint) -> Self { PyMaybeRelocatable::Int(val) } } #[cfg(test)] mod test { - use cairo_rs::{bigint, types::relocatable::MaybeRelocatable}; - use num_bigint::BigInt; - use pyo3::ToPyObject; - use pyo3::{pyclass::CompareOp, Python}; - + use super::*; + use crate::relocatable::biguint; use crate::relocatable::Relocatable; use crate::relocatable::{PyMaybeRelocatable, PyRelocatable}; + use cairo_vm::types::relocatable::MaybeRelocatable; + use pyo3::ToPyObject; + use pyo3::{pyclass::CompareOp, Python}; #[test] fn py_relocatable_new() { @@ -252,8 +253,8 @@ mod test { let values = (1, 2); let py_relocatable = PyRelocatable::new(values); - let bigint_value = bigint!(1); - let py_maybe_relocatable_int_variant = PyMaybeRelocatable::Int(bigint_value); + let biguint_value = biguint!(1_u32); + let py_maybe_relocatable_int_variant = PyMaybeRelocatable::Int(biguint_value); let substraction = py_relocatable .__sub__(py_maybe_relocatable_int_variant, py) .unwrap() @@ -286,7 +287,7 @@ mod test { .extract::(py) .unwrap(); - assert_eq!(substraction, PyMaybeRelocatable::Int(bigint!(1))); + assert_eq!(substraction, PyMaybeRelocatable::Int(biguint!(1_u32))); }); } @@ -356,13 +357,13 @@ mod test { #[test] fn maybe_relocatable_from_py_maybe_relocatable() { - let py_maybe_relocatable_int = PyMaybeRelocatable::Int(bigint!(1)); + let py_maybe_relocatable_int = PyMaybeRelocatable::Int(biguint!(1_u32)); let py_relocatable = PyRelocatable::new((1, 1)); let py_maybe_relocatable_relocatable = PyMaybeRelocatable::RelocatableValue(py_relocatable); assert_eq!( MaybeRelocatable::from(py_maybe_relocatable_int), - MaybeRelocatable::Int(bigint!(1)) + MaybeRelocatable::from(1) ); assert_eq!( MaybeRelocatable::from(py_maybe_relocatable_relocatable), @@ -372,13 +373,13 @@ mod test { #[test] fn maybe_relocatable_from_py_maybe_relocatable_ref() { - let py_maybe_relocatable_int = PyMaybeRelocatable::Int(bigint!(1)); + let py_maybe_relocatable_int = PyMaybeRelocatable::Int(biguint!(1_u32)); let py_relocatable = PyRelocatable::new((1, 1)); let py_maybe_relocatable_relocatable = PyMaybeRelocatable::RelocatableValue(py_relocatable); assert_eq!( MaybeRelocatable::from(&py_maybe_relocatable_int), - MaybeRelocatable::Int(bigint!(1)) + MaybeRelocatable::from(1) ); assert_eq!( MaybeRelocatable::from(&py_maybe_relocatable_relocatable), @@ -388,7 +389,7 @@ mod test { #[test] fn py_maybe_relocatable_from_maybe_relocatable() { - let maybe_relocatable_int = MaybeRelocatable::Int(bigint!(1)); + let maybe_relocatable_int = MaybeRelocatable::from(1); let maybe_relocatable_reloc = MaybeRelocatable::RelocatableValue(Relocatable { segment_index: 1, offset: 1, @@ -396,7 +397,7 @@ mod test { assert_eq!( PyMaybeRelocatable::from(maybe_relocatable_int), - PyMaybeRelocatable::Int(bigint!(1)) + PyMaybeRelocatable::Int(biguint!(1_u32)) ); assert_eq!( @@ -410,7 +411,7 @@ mod test { #[test] fn py_maybe_relocatable_from_maybe_relocatable_ref() { - let maybe_relocatable_int = MaybeRelocatable::Int(bigint!(1)); + let maybe_relocatable_int = MaybeRelocatable::from(1); let maybe_relocatable_reloc = MaybeRelocatable::RelocatableValue(Relocatable { segment_index: 1, offset: 1, @@ -418,7 +419,7 @@ mod test { assert_eq!( PyMaybeRelocatable::from(&maybe_relocatable_int), - PyMaybeRelocatable::Int(bigint!(1)) + PyMaybeRelocatable::Int(biguint!(1_u32)) ); assert_eq!( @@ -508,8 +509,8 @@ mod test { } #[test] - fn py_maybe_relocatable_from_bigint() { - let value = bigint!(7654321); + fn py_maybe_relocatable_from_biguint() { + let value = biguint!(7654321_u32); assert_eq!( PyMaybeRelocatable::from(value.clone()), @@ -518,8 +519,8 @@ mod test { } #[test] - fn py_maybe_relocatable_from_bigint_ref() { - let value = bigint!(7654321); + fn py_maybe_relocatable_from_biguint_ref() { + let value = biguint!(7654321_u32); assert_eq!( PyMaybeRelocatable::from(&value), @@ -529,7 +530,7 @@ mod test { #[test] fn py_maybe_relocatable_to_object() { - let py_maybe_relocatable_int = PyMaybeRelocatable::Int(bigint!(6543)); + let py_maybe_relocatable_int = PyMaybeRelocatable::Int(biguint!(6543_u32)); let py_maybe_relocatable_reloc = PyMaybeRelocatable::RelocatableValue(PyRelocatable { segment_index: 43, offset: 123, @@ -541,7 +542,7 @@ mod test { .extract::(py) .unwrap(); - assert_eq!(py_object_int, PyMaybeRelocatable::Int(bigint!(6543))); + assert_eq!(py_object_int, PyMaybeRelocatable::Int(biguint!(6543_u32))); let py_object_reloc = py_maybe_relocatable_reloc .to_object(py) diff --git a/src/run_context.rs b/src/run_context.rs index 854f2ae7..c452b43f 100644 --- a/src/run_context.rs +++ b/src/run_context.rs @@ -1,5 +1,5 @@ use crate::relocatable::PyRelocatable; -use cairo_rs::types::relocatable::Relocatable; +use cairo_vm::types::relocatable::Relocatable; use pyo3::{pyclass, pymethods}; #[pyclass] diff --git a/src/scope_manager.rs b/src/scope_manager.rs index 02dca8a3..1d4c1d96 100644 --- a/src/scope_manager.rs +++ b/src/scope_manager.rs @@ -1,6 +1,6 @@ use std::{any::Any, collections::HashMap}; -use cairo_rs::{any_box, types::exec_scope::ExecutionScopes}; +use cairo_vm::{any_box, types::exec_scope::ExecutionScopes}; use pyo3::{pyclass, pymethods, PyErr, PyObject}; use crate::utils::to_py_error; diff --git a/src/to_felt_or_relocatable.rs b/src/to_felt_or_relocatable.rs index b9a4d836..839add53 100644 --- a/src/to_felt_or_relocatable.rs +++ b/src/to_felt_or_relocatable.rs @@ -1,4 +1,4 @@ -use num_bigint::BigInt; +use num_bigint::BigUint; use pyo3::prelude::*; use crate::relocatable::{PyMaybeRelocatable, PyRelocatable}; @@ -12,7 +12,7 @@ impl ToFeltOrRelocatableFunc { match any.extract::(py) { Ok(rel) => Ok(Into::::into(rel).to_object(py)), Err(_) => Ok(Into::::into( - any.call_method0(py, "__int__")?.extract::(py)?, + any.call_method0(py, "__int__")?.extract::(py)?, ) .to_object(py)), } diff --git a/src/utils.rs b/src/utils.rs index bc273173..70e8cc41 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,6 @@ -use num_bigint::BigInt; +use cairo_felt::{Felt, FeltOps}; +use cairo_vm::vm::errors::vm_errors::VirtualMachineError; +use num_bigint::BigUint; use pyo3::{exceptions::PyValueError, PyErr}; use std::{collections::HashMap, fmt::Display}; @@ -12,12 +14,26 @@ pub fn to_py_error(error: T) -> PyErr { PyValueError::new_err(error.to_string()) } -pub fn const_path_to_const_name(constants: &HashMap) -> HashMap { +#[macro_export] +macro_rules! biguint { + ($val : expr) => { + Into::::into($val) + }; +} + +pub fn const_path_to_const_name(constants: &HashMap) -> HashMap { constants .iter() .map(|(name, value)| { let name = name.rsplit('.').next().unwrap_or(name); - (name.to_string(), value.clone()) + (name.to_string(), value.to_biguint()) }) .collect() } + +//Tries to convert a biguint value to usize +pub fn biguint_to_usize(biguint: &BigUint) -> Result { + biguint + .try_into() + .map_err(|_| VirtualMachineError::BigintToUsizeFail) +} diff --git a/src/vm_core.rs b/src/vm_core.rs index 5385983d..a267e5d1 100644 --- a/src/vm_core.rs +++ b/src/vm_core.rs @@ -9,16 +9,19 @@ use crate::{ memory::PyMemory, memory_segments::PySegmentManager, range_check::PyRangeCheck, relocatable::PyRelocatable, }; -use cairo_rs::any_box; -use cairo_rs::hint_processor::hint_processor_definition::HintProcessor; -use cairo_rs::serde::deserialize_program::{Attribute, Member}; -use cairo_rs::types::exec_scope::ExecutionScopes; -use cairo_rs::vm::vm_core::VirtualMachine; -use cairo_rs::{ +use cairo_felt::{Felt, FIELD}; +use cairo_vm::any_box; +use cairo_vm::hint_processor::hint_processor_definition::HintProcessor; +use cairo_vm::serde::deserialize_program::Member; +use cairo_vm::types::exec_scope::ExecutionScopes; +use cairo_vm::vm::errors::hint_errors::HintError; +use cairo_vm::vm::vm_core::VirtualMachine; +use cairo_vm::{ hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData, vm::errors::vm_errors::VirtualMachineError, }; -use num_bigint::BigInt; +use lazy_static::lazy_static; +use num_bigint::BigUint; use pyo3::{pyclass, pymethods, PyObject, ToPyObject}; use pyo3::{types::PyDict, Python}; use pyo3::{PyCell, PyErr}; @@ -47,6 +50,11 @@ const GLOBAL_NAMES: [&str; 18] = [ "__name__", ]; +lazy_static! { + pub static ref CAIRO_PRIME: BigUint = + (Into::::into(FIELD.0) << 128) + Into::::into(FIELD.1); +} + #[derive(Clone)] #[pyclass(unsendable)] pub struct PyVM { @@ -64,17 +72,9 @@ impl PyVM { } impl PyVM { - pub fn new( - prime: BigInt, - trace_enabled: bool, - error_message_attributes: Vec, - ) -> PyVM { + pub fn new(trace_enabled: bool) -> PyVM { PyVM { - vm: Rc::new(RefCell::new(VirtualMachine::new( - prime, - trace_enabled, - error_message_attributes, - ))), + vm: Rc::new(RefCell::new(VirtualMachine::new(trace_enabled))), failed_hint_index: None, } } @@ -84,11 +84,11 @@ impl PyVM { } pub(crate) fn execute_hint( - &mut self, + &self, hint_data: &HintProcessorData, hint_locals: &mut HashMap, exec_scopes: &mut ExecutionScopes, - constants: &HashMap, + constants: &HashMap, struct_types: Rc>>, static_locals: Option<&HashMap>, ) -> Result<(), PyErr> { @@ -109,7 +109,7 @@ impl PyVM { let range_check_builtin = PyRangeCheck::from((*self.vm).borrow().get_range_check_builtin()); let ecdsa_builtin = pycell!(py, PySignature::new()); - let prime = (*self.vm).borrow().get_prime().clone(); + let prime: BigUint = CAIRO_PRIME.clone(); let to_felt_or_relocatable = ToFeltOrRelocatableFunc; // This line imports Python builtins. If not imported, this will run only with Python 3.10 @@ -172,7 +172,7 @@ impl PyVM { exec_scopes: &mut ExecutionScopes, hint_data_dictionary: &HashMap>>, struct_types: Rc>>, - constants: &HashMap, + constants: &HashMap, static_locals: Option<&HashMap>, ) -> Result<(), PyErr> { let pc_offset = (*self.vm).borrow().get_pc().offset; @@ -180,12 +180,23 @@ impl PyVM { if let Some(hint_list) = hint_data_dictionary.get(&pc_offset) { for (hint_index, hint_data) in hint_list.iter().enumerate() { if self - .should_run_py_hint(hint_executor, exec_scopes, hint_data, constants) + .should_run_py_hint( + hint_executor, + exec_scopes, + hint_data, + constants, + hint_index, + ) .map_err(to_py_error)? { let hint_data = hint_data .downcast_ref::() - .ok_or(VirtualMachineError::WrongHintData) + .ok_or_else(|| { + VirtualMachineError::Hint( + hint_index, + Box::new(HintError::WrongHintData), + ) + }) .map_err(to_py_error)?; if let Err(hint_error) = self.execute_hint( @@ -214,7 +225,7 @@ impl PyVM { exec_scopes: &mut ExecutionScopes, hint_data_dictionary: &HashMap>>, struct_types: Rc>>, - constants: &HashMap, + constants: &HashMap, static_locals: Option<&HashMap>, ) -> Result<(), PyErr> { self.step_hint( @@ -230,17 +241,21 @@ impl PyVM { } fn should_run_py_hint( - &self, + &mut self, hint_executor: &mut dyn HintProcessor, exec_scopes: &mut ExecutionScopes, hint_data: &Box, - constants: &HashMap, + constants: &HashMap, + hint_index: usize, ) -> Result { let mut vm = self.vm.borrow_mut(); match hint_executor.execute_hint(&mut vm, exec_scopes, hint_data, constants) { Ok(()) => Ok(false), - Err(VirtualMachineError::UnknownHint(_)) => Ok(true), - Err(e) => Err(e), + Err(HintError::UnknownHint(_)) => Ok(true), + Err(e) => { + self.failed_hint_index = Some(hint_index); + Err(VirtualMachineError::Hint(hint_index, Box::new(e))) + } } } } @@ -281,9 +296,10 @@ pub(crate) fn update_scope_hint_locals( #[cfg(test)] mod test { - use crate::{relocatable::PyMaybeRelocatable, vm_core::PyVM}; - use cairo_rs::{ - bigint, + use super::*; + use crate::{biguint, relocatable::PyMaybeRelocatable, vm_core::PyVM}; + use cairo_felt::{Felt, NewFelt}; + use cairo_vm::{ hint_processor::{ builtin_hint_processor::builtin_hint_processor_definition::{ BuiltinHintProcessor, HintProcessorData, @@ -295,17 +311,12 @@ mod test { relocatable::{MaybeRelocatable, Relocatable}, }, }; - use num_bigint::{BigInt, Sign}; use pyo3::{PyObject, Python, ToPyObject}; use std::{any::Any, collections::HashMap, rc::Rc}; #[test] fn execute_print_hint() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let code = "print(ap)"; let hint_data = HintProcessorData::new_default(code.to_string(), HashMap::new()); assert!(vm @@ -322,11 +333,7 @@ mod test { #[test] fn set_memory_item_hint() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let code = "print(ap)"; let hint_data = HintProcessorData::new_default(code.to_string(), HashMap::new()); assert!(vm @@ -343,11 +350,7 @@ mod test { #[test] fn ids_hint() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -357,10 +360,7 @@ mod test { ]); vm.vm .borrow_mut() - .insert_value( - &Relocatable::from((1, 1)), - &MaybeRelocatable::from(bigint!(2usize)), - ) + .insert_value(&Relocatable::from((1, 1)), &MaybeRelocatable::from(2)) .unwrap(); let code = "ids.a = ids.b"; let hint_data = HintProcessorData::new_default(code.to_string(), references); @@ -376,20 +376,16 @@ mod test { .is_ok()); assert_eq!( vm.vm.borrow().get_maybe(&Relocatable::from((1, 2))), - Ok(Some(MaybeRelocatable::from(bigint!(2)))) + Ok(Some(MaybeRelocatable::from(2))) ); } #[test] // Test the availability of cairo constants in ids fn const_ids() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); - let constants = HashMap::from([(String::from("CONST"), bigint!(1))]); + let constants = HashMap::from([(String::from("CONST"), Felt::new(1))]); let mut exec_scopes = ExecutionScopes::new(); let code_1 = "assert(ids.CONST != 2)"; @@ -424,11 +420,7 @@ mod test { #[test] // This test is analogous to the `test_step_for_preset_memory` unit test in the cairo-rs crate. fn test_step_with_no_hint() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let mut vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); @@ -442,15 +434,15 @@ mod test { vm.vm .borrow_mut() - .insert_value(&Relocatable::from((0, 0)), bigint!(2345108766317314046_u64)) + .insert_value(&Relocatable::from((0, 0)), 2345108766317314046) .unwrap(); vm.vm .borrow_mut() - .insert_value(&Relocatable::from((1, 0)), &Relocatable::from((2, 0))) + .insert_value(&Relocatable::from((1, 0)), Relocatable::from((2, 0))) .unwrap(); vm.vm .borrow_mut() - .insert_value(&Relocatable::from((1, 1)), &Relocatable::from((3, 0))) + .insert_value(&Relocatable::from((1, 1)), Relocatable::from((3, 0))) .unwrap(); assert!(vm @@ -468,11 +460,7 @@ mod test { #[test] fn test_step_with_print_hint() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let mut vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); @@ -486,15 +474,15 @@ mod test { vm.vm .borrow_mut() - .insert_value(&Relocatable::from((0, 0)), bigint!(2345108766317314046_u64)) + .insert_value(&Relocatable::from((0, 0)), 2345108766317314046) .unwrap(); vm.vm .borrow_mut() - .insert_value(&Relocatable::from((1, 0)), &Relocatable::from((2, 0))) + .insert_value(&Relocatable::from((1, 0)), Relocatable::from((2, 0))) .unwrap(); vm.vm .borrow_mut() - .insert_value(&Relocatable::from((1, 1)), &Relocatable::from((3, 0))) + .insert_value(&Relocatable::from((1, 1)), Relocatable::from((3, 0))) .unwrap(); let code = "print(ap)"; @@ -518,11 +506,7 @@ mod test { #[test] fn scopes_hint() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -557,11 +541,7 @@ mod test { #[test] fn scopes_hint_modify() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -619,11 +599,7 @@ mod test { #[test] fn modify_hint_locals() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let code = "word = word[::-1] print(word)"; let hint_data = HintProcessorData::new_default(code.to_string(), HashMap::new()); @@ -651,11 +627,7 @@ print(word)"; #[test] fn exit_main_scope_hint() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "vm_exit_scope()"; let hint_data = HintProcessorData::new_default(code.to_string(), HashMap::new()); @@ -672,11 +644,7 @@ print(word)"; #[test] fn enter_scope_empty_hint() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "vm_enter_scope()"; let hint_data = HintProcessorData::new_default(code.to_string(), HashMap::new()); @@ -695,11 +663,7 @@ print(word)"; #[test] fn enter_exit_scope_same_hint() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "vm_enter_scope() vm_exit_scope()"; @@ -719,11 +683,7 @@ vm_exit_scope()"; #[test] fn enter_exit_scope_separate_hints() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code_a = "vm_enter_scope()"; let code_b = "vm_exit_scope()"; @@ -755,11 +715,7 @@ vm_exit_scope()"; #[test] fn enter_exit_enter_scope_same_hint() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "vm_enter_scope() vm_exit_scope() @@ -780,11 +736,7 @@ vm_enter_scope()"; #[test] fn list_comprehension() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "lista_a = [1,2,3] lista_b = [lista_a[k] for k in range(2)]"; @@ -803,11 +755,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn enter_scope_non_empty_hint() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code_a = "vm_enter_scope({'n': 12})"; let code_b = "assert(n == 12)"; @@ -839,11 +787,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn access_relocatable_segment_index() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "assert(ap.segment_index == 1)"; let hint_data = HintProcessorData::new_default(code.to_string(), HashMap::new()); @@ -861,11 +805,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn to_felt_or_relocatable_number() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "felt = to_felt_or_relocatable(456)"; let hint_data = HintProcessorData::new_default(code.to_string(), HashMap::new()); @@ -888,18 +828,14 @@ lista_b = [lista_a[k] for k in range(2)]"; .unwrap() .extract::(py) .unwrap(), - PyMaybeRelocatable::from(bigint!(456)) + PyMaybeRelocatable::from(biguint!(456_u32)) ); }); } #[test] fn to_felt_or_relocatable_list_should_fail() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "felt = to_felt_or_relocatable([1,2,3])"; let hint_data = HintProcessorData::new_default(code.to_string(), HashMap::new()); @@ -917,11 +853,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn to_felt_or_relocatable_relocatable() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "ids.test_value = to_felt_or_relocatable(ids.relocatable)"; vm.vm.borrow_mut().add_memory_segment(); @@ -958,11 +890,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn test_get_range() { - let mut pyvm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let pyvm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "assert(memory.get_range(ids.address, 3) == [1,2,7])"; @@ -979,17 +907,17 @@ lista_b = [lista_a[k] for k in range(2)]"; pyvm.vm .borrow_mut() - .insert_value(&Relocatable::from((2, 0)), bigint!(1)) + .insert_value(&Relocatable::from((2, 0)), 1) .unwrap(); pyvm.vm .borrow_mut() - .insert_value(&Relocatable::from((2, 1)), bigint!(2)) + .insert_value(&Relocatable::from((2, 1)), 2) .unwrap(); pyvm.vm .borrow_mut() - .insert_value(&Relocatable::from((2, 2)), bigint!(7)) + .insert_value(&Relocatable::from((2, 2)), 7) .unwrap(); let hint_data = HintProcessorData::new_default(code.to_string(), ids); @@ -1007,11 +935,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn test_segments_memory_get_range() { - let mut pyvm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let pyvm = PyVM::new(false); let code = "assert(segments.memory.get_range(ids.address, 2) == [9,12])"; let ids = HashMap::from([("address".to_string(), HintReference::new_simple(0))]); @@ -1027,12 +951,12 @@ lista_b = [lista_a[k] for k in range(2)]"; pyvm.vm .borrow_mut() - .insert_value(&Relocatable::from((2, 0)), bigint!(9)) + .insert_value(&Relocatable::from((2, 0)), 9) .unwrap(); pyvm.vm .borrow_mut() - .insert_value(&Relocatable::from((2, 1)), bigint!(12)) + .insert_value(&Relocatable::from((2, 1)), 12) .unwrap(); let hint_data = HintProcessorData::new_default(code.to_string(), ids); @@ -1050,11 +974,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn run_hint_with_static_locals() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let static_locals = HashMap::from([( "__number_max".to_string(), Python::with_gil(|py| -> PyObject { 90.to_object(py) }), @@ -1086,11 +1006,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn run_hint_with_static_locals_shouldnt_change_its_value() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let static_locals = HashMap::from([( "__number_max".to_string(), Python::with_gil(|py| -> PyObject { 90.to_object(py) }), @@ -1120,11 +1036,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn run_hint_with_static_locals_shouldnt_affect_scope_or_hint_locals() { - let mut vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let static_locals = HashMap::from([( "__number_max".to_string(), Python::with_gil(|py| -> PyObject { 90.to_object(py) }), @@ -1149,11 +1061,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn should_run_py_hint_nonsense_data_should_fail() { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let mut vm = PyVM::new(false); let hint_data: Box = Box::new("nonsense"); let mut hint_processor = BuiltinHintProcessor::new_empty(); assert!(vm @@ -1162,17 +1070,14 @@ lista_b = [lista_a[k] for k in range(2)]"; &mut ExecutionScopes::new(), &hint_data, &HashMap::new(), + 0 ) .is_err()); } #[test] fn run_context() { - let vm = PyVM::new( - BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - false, - Vec::new(), - ); + let vm = PyVM::new(false); let run_context = vm.run_context(); assert_eq!(run_context.pc(), (0, 0).into());