@@ -5,8 +5,9 @@ use crate::{
55 bytecode:: operand:: { MemOrConstant , MemOrFp } ,
66 constant:: { DIMENSION , EF , F } ,
77 context:: run_context:: RunContext ,
8- errors:: vm:: VirtualMachineError ,
8+ errors:: { memory :: MemoryError , vm:: VirtualMachineError } ,
99 memory:: { address:: MemoryAddress , manager:: MemoryManager } ,
10+ witness:: multilinear_eval:: WitnessMultilinearEval ,
1011} ;
1112
1213/// An instruction to evaluate a multilinear polynomial at a point in the extension field.
@@ -64,7 +65,7 @@ impl MultilinearEvalInstruction {
6465 let addr = ( ptr_point + i) ?;
6566 let vector_coeffs = memory_manager. memory . get_array_as :: < F , DIMENSION > ( addr) ?;
6667 EF :: from_basis_coefficients_slice ( & vector_coeffs)
67- . ok_or ( VirtualMachineError :: InvalidExtensionField )
68+ . ok_or ( MemoryError :: InvalidExtensionFieldConversion )
6869 } )
6970 . collect :: < Result < _ , _ > > ( ) ?;
7071
@@ -76,4 +77,46 @@ impl MultilinearEvalInstruction {
7677
7778 Ok ( ( ) )
7879 }
80+
81+ /// Generates the witness for a `MultilinearEval` instruction execution.
82+ ///
83+ /// This function reads the necessary data from memory (operands and result)
84+ /// to construct a `WitnessMultilinearEval` struct, which captures the complete
85+ /// state of the operation at a specific cycle.
86+ pub fn generate_witness (
87+ & self ,
88+ cycle : usize ,
89+ run_context : & RunContext ,
90+ memory_manager : & MemoryManager ,
91+ ) -> Result < WitnessMultilinearEval , VirtualMachineError > {
92+ // Resolve the memory addresses for the coefficients, point, and result.
93+ let addr_coeffs = run_context
94+ . value_from_mem_or_constant ( & self . coeffs , memory_manager) ?
95+ . try_into ( ) ?;
96+ let addr_point = run_context
97+ . value_from_mem_or_constant ( & self . point , memory_manager) ?
98+ . try_into ( ) ?;
99+ let addr_res = run_context
100+ . value_from_mem_or_fp ( & self . res , memory_manager) ?
101+ . try_into ( ) ?;
102+
103+ // Read the evaluation point from memory using the helper function.
104+ let point = memory_manager
105+ . memory
106+ . get_vectorized_slice_extension ( addr_point, self . n_vars ) ?;
107+
108+ // Read the result of the evaluation from memory.
109+ let res = memory_manager. memory . get_extension ( addr_res) ?;
110+
111+ // Construct and return the witness struct.
112+ Ok ( WitnessMultilinearEval {
113+ cycle,
114+ addr_coeffs,
115+ addr_point,
116+ addr_res,
117+ n_vars : self . n_vars ,
118+ point,
119+ res,
120+ } )
121+ }
79122}
0 commit comments