Skip to content

Commit b52c3ae

Browse files
committed
instruction: generate witness for dot product
1 parent 62b5359 commit b52c3ae

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

crates/leanVm/src/air/dot_product.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,19 @@ pub fn build_dot_product_columns(witness: &[WitnessDotProduct]) -> (Vec<Vec<EF>>
210210

211211
// The `index_a` and `index_b` columns are the memory addresses, incrementing from the start.
212212
index_a.extend(
213-
(dot_product.addr_0..(dot_product.addr_0 + dot_product.len)).map(EF::from_usize),
213+
(dot_product.addr_0.offset..(dot_product.addr_0.offset + dot_product.len))
214+
.map(EF::from_usize),
214215
);
215216
index_b.extend(
216-
(dot_product.addr_1..(dot_product.addr_1 + dot_product.len)).map(EF::from_usize),
217+
(dot_product.addr_1.offset..(dot_product.addr_1.offset + dot_product.len))
218+
.map(EF::from_usize),
217219
);
218220

219221
// The `index_res` column holds the constant result address, repeated for every row.
220-
index_res.extend(vec![EF::from_usize(dot_product.addr_res); dot_product.len]);
222+
index_res.extend(vec![
223+
EF::from_usize(dot_product.addr_res.offset);
224+
dot_product.len
225+
]);
221226

222227
// The `value_a` and `value_b` columns are direct copies of the input slices.
223228
value_a.extend_from_slice(&dot_product.slice_0);

crates/leanVm/src/bytecode/instruction/dot_product.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
context::run_context::RunContext,
77
errors::vm::VirtualMachineError,
88
memory::{address::MemoryAddress, manager::MemoryManager},
9+
witness::dot_product::WitnessDotProduct,
910
};
1011

1112
/// An instruction to compute the dot product of two vectors of extension field elements.
@@ -63,4 +64,48 @@ impl DotProductInstruction {
6364

6465
Ok(())
6566
}
67+
68+
/// Generates the witness for the dot product instruction.
69+
pub fn generate_witness(
70+
&self,
71+
cycle: usize,
72+
run_context: &RunContext,
73+
memory_manager: &MemoryManager,
74+
) -> Result<WitnessDotProduct, VirtualMachineError> {
75+
// Resolve pointers for inputs and result.
76+
let addr_0: MemoryAddress = run_context
77+
.value_from_mem_or_constant(&self.arg0, memory_manager)?
78+
.try_into()?;
79+
let addr_1: MemoryAddress = run_context
80+
.value_from_mem_or_constant(&self.arg1, memory_manager)?
81+
.try_into()?;
82+
let addr_res: MemoryAddress = run_context
83+
.value_from_mem_or_fp(&self.res, memory_manager)?
84+
.try_into()?;
85+
86+
// Read the first vector slice from memory.
87+
let slice_0 = memory_manager
88+
.memory
89+
.get_vectorized_slice_extension(addr_0, self.size)?;
90+
91+
// Read the second vector slice from memory.
92+
let slice_1 = memory_manager
93+
.memory
94+
.get_vectorized_slice_extension(addr_1, self.size)?;
95+
96+
// Read the result from memory.
97+
let res = memory_manager.memory.get_extension(addr_res)?;
98+
99+
// Construct and return the witness.
100+
Ok(WitnessDotProduct {
101+
cycle,
102+
addr_0,
103+
addr_1,
104+
addr_res,
105+
len: self.size,
106+
slice_0,
107+
slice_1,
108+
res,
109+
})
110+
}
66111
}

crates/leanVm/src/witness/dot_product.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
use crate::constant::EF;
1+
use crate::{constant::EF, memory::address::MemoryAddress};
22

33
/// Holds the high-level witness data for a single dot product precompile execution.
44
#[derive(Debug)]
55
pub struct WitnessDotProduct {
66
/// The CPU cycle at which the dot product operation is initiated.
77
pub cycle: usize,
88
/// The starting memory address (vectorized pointer) of the first input slice.
9-
pub addr_0: usize,
9+
pub addr_0: MemoryAddress,
1010
/// The starting memory address (vectorized pointer) of the second input slice.
11-
pub addr_1: usize,
11+
pub addr_1: MemoryAddress,
1212
/// The memory address (vectorized pointer) where the final result is stored.
13-
pub addr_res: usize,
13+
pub addr_res: MemoryAddress,
1414
/// The number of elements in each input slice.
1515
pub len: usize,
1616
/// The actual data values of the first input slice.

0 commit comments

Comments
 (0)