Skip to content

Commit

Permalink
feat: Extensible VirtualMachinError and removed PartialEq trait
Browse files Browse the repository at this point in the history
  • Loading branch information
zarboq committed Jan 27, 2023
1 parent 639ab22 commit 148140e
Show file tree
Hide file tree
Showing 49 changed files with 1,282 additions and 1,043 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ sha2 = {version="0.10.2", features=["compress"]}
thiserror = "1.0.32"
generic-array = "0.14.6"
keccak = "0.1.2"
anyhow = "1.0.68"
assert_matches = "1.5.0"
# This crate has only one function `take_until_unbalanced` that is
# very useful for our parsing purposes:
# https://stackoverflow.com/questions/70630556/parse-allowing-nested-parentheses-in-nom
Expand Down
46 changes: 24 additions & 22 deletions src/hint_processor/builtin_hint_processor/blake2s_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ mod tests {
//Create hint data
let ids_data = ids_data!["output"];
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::CantSubOffset(
5, 26
Expand All @@ -266,11 +266,11 @@ mod tests {
//Create hint data
let ids_data = ids_data!["output"];
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::ExpectedInteger(
MaybeRelocatable::from((2, 0))
)))
x
))) if x == MaybeRelocatable::from((2, 0))
);
}

Expand All @@ -286,11 +286,11 @@ mod tests {
//Create hint data
let ids_data = ids_data!["output"];
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(
VirtualMachineError::ExpectedRelocatable(MaybeRelocatable::from((1, 0)))
))
VirtualMachineError::ExpectedRelocatable(x)
)) if x == MaybeRelocatable::from((1, 0))
);
}

Expand All @@ -316,7 +316,7 @@ mod tests {
//Create hint data
let ids_data = ids_data!["output"];
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::BigintToU32Fail)
);
Expand All @@ -334,11 +334,11 @@ mod tests {
//Create hint data
let ids_data = ids_data!["output"];
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::ExpectedInteger(
MaybeRelocatable::from((2, 0))
)))
x
))) if x == MaybeRelocatable::from((2, 0))
);
}

Expand All @@ -355,7 +355,7 @@ mod tests {
//Create hint data
let ids_data = ids_data!["blake2s_ptr_end"];
//Execute the hint
assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(()));
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
//Check the inserted data
let expected_data: [u32; 204] = [
1795745351, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635,
Expand Down Expand Up @@ -398,15 +398,17 @@ mod tests {
vm.memory = memory![((1, 0), (2, 0)), ((2, 0), (2, 0))];
let ids_data = ids_data!["blake2s_ptr_end"];
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::MemoryError(
MemoryError::InconsistentMemory(
MaybeRelocatable::from((2, 0)),
MaybeRelocatable::from((2, 0)),
MaybeRelocatable::from(Felt::new(1795745351))
x,
y,
z
)
)))
))) if x == MaybeRelocatable::from((2, 0)) &&
y == MaybeRelocatable::from((2, 0)) &&
z == MaybeRelocatable::from(Felt::new(1795745351))
);
}

Expand All @@ -418,7 +420,7 @@ mod tests {
//Initialize fp
vm.run_context.fp = 1;
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code),
Err(HintError::FailedToGetIds)
);
Expand All @@ -436,7 +438,7 @@ mod tests {
vm.segments.add(&mut vm.memory);
let ids_data = ids_data!["data", "high", "low"];
//Execute the hint
assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(()));
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
//Check data ptr
check_memory![
vm.memory,
Expand Down Expand Up @@ -464,7 +466,7 @@ mod tests {
vm.segments.add(&mut vm.memory);
let ids_data = ids_data!["data", "high", "low"];
//Execute the hint
assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(()));
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
//Check data ptr
check_memory![
vm.memory,
Expand Down Expand Up @@ -492,7 +494,7 @@ mod tests {
add_segments!(vm, 1);
let ids_data = ids_data!["data", "high", "low"];
//Execute the hint
assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(()));
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
//Check data ptr
check_memory![
vm.memory,
Expand Down Expand Up @@ -520,7 +522,7 @@ mod tests {
vm.segments.add(&mut vm.memory);
let ids_data = ids_data!["data", "high", "low"];
//Execute the hint
assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(()));
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
//Check data ptr
check_memory![
vm.memory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,25 +502,27 @@ mod tests {
//Insert something into ap
vm.memory = memory![((1, 6), (1, 6))];
//ids and references are not needed for this test
assert_eq!(
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code),
Err(HintError::Internal(VirtualMachineError::MemoryError(
MemoryError::InconsistentMemory(
MaybeRelocatable::from((1, 6)),
MaybeRelocatable::from((1, 6)),
MaybeRelocatable::from((3, 0))
x,
y,
z
)
)))
))) if x == MaybeRelocatable::from((1, 6)) &&
y == MaybeRelocatable::from((1, 6)) &&
z == MaybeRelocatable::from((3, 0))
);
}

#[test]
fn run_unknown_hint() {
let hint_code = "random_invalid_code";
let mut vm = vm!();
assert_eq!(
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code),
Err(HintError::UnknownHint(hint_code.to_string())),
Err(HintError::UnknownHint(x)) if x == *hint_code.to_string()
);
}

Expand Down Expand Up @@ -551,11 +553,11 @@ mod tests {
vm.memory = memory![((1, 1), (1, 0))];

let ids_data = ids_data!["len"];
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::ExpectedInteger(
MaybeRelocatable::from((1, 1))
)))
x
))) if x == MaybeRelocatable::from((1, 1))
);
}

Expand Down Expand Up @@ -588,9 +590,9 @@ mod tests {
// initialize ids
vm.memory = memory![((0, 2), 5)];
let ids_data = ids_data!["continue_copying"];
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::VariableNotInScopeError("n".to_string()))
Err(HintError::VariableNotInScopeError(x)) if x == *"n".to_string()
);
}

Expand All @@ -609,15 +611,17 @@ mod tests {
vm.memory = memory![((1, 1), 5)];

let ids_data = ids_data!["continue_copying"];
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code, &mut exec_scopes),
Err(HintError::Internal(VirtualMachineError::MemoryError(
MemoryError::InconsistentMemory(
MaybeRelocatable::from((1, 1)),
MaybeRelocatable::from(Felt::new(5)),
MaybeRelocatable::from(Felt::zero())
x,
y,
z
)
)))
))) if x == MaybeRelocatable::from((1, 1)) &&
y == MaybeRelocatable::from(Felt::new(5)) &&
z == MaybeRelocatable::from(Felt::zero())
);
}

Expand All @@ -641,7 +645,7 @@ mod tests {
// new vm scope is not created so that the hint raises an error:
// initialize memory segments
add_segments!(vm, 1);
assert_eq!(
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code),
Err(HintError::FromScopeError(
ExecScopeError::ExitMainScopeError
Expand All @@ -656,7 +660,7 @@ mod tests {
let mut vm = vm!();
let mut exec_scopes = ExecutionScopes::new();
//Execute the hint
assert_eq!(
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes),
Ok(())
);
Expand Down Expand Up @@ -706,9 +710,9 @@ mod tests {
];
let ids_data = ids_data!["length", "data", "high", "low"];
let mut exec_scopes = scope![("__keccak_max_size", Felt::new(2))];
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code, &mut exec_scopes),
Err(HintError::KeccakMaxSize(Felt::new(5), Felt::new(2)))
Err(HintError::KeccakMaxSize(x, y)) if x == Felt::new(5) && y == Felt::new(2)
);
}

Expand Down Expand Up @@ -752,9 +756,9 @@ mod tests {
];
let ids_data = ids_data!["length", "data", "high", "low"];
let mut exec_scopes = scope![("__keccak_max_size", Felt::new(10))];
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code, &mut exec_scopes),
Err(HintError::InvalidWordSize(Felt::new(-1)))
Err(HintError::InvalidWordSize(x)) if x == Felt::new(-1)
);
}

Expand Down Expand Up @@ -794,7 +798,7 @@ mod tests {
((1, 8), 0)
];
let ids_data = non_continuous_ids_data![("keccak_state", -7), ("high", -3), ("low", -2)];
assert_eq!(
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::NoneInMemoryRange))
);
Expand Down Expand Up @@ -842,7 +846,7 @@ mod tests {
assert_eq!(exec_scopes.data.len(), 1);
let hint_data =
HintProcessorData::new_default(String::from("enter_scope_custom_a"), HashMap::new());
assert_eq!(
assert_matches!(
hint_processor.execute_hint(
&mut vm,
exec_scopes,
Expand All @@ -854,7 +858,7 @@ mod tests {
assert_eq!(exec_scopes.data.len(), 2);
let hint_data =
HintProcessorData::new_default(String::from("enter_scope_custom_a"), HashMap::new());
assert_eq!(
assert_matches!(
hint_processor.execute_hint(
&mut vm,
exec_scopes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ mod tests {
vm.run_context.fp = 3;
//Create ids
let ids_data = ids_data!["low", "high", "inputs"];
assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(()));
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));
}

#[test]
Expand All @@ -298,10 +298,10 @@ mod tests {
//Create ids
let ids_data = ids_data!["low", "high", "inputs"];
let error = run_hint!(vm, ids_data, hint_code);
assert!(matches!(
assert_matches!(
error,
Err(HintError::Internal(VirtualMachineError::MemoryError(_)))
));
);
}

#[test]
Expand All @@ -315,7 +315,7 @@ mod tests {

run_context!(vm, 0, 1, 1);
let ids_data = ids_data!["n_bytes"];
assert_eq!(
assert_matches!(
run_hint!(
vm,
ids_data,
Expand Down Expand Up @@ -343,7 +343,7 @@ mod tests {
run_context!(vm, 0, 1, 1);

let ids_data = ids_data!["n_bytes"];
assert_eq!(
assert_matches!(
run_hint!(
vm,
ids_data,
Expand All @@ -370,7 +370,7 @@ mod tests {
run_context!(vm, 0, 1, 1);

let ids_data = ids_data!["n_bytes"];
assert_eq!(
assert_matches!(
run_hint!(
vm,
ids_data,
Expand Down

0 comments on commit 148140e

Please sign in to comment.