From 5761e7a5b97ac99fd000368001fbbdeff167ab74 Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Fri, 3 May 2024 18:23:24 -0300 Subject: [PATCH 1/5] Remove CairoRunError::CairoPieValidation error --- vm/src/cairo_run.rs | 7 ++++--- vm/src/vm/errors/cairo_run_errors.rs | 3 --- vm/src/vm/errors/runner_errors.rs | 3 +++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/vm/src/cairo_run.rs b/vm/src/cairo_run.rs index 4a245dc1b1..7eae47ea17 100644 --- a/vm/src/cairo_run.rs +++ b/vm/src/cairo_run.rs @@ -13,7 +13,6 @@ use crate::{ use crate::Felt252; use bincode::enc::write::Writer; - use thiserror_no_std::Error; #[cfg(feature = "arbitrary")] @@ -126,7 +125,8 @@ pub fn cairo_run_pie( { return Err(RunnerError::PieNStepsVsRunResourcesNStepsMismatch.into()); } - pie.run_validity_checks()?; + pie.run_validity_checks() + .map_err(|cairo_pie_err| CairoRunError::Runner(cairo_pie_err.into()))?; let secure_run = cairo_run_config.secure_run.unwrap_or(true); let allow_missing_builtins = cairo_run_config.allow_missing_builtins.unwrap_or_default(); @@ -170,7 +170,8 @@ pub fn cairo_run_pie( // Check that the Cairo PIE produced by this run is compatible with the Cairo PIE received cairo_runner .get_cairo_pie(&vm)? - .check_pie_compatibility(pie)?; + .check_pie_compatibility(pie) + .map_err(|cairo_pie_err| CairoRunError::Runner(cairo_pie_err.into()))?; } cairo_runner.relocate(&mut vm, cairo_run_config.relocate_mem)?; diff --git a/vm/src/vm/errors/cairo_run_errors.rs b/vm/src/vm/errors/cairo_run_errors.rs index 7766d472cc..24a8a3552b 100644 --- a/vm/src/vm/errors/cairo_run_errors.rs +++ b/vm/src/vm/errors/cairo_run_errors.rs @@ -1,6 +1,5 @@ use thiserror_no_std::Error; -use super::cairo_pie_errors::CairoPieValidationError; use super::memory_errors::MemoryError; use super::vm_exception::VmException; use crate::types::errors::program_errors::ProgramError; @@ -22,6 +21,4 @@ pub enum CairoRunError { MemoryError(#[from] MemoryError), #[error(transparent)] VmException(#[from] VmException), - #[error("Cairo Pie validation failed: {0}")] - CairoPieValidation(#[from] CairoPieValidationError), } diff --git a/vm/src/vm/errors/runner_errors.rs b/vm/src/vm/errors/runner_errors.rs index 561f89997e..7579220fce 100644 --- a/vm/src/vm/errors/runner_errors.rs +++ b/vm/src/vm/errors/runner_errors.rs @@ -6,6 +6,7 @@ use crate::types::builtin_name::BuiltinName; use crate::types::layout_name::LayoutName; use thiserror_no_std::Error; +use super::cairo_pie_errors::CairoPieValidationError; use super::{memory_errors::MemoryError, trace_errors::TraceError}; use crate::types::{errors::math_errors::MathError, relocatable::Relocatable}; use crate::Felt252; @@ -132,6 +133,8 @@ pub enum RunnerError { CairoPieProofMode, #[error("{0}: Invalid additional data")] InvalidAdditionalData(BuiltinName), + #[error("Cairo Pie validation failed: {0}")] + CairoPieValidation(#[from] CairoPieValidationError), } #[cfg(test)] From bc6d06d7ffe57364f01a912c3db58fd651d6d7c4 Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Tue, 7 May 2024 14:43:35 -0300 Subject: [PATCH 2/5] Revert "Remove CairoRunError::CairoPieValidation error" This reverts commit 5761e7a5b97ac99fd000368001fbbdeff167ab74. --- vm/src/cairo_run.rs | 7 +++---- vm/src/vm/errors/cairo_run_errors.rs | 3 +++ vm/src/vm/errors/runner_errors.rs | 3 --- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/vm/src/cairo_run.rs b/vm/src/cairo_run.rs index 7eae47ea17..4a245dc1b1 100644 --- a/vm/src/cairo_run.rs +++ b/vm/src/cairo_run.rs @@ -13,6 +13,7 @@ use crate::{ use crate::Felt252; use bincode::enc::write::Writer; + use thiserror_no_std::Error; #[cfg(feature = "arbitrary")] @@ -125,8 +126,7 @@ pub fn cairo_run_pie( { return Err(RunnerError::PieNStepsVsRunResourcesNStepsMismatch.into()); } - pie.run_validity_checks() - .map_err(|cairo_pie_err| CairoRunError::Runner(cairo_pie_err.into()))?; + pie.run_validity_checks()?; let secure_run = cairo_run_config.secure_run.unwrap_or(true); let allow_missing_builtins = cairo_run_config.allow_missing_builtins.unwrap_or_default(); @@ -170,8 +170,7 @@ pub fn cairo_run_pie( // Check that the Cairo PIE produced by this run is compatible with the Cairo PIE received cairo_runner .get_cairo_pie(&vm)? - .check_pie_compatibility(pie) - .map_err(|cairo_pie_err| CairoRunError::Runner(cairo_pie_err.into()))?; + .check_pie_compatibility(pie)?; } cairo_runner.relocate(&mut vm, cairo_run_config.relocate_mem)?; diff --git a/vm/src/vm/errors/cairo_run_errors.rs b/vm/src/vm/errors/cairo_run_errors.rs index 24a8a3552b..7766d472cc 100644 --- a/vm/src/vm/errors/cairo_run_errors.rs +++ b/vm/src/vm/errors/cairo_run_errors.rs @@ -1,5 +1,6 @@ use thiserror_no_std::Error; +use super::cairo_pie_errors::CairoPieValidationError; use super::memory_errors::MemoryError; use super::vm_exception::VmException; use crate::types::errors::program_errors::ProgramError; @@ -21,4 +22,6 @@ pub enum CairoRunError { MemoryError(#[from] MemoryError), #[error(transparent)] VmException(#[from] VmException), + #[error("Cairo Pie validation failed: {0}")] + CairoPieValidation(#[from] CairoPieValidationError), } diff --git a/vm/src/vm/errors/runner_errors.rs b/vm/src/vm/errors/runner_errors.rs index 7579220fce..561f89997e 100644 --- a/vm/src/vm/errors/runner_errors.rs +++ b/vm/src/vm/errors/runner_errors.rs @@ -6,7 +6,6 @@ use crate::types::builtin_name::BuiltinName; use crate::types::layout_name::LayoutName; use thiserror_no_std::Error; -use super::cairo_pie_errors::CairoPieValidationError; use super::{memory_errors::MemoryError, trace_errors::TraceError}; use crate::types::{errors::math_errors::MathError, relocatable::Relocatable}; use crate::Felt252; @@ -133,8 +132,6 @@ pub enum RunnerError { CairoPieProofMode, #[error("{0}: Invalid additional data")] InvalidAdditionalData(BuiltinName), - #[error("Cairo Pie validation failed: {0}")] - CairoPieValidation(#[from] CairoPieValidationError), } #[cfg(test)] From d4f04e89729ccae46d57fc7fae471001d5d609a7 Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Tue, 7 May 2024 00:15:23 -0300 Subject: [PATCH 3/5] test unwrap() --- vm/src/cairo_run.rs | 10 +++++----- vm/src/vm/errors/cairo_run_errors.rs | 3 --- vm/src/vm/runners/cairo_runner.rs | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/vm/src/cairo_run.rs b/vm/src/cairo_run.rs index 4a245dc1b1..f5970d87e5 100644 --- a/vm/src/cairo_run.rs +++ b/vm/src/cairo_run.rs @@ -2,9 +2,7 @@ use crate::{ hint_processor::hint_processor_definition::HintProcessor, types::{builtin_name::BuiltinName, layout_name::LayoutName, program::Program}, vm::{ - errors::{ - cairo_run_errors::CairoRunError, runner_errors::RunnerError, vm_exception::VmException, - }, + errors::{runner_errors::RunnerError, vm_exception::VmException}, runners::{cairo_pie::CairoPie, cairo_runner::CairoRunner}, security::verify_secure_runner, vm_core::VirtualMachine, @@ -112,6 +110,7 @@ pub fn cairo_run( /// WARNING: As the RunResources are part of the HintProcessor trait, the caller should make sure that /// the number of steps in the `RunResources` matches that of the `ExecutionResources` in the `CairoPie`. /// An error will be returned if this doesn't hold. +use crate::vm::errors::cairo_run_errors::CairoRunError; pub fn cairo_run_pie( pie: &CairoPie, cairo_run_config: &CairoRunConfig, @@ -126,7 +125,7 @@ pub fn cairo_run_pie( { return Err(RunnerError::PieNStepsVsRunResourcesNStepsMismatch.into()); } - pie.run_validity_checks()?; + pie.run_validity_checks().unwrap(); let secure_run = cairo_run_config.secure_run.unwrap_or(true); let allow_missing_builtins = cairo_run_config.allow_missing_builtins.unwrap_or_default(); @@ -170,7 +169,8 @@ pub fn cairo_run_pie( // Check that the Cairo PIE produced by this run is compatible with the Cairo PIE received cairo_runner .get_cairo_pie(&vm)? - .check_pie_compatibility(pie)?; + .check_pie_compatibility(pie) + .unwrap(); } cairo_runner.relocate(&mut vm, cairo_run_config.relocate_mem)?; diff --git a/vm/src/vm/errors/cairo_run_errors.rs b/vm/src/vm/errors/cairo_run_errors.rs index 7766d472cc..24a8a3552b 100644 --- a/vm/src/vm/errors/cairo_run_errors.rs +++ b/vm/src/vm/errors/cairo_run_errors.rs @@ -1,6 +1,5 @@ use thiserror_no_std::Error; -use super::cairo_pie_errors::CairoPieValidationError; use super::memory_errors::MemoryError; use super::vm_exception::VmException; use crate::types::errors::program_errors::ProgramError; @@ -22,6 +21,4 @@ pub enum CairoRunError { MemoryError(#[from] MemoryError), #[error(transparent)] VmException(#[from] VmException), - #[error("Cairo Pie validation failed: {0}")] - CairoPieValidation(#[from] CairoPieValidationError), } diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 0fd7603b14..09568b7a85 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -9,6 +9,7 @@ use crate::{ }, types::{builtin_name::BuiltinName, layout::MEMORY_UNITS_PER_STEP, layout_name::LayoutName}, vm::{ + errors::cairo_run_errors::CairoRunError, runners::builtin_runner::SegmentArenaBuiltinRunner, trace::trace_entry::{relocate_trace_register, RelocatedTraceEntry}, }, @@ -28,7 +29,6 @@ use crate::{ utils::is_subsequence, vm::{ errors::{ - cairo_run_errors::CairoRunError, memory_errors::{InsufficientAllocatedCellsError, MemoryError}, runner_errors::RunnerError, trace_errors::TraceError, From 9a772f75137bd5824a58a14e5c289c074f851993 Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Tue, 7 May 2024 00:40:40 -0300 Subject: [PATCH 4/5] remove some errors --- vm/src/types/errors/math_errors.rs | 4 ---- vm/src/vm/errors/memory_errors.rs | 12 ------------ vm/src/vm/errors/runner_errors.rs | 2 -- vm/src/vm/errors/vm_errors.rs | 20 ++++---------------- 4 files changed, 4 insertions(+), 34 deletions(-) diff --git a/vm/src/types/errors/math_errors.rs b/vm/src/types/errors/math_errors.rs index 2a12efe12a..05df81f574 100644 --- a/vm/src/types/errors/math_errors.rs +++ b/vm/src/types/errors/math_errors.rs @@ -11,14 +11,10 @@ use crate::types::relocatable::{MaybeRelocatable, Relocatable}; #[derive(Debug, Error, PartialEq)] pub enum MathError { // Math functions - #[error("Can't calculate the square root of negative number: {0})")] - SqrtNegative(Box), #[error("{} is not divisible by {}", (*.0).0, (*.0).1)] SafeDivFail(Box<(Felt252, Felt252)>), #[error("{} is not divisible by {}", (*.0).0, (*.0).1)] SafeDivFailBigInt(Box<(BigInt, BigInt)>), - #[error("{} is not divisible by {}", (*.0).0, (*.0).1)] - SafeDivFailBigUint(Box<(BigUint, BigUint)>), #[error("{0} is not divisible by {1}")] SafeDivFailU32(u32, u32), #[error("{} is not divisible by {}", (*.0).0, (*.0).1)] diff --git a/vm/src/vm/errors/memory_errors.rs b/vm/src/vm/errors/memory_errors.rs index b6f02b0a85..a40df3841f 100644 --- a/vm/src/vm/errors/memory_errors.rs +++ b/vm/src/vm/errors/memory_errors.rs @@ -53,8 +53,6 @@ pub enum MemoryError { MissingMemoryCells(Box), #[error("Missing memory cells for {}: {:?}", (*.0).0, (*.0).1)] MissingMemoryCellsWithOffsets(Box<(BuiltinName, Vec)>), - #[error("ErrorInitializing Verifying Key from public key: {0:?}")] - InitializingVerifyingKey(Box>), #[error( "Signature {}, is invalid, with respect to the public key {}, and the message hash {}.", (*.0).0, (*.0).1, (*.0).2 @@ -65,20 +63,10 @@ pub enum MemoryError { Add it using 'ecdsa_builtin.add_signature'." )] SignatureNotFound(Box), - #[error("Could not create pubkey from: {0:?}")] - ErrorParsingPubKey(Box), - #[error("Could not retrieve message from: {0:?}")] - ErrorRetrievingMessage(Box), - #[error("Error verifying given signature")] - ErrorVerifyingSignature, - #[error("Couldn't obtain a mutable accessed offset")] - CantGetMutAccessedOffset, #[error("ECDSA builtin: Expected public key at address {0} to be an integer")] PubKeyNonInt(Box), #[error("ECDSA builtin: Expected message hash at address {0} to be an integer")] MsgNonInt(Box), - #[error("Failed to convert String: {0} to FieldElement")] - FailedStringToFieldElementConversion(Box), #[error("Failed to fetch {} return values, ap is only {}", (*.0).0, (*.0).1)] FailedToGetReturnValues(Box<(usize, Relocatable)>), #[error("Segment {} has {} amount of accessed addresses but its size is only {}.", (*.0).0, (*.0).1, (*.0).2)] diff --git a/vm/src/vm/errors/runner_errors.rs b/vm/src/vm/errors/runner_errors.rs index 561f89997e..730df4326a 100644 --- a/vm/src/vm/errors/runner_errors.rs +++ b/vm/src/vm/errors/runner_errors.rs @@ -68,8 +68,6 @@ pub enum RunnerError { FailedAddingReturnValues, #[error("Missing execution public memory")] NoExecPublicMemory, - #[error("Coulnd't parse prime from felt lib")] - CouldntParsePrime, #[error("Could not convert vec with Maybe Relocatables into u64 array")] MaybeRelocVecToU64ArrayError, #[error("Expected Integer value, got Relocatable instead")] diff --git a/vm/src/vm/errors/vm_errors.rs b/vm/src/vm/errors/vm_errors.rs index c3698eda02..fdd2f192ef 100644 --- a/vm/src/vm/errors/vm_errors.rs +++ b/vm/src/vm/errors/vm_errors.rs @@ -32,8 +32,9 @@ pub enum VirtualMachineError { TracerError(#[from] TraceError), #[error(transparent)] MainScopeError(#[from] ExecScopeError), - #[error(transparent)] - Other(anyhow::Error), + // TODO: CHECK THIS + // #[error(transparent)] + // Other(anyhow::Error), #[error("Instruction MSB should be 0")] InstructionNonZeroHighBit, #[error("Instruction should be an int")] @@ -76,12 +77,8 @@ pub enum VirtualMachineError { InvalidRes(u64), #[error("Invalid opcode value: {0}")] InvalidOpcode(u64), - #[error("This is not implemented")] - NotImplemented, #[error("Inconsistent auto-deduction for {}, expected {}, got {:?}", (*.0).0, (*.0).1, (*.0).2)] InconsistentAutoDeduction(Box<(BuiltinName, MaybeRelocatable, Option)>), - #[error("Invalid hint encoding at pc: {0}")] - InvalidHintEncoding(Box), #[error("Expected output builtin to be present")] NoOutputBuiltin, #[error("Expected range_check builtin to be present")] @@ -96,8 +93,6 @@ pub enum VirtualMachineError { DiffTypeComparison(Box<(MaybeRelocatable, MaybeRelocatable)>), #[error("Failed to compare {} and {}, cant compare two relocatable values of different segment indexes", (*.0).0, (*.0).1)] DiffIndexComp(Box<(Relocatable, Relocatable)>), - #[error("Couldn't convert usize to u32")] - NoneInMemoryRange, #[error("Expected integer, found: {0:?}")] ExpectedIntAtRange(Box>), #[error("Could not convert slice to array")] @@ -108,18 +103,13 @@ pub enum VirtualMachineError { NoImm, #[error("Execution reached the end of the program. Requested remaining steps: {0}.")] EndOfProgram(usize), - #[error("Could not reach the end of the program. Executed steps: {0}.")] - StepsLimit(u64), #[error("Could not reach the end of the program. RunResources has no remaining steps.")] UnfinishedExecution, #[error("Current run is not finished")] RunNotFinished, - #[error("Invalid argument count, expected {} but got {}", (*.0).0, (*.0).1)] - InvalidArgCount(Box<(usize, usize)>), - #[error("Couldn't parse prime: {0}")] - CouldntParsePrime(Box), #[error("{HINT_ERROR_STR}{}", (*.0).1)] Hint(Box<(usize, HintError)>), + // TODO CHECK THIS #[error("Unexpected Failure")] Unexpected, #[error("Out of bounds access to builtin segment")] @@ -128,8 +118,6 @@ pub enum VirtualMachineError { OutOfBoundsProgramSegmentAccess, #[error("Security Error: Invalid Memory Value: temporary address not relocated: {0}")] InvalidMemoryValueTemporaryAddress(Box), - #[error("accessed_addresses is None.")] - MissingAccessedAddresses, #[error("Failed to write the output builtin content")] FailedToWriteOutput, #[error("Failed to find index {0} in the vm's relocation table")] From e36d9a816238f92f1cecf57c67c8be5e5218ae23 Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Tue, 7 May 2024 14:30:22 -0300 Subject: [PATCH 5/5] remove unused errors --- vm/src/vm/errors/runner_errors.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/vm/src/vm/errors/runner_errors.rs b/vm/src/vm/errors/runner_errors.rs index 730df4326a..5b3a0430d1 100644 --- a/vm/src/vm/errors/runner_errors.rs +++ b/vm/src/vm/errors/runner_errors.rs @@ -28,8 +28,6 @@ pub enum RunnerError { MemoryValidationError(MemoryError), #[error("Memory loading failed during state initialization: {0}")] MemoryInitializationError(MemoryError), - #[error("Failed to convert string to FieldElement")] - FailedStringConversion, #[error("EcOpBuiltin: m should be at most {0}")] EcOpBuiltinScalarLimit(Box), #[error("Given builtins are not in appropiate order")] @@ -68,20 +66,12 @@ pub enum RunnerError { FailedAddingReturnValues, #[error("Missing execution public memory")] NoExecPublicMemory, - #[error("Could not convert vec with Maybe Relocatables into u64 array")] - MaybeRelocVecToU64ArrayError, - #[error("Expected Integer value, got Relocatable instead")] - FoundNonInt, #[error(transparent)] Memory(#[from] MemoryError), #[error(transparent)] Math(#[from] MathError), - #[error("keccak_builtin: Failed to get first input address")] - KeccakNoFirstInput, #[error("{}: Expected integer at address {}", (*.0).0, (*.0).1)] BuiltinExpectedInteger(Box<(BuiltinName, Relocatable)>), - #[error("keccak_builtin: Failed to convert input cells to u64 values")] - KeccakInputCellsNotU64, #[error("Unexpected ret_fp_segment size")] UnexpectedRetFpSegmentSize, #[error("Unexpected ret_pc_segment size")]