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

fix: do not panic on indices which are not valid u32s #6976

Merged
merged 3 commits into from
Jan 8, 2025
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 @@ -55,7 +55,7 @@
RequiresForeignCall(ForeignCallWaitInfo<F>),

/// The ACVM has encountered a request for an ACIR [call][acir::circuit::Opcode]
/// to execute a separate ACVM instance. The result of the ACIR call must be passd back to the ACVM.

Check warning on line 58 in acvm-repo/acvm/src/pwg/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (passd)
///
/// Once this is done, the ACVM can be restarted to solve the remaining opcodes.
RequiresAcirCall(AcirCallWaitInfo<F>),
Expand Down Expand Up @@ -126,7 +126,7 @@
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
Loading