From 40bacb10dd7a8e76c077cae1d04c3d0d07154a75 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 6 Jan 2023 18:28:34 -0300 Subject: [PATCH 01/12] Fix incompatibilities --- Cargo.lock | 18 ++- Cargo.toml | 3 +- src/cairo_runner.rs | 88 ++++----------- src/ecdsa.rs | 6 +- src/ids.rs | 131 +++++++--------------- src/memory.rs | 83 ++++---------- src/memory_segments.rs | 64 ++++------- src/range_check.rs | 40 +++---- src/relocatable.rs | 29 ++--- src/utils.rs | 20 +++- src/vm_core.rs | 247 +++++++++++++---------------------------- 11 files changed, 255 insertions(+), 474 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f01f4fb3..b2362ab5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -132,10 +132,11 @@ checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" [[package]] name = "cairo-rs" 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=2a687c0795a99ce353d186c305c00feccba73bee#2a687c0795a99ce353d186c305c00feccba73bee" dependencies = [ "bincode", "clap", + "felt", "generic-array", "hex", "keccak", @@ -161,6 +162,7 @@ name = "cairo-rs-py" version = "0.1.0" dependencies = [ "cairo-rs", + "felt", "lazy_static", "num-bigint", "pyo3", @@ -308,6 +310,18 @@ dependencies = [ "indexmap", ] +[[package]] +name = "felt" +version = "0.1.0" +source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=2a687c0795a99ce353d186c305c00feccba73bee#2a687c0795a99ce353d186c305c00feccba73bee" +dependencies = [ + "lazy_static", + "num-bigint", + "num-integer", + "num-traits", + "serde", +] + [[package]] name = "fsio" version = "0.1.3" @@ -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=2a687c0795a99ce353d186c305c00feccba73bee#2a687c0795a99ce353d186c305c00feccba73bee" dependencies = [ "nom", ] diff --git a/Cargo.toml b/Cargo.toml index 3232be07..d890010d 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-rs = { git = "https://github.com/lambdaclass/cairo-rs.git", rev = "2a687c0795a99ce353d186c305c00feccba73bee" } +felt = { git = "https://github.com/lambdaclass/cairo-rs.git", rev = "2a687c0795a99ce353d186c305c00feccba73bee" } num-bigint = "0.4" lazy_static = "1.4.0" diff --git a/src/cairo_runner.rs b/src/cairo_runner.rs index b28b5194..ccd93343 100644 --- a/src/cairo_runner.rs +++ b/src/cairo_runner.rs @@ -7,7 +7,6 @@ use crate::{ vm_core::PyVM, }; use cairo_rs::{ - bigint, 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), @@ -353,6 +351,7 @@ impl PyCairoRunner { } #[allow(clippy::too_many_arguments)] + #[allow(clippy::unused_variables)] pub fn run_from_entrypoint( &mut self, py: Python, @@ -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,8 +686,9 @@ impl PyExecutionResources { #[cfg(test)] mod test { use super::*; + use crate::bigint; use crate::relocatable::PyMaybeRelocatable::RelocatableValue; - use cairo_rs::bigint; + use felt::{Felt, NewFelt}; use num_bigint::BigInt; use std::env::temp_dir; use std::fs; @@ -1682,7 +1661,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)); }); } @@ -1706,11 +1685,7 @@ mod test { (*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(),]), ) } @@ -1735,7 +1710,7 @@ mod test { (*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 +1871,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(1), + &Felt::new(1), ); assert_eq!( vm_ref @@ -1905,7 +1880,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(2), + &Felt::new(2), ); let relocatable = vm_ref @@ -1923,7 +1898,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(3), + &Felt::new(3), ); assert_eq!( vm_ref @@ -1932,7 +1907,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(4), + &Felt::new(4), ); assert!(vm_ref.get_maybe(&(&relocatable + 2)).unwrap().is_none()); @@ -1951,7 +1926,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(5), + &Felt::new(5), ); assert_eq!( vm_ref @@ -1960,7 +1935,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 +2049,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 +2072,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 +2088,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 +2124,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"); }); } diff --git a/src/ecdsa.rs b/src/ecdsa.rs index b7a77b8a..05c73b00 100644 --- a/src/ecdsa.rs +++ b/src/ecdsa.rs @@ -5,6 +5,7 @@ use cairo_rs::{ vm::{errors::vm_errors::VirtualMachineError, runners::builtin_runner::SignatureBuiltinRunner}, }; +use felt::Felt; use num_bigint::BigInt; use pyo3::prelude::*; @@ -13,7 +14,7 @@ use crate::relocatable::PyRelocatable; #[pyclass(name = "Signature")] #[derive(Clone, Debug, PartialEq, Eq)] pub struct PySignature { - signatures: HashMap, + signatures: HashMap, } #[pymethods] @@ -26,7 +27,8 @@ impl PySignature { } pub fn add_signature(&mut self, address: PyRelocatable, pair: (BigInt, BigInt)) { - self.signatures.insert(address, pair); + self.signatures + .insert(address, (pair.0.into(), pair.1.into())); } } diff --git a/src/ids.rs b/src/ids.rs index 90b711c1..8acad635 100644 --- a/src/ids.rs +++ b/src/ids.rs @@ -1,4 +1,5 @@ use crate::utils::const_path_to_const_name; +use felt::{Felt, FeltOps}; use num_bigint::BigInt; use pyo3::exceptions::PyValueError; use std::{ @@ -7,7 +8,6 @@ use std::{ rc::Rc, }; -use cairo_rs::serde::deserialize_program::OffsetValue; use cairo_rs::{ hint_processor::{ hint_processor_definition::HintReference, @@ -15,8 +15,9 @@ use cairo_rs::{ }, serde::deserialize_program::{ApTracking, Member}, types::relocatable::Relocatable, - vm::{errors::vm_errors::VirtualMachineError, vm_core::VirtualMachine}, + vm::vm_core::VirtualMachine, }; +use cairo_rs::{serde::deserialize_program::OffsetValue, vm::errors::hint_errors::HintError}; use pyo3::{ exceptions::PyAttributeError, pyclass, pymethods, IntoPy, PyObject, PyResult, Python, ToPyObject, @@ -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_bigint())); } //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) } @@ -274,11 +275,8 @@ pub fn compute_addr_from_reference( #[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_rs::types::{instruction::Register, relocatable::MaybeRelocatable}; + use felt::NewFelt; 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/memory.rs b/src/memory.rs index 3006cef1..064e6d29 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -7,12 +7,13 @@ use cairo_rs::{ types::relocatable::{MaybeRelocatable, Relocatable}, vm::vm_core::VirtualMachine, }; +use felt::FeltOps; use num_bigint::BigInt; 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"; @@ -93,31 +94,27 @@ impl PyMemory { .get_integer_range(&Relocatable::from(&addr), size) .map_err(to_py_error)? .into_iter() - .map(Cow::into_owned) + .map(|num| num.into_owned().to_bigint()) .collect()) } } #[cfg(test)] mod test { + use crate::bigint; 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 num_bigint::BigInt; 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,7 +215,7 @@ 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() @@ -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,7 +267,7 @@ 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() @@ -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"); @@ -350,11 +322,7 @@ assert memory[ap] == fp // 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 +331,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 +352,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 +361,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..8792998f 100644 --- a/src/memory_segments.rs +++ b/src/memory_segments.rs @@ -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_rs::types::relocatable::{MaybeRelocatable, Relocatable}; + use felt::{Felt, NewFelt}; 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 @@ -180,7 +164,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(3), + &Felt::new(3), ); assert_eq!( vm_ref @@ -189,7 +173,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(4), + &Felt::new(4), ); assert!(vm_ref.get_maybe(&(&relocatable + 2)).unwrap().is_none()); @@ -208,7 +192,7 @@ mod test { .unwrap() .get_int_ref() .unwrap(), - &bigint!(5), + &Felt::new(5), ); assert_eq!( vm_ref @@ -217,7 +201,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 +214,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 +222,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 +233,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..67f09617 100644 --- a/src/range_check.rs +++ b/src/range_check.rs @@ -2,6 +2,7 @@ use cairo_rs::vm::{ errors::vm_errors::VirtualMachineError, runners::builtin_runner::RangeCheckBuiltinRunner, }; +use felt::FeltOps; use num_bigint::BigInt; use pyo3::prelude::*; @@ -9,18 +10,21 @@ use pyo3::prelude::*; #[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_bigint()), } } } @@ -49,14 +53,12 @@ impl ToPyObject for PyRangeCheck { #[cfg(test)] mod test { + use crate::bigint; + use super::PyRangeCheck; use super::*; - use cairo_rs::{ - bigint, - vm::{ - errors::vm_errors::VirtualMachineError, - runners::builtin_runner::RangeCheckBuiltinRunner, - }, + use cairo_rs::vm::{ + errors::vm_errors::VirtualMachineError, runners::builtin_runner::RangeCheckBuiltinRunner, }; use num_bigint::BigInt; use pyo3::ToPyObject; @@ -64,15 +66,15 @@ mod test { #[test] fn py_range_check_new() { let value = bigint!(12); - let new_py_range_check = PyRangeCheck::new(value.clone()); + 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 new_py_range_check = PyRangeCheck::new(Some(value)); assert_eq!(new_py_range_check.__repr__(), String::from("Bound: 12")); } @@ -87,7 +89,7 @@ mod test { assert_eq!( PyRangeCheck::from(result_with_range_check_builtin), - PyRangeCheck::new(bound) + PyRangeCheck::new(Some(bound)) ); } @@ -98,7 +100,7 @@ mod test { assert_eq!( PyRangeCheck::from(result_with_range_check_builtin), - PyRangeCheck::new(BigInt::from(0)) + PyRangeCheck::new(None) ); } @@ -110,14 +112,14 @@ mod test { 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 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..100d0d6a 100644 --- a/src/relocatable.rs +++ b/src/relocatable.rs @@ -1,13 +1,15 @@ use cairo_rs::{ - bigint, - hint_processor::hint_processor_utils::bigint_to_usize, types::relocatable::{MaybeRelocatable, Relocatable}, vm::errors::vm_errors::VirtualMachineError, }; +use felt::FeltOps; use num_bigint::BigInt; use pyo3::{exceptions::PyArithmeticError, prelude::*, pyclass::CompareOp}; -use crate::utils::to_py_error; +use crate::{ + bigint, + utils::{bigint_to_usize, to_py_error}, +}; const PYRELOCATABLE_COMPARE_ERROR: &str = "Cannot compare Relocatables of different segments"; @@ -111,7 +113,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 +124,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 +135,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_bigint()), } } } @@ -144,7 +146,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_bigint()), } } } @@ -202,11 +204,12 @@ impl From for PyMaybeRelocatable { #[cfg(test)] mod test { - use cairo_rs::{bigint, types::relocatable::MaybeRelocatable}; - use num_bigint::BigInt; + use crate::relocatable::BigInt; + use cairo_rs::types::relocatable::MaybeRelocatable; use pyo3::ToPyObject; use pyo3::{pyclass::CompareOp, Python}; + use crate::bigint; use crate::relocatable::Relocatable; use crate::relocatable::{PyMaybeRelocatable, PyRelocatable}; @@ -362,7 +365,7 @@ mod test { assert_eq!( MaybeRelocatable::from(py_maybe_relocatable_int), - MaybeRelocatable::Int(bigint!(1)) + MaybeRelocatable::from(1) ); assert_eq!( MaybeRelocatable::from(py_maybe_relocatable_relocatable), @@ -378,7 +381,7 @@ mod test { 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 +391,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, @@ -410,7 +413,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, diff --git a/src/utils.rs b/src/utils.rs index bc273173..b83a50d6 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,5 @@ +use cairo_rs::vm::errors::vm_errors::VirtualMachineError; +use felt::{Felt, FeltOps}; use num_bigint::BigInt; 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! bigint { + ($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_bigint().clone()) }) .collect() } + +//Tries to convert a BigInt value to usize +pub fn bigint_to_usize(bigint: &BigInt) -> Result { + bigint + .try_into() + .map_err(|_| VirtualMachineError::BigintToUsizeFail) +} diff --git a/src/vm_core.rs b/src/vm_core.rs index 5385983d..225f8762 100644 --- a/src/vm_core.rs +++ b/src/vm_core.rs @@ -11,13 +11,16 @@ use crate::{ }; 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::serde::deserialize_program::Member; use cairo_rs::types::exec_scope::ExecutionScopes; +use cairo_rs::vm::errors::hint_errors::HintError; use cairo_rs::vm::vm_core::VirtualMachine; use cairo_rs::{ hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData, vm::errors::vm_errors::VirtualMachineError, }; +use felt::{Felt, FIELD}; +use lazy_static::lazy_static; use num_bigint::BigInt; use pyo3::{pyclass, pymethods, PyObject, ToPyObject}; use pyo3::{types::PyDict, Python}; @@ -47,6 +50,11 @@ const GLOBAL_NAMES: [&str; 18] = [ "__name__", ]; +lazy_static! { + pub static ref CAIRO_PRIME: BigInt = + (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, } } @@ -88,7 +88,7 @@ impl PyVM { 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: BigInt = 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,21 @@ 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(VirtualMachineError::Hint( + hint_index, + Box::new(HintError::WrongHintData), + )) .map_err(to_py_error)?; if let Err(hint_error) = self.execute_hint( @@ -214,7 +223,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 +239,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 +294,8 @@ pub(crate) fn update_scope_hint_locals( #[cfg(test)] mod test { - use crate::{relocatable::PyMaybeRelocatable, vm_core::PyVM}; + use crate::{bigint, relocatable::PyMaybeRelocatable, vm_core::PyVM}; use cairo_rs::{ - bigint, hint_processor::{ builtin_hint_processor::builtin_hint_processor_definition::{ BuiltinHintProcessor, HintProcessorData, @@ -295,17 +307,14 @@ mod test { relocatable::{MaybeRelocatable, Relocatable}, }, }; - use num_bigint::{BigInt, Sign}; + use felt::{Felt, NewFelt}; + use num_bigint::BigInt; 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 mut vm = PyVM::new(false); let code = "print(ap)"; let hint_data = HintProcessorData::new_default(code.to_string(), HashMap::new()); assert!(vm @@ -322,11 +331,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 mut vm = PyVM::new(false); let code = "print(ap)"; let hint_data = HintProcessorData::new_default(code.to_string(), HashMap::new()); assert!(vm @@ -343,11 +348,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 mut vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -357,10 +358,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 +374,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 mut 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 +418,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,7 +432,7 @@ 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() @@ -468,11 +458,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,7 +472,7 @@ 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() @@ -518,11 +504,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 mut vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -557,11 +539,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 mut vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -619,11 +597,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 mut vm = PyVM::new(false); let code = "word = word[::-1] print(word)"; let hint_data = HintProcessorData::new_default(code.to_string(), HashMap::new()); @@ -651,11 +625,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 mut 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 +642,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 mut 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 +661,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 mut vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "vm_enter_scope() vm_exit_scope()"; @@ -719,11 +681,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 mut vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code_a = "vm_enter_scope()"; let code_b = "vm_exit_scope()"; @@ -755,11 +713,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 mut vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "vm_enter_scope() vm_exit_scope() @@ -780,11 +734,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 mut 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 +753,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 mut 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 +785,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 mut 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 +803,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 mut 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()); @@ -895,11 +833,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[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 mut 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 +851,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 mut 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 +888,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 mut pyvm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "assert(memory.get_range(ids.address, 3) == [1,2,7])"; @@ -979,17 +905,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 +933,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 mut 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 +949,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 +972,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 mut vm = PyVM::new(false); let static_locals = HashMap::from([( "__number_max".to_string(), Python::with_gil(|py| -> PyObject { 90.to_object(py) }), @@ -1086,11 +1004,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 mut vm = PyVM::new(false); let static_locals = HashMap::from([( "__number_max".to_string(), Python::with_gil(|py| -> PyObject { 90.to_object(py) }), @@ -1120,11 +1034,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 mut vm = PyVM::new(false); let static_locals = HashMap::from([( "__number_max".to_string(), Python::with_gil(|py| -> PyObject { 90.to_object(py) }), @@ -1149,11 +1059,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 +1068,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()); From 0693b02b5fd5bb00b02872dd54e2e98a4ae80300 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 6 Jan 2023 18:43:20 -0300 Subject: [PATCH 02/12] Clippy(most) --- src/cairo_runner.rs | 20 ++++++++------------ src/memory.rs | 8 ++++---- src/memory_segments.rs | 6 ++---- src/utils.rs | 2 +- src/vm_core.rs | 8 ++++---- 5 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/cairo_runner.rs b/src/cairo_runner.rs index ccd93343..1cb2cf02 100644 --- a/src/cairo_runner.rs +++ b/src/cairo_runner.rs @@ -723,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 @@ -763,7 +761,7 @@ mod test { #[pymethods] impl TypeFelt { fn __repr__(&self) -> String { - format!("TypeFelt") + "TypeFelt".to_string() } } @@ -775,7 +773,7 @@ mod test { #[pymethods] impl TypePointer { fn __repr__(&self) -> String { - format!("TypePointer") + "TypePointer".to_string() } } @@ -787,7 +785,7 @@ mod test { #[pymethods] impl TypeStruct { fn __repr__(&self) -> String { - format!("TypeStruct") + "TypeStruct".to_string() } } } @@ -1421,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( @@ -1439,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 @@ -1888,8 +1886,7 @@ mod test { .unwrap() .unwrap() .get_relocatable() - .unwrap() - .clone(); + .unwrap(); assert_eq!( vm_ref @@ -1916,8 +1913,7 @@ mod test { .unwrap() .unwrap() .get_relocatable() - .unwrap() - .clone(); + .unwrap(); assert_eq!( vm_ref diff --git a/src/memory.rs b/src/memory.rs index 064e6d29..5ef28266 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -219,11 +219,11 @@ assert memory[ap] == fp .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)); @@ -271,11 +271,11 @@ assert memory[ap] == fp .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)); diff --git a/src/memory_segments.rs b/src/memory_segments.rs index 8792998f..4674f504 100644 --- a/src/memory_segments.rs +++ b/src/memory_segments.rs @@ -154,8 +154,7 @@ mod test { .unwrap() .unwrap() .get_relocatable() - .unwrap() - .clone(); + .unwrap(); assert_eq!( vm_ref @@ -182,8 +181,7 @@ mod test { .unwrap() .unwrap() .get_relocatable() - .unwrap() - .clone(); + .unwrap(); assert_eq!( vm_ref diff --git a/src/utils.rs b/src/utils.rs index b83a50d6..4942f048 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -26,7 +26,7 @@ pub fn const_path_to_const_name(constants: &HashMap) -> HashMap Date: Tue, 17 Jan 2023 15:16:36 -0300 Subject: [PATCH 03/12] Clippy --- src/vm_core.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/vm_core.rs b/src/vm_core.rs index 49441621..bf595d72 100644 --- a/src/vm_core.rs +++ b/src/vm_core.rs @@ -191,10 +191,12 @@ impl PyVM { { let hint_data = hint_data .downcast_ref::() - .ok_or(VirtualMachineError::Hint( - hint_index, - Box::new(HintError::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( From 99f4d65e5d44e982b6ad88e1af0024a4191c70e5 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 17 Jan 2023 15:20:38 -0300 Subject: [PATCH 04/12] Fix allows --- src/cairo_runner.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cairo_runner.rs b/src/cairo_runner.rs index 1cb2cf02..af5b09bd 100644 --- a/src/cairo_runner.rs +++ b/src/cairo_runner.rs @@ -350,8 +350,8 @@ impl PyCairoRunner { .to_object(py)) } + #[allow(dead_code, unused)] #[allow(clippy::too_many_arguments)] - #[allow(clippy::unused_variables)] pub fn run_from_entrypoint( &mut self, py: Python, From 8b518080783c0e07a2753bd8115f11b008e97950 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 17 Jan 2023 15:21:19 -0300 Subject: [PATCH 05/12] Fix allows --- src/cairo_runner.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cairo_runner.rs b/src/cairo_runner.rs index af5b09bd..45deaf6b 100644 --- a/src/cairo_runner.rs +++ b/src/cairo_runner.rs @@ -350,7 +350,7 @@ impl PyCairoRunner { .to_object(py)) } - #[allow(dead_code, unused)] + #[allow(unused)] #[allow(clippy::too_many_arguments)] pub fn run_from_entrypoint( &mut self, From f758d145dc1dce609ceff7716add12fa47b79ddf Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 17 Jan 2023 16:26:09 -0300 Subject: [PATCH 06/12] Update versions and crate names to main --- Cargo.lock | 56 +++++++++++++++++++++++++++--------------------------- Cargo.toml | 4 ++-- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b2362ab5..da6c475f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,13 +130,37 @@ 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=2a687c0795a99ce353d186c305c00feccba73bee#2a687c0795a99ce353d186c305c00feccba73bee" +source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=9321055390512cd2e668a82fa34619c20baee9d7#9321055390512cd2e668a82fa34619c20baee9d7" +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=9321055390512cd2e668a82fa34619c20baee9d7#9321055390512cd2e668a82fa34619c20baee9d7" dependencies = [ "bincode", + "cairo-felt", "clap", - "felt", "generic-array", "hex", "keccak", @@ -157,18 +181,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "cairo-rs-py" -version = "0.1.0" -dependencies = [ - "cairo-rs", - "felt", - "lazy_static", - "num-bigint", - "pyo3", - "rusty-hook", -] - [[package]] name = "cc" version = "1.0.77" @@ -310,18 +322,6 @@ dependencies = [ "indexmap", ] -[[package]] -name = "felt" -version = "0.1.0" -source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=2a687c0795a99ce353d186c305c00feccba73bee#2a687c0795a99ce353d186c305c00feccba73bee" -dependencies = [ - "lazy_static", - "num-bigint", - "num-integer", - "num-traits", - "serde", -] - [[package]] name = "fsio" version = "0.1.3" @@ -590,7 +590,7 @@ dependencies = [ [[package]] name = "parse-hyperlinks" version = "0.23.4" -source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=2a687c0795a99ce353d186c305c00feccba73bee#2a687c0795a99ce353d186c305c00feccba73bee" +source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=9321055390512cd2e668a82fa34619c20baee9d7#9321055390512cd2e668a82fa34619c20baee9d7" dependencies = [ "nom", ] diff --git a/Cargo.toml b/Cargo.toml index d890010d..cb2a31c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,8 @@ edition = "2021" [dependencies] pyo3 = { version = "0.16.5", features = ["num-bigint"] } -cairo-rs = { git = "https://github.com/lambdaclass/cairo-rs.git", rev = "2a687c0795a99ce353d186c305c00feccba73bee" } -felt = { git = "https://github.com/lambdaclass/cairo-rs.git", rev = "2a687c0795a99ce353d186c305c00feccba73bee" } +cairo-vm = { git = "https://github.com/lambdaclass/cairo-rs.git", rev = "9321055390512cd2e668a82fa34619c20baee9d7" } +cairo-felt = { git = "https://github.com/lambdaclass/cairo-rs.git", rev = "9321055390512cd2e668a82fa34619c20baee9d7" } num-bigint = "0.4" lazy_static = "1.4.0" From 8cd337a4609bdfae2206846f203a7e6c2dbf25b6 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 17 Jan 2023 16:32:21 -0300 Subject: [PATCH 07/12] Update crate names --- src/cairo_runner.rs | 8 ++++---- src/ecdsa.rs | 4 ++-- src/ids.rs | 14 +++++++------- src/instruction_location.rs | 2 +- src/memory.rs | 6 +++--- src/memory_segments.rs | 6 +++--- src/range_check.rs | 6 +++--- src/relocatable.rs | 6 +++--- src/run_context.rs | 2 +- src/scope_manager.rs | 2 +- src/utils.rs | 4 ++-- src/vm_core.rs | 20 ++++++++++---------- 12 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/cairo_runner.rs b/src/cairo_runner.rs index 45deaf6b..f9abf76e 100644 --- a/src/cairo_runner.rs +++ b/src/cairo_runner.rs @@ -6,7 +6,7 @@ use crate::{ utils::to_py_error, vm_core::PyVM, }; -use cairo_rs::{ +use cairo_vm::{ cairo_run::write_output, hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, serde::deserialize_program::Member, @@ -146,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) @@ -156,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)))?; } @@ -688,7 +688,7 @@ mod test { use super::*; use crate::bigint; use crate::relocatable::PyMaybeRelocatable::RelocatableValue; - use felt::{Felt, NewFelt}; + use cairo_felt::{Felt, NewFelt}; use num_bigint::BigInt; use std::env::temp_dir; use std::fs; diff --git a/src/ecdsa.rs b/src/ecdsa.rs index 05c73b00..e57d0b45 100644 --- a/src/ecdsa.rs +++ b/src/ecdsa.rs @@ -1,11 +1,11 @@ use std::collections::HashMap; -use cairo_rs::{ +use cairo_vm::{ types::relocatable::Relocatable, vm::{errors::vm_errors::VirtualMachineError, runners::builtin_runner::SignatureBuiltinRunner}, }; -use felt::Felt; +use cairo_felt::Felt; use num_bigint::BigInt; use pyo3::prelude::*; diff --git a/src/ids.rs b/src/ids.rs index 8acad635..b363a0c5 100644 --- a/src/ids.rs +++ b/src/ids.rs @@ -1,5 +1,5 @@ use crate::utils::const_path_to_const_name; -use felt::{Felt, FeltOps}; +use cairo_felt::{Felt, FeltOps}; use num_bigint::BigInt; use pyo3::exceptions::PyValueError; use std::{ @@ -8,16 +8,16 @@ use std::{ rc::Rc, }; -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::vm_core::VirtualMachine, }; -use cairo_rs::{serde::deserialize_program::OffsetValue, vm::errors::hint_errors::HintError}; +use cairo_vm::{serde::deserialize_program::OffsetValue, vm::errors::hint_errors::HintError}; use pyo3::{ exceptions::PyAttributeError, pyclass, pymethods, IntoPy, PyObject, PyResult, Python, ToPyObject, @@ -269,14 +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::types::{instruction::Register, relocatable::MaybeRelocatable}; - use felt::NewFelt; + use cairo_felt::NewFelt; + use cairo_vm::types::{instruction::Register, relocatable::MaybeRelocatable}; use pyo3::{types::PyDict, PyCell}; use super::*; 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 5ef28266..bd00543b 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -3,11 +3,11 @@ 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 felt::FeltOps; use num_bigint::BigInt; use pyo3::{ exceptions::{PyTypeError, PyValueError}, @@ -106,7 +106,7 @@ mod test { use crate::relocatable::PyMaybeRelocatable::RelocatableValue; use crate::vm_core::PyVM; use crate::{memory::PyMemory, relocatable::PyRelocatable}; - use cairo_rs::types::relocatable::{MaybeRelocatable, Relocatable}; + use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable}; use num_bigint::BigInt; use pyo3::PyCell; use pyo3::{types::PyDict, Python}; diff --git a/src/memory_segments.rs b/src/memory_segments.rs index 4674f504..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}; @@ -98,8 +98,8 @@ impl PySegmentManager { mod test { use super::PySegmentManager; use crate::{memory::PyMemory, relocatable::PyMaybeRelocatable, vm_core::PyVM}; - use cairo_rs::types::relocatable::{MaybeRelocatable, Relocatable}; - use felt::{Felt, NewFelt}; + use cairo_felt::{Felt, NewFelt}; + use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable}; use pyo3::{Python, ToPyObject}; #[test] diff --git a/src/range_check.rs b/src/range_check.rs index 67f09617..756f7320 100644 --- a/src/range_check.rs +++ b/src/range_check.rs @@ -1,8 +1,8 @@ -use cairo_rs::vm::{ +use cairo_vm::vm::{ errors::vm_errors::VirtualMachineError, runners::builtin_runner::RangeCheckBuiltinRunner, }; -use felt::FeltOps; +use cairo_felt::FeltOps; use num_bigint::BigInt; use pyo3::prelude::*; @@ -57,7 +57,7 @@ mod test { use super::PyRangeCheck; use super::*; - use cairo_rs::vm::{ + use cairo_vm::vm::{ errors::vm_errors::VirtualMachineError, runners::builtin_runner::RangeCheckBuiltinRunner, }; use num_bigint::BigInt; diff --git a/src/relocatable.rs b/src/relocatable.rs index 100d0d6a..0d12bb57 100644 --- a/src/relocatable.rs +++ b/src/relocatable.rs @@ -1,8 +1,8 @@ -use cairo_rs::{ +use cairo_felt::FeltOps; +use cairo_vm::{ types::relocatable::{MaybeRelocatable, Relocatable}, vm::errors::vm_errors::VirtualMachineError, }; -use felt::FeltOps; use num_bigint::BigInt; use pyo3::{exceptions::PyArithmeticError, prelude::*, pyclass::CompareOp}; @@ -205,7 +205,7 @@ impl From for PyMaybeRelocatable { #[cfg(test)] mod test { use crate::relocatable::BigInt; - use cairo_rs::types::relocatable::MaybeRelocatable; + use cairo_vm::types::relocatable::MaybeRelocatable; use pyo3::ToPyObject; use pyo3::{pyclass::CompareOp, Python}; 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/utils.rs b/src/utils.rs index 4942f048..ef4c6238 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,5 @@ -use cairo_rs::vm::errors::vm_errors::VirtualMachineError; -use felt::{Felt, FeltOps}; +use cairo_felt::{Felt, FeltOps}; +use cairo_vm::vm::errors::vm_errors::VirtualMachineError; use num_bigint::BigInt; use pyo3::{exceptions::PyValueError, PyErr}; use std::{collections::HashMap, fmt::Display}; diff --git a/src/vm_core.rs b/src/vm_core.rs index bf595d72..cf29e864 100644 --- a/src/vm_core.rs +++ b/src/vm_core.rs @@ -9,17 +9,17 @@ 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::Member; -use cairo_rs::types::exec_scope::ExecutionScopes; -use cairo_rs::vm::errors::hint_errors::HintError; -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 felt::{Felt, FIELD}; use lazy_static::lazy_static; use num_bigint::BigInt; use pyo3::{pyclass, pymethods, PyObject, ToPyObject}; @@ -297,7 +297,8 @@ pub(crate) fn update_scope_hint_locals( #[cfg(test)] mod test { use crate::{bigint, relocatable::PyMaybeRelocatable, vm_core::PyVM}; - use cairo_rs::{ + use cairo_felt::{Felt, NewFelt}; + use cairo_vm::{ hint_processor::{ builtin_hint_processor::builtin_hint_processor_definition::{ BuiltinHintProcessor, HintProcessorData, @@ -309,7 +310,6 @@ mod test { relocatable::{MaybeRelocatable, Relocatable}, }, }; - use felt::{Felt, NewFelt}; use num_bigint::BigInt; use pyo3::{PyObject, Python, ToPyObject}; use std::{any::Any, collections::HashMap, rc::Rc}; From 9f7537da412687b4109ede080bb865581d89cf46 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 18 Jan 2023 13:49:46 -0300 Subject: [PATCH 08/12] Swap BigInt for BigUint --- src/cairo_runner.rs | 104 +++++++++++++++++----------------- src/ids.rs | 6 +- src/memory.rs | 17 ++++-- src/range_check.rs | 22 +++---- src/relocatable.rs | 64 ++++++++++----------- src/to_felt_or_relocatable.rs | 4 +- src/utils.rs | 16 +++--- src/vm_core.rs | 15 ++--- 8 files changed, 126 insertions(+), 122 deletions(-) diff --git a/src/cairo_runner.rs b/src/cairo_runner.rs index f9abf76e..5984dcfe 100644 --- a/src/cairo_runner.rs +++ b/src/cairo_runner.rs @@ -686,10 +686,10 @@ impl PyExecutionResources { #[cfg(test)] mod test { use super::*; - use crate::bigint; + use crate::biguint; use crate::relocatable::PyMaybeRelocatable::RelocatableValue; use cairo_felt::{Felt, NewFelt}; - use num_bigint::BigInt; + use num_bigint::BigUint; use std::env::temp_dir; use std::fs; @@ -1301,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(), ), @@ -1589,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], }; @@ -1630,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, @@ -1671,13 +1671,13 @@ 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()) @@ -1696,13 +1696,13 @@ 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()) @@ -2135,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(), ), @@ -2149,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)), ] ); }) @@ -2211,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); }); } @@ -2298,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(), ), @@ -2320,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)) @@ -2331,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); }); } @@ -2368,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)) @@ -2379,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); }); } @@ -2558,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/ids.rs b/src/ids.rs index b363a0c5..506b04a8 100644 --- a/src/ids.rs +++ b/src/ids.rs @@ -1,6 +1,6 @@ use crate::utils::const_path_to_const_name; use cairo_felt::{Felt, FeltOps}; -use num_bigint::BigInt; +use num_bigint::BigUint; use pyo3::exceptions::PyValueError; use std::{ cell::RefCell, @@ -34,7 +34,7 @@ pub struct PyIds { vm: Rc>, references: HashMap, ap_tracking: ApTracking, - constants: HashMap, + constants: HashMap, struct_types: Rc>>, } @@ -245,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.to_bigint())); + return Ok(PyMaybeRelocatable::from(num.to_biguint())); } //Then calculate address let var_addr = compute_addr_from_reference(hint_reference, vm, ap_tracking)?; diff --git a/src/memory.rs b/src/memory.rs index bd00543b..2fc5ae77 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -8,7 +8,7 @@ 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::*, @@ -87,27 +87,27 @@ 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(|num| num.into_owned().to_bigint()) + .map(|num| num.into_owned().to_biguint()) .collect()) } } #[cfg(test)] mod test { - use crate::bigint; + 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_vm::types::relocatable::{MaybeRelocatable, Relocatable}; - use num_bigint::BigInt; + use num_bigint::BigUint; use pyo3::PyCell; use pyo3::{types::PyDict, Python}; @@ -315,7 +315,12 @@ 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) + ], ); } diff --git a/src/range_check.rs b/src/range_check.rs index 756f7320..c3a648a7 100644 --- a/src/range_check.rs +++ b/src/range_check.rs @@ -3,20 +3,20 @@ use cairo_vm::vm::{ }; use cairo_felt::FeltOps; -use num_bigint::BigInt; +use num_bigint::BigUint; use pyo3::prelude::*; #[pyclass(name = "RangeCheck")] #[derive(Clone, Debug, PartialEq, Eq)] pub struct PyRangeCheck { #[pyo3(get)] - bound: Option, + bound: Option, } #[pymethods] impl PyRangeCheck { #[new] - pub fn new(value: Option) -> Self { + pub fn new(value: Option) -> Self { Self { bound: value } } @@ -40,7 +40,7 @@ impl From> for PyRangeChec impl From<&RangeCheckBuiltinRunner> for PyRangeCheck { fn from(val: &RangeCheckBuiltinRunner) -> Self { Self { - bound: val._bound.as_ref().map(|num| num.to_bigint()), + bound: val._bound.as_ref().map(|num| num.to_biguint()), } } } @@ -53,19 +53,19 @@ impl ToPyObject for PyRangeCheck { #[cfg(test)] mod test { - use crate::bigint; + use crate::biguint; + use num_bigint::BigUint; use super::PyRangeCheck; use super::*; 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 value = biguint!(12_u32); let new_py_range_check = PyRangeCheck::new(Some(value.clone())); assert_eq!(new_py_range_check, PyRangeCheck { bound: Some(value) }); @@ -73,7 +73,7 @@ mod test { #[test] fn py_range_check_repr() { - let value = bigint!(12); + 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")); @@ -82,7 +82,7 @@ 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); @@ -107,7 +107,7 @@ mod test { #[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!( @@ -118,7 +118,7 @@ mod test { #[test] fn py_range_check_to_py_object() { - let value = bigint!(12); + let value = biguint!(12_u32); let new_py_range_check = PyRangeCheck::new(Some(value.clone())); Python::with_gil(|py| { diff --git a/src/relocatable.rs b/src/relocatable.rs index 0d12bb57..f110e5ad 100644 --- a/src/relocatable.rs +++ b/src/relocatable.rs @@ -1,21 +1,20 @@ +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::{ - bigint, - utils::{bigint_to_usize, 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), } @@ -50,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)) } @@ -58,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)) } } } @@ -135,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.to_bigint()), + MaybeRelocatable::Int(num) => PyMaybeRelocatable::Int(num.to_biguint()), } } } @@ -146,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.to_bigint()), + MaybeRelocatable::Int(num) => PyMaybeRelocatable::Int(num.to_biguint()), } } } @@ -190,29 +189,28 @@ 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 crate::relocatable::BigInt; + 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}; - use crate::bigint; - use crate::relocatable::Relocatable; - use crate::relocatable::{PyMaybeRelocatable, PyRelocatable}; - #[test] fn py_relocatable_new() { let values = (1, 2); @@ -255,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() @@ -289,7 +287,7 @@ mod test { .extract::(py) .unwrap(); - assert_eq!(substraction, PyMaybeRelocatable::Int(bigint!(1))); + assert_eq!(substraction, PyMaybeRelocatable::Int(biguint!(1_u32))); }); } @@ -359,7 +357,7 @@ 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); @@ -375,7 +373,7 @@ 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); @@ -399,7 +397,7 @@ mod test { assert_eq!( PyMaybeRelocatable::from(maybe_relocatable_int), - PyMaybeRelocatable::Int(bigint!(1)) + PyMaybeRelocatable::Int(biguint!(1_u32)) ); assert_eq!( @@ -421,7 +419,7 @@ mod test { assert_eq!( PyMaybeRelocatable::from(&maybe_relocatable_int), - PyMaybeRelocatable::Int(bigint!(1)) + PyMaybeRelocatable::Int(biguint!(1_u32)) ); assert_eq!( @@ -511,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()), @@ -521,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), @@ -532,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, @@ -544,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/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 ef4c6238..70e8cc41 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,6 @@ use cairo_felt::{Felt, FeltOps}; use cairo_vm::vm::errors::vm_errors::VirtualMachineError; -use num_bigint::BigInt; +use num_bigint::BigUint; use pyo3::{exceptions::PyValueError, PyErr}; use std::{collections::HashMap, fmt::Display}; @@ -15,25 +15,25 @@ pub fn to_py_error(error: T) -> PyErr { } #[macro_export] -macro_rules! bigint { +macro_rules! biguint { ($val : expr) => { - Into::::into($val) + Into::::into($val) }; } -pub fn const_path_to_const_name(constants: &HashMap) -> HashMap { +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.to_bigint()) + (name.to_string(), value.to_biguint()) }) .collect() } -//Tries to convert a BigInt value to usize -pub fn bigint_to_usize(bigint: &BigInt) -> Result { - bigint +//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 cf29e864..d025a3ba 100644 --- a/src/vm_core.rs +++ b/src/vm_core.rs @@ -21,7 +21,7 @@ use cairo_vm::{ vm::errors::vm_errors::VirtualMachineError, }; use lazy_static::lazy_static; -use num_bigint::BigInt; +use num_bigint::BigUint; use pyo3::{pyclass, pymethods, PyObject, ToPyObject}; use pyo3::{types::PyDict, Python}; use pyo3::{PyCell, PyErr}; @@ -51,8 +51,8 @@ const GLOBAL_NAMES: [&str; 18] = [ ]; lazy_static! { - pub static ref CAIRO_PRIME: BigInt = - (Into::::into(FIELD.0) << 128) + Into::::into(FIELD.1); + pub static ref CAIRO_PRIME: BigUint = + (Into::::into(FIELD.0) << 128) + Into::::into(FIELD.1); } #[derive(Clone)] @@ -93,6 +93,7 @@ impl PyVM { static_locals: Option<&HashMap>, ) -> Result<(), PyErr> { Python::with_gil(|py| -> Result<(), PyErr> { + println!("Excecuting Hint: {}", hint_data.code); let memory = PyMemory::new(self); let segments = PySegmentManager::new(self, memory.clone()); let ap = PyRelocatable::from((*self.vm).borrow().get_ap()); @@ -109,7 +110,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: BigInt = CAIRO_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 @@ -296,7 +297,8 @@ pub(crate) fn update_scope_hint_locals( #[cfg(test)] mod test { - use crate::{bigint, relocatable::PyMaybeRelocatable, vm_core::PyVM}; + use super::*; + use crate::{biguint, relocatable::PyMaybeRelocatable, vm_core::PyVM}; use cairo_felt::{Felt, NewFelt}; use cairo_vm::{ hint_processor::{ @@ -310,7 +312,6 @@ mod test { relocatable::{MaybeRelocatable, Relocatable}, }, }; - use num_bigint::BigInt; use pyo3::{PyObject, Python, ToPyObject}; use std::{any::Any, collections::HashMap, rc::Rc}; @@ -828,7 +829,7 @@ lista_b = [lista_a[k] for k in range(2)]"; .unwrap() .extract::(py) .unwrap(), - PyMaybeRelocatable::from(bigint!(456)) + PyMaybeRelocatable::from(biguint!(456_u32)) ); }); } From db213a205873745a6afbba9d1bfa837a7aa53261 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 18 Jan 2023 13:53:28 -0300 Subject: [PATCH 09/12] Remove debug print --- src/vm_core.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/vm_core.rs b/src/vm_core.rs index d025a3ba..bd6337e6 100644 --- a/src/vm_core.rs +++ b/src/vm_core.rs @@ -93,7 +93,6 @@ impl PyVM { static_locals: Option<&HashMap>, ) -> Result<(), PyErr> { Python::with_gil(|py| -> Result<(), PyErr> { - println!("Excecuting Hint: {}", hint_data.code); let memory = PyMemory::new(self); let segments = PySegmentManager::new(self, memory.clone()); let ap = PyRelocatable::from((*self.vm).borrow().get_ap()); From d3127c958a03644389c160416ddceaa2dbd3d471 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 18 Jan 2023 14:01:21 -0300 Subject: [PATCH 10/12] Remove BigUint from ecdsa.rs --- src/ecdsa.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/ecdsa.rs b/src/ecdsa.rs index e57d0b45..fd6ec28b 100644 --- a/src/ecdsa.rs +++ b/src/ecdsa.rs @@ -1,3 +1,4 @@ +use num_bigint::BigUint; use std::collections::HashMap; use cairo_vm::{ @@ -6,7 +7,6 @@ use cairo_vm::{ }; use cairo_felt::Felt; -use num_bigint::BigInt; use pyo3::prelude::*; use crate::relocatable::PyRelocatable; @@ -26,7 +26,7 @@ impl PySignature { } } - pub fn add_signature(&mut self, address: PyRelocatable, pair: (BigInt, BigInt)) { + pub fn add_signature(&mut self, address: PyRelocatable, pair: (BigUint, BigUint)) { self.signatures .insert(address, (pair.0.into(), pair.1.into())); } @@ -61,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; @@ -81,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(); @@ -98,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(); @@ -135,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(); From 6c0aad6ae8011950dd477a4515624b7ddd2f17af Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 18 Jan 2023 14:58:14 -0300 Subject: [PATCH 11/12] Change cairo-rs rev --- Cargo.lock | 6 +++--- Cargo.toml | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index da6c475f..fbb33a5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -132,7 +132,7 @@ checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" [[package]] name = "cairo-felt" version = "0.1.0" -source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=9321055390512cd2e668a82fa34619c20baee9d7#9321055390512cd2e668a82fa34619c20baee9d7" +source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=7f8c2fb5ddebfdfe557c79f0072483f550e9e0e3#7f8c2fb5ddebfdfe557c79f0072483f550e9e0e3" dependencies = [ "lazy_static", "num-bigint", @@ -156,7 +156,7 @@ dependencies = [ [[package]] name = "cairo-vm" version = "0.1.1" -source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=9321055390512cd2e668a82fa34619c20baee9d7#9321055390512cd2e668a82fa34619c20baee9d7" +source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=7f8c2fb5ddebfdfe557c79f0072483f550e9e0e3#7f8c2fb5ddebfdfe557c79f0072483f550e9e0e3" dependencies = [ "bincode", "cairo-felt", @@ -590,7 +590,7 @@ dependencies = [ [[package]] name = "parse-hyperlinks" version = "0.23.4" -source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=9321055390512cd2e668a82fa34619c20baee9d7#9321055390512cd2e668a82fa34619c20baee9d7" +source = "git+https://github.com/lambdaclass/cairo-rs.git?rev=7f8c2fb5ddebfdfe557c79f0072483f550e9e0e3#7f8c2fb5ddebfdfe557c79f0072483f550e9e0e3" dependencies = [ "nom", ] diff --git a/Cargo.toml b/Cargo.toml index cb2a31c3..a560cf61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,8 @@ edition = "2021" [dependencies] pyo3 = { version = "0.16.5", features = ["num-bigint"] } -cairo-vm = { git = "https://github.com/lambdaclass/cairo-rs.git", rev = "9321055390512cd2e668a82fa34619c20baee9d7" } -cairo-felt = { git = "https://github.com/lambdaclass/cairo-rs.git", rev = "9321055390512cd2e668a82fa34619c20baee9d7" } +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" From cca7db3388edd05e771c0448625da38318928529 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 18 Jan 2023 15:15:59 -0300 Subject: [PATCH 12/12] Remove uneeded mut from execute_hint --- src/vm_core.rs | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/vm_core.rs b/src/vm_core.rs index bd6337e6..a267e5d1 100644 --- a/src/vm_core.rs +++ b/src/vm_core.rs @@ -84,7 +84,7 @@ impl PyVM { } pub(crate) fn execute_hint( - &mut self, + &self, hint_data: &HintProcessorData, hint_locals: &mut HashMap, exec_scopes: &mut ExecutionScopes, @@ -316,7 +316,7 @@ mod test { #[test] fn execute_print_hint() { - let mut vm = PyVM::new(false); + let vm = PyVM::new(false); let code = "print(ap)"; let hint_data = HintProcessorData::new_default(code.to_string(), HashMap::new()); assert!(vm @@ -333,7 +333,7 @@ mod test { #[test] fn set_memory_item_hint() { - let mut vm = PyVM::new(false); + let vm = PyVM::new(false); let code = "print(ap)"; let hint_data = HintProcessorData::new_default(code.to_string(), HashMap::new()); assert!(vm @@ -350,7 +350,7 @@ mod test { #[test] fn ids_hint() { - let mut vm = PyVM::new(false); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -383,7 +383,7 @@ mod test { #[test] // Test the availability of cairo constants in ids fn const_ids() { - let mut vm = PyVM::new(false); + let vm = PyVM::new(false); let constants = HashMap::from([(String::from("CONST"), Felt::new(1))]); @@ -506,7 +506,7 @@ mod test { #[test] fn scopes_hint() { - let mut vm = PyVM::new(false); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -541,7 +541,7 @@ mod test { #[test] fn scopes_hint_modify() { - let mut vm = PyVM::new(false); + let vm = PyVM::new(false); for _ in 0..2 { vm.vm.borrow_mut().add_memory_segment(); } @@ -599,7 +599,7 @@ mod test { #[test] fn modify_hint_locals() { - let mut vm = PyVM::new(false); + let vm = PyVM::new(false); let code = "word = word[::-1] print(word)"; let hint_data = HintProcessorData::new_default(code.to_string(), HashMap::new()); @@ -627,7 +627,7 @@ print(word)"; #[test] fn exit_main_scope_hint() { - let mut vm = PyVM::new(false); + 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()); @@ -644,7 +644,7 @@ print(word)"; #[test] fn enter_scope_empty_hint() { - let mut vm = PyVM::new(false); + 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()); @@ -663,7 +663,7 @@ print(word)"; #[test] fn enter_exit_scope_same_hint() { - let mut vm = PyVM::new(false); + let vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "vm_enter_scope() vm_exit_scope()"; @@ -683,7 +683,7 @@ vm_exit_scope()"; #[test] fn enter_exit_scope_separate_hints() { - let mut vm = PyVM::new(false); + let vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code_a = "vm_enter_scope()"; let code_b = "vm_exit_scope()"; @@ -715,7 +715,7 @@ vm_exit_scope()"; #[test] fn enter_exit_enter_scope_same_hint() { - let mut vm = PyVM::new(false); + let vm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "vm_enter_scope() vm_exit_scope() @@ -736,7 +736,7 @@ vm_enter_scope()"; #[test] fn list_comprehension() { - let mut vm = PyVM::new(false); + 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)]"; @@ -755,7 +755,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn enter_scope_non_empty_hint() { - let mut vm = PyVM::new(false); + 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)"; @@ -787,7 +787,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn access_relocatable_segment_index() { - let mut vm = PyVM::new(false); + 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()); @@ -805,7 +805,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn to_felt_or_relocatable_number() { - let mut vm = PyVM::new(false); + 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()); @@ -835,7 +835,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn to_felt_or_relocatable_list_should_fail() { - let mut vm = PyVM::new(false); + 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()); @@ -853,7 +853,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn to_felt_or_relocatable_relocatable() { - let mut vm = PyVM::new(false); + 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(); @@ -890,7 +890,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn test_get_range() { - let mut pyvm = PyVM::new(false); + let pyvm = PyVM::new(false); let mut exec_scopes = ExecutionScopes::new(); let code = "assert(memory.get_range(ids.address, 3) == [1,2,7])"; @@ -935,7 +935,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn test_segments_memory_get_range() { - let mut pyvm = PyVM::new(false); + 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))]); @@ -974,7 +974,7 @@ lista_b = [lista_a[k] for k in range(2)]"; #[test] fn run_hint_with_static_locals() { - let mut vm = PyVM::new(false); + let vm = PyVM::new(false); let static_locals = HashMap::from([( "__number_max".to_string(), Python::with_gil(|py| -> PyObject { 90.to_object(py) }), @@ -1006,7 +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(false); + let vm = PyVM::new(false); let static_locals = HashMap::from([( "__number_max".to_string(), Python::with_gil(|py| -> PyObject { 90.to_object(py) }), @@ -1036,7 +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(false); + let vm = PyVM::new(false); let static_locals = HashMap::from([( "__number_max".to_string(), Python::with_gil(|py| -> PyObject { 90.to_object(py) }),