diff --git a/src/spartan/polys/power.rs b/src/spartan/polys/power.rs index fd6ef50a..13d52ba0 100644 --- a/src/spartan/polys/power.rs +++ b/src/spartan/polys/power.rs @@ -27,7 +27,7 @@ impl PowPolynomial { /// Create powers the following powers of `t`: /// [t^{2^0}, t^{2^1}, ..., t^{2^{ell-1}}] - pub(in crate::spartan) fn squares(t: &Scalar, ell: usize) -> Vec { + pub fn squares(t: &Scalar, ell: usize) -> Vec { successors(Some(*t), |p: &Scalar| Some(p.square())) .take(ell) .collect::>() diff --git a/src/spartan/ppsnark.rs b/src/spartan/ppsnark.rs index c4a152ad..60681e51 100644 --- a/src/spartan/ppsnark.rs +++ b/src/spartan/ppsnark.rs @@ -19,7 +19,7 @@ use crate::{ univariate::{CompressedUniPoly, UniPoly}, }, powers, - sumcheck::SumcheckProof, + sumcheck::{SumcheckEngine, SumcheckProof}, PolyEvalInstance, PolyEvalWitness, SparsePolynomial, }, traits::{ @@ -174,7 +174,7 @@ impl R1CSShapeSparkRepr { } } - pub(in crate::spartan) fn commit(&self, ck: &CommitmentKey) -> R1CSShapeSparkCommitment { + fn commit(&self, ck: &CommitmentKey) -> R1CSShapeSparkCommitment { let comm_vec: Vec> = [ &self.row, &self.col, @@ -237,27 +237,6 @@ impl R1CSShapeSparkRepr { } } -/// Defines a trait for implementing sum-check in a generic manner -pub trait SumcheckEngine: Send + Sync { - /// returns the initial claims - fn initial_claims(&self) -> Vec; - - /// degree of the sum-check polynomial - fn degree(&self) -> usize; - - /// the size of the polynomials - fn size(&self) -> usize; - - /// returns evaluation points at 0, 2, d-1 (where d is the degree of the sum-check polynomial) - fn evaluation_points(&self) -> Vec>; - - /// bounds a variable in the constituent polynomials - fn bound(&mut self, r: &E::Scalar); - - /// returns the final claims - fn final_claims(&self) -> Vec>; -} - /// The [WitnessBoundSumcheck] ensures that the witness polynomial W defined over n = log(N) variables, /// is zero outside of the first `num_vars = 2^m` entries. /// @@ -272,13 +251,13 @@ pub trait SumcheckEngine: Send + Sync { /// It is equivalent to the expression /// `0 = ∑_{2^m≤i<2^n} eq[i] * W[i]` /// Since `eq` is random, the instance is only satisfied if `W[2^{m}..] = 0`. -pub(in crate::spartan) struct WitnessBoundSumcheck { +pub struct WitnessBoundSumcheck { poly_W: MultilinearPolynomial, poly_masked_eq: MultilinearPolynomial, } impl WitnessBoundSumcheck { - pub fn new(tau: E::Scalar, poly_W_padded: Vec, num_vars: usize) -> Self { + fn new(tau: E::Scalar, poly_W_padded: Vec, num_vars: usize) -> Self { let num_vars_log = num_vars.log_2(); // When num_vars = num_rounds, we shouldn't have to prove anything // but we still want this instance to compute the evaluation of W @@ -336,7 +315,7 @@ impl SumcheckEngine for WitnessBoundSumcheck { } } -pub(in crate::spartan) struct MemorySumcheckInstance { +struct MemorySumcheckInstance { // row w_plus_r_row: MultilinearPolynomial, t_plus_r_row: MultilinearPolynomial, @@ -1193,10 +1172,11 @@ impl> RelaxedR1CSSNARKTrait for Relax let w: PolyEvalWitness = PolyEvalWitness::batch(&poly_vec, &c); let u: PolyEvalInstance = PolyEvalInstance::batch(&comm_vec, &tau_coords, &eval_vec, &c); - // we now need to prove three claims + // we now need to prove four claims // (1) 0 = \sum_x poly_tau(x) * (poly_Az(x) * poly_Bz(x) - poly_uCz_E(x)), and eval_Az_at_tau + r * eval_Bz_at_tau + r^2 * eval_Cz_at_tau = (Az+r*Bz+r^2*Cz)(tau) // (2) eval_Az_at_tau + c * eval_Bz_at_tau + c^2 * eval_Cz_at_tau = \sum_y L_row(y) * (val_A(y) + c * val_B(y) + c^2 * val_C(y)) * L_col(y) // (3) L_row(i) = eq(tau, row(i)) and L_col(i) = z(col(i)) + // (4) Check that the witness polynomial W is well-formed e.g., it is padded with only zeros let gamma = transcript.squeeze(b"g")?; let r = transcript.squeeze(b"r")?; diff --git a/src/spartan/snark.rs b/src/spartan/snark.rs index 9755f9fe..08aa12d2 100644 --- a/src/spartan/snark.rs +++ b/src/spartan/snark.rs @@ -245,7 +245,7 @@ impl> RelaxedR1CSSNARKTrait for Relax ]; let (batched_u, batched_w, sc_proof_batch, claims_batch_left) = - batch_eval_prove(u_vec, w_vec, &mut transcript)?; + batch_eval_reduce(u_vec, w_vec, &mut transcript)?; let eval_arg = EE::prove( ck, @@ -410,8 +410,8 @@ impl> RelaxedR1CSSNARKTrait for Relax } } -/// Proves a batch of polynomial evaluation claims using Sumcheck -/// reducing them to a single claim at the same point. +/// Reduces a batch of polynomial evaluation claims using Sumcheck +/// to a single claim at the same point. /// /// # Details /// @@ -424,7 +424,7 @@ impl> RelaxedR1CSSNARKTrait for Relax /// /// We allow the polynomial Pᵢ to have different sizes, by appropriately scaling /// the claims and resulting evaluations from Sumcheck. -pub(in crate::spartan) fn batch_eval_prove( +fn batch_eval_reduce( u_vec: Vec>, w_vec: Vec>, transcript: &mut E::TE, @@ -498,7 +498,7 @@ pub(in crate::spartan) fn batch_eval_prove( /// Verifies a batch of polynomial evaluation claims using Sumcheck /// reducing them to a single claim at the same point. -pub(in crate::spartan) fn batch_eval_verify( +fn batch_eval_verify( u_vec: Vec>, transcript: &mut E::TE, sc_proof_batch: &SumcheckProof, diff --git a/src/spartan/sumcheck.rs b/src/spartan/sumcheck.rs index 33759983..93c75a7f 100644 --- a/src/spartan/sumcheck.rs +++ b/src/spartan/sumcheck.rs @@ -9,6 +9,27 @@ use itertools::Itertools as _; use rayon::prelude::*; use serde::{Deserialize, Serialize}; +/// Defines a trait for implementing sum-check in a generic manner +pub trait SumcheckEngine: Send + Sync { + /// returns the initial claims + fn initial_claims(&self) -> Vec; + + /// degree of the sum-check polynomial + fn degree(&self) -> usize; + + /// the size of the polynomials + fn size(&self) -> usize; + + /// returns evaluation points at 0, 2, d-1 (where d is the degree of the sum-check polynomial) + fn evaluation_points(&self) -> Vec>; + + /// bounds a variable in the constituent polynomials + fn bound(&mut self, r: &E::Scalar); + + /// returns the final claims + fn final_claims(&self) -> Vec>; +} + #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(bound = "")] pub(crate) struct SumcheckProof { @@ -298,7 +319,7 @@ impl SumcheckProof { } #[inline] - pub(in crate::spartan) fn compute_eval_points_cubic( + pub fn compute_eval_points_cubic( poly_A: &MultilinearPolynomial, poly_B: &MultilinearPolynomial, poly_C: &MultilinearPolynomial, @@ -342,7 +363,7 @@ impl SumcheckProof { } #[inline] - pub(in crate::spartan) fn compute_eval_points_cubic_with_additive_term( + pub fn compute_eval_points_cubic_with_additive_term( poly_A: &MultilinearPolynomial, poly_B: &MultilinearPolynomial, poly_C: &MultilinearPolynomial,