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::rc::Rc;
 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)]
13pub struct WitnessColumn<F: PrimeField>(pub(crate) Vec<F>);
14
15impl<F: PrimeField> WitnessColumn<F> {
16    /// the number of variables for MLE to present a column.
17    pub fn get_nv(&self) -> usize {
18        log2(self.0.len()) as usize
19    }
20
21    /// Build witness columns from rows
22    pub fn from_witness_rows(
23        witness_rows: &[WitnessRow<F>],
24    ) -> Result<Vec<Self>, HyperPlonkErrors> {
25        if witness_rows.is_empty() {
26            return Err(HyperPlonkErrors::InvalidParameters(
27                "empty witness rows".to_string(),
28            ));
29        }
30
31        let mut res = Vec::with_capacity(witness_rows.len());
32        let num_wires = witness_rows[0].0.len();
33
34        for i in 0..num_wires {
35            let mut cur_column = Vec::new();
36            for row in witness_rows.iter() {
37                cur_column.push(row.0[i])
38            }
39            res.push(Self(cur_column))
40        }
41
42        Ok(res)
43    }
44}
45
46impl<F: PrimeField> From<&WitnessColumn<F>> for DenseMultilinearExtension<F> {
47    fn from(witness: &WitnessColumn<F>) -> Self {
48        let nv = witness.get_nv();
49        Self::from_evaluations_slice(nv, witness.0.as_ref())
50    }
51}
52
53impl<F: PrimeField> WitnessRow<F> {
54    /// Build MLE from matrix of witnesses.
55    ///
56    /// Given a matrix := [row1, row2, ...] where
57    /// row1:= (a1, a2, ...)
58    /// row2:= (b1, b2, ...)
59    /// row3:= (c1, c2, ...)
60    ///
61    /// output mle(a1,b1,c1, ...), mle(a2,b2,c2, ...), ...
62    pub fn build_mles(
63        matrix: &[Self],
64    ) -> Result<Vec<Rc<DenseMultilinearExtension<F>>>, HyperPlonkErrors> {
65        build_mle!(matrix)
66    }
67}