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