Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move memory-related errors to MemoryError #854

Merged
merged 19 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 37 additions & 35 deletions src/hint_processor/builtin_hint_processor/blake2s_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ use crate::{
},
serde::deserialize_program::ApTracking,
types::relocatable::{MaybeRelocatable, Relocatable},
vm::{
errors::{hint_errors::HintError, vm_errors::VirtualMachineError},
vm_core::VirtualMachine,
},
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};
use felt::Felt;
use num_traits::ToPrimitive;
Expand Down Expand Up @@ -56,7 +53,7 @@ fn compute_blake2s_func(vm: &mut VirtualMachine, output_rel: Relocatable) -> Res
get_maybe_relocatable_array_from_u32(&blake2s_compress(&h, &message, t, 0, f, 0));
let output_ptr = MaybeRelocatable::RelocatableValue(output_rel);
vm.load_data(&output_ptr, &new_state)
.map_err(VirtualMachineError::MemoryError)?;
.map_err(HintError::Memory)?;
Ok(())
}

Expand Down Expand Up @@ -117,7 +114,7 @@ pub fn finalize_blake2s(
}
let data = get_maybe_relocatable_array_from_u32(&full_padding);
vm.load_data(&MaybeRelocatable::RelocatableValue(blake2s_ptr_end), &data)
.map_err(VirtualMachineError::MemoryError)?;
.map_err(HintError::Memory)?;
Ok(())
}

Expand Down Expand Up @@ -152,7 +149,7 @@ pub fn blake2s_add_uint256(
//Insert first batch of data
let data = get_maybe_relocatable_array_from_felt(&inner_data);
vm.load_data(&MaybeRelocatable::RelocatableValue(data_ptr), &data)
.map_err(VirtualMachineError::MemoryError)?;
.map_err(HintError::Memory)?;
//Build second batch of data
let mut inner_data = Vec::<Felt>::new();
for i in 0..4 {
Expand All @@ -164,7 +161,7 @@ pub fn blake2s_add_uint256(
&MaybeRelocatable::RelocatableValue(data_ptr).add_usize(4),
&data,
)
.map_err(VirtualMachineError::MemoryError)?;
.map_err(HintError::Memory)?;
Ok(())
}

Expand Down Expand Up @@ -199,7 +196,7 @@ pub fn blake2s_add_uint256_bigend(
//Insert first batch of data
let data = get_maybe_relocatable_array_from_felt(&inner_data);
vm.load_data(&MaybeRelocatable::RelocatableValue(data_ptr), &data)
.map_err(VirtualMachineError::MemoryError)?;
.map_err(HintError::Memory)?;
//Build second batch of data
let mut inner_data = Vec::<Felt>::new();
for i in 0..4 {
Expand All @@ -211,13 +208,14 @@ pub fn blake2s_add_uint256_bigend(
&MaybeRelocatable::RelocatableValue(data_ptr).add_usize(4),
&data,
)
.map_err(VirtualMachineError::MemoryError)?;
.map_err(HintError::Memory)?;
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::vm::errors::vm_errors::VirtualMachineError;
use crate::vm::vm_memory::memory_segments::MemorySegmentManager;
use crate::{
any_box,
Expand Down Expand Up @@ -270,9 +268,9 @@ mod tests {
//Execute the hint
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::ExpectedInteger(
Err(HintError::Memory(MemoryError::UnknownMemoryCell(
x
))) if x == MaybeRelocatable::from((2, 0))
))) if x == Relocatable::from((2, 0))
);
}

Expand All @@ -290,9 +288,9 @@ mod tests {
//Execute the hint
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(
VirtualMachineError::ExpectedRelocatable(x)
)) if x == MaybeRelocatable::from((1, 0))
Err(HintError::Memory(
MemoryError::ExpectedRelocatable(x)
)) if x == Relocatable::from((1, 0))
);
}

Expand Down Expand Up @@ -338,9 +336,9 @@ mod tests {
//Execute the hint
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::ExpectedInteger(
Err(HintError::Memory(MemoryError::ExpectedInteger(
x
))) if x == MaybeRelocatable::from((2, 0))
))) if x == Relocatable::from((2, 0))
);
}

Expand Down Expand Up @@ -403,13 +401,13 @@ mod tests {
//Execute the hint
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::MemoryError(
Err(HintError::Memory(
MemoryError::InconsistentMemory(
x,
y,
z
)
))) if x == MaybeRelocatable::from((2, 0)) &&
)) if x == MaybeRelocatable::from((2, 0)) &&
y == MaybeRelocatable::from((2, 0)) &&
z == MaybeRelocatable::from(Felt::new(1795745351))
);
Expand Down Expand Up @@ -454,10 +452,11 @@ mod tests {
((2, 6), 0),
((2, 7), 0)
];
assert_eq!(
vm.segments.memory.get(&MaybeRelocatable::from((2, 8))),
Ok(None)
);
assert!(vm
.segments
.memory
.get(&MaybeRelocatable::from((2, 8)))
.is_none());
}

#[test]
Expand Down Expand Up @@ -485,10 +484,11 @@ mod tests {
((2, 6), 0),
((2, 7), 0)
];
assert_eq!(
vm.segments.memory.get(&MaybeRelocatable::from((2, 8))),
Ok(None)
);
assert!(vm
.segments
.memory
.get(&MaybeRelocatable::from((2, 8)))
.is_none());
}

#[test]
Expand Down Expand Up @@ -516,10 +516,11 @@ mod tests {
((2, 6), 0),
((2, 7), 0)
];
assert_eq!(
vm.segments.memory.get(&MaybeRelocatable::from((2, 8))),
Ok(None)
);
assert!(vm
.segments
.memory
.get(&MaybeRelocatable::from((2, 8)))
.is_none());
}

#[test]
Expand Down Expand Up @@ -547,9 +548,10 @@ mod tests {
((2, 6), 0),
((2, 7), 20)
];
assert_eq!(
vm.segments.memory.get(&MaybeRelocatable::from((2, 8))),
Ok(None)
);
assert!(vm
.segments
.memory
.get(&MaybeRelocatable::from((2, 8)))
.is_none());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,15 @@ impl HintProcessor for BuiltinHintProcessor {
#[cfg(test)]
mod tests {
use super::*;
use crate::types::relocatable::Relocatable;
use crate::vm::vm_memory::memory_segments::MemorySegmentManager;
use crate::{
any_box,
hint_processor::hint_processor_definition::HintProcessor,
types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable},
utils::test_utils::*,
vm::{
errors::{
exec_scope_errors::ExecScopeError, memory_errors::MemoryError,
vm_errors::VirtualMachineError,
},
errors::{exec_scope_errors::ExecScopeError, memory_errors::MemoryError},
vm_core::VirtualMachine,
vm_memory::memory::Memory,
},
Expand Down Expand Up @@ -506,13 +504,13 @@ mod tests {
//ids and references are not needed for this test
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code),
Err(HintError::Internal(VirtualMachineError::MemoryError(
Err(HintError::Memory(
MemoryError::InconsistentMemory(
x,
y,
z
)
))) if x == MaybeRelocatable::from((1, 6)) &&
)) if x == MaybeRelocatable::from((1, 6)) &&
y == MaybeRelocatable::from((1, 6)) &&
z == MaybeRelocatable::from((3, 0))
);
Expand Down Expand Up @@ -557,9 +555,9 @@ mod tests {
let ids_data = ids_data!["len"];
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::ExpectedInteger(
Err(HintError::Memory(MemoryError::ExpectedInteger(
x
))) if x == MaybeRelocatable::from((1, 1))
))) if x == Relocatable::from((1, 1))
);
}

Expand Down Expand Up @@ -615,13 +613,13 @@ mod tests {
let ids_data = ids_data!["continue_copying"];
assert_matches!(
run_hint!(vm, ids_data, hint_code, &mut exec_scopes),
Err(HintError::Internal(VirtualMachineError::MemoryError(
Err(HintError::Memory(
MemoryError::InconsistentMemory(
x,
y,
z
)
))) if x == MaybeRelocatable::from((1, 1)) &&
)) if x == MaybeRelocatable::from((1, 1)) &&
y == MaybeRelocatable::from(Felt::new(5)) &&
z == MaybeRelocatable::from(Felt::zero())
);
Expand Down Expand Up @@ -802,7 +800,7 @@ mod tests {
let ids_data = non_continuous_ids_data![("keccak_state", -7), ("high", -3), ("low", -2)];
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Internal(VirtualMachineError::NoneInMemoryRange))
Err(HintError::Memory(MemoryError::UnknownMemoryCell(_)))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ pub fn keccak_write_args(

let low_args: Vec<_> = low_args.into_iter().map(MaybeRelocatable::from).collect();
vm.write_arg(inputs_ptr, &low_args)
.map_err(VirtualMachineError::MemoryError)?;
.map_err(HintError::Memory)?;

let high_args: Vec<_> = high_args.into_iter().map(MaybeRelocatable::from).collect();
vm.write_arg(inputs_ptr.add(2_i32), &high_args)
.map_err(VirtualMachineError::MemoryError)?;
.map_err(HintError::Memory)?;

Ok(())
}
Expand Down Expand Up @@ -148,7 +148,7 @@ pub fn block_permutation(
&MaybeRelocatable::RelocatableValue(keccak_ptr.sub_usize(keccak_state_size_felts)?),
keccak_state_size_felts,
)
.map_err(VirtualMachineError::MemoryError)?;
.map_err(HintError::Memory)?;

let mut u64_values = maybe_reloc_vec_to_u64_array(&values)?
.try_into()
Expand All @@ -161,7 +161,7 @@ pub fn block_permutation(
let bigint_values = u64_array_to_mayberelocatable_vec(&u64_values);

vm.write_arg(keccak_ptr, &bigint_values)
.map_err(VirtualMachineError::MemoryError)?;
.map_err(HintError::Memory)?;

Ok(())
}
Expand Down Expand Up @@ -221,7 +221,7 @@ pub fn cairo_keccak_finalize(
let keccak_ptr_end = get_ptr_from_var_name("keccak_ptr_end", vm, ids_data, ap_tracking)?;

vm.write_arg(keccak_ptr_end, &padding)
.map_err(VirtualMachineError::MemoryError)?;
.map_err(HintError::Memory)?;

Ok(())
}
Expand Down Expand Up @@ -300,10 +300,7 @@ mod tests {
//Create ids
let ids_data = ids_data!["low", "high", "inputs"];
let error = run_hint!(vm, ids_data, hint_code);
assert_matches!(
error,
Err(HintError::Internal(VirtualMachineError::MemoryError(_)))
);
assert_matches!(error, Err(HintError::Memory(_)));
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ mod tests {
use crate::hint_processor::builtin_hint_processor::hint_code;
use crate::hint_processor::hint_processor_definition::HintProcessor;
use crate::types::exec_scope::ExecutionScopes;
use crate::vm::errors::vm_errors::VirtualMachineError;
use crate::vm::vm_memory::memory::Memory;
use crate::vm::vm_memory::memory_segments::MemorySegmentManager;
use crate::{
Expand Down Expand Up @@ -325,13 +324,13 @@ mod tests {
//ids and references are not needed for this test
assert_matches!(
run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes),
Err(HintError::Internal(VirtualMachineError::MemoryError(
Err(HintError::Memory(
MemoryError::InconsistentMemory(
x,
y,
z
)
))) if x == MaybeRelocatable::from((1, 0)) &&
)) if x == MaybeRelocatable::from((1, 0)) &&
y == MaybeRelocatable::from(1) &&
z == MaybeRelocatable::from((2, 0))
);
Expand All @@ -357,7 +356,6 @@ mod tests {
.memory
.get(&MaybeRelocatable::from((1, 1)))
.unwrap()
.unwrap()
.as_ref(),
&MaybeRelocatable::from(12)
);
Expand Down
13 changes: 1 addition & 12 deletions src/hint_processor/builtin_hint_processor/dict_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ use std::collections::HashMap;

use crate::{
types::relocatable::{MaybeRelocatable, Relocatable},
vm::{
errors::{
hint_errors::HintError, memory_errors::MemoryError, vm_errors::VirtualMachineError,
},
vm_core::VirtualMachine,
},
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};

#[derive(PartialEq, Eq, Debug, Clone)]
Expand Down Expand Up @@ -80,12 +75,6 @@ impl DictManager {
return Err(HintError::CantCreateDictionaryOnTakenSegment(
base.segment_index,
));
}

if base.segment_index < 0 {
Err(VirtualMachineError::MemoryError(
MemoryError::AddressInTemporarySegment(base.segment_index),
))?;
};

self.trackers.insert(
Expand Down
Loading