From 370a6108912cd92b1eec22e0164ab691dc9e98fa Mon Sep 17 00:00:00 2001 From: Ana Perez Ghiglia Date: Fri, 28 Jun 2024 10:57:42 -0600 Subject: [PATCH] replace BrilligFunctionFailed.found_trap to .reason * Abstract `extract_failure_payload` function for improving readability --- acvm-repo/acvm/src/pwg/brillig.rs | 103 +++++++++++++++--------------- acvm-repo/acvm/src/pwg/mod.rs | 7 +- acvm-repo/acvm/tests/solver.rs | 4 +- tooling/debugger/src/context.rs | 8 ++- 4 files changed, 65 insertions(+), 57 deletions(-) diff --git a/acvm-repo/acvm/src/pwg/brillig.rs b/acvm-repo/acvm/src/pwg/brillig.rs index d1eb04f8285..ac6abdead52 100644 --- a/acvm-repo/acvm/src/pwg/brillig.rs +++ b/acvm-repo/acvm/src/pwg/brillig.rs @@ -169,59 +169,11 @@ impl<'b, B: BlackBoxFunctionSolver, F: AcirField> BrilligSolver<'b, F, B> { brillig_index: *brillig_index, }) .collect(); - let (payload, found_trap) = match reason { - FailureReason::RuntimeError { message } => { - (Some(ResolvedAssertionPayload::String(message)), false) - } - - FailureReason::Trap { revert_data_offset, revert_data_size } => { - // Since noir can only revert with strings currently, we can parse return data as a string - let payload = if revert_data_size == 0 { - None - } else { - let memory = self.vm.get_memory(); - let mut revert_values_iter = memory - [revert_data_offset..(revert_data_offset + revert_data_size)] - .iter(); - let error_selector = ErrorSelector::new( - revert_values_iter - .next() - .expect("Incorrect revert data size") - .try_into() - .expect("Error selector is not u64"), - ); - match error_selector { - STRING_ERROR_SELECTOR => { - // If the error selector is 0, it means the error is a string - let string = revert_values_iter - .map(|memory_value| { - let as_u8: u8 = memory_value - .try_into() - .expect("String item is not u8"); - as_u8 as char - }) - .collect(); - Some(ResolvedAssertionPayload::String(string)) - } - _ => { - // If the error selector is not 0, it means the error is a custom error - Some(ResolvedAssertionPayload::Raw(RawAssertionPayload { - selector: error_selector, - data: revert_values_iter - .map(|value| value.to_field()) - .collect(), - })) - } - } - }; - (payload, true) - } - }; Err(OpcodeResolutionError::BrilligFunctionFailed { - payload, + payload: self.extract_failure_payload(&reason), call_stack, - found_trap, + reason, }) } VMStatus::ForeignCallWait { function, inputs } => { @@ -230,6 +182,57 @@ impl<'b, B: BlackBoxFunctionSolver, F: AcirField> BrilligSolver<'b, F, B> { } } + fn extract_failure_payload( + &self, + reason: &FailureReason, + ) -> Option> { + match reason { + FailureReason::RuntimeError { message } => { + Some(ResolvedAssertionPayload::String(message.clone())) + } + FailureReason::Trap { revert_data_offset, revert_data_size } => { + // Since noir can only revert with strings currently, we can parse return data as a string + let payload = if *revert_data_size == 0 { + None + } else { + let memory = self.vm.get_memory(); + let mut revert_values_iter = memory + [*revert_data_offset..(*revert_data_offset + *revert_data_size)] + .iter(); + let error_selector = ErrorSelector::new( + revert_values_iter + .next() + .expect("Incorrect revert data size") + .try_into() + .expect("Error selector is not u64"), + ); + + match error_selector { + STRING_ERROR_SELECTOR => { + // If the error selector is 0, it means the error is a string + let string = revert_values_iter + .map(|memory_value| { + let as_u8: u8 = + memory_value.try_into().expect("String item is not u8"); + as_u8 as char + }) + .collect(); + Some(ResolvedAssertionPayload::String(string)) + } + _ => { + // If the error selector is not 0, it means the error is a custom error + Some(ResolvedAssertionPayload::Raw(RawAssertionPayload { + selector: error_selector, + data: revert_values_iter.map(|value| value.to_field()).collect(), + })) + } + } + }; + payload + } + } + } + pub(crate) fn finalize( self, witness: &mut WitnessMap, diff --git a/acvm-repo/acvm/src/pwg/mod.rs b/acvm-repo/acvm/src/pwg/mod.rs index f85e4674b41..341454ddfda 100644 --- a/acvm-repo/acvm/src/pwg/mod.rs +++ b/acvm-repo/acvm/src/pwg/mod.rs @@ -13,6 +13,7 @@ use acir::{ AcirField, BlackBoxFunc, }; use acvm_blackbox_solver::BlackBoxResolutionError; +use brillig_vm::FailureReason; use self::{ arithmetic::ExpressionSolver, blackbox::bigint::AcvmBigIntSolver, directives::solve_directives, @@ -132,7 +133,7 @@ pub enum OpcodeResolutionError { BrilligFunctionFailed { call_stack: Vec, payload: Option>, - found_trap: bool, + reason: FailureReason, }, AcirMainCallAttempted { opcode_location: ErrorLocation, @@ -151,8 +152,8 @@ impl std::fmt::Display for OpcodeResolutionError { OpcodeResolutionError::UnsatisfiedConstrain { .. } => write!(f, "Cannot satisfy constraint"), OpcodeResolutionError::IndexOutOfBounds { array_size, index, .. } => write!(f, "Index out of bounds, array has size {}, but index was {}", array_size, index), OpcodeResolutionError::BlackBoxFunctionFailed(func, reason) => write!(f,"Failed to solve blackbox function: {}, reason: {}", func, reason), - OpcodeResolutionError::BrilligFunctionFailed{found_trap: false, .. } => write!(f, "Failed to solve brillig function"), - OpcodeResolutionError::BrilligFunctionFailed{found_trap: true, .. } => write!(f, "Cannot satisfy constraint"), + OpcodeResolutionError::BrilligFunctionFailed{reason: FailureReason::Trap {.. }, .. } => write!(f, "Cannot satisfy constraint"), + OpcodeResolutionError::BrilligFunctionFailed{ .. } => write!(f, "Failed to solve brillig function"), OpcodeResolutionError::AcirMainCallAttempted{ .. } => write!(f, "Attempted to call `main` with a `Call` opcode"), OpcodeResolutionError::AcirCallOutputsMismatch {results_size, outputs_size, .. } => write!(f, "{} result values were provided for {} call output witnesses, most likely due to bad ACIR codegen", results_size, outputs_size), } diff --git a/acvm-repo/acvm/tests/solver.rs b/acvm-repo/acvm/tests/solver.rs index 69b7b5272da..0b407a6ac6b 100644 --- a/acvm-repo/acvm/tests/solver.rs +++ b/acvm-repo/acvm/tests/solver.rs @@ -14,7 +14,7 @@ use acir::{ use acvm::pwg::{ACVMStatus, ErrorLocation, ForeignCallWaitInfo, OpcodeResolutionError, ACVM}; use acvm_blackbox_solver::StubbedBlackBoxSolver; -use brillig_vm::brillig::HeapValueType; +use brillig_vm::{brillig::HeapValueType, FailureReason}; // Reenable these test cases once we move the brillig implementation of inversion down into the acvm stdlib. @@ -673,7 +673,7 @@ fn unsatisfied_opcode_resolved_brillig() { ACVMStatus::Failure(OpcodeResolutionError::BrilligFunctionFailed { payload: None, call_stack: vec![OpcodeLocation::Brillig { acir_index: 0, brillig_index: 3 }], - found_trap: true, + reason: FailureReason::Trap { revert_data_offset: 0, revert_data_size: 0 }, }), "The first opcode is not satisfiable, expected an error indicating this" ); diff --git a/tooling/debugger/src/context.rs b/tooling/debugger/src/context.rs index 7b1ad7294c8..f07ffbb6658 100644 --- a/tooling/debugger/src/context.rs +++ b/tooling/debugger/src/context.rs @@ -2,7 +2,7 @@ use crate::foreign_calls::DebugForeignCallExecutor; use acvm::acir::circuit::brillig::BrilligBytecode; use acvm::acir::circuit::{Circuit, Opcode, OpcodeLocation}; use acvm::acir::native_types::{Witness, WitnessMap, WitnessStack}; -use acvm::brillig_vm::MemoryValue; +use acvm::brillig_vm::{FailureReason, MemoryValue}; use acvm::pwg::{ ACVMStatus, AcirCallWaitInfo, BrilligSolver, BrilligSolverStatus, ForeignCallWaitInfo, OpcodeNotSolvable, OpcodeResolutionError, StepResult, ACVM, @@ -472,7 +472,11 @@ impl<'a, B: BlackBoxFunctionSolver> DebugContext<'a, B> { self.handle_foreign_call(foreign_call) } Err(err) => { - if let OpcodeResolutionError::BrilligFunctionFailed { found_trap: true, .. } = err { + if let OpcodeResolutionError::BrilligFunctionFailed { + reason: FailureReason::Trap { .. }, + .. + } = err + { // return solver ownership so brillig_solver it has the right opcode location self.brillig_solver = Some(solver); }