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