arnaucube
arnaucube/hyperplonkPublic Self-hosted repository
hyperplonk/hyperplonk/src/selectors.rs
hyperplonk/src/selectors.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 selector of width `#selectors`
 8#[derive(Debug, Clone, PartialEq, Eq)]
 9pub struct SelectorRow<F: PrimeField>(pub(crate) Vec<F>);
10
11/// A column of selectors of length `#constraints`
12#[derive(Debug, Clone, PartialEq, Eq, Default)]
13pub struct SelectorColumn<F: PrimeField>(pub(crate) Vec<F>);
14
15impl<F: PrimeField> SelectorColumn<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 selector column
23    pub fn append(&mut self, new_element: F) {
24        self.0.push(new_element)
25    }
26
27    /// Build selector columns from rows
28    pub fn from_selector_rows(
29        selector_rows: &[SelectorRow<F>],
30    ) -> Result<Vec<Self>, HyperPlonkErrors> {
31        if selector_rows.is_empty() {
32            return Err(HyperPlonkErrors::InvalidParameters(
33                "empty witness rows".to_string(),
34            ));
35        }
36
37        let mut res = Vec::with_capacity(selector_rows.len());
38        let num_colnumns = selector_rows[0].0.len();
39
40        for i in 0..num_colnumns {
41            let mut cur_column = Vec::new();
42            for row in selector_rows.iter() {
43                cur_column.push(row.0[i])
44            }
45            res.push(Self(cur_column))
46        }
47
48        Ok(res)
49    }
50}
51
52impl<F: PrimeField> From<&SelectorColumn<F>> for DenseMultilinearExtension<F> {
53    fn from(witness: &SelectorColumn<F>) -> Self {
54        let nv = witness.get_nv();
55        Self::from_evaluations_slice(nv, witness.0.as_ref())
56    }
57}
58
59impl<F: PrimeField> SelectorRow<F> {
60    /// Build MLE from matrix of selectors.
61    ///
62    /// Given a matrix := [row1, row2, ...] where
63    /// row1:= (a1, a2, ...)
64    /// row2:= (b1, b2, ...)
65    /// row3:= (c1, c2, ...)
66    ///
67    /// output mle(a1,b1,c1, ...), mle(a2,b2,c2, ...), ...
68    pub fn build_mles(
69        matrix: &[Self],
70    ) -> Result<Vec<Arc<DenseMultilinearExtension<F>>>, HyperPlonkErrors> {
71        build_mle!(matrix)
72    }
73}