Skip to content

Commit

Permalink
refactor: Remove clone trait from key and SNARK structs
Browse files Browse the repository at this point in the history
- Removed `Clone` trait from various structs across multiple files, including `ProverKey`, `VerifierKey`, and multiple versions of `RelaxedR1CSSNARK` and `BatchedRelaxedR1CSSNARK`, among others.
- Instituted tests to confirm that the `Clone` trait is **not** implemented for previously mentioned structs where Clone will not make sense.
- Added a new `static_assertions` dependency to the project configuration file, Cargo.toml.
  • Loading branch information
huitseeker committed Jan 17, 2024
1 parent 1b02239 commit 6b9cee9
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ itertools = "0.12.0"
rand = "0.8.5"
ref-cast = "1.0.20"
derive_more = "0.99.17"
static_assertions = "1.1.0"

[target.'cfg(any(target_arch = "x86_64", target_arch = "aarch64"))'.dependencies]
# grumpkin-msm has been patched to support MSMs for the pasta curve cycle
Expand Down
21 changes: 18 additions & 3 deletions src/spartan/batched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::{
/// A succinct proof of knowledge of a witness to a batch of relaxed R1CS instances
/// The proof is produced using Spartan's combination of the sum-check and
/// the commitment to a vector viewed as a polynomial commitment
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct BatchedRelaxedR1CSSNARK<E: Engine, EE: EvaluationEngineTrait<E>> {
sc_proof_outer: SumcheckProof<E>,
Expand All @@ -59,7 +59,7 @@ pub struct BatchedRelaxedR1CSSNARK<E: Engine, EE: EvaluationEngineTrait<E>> {
}

/// A type that represents the prover's key
#[derive(Clone, Serialize, Deserialize, Abomonation)]
#[derive(Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(where <E::Scalar as ff::PrimeField>::Repr: Abomonation)]
pub struct ProverKey<E: Engine, EE: EvaluationEngineTrait<E>> {
Expand All @@ -69,7 +69,7 @@ pub struct ProverKey<E: Engine, EE: EvaluationEngineTrait<E>> {
}

/// A type that represents the verifier's key
#[derive(Clone, Serialize, Deserialize, Abomonation)]
#[derive(Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(where <E::Scalar as ff::PrimeField>::Repr: Abomonation)]
pub struct VerifierKey<E: Engine, EE: EvaluationEngineTrait<E>> {
Expand Down Expand Up @@ -625,3 +625,18 @@ where
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::{BatchedRelaxedR1CSSNARK, ProverKey, VerifierKey};
use static_assertions::assert_not_impl_any;

use crate::provider::{ipa_pc::EvaluationEngine, PallasEngine};

#[test]
fn test_keys_and_snarks_should_not_be_cloned() {
assert_not_impl_any!(ProverKey<PallasEngine, EvaluationEngine<PallasEngine>>: Clone);
assert_not_impl_any!(VerifierKey<PallasEngine, EvaluationEngine<PallasEngine>>: Clone);
assert_not_impl_any!(BatchedRelaxedR1CSSNARK<PallasEngine, EvaluationEngine<PallasEngine>>: Clone);
}
}
21 changes: 18 additions & 3 deletions src/spartan/batched_ppsnark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use rayon::prelude::*;
use serde::{Deserialize, Serialize};

/// A type that represents the prover's key
#[derive(Clone, Serialize, Deserialize, Abomonation)]
#[derive(Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(where < E::Scalar as PrimeField >::Repr: Abomonation)]
pub struct ProverKey<E: Engine, EE: EvaluationEngineTrait<E>> {
Expand All @@ -55,7 +55,7 @@ pub struct ProverKey<E: Engine, EE: EvaluationEngineTrait<E>> {
}

/// A type that represents the verifier's key
#[derive(Clone, Serialize, Deserialize, Abomonation)]
#[derive(Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(where < E::Scalar as PrimeField >::Repr: Abomonation)]
pub struct VerifierKey<E: Engine, EE: EvaluationEngineTrait<E>> {
Expand Down Expand Up @@ -99,7 +99,7 @@ impl<E: Engine, EE: EvaluationEngineTrait<E>> DigestHelperTrait<E> for VerifierK
/// A succinct proof of knowledge of a witness to a relaxed R1CS instance
/// The proof is produced using Spartan's combination of the sum-check and
/// the commitment to a vector viewed as a polynomial commitment
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct BatchedRelaxedR1CSSNARK<E: Engine, EE: EvaluationEngineTrait<E>> {
// commitment to oracles: the first three are for Az, Bz, Cz,
Expand Down Expand Up @@ -1361,3 +1361,18 @@ where
.collect()
}
}

#[cfg(test)]
mod tests {
use super::{BatchedRelaxedR1CSSNARK, ProverKey, VerifierKey};
use static_assertions::assert_not_impl_any;

use crate::provider::{ipa_pc::EvaluationEngine, PallasEngine};

#[test]
fn test_keys_and_snarks_should_not_be_cloned() {
assert_not_impl_any!(ProverKey<PallasEngine, EvaluationEngine<PallasEngine>>: Clone);
assert_not_impl_any!(VerifierKey<PallasEngine, EvaluationEngine<PallasEngine>>: Clone);
assert_not_impl_any!(BatchedRelaxedR1CSSNARK<PallasEngine, EvaluationEngine<PallasEngine>>: Clone);
}
}
2 changes: 1 addition & 1 deletion src/spartan/polys/univariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct UniPoly<Scalar> {

// ax^2 + bx + c stored as vec![c, a]
// ax^3 + bx^2 + cx + d stored as vec![d, c, a]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct CompressedUniPoly<Scalar> {
coeffs_except_linear_term: Vec<Scalar>,
}
Expand Down
19 changes: 15 additions & 4 deletions src/spartan/ppsnark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn padded<E: Engine>(v: &[E::Scalar], n: usize, e: &E::Scalar) -> Vec<E::Scalar>
}

/// A type that holds `R1CSShape` in a form amenable to memory checking
#[derive(Clone, Serialize, Deserialize, Abomonation)]
#[derive(Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(where <E::Scalar as PrimeField>::Repr: Abomonation)]
pub struct R1CSShapeSparkRepr<E: Engine> {
Expand Down Expand Up @@ -255,7 +255,7 @@ impl<E: Engine> R1CSShapeSparkRepr<E> {
}

/// A type that represents the prover's key
#[derive(Clone, Serialize, Deserialize, Abomonation)]
#[derive(Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(where <E::Scalar as PrimeField>::Repr: Abomonation)]
pub struct ProverKey<E: Engine, EE: EvaluationEngineTrait<E>> {
Expand All @@ -267,7 +267,7 @@ pub struct ProverKey<E: Engine, EE: EvaluationEngineTrait<E>> {
}

/// A type that represents the verifier's key
#[derive(Clone, Serialize, Deserialize, Abomonation)]
#[derive(Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(where <E::Scalar as PrimeField>::Repr: Abomonation)]
pub struct VerifierKey<E: Engine, EE: EvaluationEngineTrait<E>> {
Expand All @@ -285,7 +285,7 @@ impl<E: Engine, EE: EvaluationEngineTrait<E>> SimpleDigestible for VerifierKey<E
/// A succinct proof of knowledge of a witness to a relaxed R1CS instance
/// The proof is produced using Spartan's combination of the sum-check and
/// the commitment to a vector viewed as a polynomial commitment
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct RelaxedR1CSSNARK<E: Engine, EE: EvaluationEngineTrait<E>> {
// commitment to oracles: the first three are for Az, Bz, Cz,
Expand Down Expand Up @@ -1071,8 +1071,12 @@ where
}
#[cfg(test)]
mod tests {
use crate::provider::ipa_pc::EvaluationEngine;
use crate::provider::PallasEngine;

use super::{ProverKey, RelaxedR1CSSNARK, VerifierKey};
use static_assertions::assert_not_impl_any;

use super::*;
use ff::Field;
use pasta_curves::Fq as Scalar;
Expand All @@ -1090,4 +1094,11 @@ mod tests {
assert_eq!(&result[0..10], &v[..]);
assert!(result[10..].iter().all(|&i| i == e));
}

#[test]
fn test_keys_and_snarks_should_not_be_cloned() {
assert_not_impl_any!(ProverKey::<PallasEngine, EvaluationEngine<PallasEngine>>: Clone);
assert_not_impl_any!(VerifierKey::<PallasEngine, EvaluationEngine<PallasEngine>>: Clone);
assert_not_impl_any!(RelaxedR1CSSNARK::<PallasEngine, EvaluationEngine<PallasEngine>>: Clone);
}
}
22 changes: 19 additions & 3 deletions src/spartan/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use rayon::prelude::*;
use serde::{Deserialize, Serialize};

/// A type that represents the prover's key
#[derive(Clone, Serialize, Deserialize, Abomonation)]
#[derive(Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(where <E::Scalar as ff::PrimeField>::Repr: Abomonation)]
pub struct ProverKey<E: Engine, EE: EvaluationEngineTrait<E>> {
Expand All @@ -43,7 +43,7 @@ pub struct ProverKey<E: Engine, EE: EvaluationEngineTrait<E>> {
}

/// A type that represents the verifier's key
#[derive(Clone, Serialize, Deserialize, Abomonation)]
#[derive(Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(where <E::Scalar as ff::PrimeField>::Repr: Abomonation)]
pub struct VerifierKey<E: Engine, EE: EvaluationEngineTrait<E>> {
Expand Down Expand Up @@ -83,7 +83,7 @@ impl<E: Engine, EE: EvaluationEngineTrait<E>> DigestHelperTrait<E> for VerifierK
/// A succinct proof of knowledge of a witness to a relaxed R1CS instance
/// The proof is produced using Spartan's combination of the sum-check and
/// the commitment to a vector viewed as a polynomial commitment
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct RelaxedR1CSSNARK<E: Engine, EE: EvaluationEngineTrait<E>> {
sc_proof_outer: SumcheckProof<E>,
Expand Down Expand Up @@ -558,3 +558,19 @@ pub(in crate::spartan) fn batch_eval_verify<E: Engine>(

Ok(u_joint)
}

#[cfg(test)]
mod tests {

use super::{ProverKey, RelaxedR1CSSNARK, VerifierKey};
use static_assertions::assert_not_impl_any;

use crate::provider::{ipa_pc::EvaluationEngine, PallasEngine};

#[test]
fn test_keys_and_snarks_should_not_be_cloned() {
assert_not_impl_any!(ProverKey::<PallasEngine, EvaluationEngine<PallasEngine>>: Clone);
assert_not_impl_any!(VerifierKey::<PallasEngine, EvaluationEngine<PallasEngine>>: Clone);
assert_not_impl_any!(RelaxedR1CSSNARK::<PallasEngine, EvaluationEngine<PallasEngine>>: Clone);
}
}
2 changes: 1 addition & 1 deletion src/spartan/sumcheck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};

pub(in crate::spartan) mod engine;

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub(crate) struct SumcheckProof<E: Engine> {
compressed_polys: Vec<CompressedUniPoly<E::Scalar>>,
Expand Down

0 comments on commit 6b9cee9

Please sign in to comment.