Skip to content

Commit

Permalink
Replace DensePolynomial
Browse files Browse the repository at this point in the history
  • Loading branch information
darth-cy committed Feb 12, 2025
1 parent b1442de commit 23fe607
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 281 deletions.
55 changes: 28 additions & 27 deletions spartan_parallel/src/custom_dense_mlpoly.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::too_many_arguments)]
use std::cmp::min;

use crate::dense_mlpoly::DensePolynomial;
use multilinear_extensions::mle::DenseMultilinearExtension;
use crate::math::Math;
use ff_ext::ExtensionField;
use rayon::prelude::*;
Expand Down Expand Up @@ -366,30 +366,31 @@ impl<E: ExtensionField> DensePolynomialPqx<E> {
return cl.index(0, 0, 0, 0);
}

// Convert to a (p, q_rev, x_rev) regular dense poly of form (p, q, x)
pub fn to_dense_poly(&self) -> DensePolynomial<E> {
let ZERO = E::ZERO;

let p_space = self.num_vars_p.pow2();
let q_space = self.num_vars_q.pow2();
let w_space = self.num_vars_w.pow2();
let x_space = self.num_vars_x.pow2();

let mut Z_poly = vec![ZERO; p_space * q_space * w_space * x_space];
for p in 0..self.num_instances {
for q in 0..self.num_proofs[p] {
for w in 0..self.num_witness_secs {
for x in 0..self.num_inputs[p][w] {
Z_poly[
p * q_space * w_space * x_space
+ q * w_space * x_space
+ w * x_space
+ x
] = self.Z[p][q][w][x];
}
}
}
}
DensePolynomial::new(Z_poly)
}
// _debug
// // Convert to a (p, q_rev, x_rev) regular dense poly of form (p, q, x)
// pub fn to_dense_poly(&self) -> DensePolynomial<E> {
// let ZERO = E::ZERO;

// let p_space = self.num_vars_p.pow2();
// let q_space = self.num_vars_q.pow2();
// let w_space = self.num_vars_w.pow2();
// let x_space = self.num_vars_x.pow2();

// let mut Z_poly = vec![ZERO; p_space * q_space * w_space * x_space];
// for p in 0..self.num_instances {
// for q in 0..self.num_proofs[p] {
// for w in 0..self.num_witness_secs {
// for x in 0..self.num_inputs[p][w] {
// Z_poly[
// p * q_space * w_space * x_space
// + q * w_space * x_space
// + w * x_space
// + x
// ] = self.Z[p][q][w][x];
// }
// }
// }
// }
// DensePolynomial::new(Z_poly)
// }
}
9 changes: 9 additions & 0 deletions spartan_parallel/src/dense_mlpoly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ use rayon::{iter::ParallelIterator, slice::ParallelSliceMut};
use serde::{Deserialize, Serialize};
use std::cmp::min;

// _debug
/*
#[derive(Debug, Clone)]
pub struct DensePolynomial<E: ExtensionField> {
num_vars: usize, // the number of variables in the multilinear polynomial
len: usize,
Z: Vec<E>, // evaluations of the polynomial in all the 2^num_vars Boolean inputs
}
*/

pub struct EqPolynomial<E: ExtensionField> {
r: Vec<E>,
Expand Down Expand Up @@ -112,6 +115,8 @@ impl<E: ExtensionField> IdentityPolynomial<E> {
}
}

// _debug
/*
impl<E: ExtensionField> DensePolynomial<E> {
pub fn new(mut Z: Vec<E>) -> Self {
// If length of Z is not a power of 2, append Z with 0
Expand Down Expand Up @@ -352,7 +357,10 @@ impl<E: ExtensionField> DensePolynomial<E> {
DenseMultilinearExtension::from_evaluation_vec_smart(self.num_vars, self.Z.clone())
}
}
*/

// _debug
/*
impl<E: ExtensionField> Index<usize> for DensePolynomial<E> {
type Output = E;
Expand All @@ -361,6 +369,7 @@ impl<E: ExtensionField> Index<usize> for DensePolynomial<E> {
&(self.Z[_index])
}
}
*/

/*
#[cfg(test)]
Expand Down
23 changes: 11 additions & 12 deletions spartan_parallel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ use std::{
io::Write, marker::PhantomData,
};

use dense_mlpoly::DensePolynomial;
use multilinear_extensions::mle::{DenseMultilinearExtension, MultilinearExtension};
use errors::{ProofVerifyError, R1CSError};
use instance::Instance;
use itertools::Itertools;
use math::Math;
use multilinear_extensions::mle::DenseMultilinearExtension;
use mpcs::{PolynomialCommitmentScheme, ProverParam};
use r1csinstance::{R1CSCommitment, R1CSDecommitment, R1CSEvalProof, R1CSInstance};
use r1csproof::R1CSProof;
Expand Down Expand Up @@ -155,7 +154,7 @@ struct IOProofs<E: ExtensionField, Pcs: PolynomialCommitmentScheme<E>> {
impl<E: ExtensionField, Pcs: PolynomialCommitmentScheme<E>> IOProofs<E, Pcs> {
// Given the polynomial in execution order, generate all proofs
fn prove(
exec_poly_inputs: &DensePolynomial<E>,
exec_poly_inputs: &DenseMultilinearExtension<E>,

num_ios: usize,
num_inputs_unpadded: usize,
Expand Down Expand Up @@ -334,8 +333,8 @@ struct ShiftProofs<E: ExtensionField, Pcs: PolynomialCommitmentScheme<E>> {

impl<E: ExtensionField, Pcs: PolynomialCommitmentScheme<E>> ShiftProofs<E, Pcs> {
fn prove(
orig_polys: Vec<&DensePolynomial<E>>,
shifted_polys: Vec<&DensePolynomial<E>>,
orig_polys: Vec<&DenseMultilinearExtension<E>>,
shifted_polys: Vec<&DenseMultilinearExtension<E>>,
// For each orig_poly, how many entries at the front of proof 0 are non-zero?
header_len_list: Vec<usize>,
transcript: &mut Transcript<E>,
Expand All @@ -346,11 +345,11 @@ impl<E: ExtensionField, Pcs: PolynomialCommitmentScheme<E>> ShiftProofs<E, Pcs>
assert_eq!(num_instances, shifted_polys.len());
let max_poly_size = orig_polys
.iter()
.fold(0, |m, p| if p.len() > m { p.len() } else { m });
.fold(0, |m, p| if p.evaluations().len() > m { p.evaluations().len() } else { m });
let max_poly_size =
shifted_polys
.iter()
.fold(max_poly_size, |m, p| if p.len() > m { p.len() } else { m });
.fold(max_poly_size, |m, p| if p.evaluations().len() > m { p.evaluations().len() } else { m });
// Open entry 0..header_len_list[p] - 1
for p in 0..num_instances {
for _i in 0..header_len_list[p] {}
Expand All @@ -368,9 +367,9 @@ impl<E: ExtensionField, Pcs: PolynomialCommitmentScheme<E>> ShiftProofs<E, Pcs>
for p in 0..num_instances {
let orig_poly = orig_polys[p];
let shifted_poly = shifted_polys[p];
let orig_eval = (0..orig_poly.len()).fold(E::ZERO, |a, b| a + orig_poly[b] * rc[b]);
let orig_eval = (0..orig_poly.evaluations().len()).fold(E::ZERO, |a, b| a + orig_poly.get_ext_field_vec()[b] * rc[b]);
let shifted_eval =
(0..shifted_poly.len()).fold(E::ZERO, |a, b| a + shifted_poly[b] * rc[b]);
(0..shifted_poly.evaluations().len()).fold(E::ZERO, |a, b| a + shifted_poly.get_ext_field_vec()[b] * rc[b]);
orig_evals.push(orig_eval);
shifted_evals.push(shifted_eval);
}
Expand Down Expand Up @@ -2826,7 +2825,7 @@ impl<E: ExtensionField + Send + Sync, Pcs: PolynomialCommitmentScheme<E>> SNARK<
}
perm_w0.extend(vec![E::ZERO; num_ios - 2 * num_inputs_unpadded]);
// create a multilinear polynomial using the supplied assignment for variables
let _perm_poly_w0 = DensePolynomial::new(perm_w0.clone());
let _perm_poly_w0: DenseMultilinearExtension<E> = DenseMultilinearExtension::from_evaluation_vec_smart(perm_w0.len().log_2(), perm_w0);

// block_w2
let block_w2_verifier = {
Expand Down Expand Up @@ -2989,7 +2988,7 @@ impl<E: ExtensionField + Send + Sync, Pcs: PolynomialCommitmentScheme<E>> SNARK<
]
.concat();
// create a multilinear polynomial using the supplied assignment for variables
let _poly_init_stacks = DensePolynomial::new(init_stacks.clone());
let _poly_init_stacks: DenseMultilinearExtension<E> = DenseMultilinearExtension::from_evaluation_vec_smart(init_stacks.len().log_2(), init_stacks);
VerifierWitnessSecInfo::new(
vec![INIT_PHY_MEM_WIDTH],
&vec![total_num_init_phy_mem_accesses],
Expand Down Expand Up @@ -3024,7 +3023,7 @@ impl<E: ExtensionField + Send + Sync, Pcs: PolynomialCommitmentScheme<E>> SNARK<
]
.concat();
// create a multilinear polynomial using the supplied assignment for variables
let _poly_init_mems = DensePolynomial::new(init_mems.clone());
let _poly_init_mems: DenseMultilinearExtension<E> = DenseMultilinearExtension::from_evaluation_vec_smart(init_mems.len().log_2(), init_mems);
VerifierWitnessSecInfo::new(
vec![INIT_VIR_MEM_WIDTH],
&vec![total_num_init_vir_mem_accesses],
Expand Down
Loading

0 comments on commit 23fe607

Please sign in to comment.