Skip to content

Commit

Permalink
fix: do not panic on indices which are not valid u32s (#6976)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Jan 8, 2025
1 parent d61633d commit bf474c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
23 changes: 18 additions & 5 deletions acvm-repo/acvm/src/pwg/memory_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ pub(crate) struct MemoryOpSolver<F> {
}

impl<F: AcirField> MemoryOpSolver<F> {
fn index_from_field(&self, index: F) -> Result<MemoryIndex, OpcodeResolutionError<F>> {
if index.num_bits() <= 32 {
let memory_index = index.try_to_u64().unwrap() as MemoryIndex;
Ok(memory_index)
} else {
Err(OpcodeResolutionError::IndexOutOfBounds {
opcode_location: ErrorLocation::Unresolved,
index,
array_size: self.block_len,
})
}
}

fn write_memory_index(
&mut self,
index: MemoryIndex,
Expand All @@ -29,7 +42,7 @@ impl<F: AcirField> MemoryOpSolver<F> {
if index >= self.block_len {
return Err(OpcodeResolutionError::IndexOutOfBounds {
opcode_location: ErrorLocation::Unresolved,
index,
index: F::from(index as u128),
array_size: self.block_len,
});
}
Expand All @@ -40,7 +53,7 @@ impl<F: AcirField> MemoryOpSolver<F> {
fn read_memory_index(&self, index: MemoryIndex) -> Result<F, OpcodeResolutionError<F>> {
self.block_value.get(&index).copied().ok_or(OpcodeResolutionError::IndexOutOfBounds {
opcode_location: ErrorLocation::Unresolved,
index,
index: F::from(index as u128),
array_size: self.block_len,
})
}
Expand Down Expand Up @@ -72,7 +85,7 @@ impl<F: AcirField> MemoryOpSolver<F> {

// Find the memory index associated with this memory operation.
let index = get_value(&op.index, initial_witness)?;
let memory_index = index.try_to_u64().unwrap() as MemoryIndex;
let memory_index = self.index_from_field(index)?;

// Calculate the value associated with this memory operation.
//
Expand Down Expand Up @@ -193,9 +206,9 @@ mod tests {
err,
Some(crate::pwg::OpcodeResolutionError::IndexOutOfBounds {
opcode_location: _,
index: 2,
index,
array_size: 2
})
}) if index == FieldElement::from(2u128)
));
}

Expand Down
2 changes: 1 addition & 1 deletion acvm-repo/acvm/src/pwg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub enum OpcodeResolutionError<F> {
payload: Option<ResolvedAssertionPayload<F>>,
},
#[error("Index out of bounds, array has size {array_size:?}, but index was {index:?}")]
IndexOutOfBounds { opcode_location: ErrorLocation, index: u32, array_size: u32 },
IndexOutOfBounds { opcode_location: ErrorLocation, index: F, array_size: u32 },
#[error("Cannot solve opcode: {invalid_input_bit_size}")]
InvalidInputBitSize {
opcode_location: ErrorLocation,
Expand Down

0 comments on commit bf474c0

Please sign in to comment.