From e88e3e2da7f544227cdb1031d1e300f3dba88d3d Mon Sep 17 00:00:00 2001 From: Tom French Date: Tue, 7 Jan 2025 20:04:17 +0000 Subject: [PATCH 1/3] fix: do not panic on indices which are not valid `u32`s --- acvm-repo/acvm/src/pwg/memory_op.rs | 19 ++++++++++++++++--- acvm-repo/acvm/src/pwg/mod.rs | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/acvm-repo/acvm/src/pwg/memory_op.rs b/acvm-repo/acvm/src/pwg/memory_op.rs index a9ed7f5d15b..d1cd999ce8e 100644 --- a/acvm-repo/acvm/src/pwg/memory_op.rs +++ b/acvm-repo/acvm/src/pwg/memory_op.rs @@ -21,6 +21,19 @@ pub(crate) struct MemoryOpSolver { } impl MemoryOpSolver { + fn index_from_field(&self, index: F) -> Result> { + 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, @@ -29,7 +42,7 @@ impl MemoryOpSolver { 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, }); } @@ -40,7 +53,7 @@ impl MemoryOpSolver { fn read_memory_index(&self, index: MemoryIndex) -> Result> { 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, }) } @@ -71,7 +84,7 @@ impl MemoryOpSolver { // 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. // diff --git a/acvm-repo/acvm/src/pwg/mod.rs b/acvm-repo/acvm/src/pwg/mod.rs index f9188cca700..524ee5d007a 100644 --- a/acvm-repo/acvm/src/pwg/mod.rs +++ b/acvm-repo/acvm/src/pwg/mod.rs @@ -126,7 +126,7 @@ pub enum OpcodeResolutionError { payload: Option>, }, #[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, From d72a0def1e4093b72619ac4fbe6f9fd3e9b67d05 Mon Sep 17 00:00:00 2001 From: Tom French Date: Tue, 7 Jan 2025 20:10:12 +0000 Subject: [PATCH 2/3] . --- acvm-repo/acvm/src/pwg/memory_op.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acvm-repo/acvm/src/pwg/memory_op.rs b/acvm-repo/acvm/src/pwg/memory_op.rs index d1cd999ce8e..d9f26baaa8d 100644 --- a/acvm-repo/acvm/src/pwg/memory_op.rs +++ b/acvm-repo/acvm/src/pwg/memory_op.rs @@ -196,7 +196,7 @@ mod tests { err, Some(crate::pwg::OpcodeResolutionError::IndexOutOfBounds { opcode_location: _, - index: 2, + index: FieldElement::from(2u128), array_size: 2 }) )); From 6e0ba8410b8a00be5cbbc7cb27b79349dccc0752 Mon Sep 17 00:00:00 2001 From: Tom French Date: Tue, 7 Jan 2025 20:11:21 +0000 Subject: [PATCH 3/3] . --- acvm-repo/acvm/src/pwg/memory_op.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/acvm-repo/acvm/src/pwg/memory_op.rs b/acvm-repo/acvm/src/pwg/memory_op.rs index d9f26baaa8d..5cf6d0d6aac 100644 --- a/acvm-repo/acvm/src/pwg/memory_op.rs +++ b/acvm-repo/acvm/src/pwg/memory_op.rs @@ -196,9 +196,9 @@ mod tests { err, Some(crate::pwg::OpcodeResolutionError::IndexOutOfBounds { opcode_location: _, - index: FieldElement::from(2u128), + index, array_size: 2 - }) + }) if index == FieldElement::from(2u128) )); }