Skip to content

Commit

Permalink
temporary fix for duplicate shape in prover keys
Browse files Browse the repository at this point in the history
  • Loading branch information
srinathsetty committed Oct 9, 2023
1 parent f657684 commit aef6168
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
10 changes: 3 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ use core::marker::PhantomData;
use errors::SpartanError;
use r1cs::{R1CSShape, RelaxedR1CSInstance, RelaxedR1CSWitness};
use serde::{Deserialize, Serialize};
use traits::{
commitment::{CommitmentEngineTrait, CommitmentTrait},
snark::RelaxedR1CSSNARKTrait,
Group,
};
use traits::{commitment::CommitmentEngineTrait, snark::RelaxedR1CSSNARKTrait, Group};

/// A type that holds the prover key
#[derive(Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -112,7 +108,7 @@ impl<G: Group, S: RelaxedR1CSSNARKTrait<G>, C: Circuit<G::Scalar>> SNARK<G, S, C
);

// prove the instance using Spartan
let snark = S::prove(&pk.ck, &pk.pk, &u_relaxed, &w_relaxed)?;
let snark = S::prove(&pk.ck, &pk.pk, &pk.S, &u_relaxed, &w_relaxed)?;

Ok(SNARK {
comm_W: u.comm_W,
Expand All @@ -136,7 +132,7 @@ impl<G: Group, S: RelaxedR1CSSNARKTrait<G>, C: Circuit<G::Scalar>> SNARK<G, S, C

type CommitmentKey<G> = <<G as traits::Group>::CE as CommitmentEngineTrait<G>>::CommitmentKey;
type Commitment<G> = <<G as Group>::CE as CommitmentEngineTrait<G>>::Commitment;
type CompressedCommitment<G> = <<<G as Group>::CE as CommitmentEngineTrait<G>>::Commitment as CommitmentTrait<G>>::CompressedCommitment;
//type CompressedCommitment<G> = <<<G as Group>::CE as CommitmentEngineTrait<G>>::Commitment as CommitmentTrait<G>>::CompressedCommitment;
type CE<G> = <G as Group>::CE;

#[cfg(test)]
Expand Down
6 changes: 4 additions & 2 deletions src/spartan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
//! In polynomial.rs we also provide foundational types and functions for manipulating multilinear polynomials.
pub(crate) mod math;
pub mod polys;
pub mod ppsnark;
// pub mod ppsnark;
pub mod snark;
mod sumcheck;

use crate::{traits::Group, Commitment};
use ff::Field;
use polys::multilinear::SparsePolynomial;
//use polys::multilinear::SparsePolynomial;

fn powers<G: Group>(s: &G::Scalar, n: usize) -> Vec<G::Scalar> {
assert!(n >= 1);
Expand Down Expand Up @@ -57,6 +57,7 @@ impl<G: Group> PolyEvalWitness<G> {
PolyEvalWitness { p }
}

#[allow(dead_code)]
fn batch(p_vec: &[&Vec<G::Scalar>], s: &G::Scalar) -> PolyEvalWitness<G> {
let powers_of_s = powers::<G>(s, p_vec.len());
let mut p = vec![G::Scalar::ZERO; p_vec[0].len()];
Expand Down Expand Up @@ -96,6 +97,7 @@ impl<G: Group> PolyEvalInstance<G> {
}
}

#[allow(dead_code)]
fn batch(
c_vec: &[Commitment<G>],
x: &[G::Scalar],
Expand Down
19 changes: 9 additions & 10 deletions src/spartan/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use serde::{Deserialize, Serialize};
#[serde(bound = "")]
pub struct ProverKey<G: Group, EE: EvaluationEngineTrait<G>> {
pk_ee: EE::ProverKey,
S: R1CSShape<G>,
vk_digest: G::Scalar, // digest of the verifier's key
}

Expand Down Expand Up @@ -100,7 +99,6 @@ impl<G: Group, EE: EvaluationEngineTrait<G>> RelaxedR1CSSNARKTrait<G> for Relaxe

let pk = ProverKey {
pk_ee,
S,
vk_digest: vk.digest(),
};

Expand All @@ -111,14 +109,15 @@ impl<G: Group, EE: EvaluationEngineTrait<G>> RelaxedR1CSSNARKTrait<G> for Relaxe
fn prove(
ck: &CommitmentKey<G>,
pk: &Self::ProverKey,
S: &R1CSShape<G>,
U: &RelaxedR1CSInstance<G>,
W: &RelaxedR1CSWitness<G>,
) -> Result<Self, SpartanError> {
let W = W.pad(&pk.S); // pad the witness
let W = W.pad(S); // pad the witness
let mut transcript = G::TE::new(b"RelaxedR1CSSNARK");

// sanity check that R1CSShape has certain size characteristics
pk.S.check_regular_shape();
S.check_regular_shape();

// append the digest of vk (which includes R1CS matrices) and the RelaxedR1CSInstance to the transcript
transcript.absorb(b"vk", &pk.vk_digest);
Expand All @@ -128,8 +127,8 @@ impl<G: Group, EE: EvaluationEngineTrait<G>> RelaxedR1CSSNARKTrait<G> for Relaxe
let mut z = [W.W.clone(), vec![U.u], U.X.clone()].concat();

let (num_rounds_x, num_rounds_y) = (
usize::try_from(pk.S.num_cons.ilog2()).unwrap(),
(usize::try_from(pk.S.num_vars.ilog2()).unwrap() + 1),
usize::try_from(S.num_cons.ilog2()).unwrap(),
(usize::try_from(S.num_vars.ilog2()).unwrap() + 1),
);

// outer sum-check
Expand All @@ -139,8 +138,8 @@ impl<G: Group, EE: EvaluationEngineTrait<G>> RelaxedR1CSSNARKTrait<G> for Relaxe

let mut poly_tau = MultilinearPolynomial::new(EqPolynomial::new(tau).evals());
let (mut poly_Az, mut poly_Bz, poly_Cz, mut poly_uCz_E) = {
let (poly_Az, poly_Bz, poly_Cz) = pk.S.multiply_vec(&z)?;
let poly_uCz_E = (0..pk.S.num_cons)
let (poly_Az, poly_Bz, poly_Cz) = S.multiply_vec(&z)?;
let poly_uCz_E = (0..S.num_cons)
.map(|i| U.u * poly_Cz[i] + W.E[i])
.collect::<Vec<G::Scalar>>();
(
Expand Down Expand Up @@ -221,7 +220,7 @@ impl<G: Group, EE: EvaluationEngineTrait<G>> RelaxedR1CSSNARKTrait<G> for Relaxe
(A_evals, B_evals, C_evals)
};

let (evals_A, evals_B, evals_C) = compute_eval_table_sparse(&pk.S, &evals_rx);
let (evals_A, evals_B, evals_C) = compute_eval_table_sparse(S, &evals_rx);

assert_eq!(evals_A.len(), evals_B.len());
assert_eq!(evals_A.len(), evals_C.len());
Expand All @@ -232,7 +231,7 @@ impl<G: Group, EE: EvaluationEngineTrait<G>> RelaxedR1CSSNARKTrait<G> for Relaxe
};

let poly_z = {
z.resize(pk.S.num_vars * 2, G::Scalar::ZERO);
z.resize(S.num_vars * 2, G::Scalar::ZERO);
z
};

Expand Down
1 change: 1 addition & 0 deletions src/traits/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub trait RelaxedR1CSSNARKTrait<G: Group>:
fn prove(
ck: &CommitmentKey<G>,
pk: &Self::ProverKey,
S: &R1CSShape<G>,
U: &RelaxedR1CSInstance<G>,
W: &RelaxedR1CSWitness<G>,
) -> Result<Self, SpartanError>;
Expand Down

0 comments on commit aef6168

Please sign in to comment.