Skip to content

Commit

Permalink
add get_u32_range to impl VirtualMachine add get_u32 and get_u32_rang…
Browse files Browse the repository at this point in the history
…e to impl Memory
  • Loading branch information
ohad-nir-starkware committed Feb 9, 2025
1 parent 1d1be8b commit 41ff50b
Show file tree
Hide file tree
Showing 4 changed files with 54 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 `get_u32_range` to `impl VirtualMachine` add `get_u32` and `get_u32_range` to `impl Memory` [#1936](https://github.com/lambdaclass/cairo-vm/pull/1936)

* 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: 1 addition & 1 deletion vm/src/vm/decoding/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
// opcode_extension| opcode|ap_update|pc_update|res_logic|op1_src|op0_reg|dst_reg
// ... 15|14 13 12| 11 10| 9 8 7| 6 5|4 3 2| 1| 0

/// Decodes an instruction. The encoding is little endian, so flags go from bit 63 to 48.
/// Decodes an instruction. The encoding is little endian, so flags go from bit 64 to 48.
pub fn decode_instruction(encoded_instr: u64) -> Result<Instruction, VirtualMachineError> {
const DST_REG_MASK: u64 = 0x0001;
const DST_REG_OFF: u64 = 0;
Expand Down
14 changes: 14 additions & 0 deletions vm/src/vm/vm_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,11 @@ impl VirtualMachine {
self.segments.memory.get_integer_range(addr, size)
}

/// Gets n u32 values from memory starting from addr (n being size),
pub fn get_u32_range(&self, addr: Relocatable, size: usize) -> Result<Vec<u32>, MemoryError> {
self.segments.memory.get_u32_range(addr, size)
}

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

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn get_u32_range_ok() {
let mut vm = vm!();
vm.segments.memory = memory![((0, 0), 0), ((0, 1), 1), ((0, 2), 4294967295), ((0, 3), 3)];
let expected_vector = vec![1, 4294967295];
assert_eq!(vm.get_u32_range((0, 1).into(), 2), Ok(expected_vector));
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn get_traceback_entries_bad_usort() {
Expand Down
37 changes: 37 additions & 0 deletions vm/src/vm/vm_memory/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,14 @@ impl Memory {
}
}

/// Gets a u32 value from memory address.
/// Returns an Error if the value at the memory address is missing or not a u32.
pub fn get_u32(&self, key: Relocatable) -> Result<u32, MemoryError> {
let felt = self.get_integer(key)?.into_owned();
felt.to_u32()
.ok_or_else(|| MemoryError::Math(MathError::Felt252ToU32Conversion(Box::new(felt))))
}

/// Gets the value from memory address as a usize.
/// Returns an Error if the value at the memory address is missing not a Felt252, or can't be converted to usize.
pub fn get_usize(&self, key: Relocatable) -> Result<usize, MemoryError> {
Expand Down Expand Up @@ -623,6 +631,18 @@ impl Memory {
Ok(values)
}

/// Gets a range of u32 memory values from addr to addr + size
/// Fails if there if any of the values inside the range is missing (memory gap) or is not a u32
pub fn get_u32_range(&self, addr: Relocatable, size: usize) -> Result<Vec<u32>, MemoryError> {
let mut values = Vec::new();

for i in 0..size {
values.push(self.get_u32((addr + i)?)?);
}

Ok(values)
}

pub fn mark_as_accessed(&mut self, addr: Relocatable) {
let (i, j) = from_relocatable_to_indexes(addr);
let data = if addr.segment_index < 0 {
Expand Down Expand Up @@ -1135,6 +1155,23 @@ mod memory_tests {
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn get_u32_too_big() {
let mut segments = MemorySegmentManager::new();
segments.add();
segments
.memory
.insert(Relocatable::from((0, 0)), &Felt252::from(1_u64 << 32))
.unwrap();
assert_matches!(
segments.memory.get_u32(Relocatable::from((0, 0))),
Err(MemoryError::Math(MathError::Felt252ToU32Conversion(
bx
))) if *bx == Felt252::from(1_u64 << 32)
);
}

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

0 comments on commit 41ff50b

Please sign in to comment.