Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@ -71,7 +84,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 @@ -183,9 +196,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