|
| 1 | +use p3_field::BasedVectorSpace; |
| 2 | +use whir_p3::poly::{coeffs::CoefficientList, multilinear::MultilinearPoint}; |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + bytecode::operand::{MemOrConstant, MemOrFp}, |
| 6 | + constant::{DIMENSION, EF, F}, |
| 7 | + context::run_context::RunContext, |
| 8 | + errors::vm::VirtualMachineError, |
| 9 | + memory::{address::MemoryAddress, manager::MemoryManager}, |
| 10 | +}; |
| 11 | + |
| 12 | +/// An instruction to evaluate a multilinear polynomial at a point in the extension field. |
| 13 | +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 14 | +pub struct MultilinearEvalInstruction { |
| 15 | + /// A pointer to the polynomial's coefficients. |
| 16 | + pub coeffs: MemOrConstant, |
| 17 | + /// A pointer to the evaluation point's coordinates. |
| 18 | + pub point: MemOrConstant, |
| 19 | + /// The destination pointer for the result. |
| 20 | + pub res: MemOrFp, |
| 21 | + /// The number of variables in the multilinear polynomial. |
| 22 | + pub n_vars: usize, |
| 23 | +} |
| 24 | + |
| 25 | +impl MultilinearEvalInstruction { |
| 26 | + /// Executes the `MultilinearEval` instruction. |
| 27 | + /// |
| 28 | + /// This function performs the following steps: |
| 29 | + /// 1. Resolves pointers to the polynomial coefficients, evaluation point, and output location. |
| 30 | + /// 2. Reads the polynomial coefficients (base field elements) from memory. |
| 31 | + /// 3. Reads the evaluation point (extension field elements) from memory. |
| 32 | + /// 4. Evaluates the polynomial at the given point. |
| 33 | + /// 5. Writes the resulting extension field element back to memory. |
| 34 | + pub fn execute( |
| 35 | + &self, |
| 36 | + run_context: &RunContext, |
| 37 | + memory_manager: &mut MemoryManager, |
| 38 | + ) -> Result<(), VirtualMachineError> { |
| 39 | + // Resolve the memory addresses for the coefficients, point, and result. |
| 40 | + let ptr_coeffs: MemoryAddress = run_context |
| 41 | + .value_from_mem_or_constant(&self.coeffs, memory_manager)? |
| 42 | + .try_into()?; |
| 43 | + let ptr_point: MemoryAddress = run_context |
| 44 | + .value_from_mem_or_constant(&self.point, memory_manager)? |
| 45 | + .try_into()?; |
| 46 | + let ptr_res: MemoryAddress = run_context |
| 47 | + .value_from_mem_or_fp(&self.res, memory_manager)? |
| 48 | + .try_into()?; |
| 49 | + |
| 50 | + // Read the polynomial coefficients from memory. |
| 51 | + // |
| 52 | + // The total number of coefficients is 2^n_vars. |
| 53 | + let num_coeffs = 1 << self.n_vars; |
| 54 | + let slice_coeffs: Vec<F> = (0..num_coeffs) |
| 55 | + .map(|i| { |
| 56 | + let addr = (ptr_coeffs + i)?; |
| 57 | + memory_manager.memory.get_as(addr) |
| 58 | + }) |
| 59 | + .collect::<Result<_, _>>()?; |
| 60 | + |
| 61 | + // Read the evaluation point from memory. |
| 62 | + let point: Vec<EF> = (0..self.n_vars) |
| 63 | + .map(|i| { |
| 64 | + let addr = (ptr_point + i)?; |
| 65 | + let vector_coeffs = memory_manager.memory.get_array_as::<F, DIMENSION>(addr)?; |
| 66 | + EF::from_basis_coefficients_slice(&vector_coeffs) |
| 67 | + .ok_or(VirtualMachineError::InvalidExtensionField) |
| 68 | + }) |
| 69 | + .collect::<Result<_, _>>()?; |
| 70 | + |
| 71 | + // Evaluate the multilinear polynomial. |
| 72 | + let eval = CoefficientList::new(slice_coeffs).evaluate(&MultilinearPoint(point)); |
| 73 | + |
| 74 | + // Write the resulting vector back to memory. |
| 75 | + memory_manager.load_data::<F>(ptr_res, eval.as_basis_coefficients_slice())?; |
| 76 | + |
| 77 | + Ok(()) |
| 78 | + } |
| 79 | +} |
0 commit comments