Skip to content

Commit

Permalink
add the functions get_u32_range and get_u32_from_felt252 to impl Virt…
Browse files Browse the repository at this point in the history
…ualMachine
  • Loading branch information
ohad-nir-starkware committed Feb 4, 2025
1 parent 6dbac44 commit de7e16c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* feat: add the functions `get_u32_range`, `get_u32_from_felt252` to `impl VirtualMachine` [#1934](https://github.com/lambdaclass/cairo-vm/pull/1934)

* feat: add the field `opcode_extension` to the structure of `Instruction` [#1933](https://github.com/lambdaclass/cairo-vm/pull/1933)

* fix(BREAKING): Fix no trace padding flow in proof mode [#1909](https://github.com/lambdaclass/cairo-vm/pull/1909)
Expand Down
2 changes: 2 additions & 0 deletions vm/src/vm/errors/memory_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub enum MemoryError {
UnrelocatedMemory,
#[error("Malformed public memory")]
MalformedPublicMemory,
#[error("Expected u32 valued Felt252, found {0}")]
ExpectedU32(Box<Felt252>),
}

#[derive(Debug, PartialEq, Eq, Error)]
Expand Down
39 changes: 38 additions & 1 deletion vm/src/vm/vm_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
instruction::{
is_call_instruction, ApUpdate, FpUpdate, Instruction, Opcode, PcUpdate, Res,
},
relocatable::{MaybeRelocatable, Relocatable},
relocatable::{MaybeRelocatable, MaybeRelocatable::RelocatableValue, Relocatable},
},
vm::{
context::run_context::RunContext,
Expand Down Expand Up @@ -929,6 +929,33 @@ impl VirtualMachine {
self.segments.memory.get_integer_range(addr, size)
}

/// Gets n u32 values from memory starting from addr (n being size),
/// Verifies that addr is &RelocatableValue and that the values are u32.
pub fn get_u32_range(
&self,
addr: &MaybeRelocatable,
size: usize,
) -> Result<Vec<u32>, MemoryError> {
let addr_relocatable: Relocatable = match addr {
&RelocatableValue(relocatable) => relocatable,
_ => return Err(MemoryError::AddressNotRelocatable),
};
let res_cow = self.get_integer_range(addr_relocatable, size)?;
res_cow
.iter()
.map(|x| VirtualMachine::get_u32_from_felt252(x.clone().into_owned()))
.collect()
}

fn get_u32_from_felt252(felt: Felt252) -> Result<u32, MemoryError> {
let le_digits = felt.to_le_digits();
if le_digits[0] >= (1 << 32) || le_digits[1] != 0 || le_digits[2] != 0 || le_digits[3] != 0
{
return Err(MemoryError::ExpectedU32(Box::new(felt)));
}
Ok(le_digits[0] as u32)
}

pub fn get_range_check_builtin(
&self,
) -> Result<&RangeCheckBuiltinRunner<RC_N_PARTS_STANDARD>, VirtualMachineError> {
Expand Down Expand Up @@ -4373,6 +4400,16 @@ mod tests {
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn get_u32_range_not_relocatable() {
let vm = vm!();
assert_matches!(
vm.get_u32_range(&MaybeRelocatable::Int(Felt252::from(1)), 1),
Err(MemoryError::AddressNotRelocatable)
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn get_traceback_entries_bad_usort() {
Expand Down

0 comments on commit de7e16c

Please sign in to comment.