arnaucube
arnaucube/hyperplonkPublic Self-hosted repository
hyperplonk/hyperplonk/src/witness.rs
hyperplonk/src/witness.rsRaw
 1use crate::{build_mle, errors::HyperPlonkErrors};
 2use ark_ff::PrimeField;
 3use ark_poly::DenseMultilinearExtension;
 4use ark_std::log2;
 5use std::sync::Arc;
 6
 7/// A row of witnesses of width `#wires`
 8#[derive(Debug, Clone)]
 9pub struct WitnessRow<F: PrimeField>(pub(crate) Vec<F>);
10
11/// A column of witnesses of length `#constraints`
12#[derive(Debug, Clone, Default)]
13pub struct WitnessColumn<F: PrimeField>(pub(crate) Vec<F>);
14
15impl<F: PrimeField> WitnessColumn<F> {
16    /// the number of variables of the multilinear polynomial that presents a
17    /// column.
18    pub fn get_nv(&self) -> usize {
19        log2(self.0.len()) as usize
20    }
21
22    /// Append a new element to the witness column
23    pub fn append(&mut self, new_element: F) {
24        self.0.push(new_element)
25    }
26
27    /// Build witness columns from rows
28    pub fn from_witness_rows(
29        witness_rows: &[WitnessRow<F>],
30    ) -> Result<Vec<Self>, HyperPlonkErrors> {
31        if witness_rows.is_empty() {
32            return Err(HyperPlonkErrors::InvalidParameters(
33                "empty witness rows".to_string(),
34            ));
35        }
36
37        let mut res = Vec::with_capacity(witness_rows.len());
38        let num_columns = witness_rows[0].0.len();
39
40        for i in 0..num_columns {
41            let mut cur_column = Vec::new();
42            for row in witness_rows.iter() {
43                cur_column.push(row.0[i])
44            }
45            res.push(Self(cur_column))
46        }
47
48        Ok(res)
49    }
50
51    pub fn coeff_ref(&self) -> &[F] {
52        self.0.as_ref()
53    }
54}
55
56impl<F: PrimeField> From<&WitnessColumn<F>> for DenseMultilinearExtension<F> {
57    fn from(witness: &WitnessColumn<F>) -> Self {
58        let nv = witness.get_nv();
59        Self::from_evaluations_slice(nv, witness.0.as_ref())
60    }
61}
62
63impl<F: PrimeField> WitnessRow<F> {
64    /// Build MLE from matrix of witnesses.
65    ///
66    /// Given a matrix := [row1, row2, ...] where
67    /// row1:= (a1, a2, ...)
68    /// row2:= (b1, b2, ...)
69    /// row3:= (c1, c2, ...)
70    ///
71    /// output mle(a1,b1,c1, ...), mle(a2,b2,c2, ...), ...
72    pub fn build_mles(
73        matrix: &[Self],
74    ) -> Result<Vec<Arc<DenseMultilinearExtension<F>>>, HyperPlonkErrors> {
75        build_mle!(matrix)
76    }
77}