Skip to content

Commit

Permalink
Check error types in vm_core.rs + simplify code (#837)
Browse files Browse the repository at this point in the history
* Remove error

* Check error types used

* Remove PureValueError

* Simplify logic

* Simplify logic deduce_op1

* Fix

* Simplify step

* Simplify deduce_op1
  • Loading branch information
fmoletta authored Feb 15, 2023
1 parent d13f496 commit 3bb8054
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 135 deletions.
15 changes: 14 additions & 1 deletion src/types/relocatable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use num_traits::{FromPrimitive, ToPrimitive, Zero};
use serde::{Deserialize, Serialize};
use std::{
fmt::{self, Display},
ops::Add,
ops::{Add, AddAssign},
};

#[derive(Eq, Hash, PartialEq, PartialOrd, Clone, Copy, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -95,6 +95,12 @@ impl Add<usize> for Relocatable {
}
}

impl AddAssign<usize> for Relocatable {
fn add_assign(&mut self, rhs: usize) {
self.offset += rhs
}
}

impl Add<i32> for Relocatable {
type Output = Relocatable;
fn add(self, other: i32) -> Self {
Expand Down Expand Up @@ -825,4 +831,11 @@ mod tests {
String::from("6")
)
}

#[test]
fn relocatable_add_assign_usize() {
let mut addr = Relocatable::from((1, 0));
addr += 1;
assert_eq!(addr, Relocatable::from((1, 1)))
}
}
2 changes: 2 additions & 0 deletions src/vm/errors/memory_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,6 @@ pub enum MemoryError {
MsgNonInt(Relocatable),
#[error("Failed to convert String: {0} to FieldElement")]
FailedStringToFieldElementConversion(String),
#[error("Failed to fetch {0} return values, ap is only {1}")]
FailedToGetReturnValues(usize, Relocatable),
}
10 changes: 8 additions & 2 deletions src/vm/errors/vm_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ pub enum VirtualMachineError {
UnconstrainedResJumpRel,
#[error("Res.UNCONSTRAINED cannot be used with Opcode.ASSERT_EQ")]
UnconstrainedResAssertEq,
#[error("An integer value as Res cannot be used with PcUpdate.JUMP_REL")]
JumpRelNotInt,
#[error(
"Failed to compute Res.MUL: Could not complete computation of non pure values {0} * {1}"
)]
ComputeResRelocatableMul(MaybeRelocatable, MaybeRelocatable),
#[error("Couldn't compute operand {0} at address {1}")]
FailedToComputeOperands(String, Relocatable),
#[error("An ASSERT_EQ instruction failed: {0} != {1}.")]
Expand All @@ -42,8 +48,6 @@ pub enum VirtualMachineError {
CantWriteReturnFp(MaybeRelocatable, MaybeRelocatable),
#[error("Couldn't get or load dst")]
NoDst,
#[error("Pure Value Error")]
PureValue,
#[error("Invalid res value: {0}")]
InvalidRes(i64),
#[error("Invalid opcode value: {0}")]
Expand Down Expand Up @@ -144,6 +148,8 @@ pub enum VirtualMachineError {
NegBuiltinBase,
#[error("Security Error: Invalid Memory Value: temporary address not relocated: {0}")]
InvalidMemoryValueTemporaryAddress(Relocatable),
#[error("accessed_addresses is None.")]
MissingAccessedAddresses,
#[error(transparent)]
Other(Box<dyn Error>),
}
Loading

0 comments on commit 3bb8054

Please sign in to comment.