Skip to content

Commit

Permalink
refactor: add missing Debug implementations (#282)
Browse files Browse the repository at this point in the history
* refactor: use zip_with in batch evaluation

- Refactored iterated zip operations in batch evaluation verification function in `src/spartan/snark.rs`
- Used `zip_with!` macro to enhance code readability and maintainability

* refactor: slightly faster evaluation

- Enhanced the efficiency of polynomial evaluation in `prove` function in `src/spartan/snark.rs` by eliminating needless cloning.

* feat: Implement Debug trait across various structs and traits

- `Debug` trait added across multiple files to enhance log and error handling capacity; applies to structures like `ProverKey`, `VerifierKey`, `R1CSWithArity`, `NovaPublicParameters`, `CompressedSNARK`, and others.
- Expanded `Debug` trait implementation to more complex structures like `CircuitDigests`, `PublicParams`, associated types in `ROTrait` and `ROCircuitTrait`, and several structures in the `spartan` and `gadgets` module.
- An additional project-wide compilation check for missing `Debug` implementations has been added to cargo build settings.
  • Loading branch information
huitseeker authored Jan 25, 2024
1 parent 5da330e commit 1f36390
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 32 deletions.
1 change: 1 addition & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ xclippy = [
"-Wclippy::unnecessary_mut_passed",
"-Wclippy::unnecessary_wraps",
"-Wclippy::use_self",
"-Wmissing_debug_implementations",
"-Wnonstandard_style",
"-Wrust_2018_idioms",
"-Wtrivial_numeric_casts",
Expand Down
4 changes: 2 additions & 2 deletions src/gadgets/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use bellpepper_core::{
use ff::{Field, PrimeField};

/// `AllocatedPoint` provides an elliptic curve abstraction inside a circuit.
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct AllocatedPoint<E: Engine> {
pub(crate) x: AllocatedNum<E::Base>,
pub(crate) y: AllocatedNum<E::Base>,
Expand Down Expand Up @@ -590,7 +590,7 @@ where
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
/// `AllocatedPoint` but one that is guaranteed to be not infinity
pub struct AllocatedPointNonInfinity<E: Engine> {
x: AllocatedNum<E::Base>,
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use traits::{
};

/// A type that holds parameters for the primary and secondary circuits of Nova and SuperNova
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Abomonation)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(where <E::Scalar as PrimeField>::Repr: Abomonation)]
pub struct R1CSWithArity<E: Engine> {
Expand All @@ -87,7 +87,7 @@ impl<E: Engine> R1CSWithArity<E> {
}

/// A type that holds public parameters of Nova
#[derive(Clone, PartialEq, Serialize, Deserialize, Abomonation)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(
where
Expand Down Expand Up @@ -706,7 +706,7 @@ where
}

/// A type that holds the verifier key for `CompressedSNARK`
#[derive(Clone, Serialize, Deserialize, Abomonation)]
#[derive(Debug, Clone, Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(
where
Expand Down Expand Up @@ -739,7 +739,7 @@ where
}

/// A SNARK that proves the knowledge of a valid `RecursiveSNARK`
#[derive(Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct CompressedSNARK<E1, E2, C1, C2, S1, S2>
where
Expand Down
6 changes: 3 additions & 3 deletions src/provider/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use neptune::{
use serde::{Deserialize, Serialize};

/// All Poseidon Constants that are used in Nova
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Abomonation)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Abomonation)]
#[abomonation_bounds(where Scalar::Repr: Abomonation)]
pub struct PoseidonConstantsCircuit<Scalar: PrimeField>(PoseidonConstants<Scalar, U24>);

Expand All @@ -35,7 +35,7 @@ impl<Scalar: PrimeField> Default for PoseidonConstantsCircuit<Scalar> {
}

/// A Poseidon-based RO to use outside circuits
#[derive(Serialize, Deserialize, Abomonation)]
#[derive(Debug, Serialize, Deserialize, Abomonation)]
#[abomonation_bounds(
where
Base: PrimeField,
Expand Down Expand Up @@ -115,7 +115,7 @@ where
}

/// A Poseidon-based RO gadget to use inside the verifier circuit.
#[derive(Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct PoseidonROCircuit<Scalar: PrimeField> {
// Internal state
state: Vec<AllocatedNum<Scalar>>,
Expand Down
1 change: 1 addition & 0 deletions src/r1cs/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ impl<F: PrimeField> SparseMatrix<F> {
}

/// Iterator for sparse matrix
#[derive(Debug)]
pub struct Iter<'a, F: PrimeField> {
matrix: &'a SparseMatrix<F>,
row: usize,
Expand Down
4 changes: 2 additions & 2 deletions src/spartan/batched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(Debug, Clone, 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(Debug, Clone, 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
4 changes: 2 additions & 2 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(Debug, Clone, 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(Debug, Clone, Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(where < E::Scalar as PrimeField >::Repr: Abomonation)]
pub struct VerifierKey<E: Engine, EE: EvaluationEngineTrait<E>> {
Expand Down
2 changes: 2 additions & 0 deletions src/spartan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fn powers<E: Engine>(s: &E::Scalar, n: usize) -> Vec<E::Scalar> {
}

/// A type that holds a witness to a polynomial evaluation instance
#[derive(Debug)]
struct PolyEvalWitness<E: Engine> {
p: Vec<E::Scalar>, // polynomial
}
Expand Down Expand Up @@ -114,6 +115,7 @@ impl<E: Engine> PolyEvalWitness<E> {
}

/// A type that holds a polynomial evaluation instance
#[derive(Debug)]
struct PolyEvalInstance<E: Engine> {
c: Commitment<E>, // commitment to the polynomial
x: Vec<E::Scalar>, // evaluation point
Expand Down
8 changes: 4 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(Debug, Clone, Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(where <E::Scalar as PrimeField>::Repr: Abomonation)]
pub struct R1CSShapeSparkRepr<E: Engine> {
Expand All @@ -78,7 +78,7 @@ pub struct R1CSShapeSparkRepr<E: Engine> {
}

/// A type that holds a commitment to a sparse polynomial
#[derive(Clone, Serialize, Deserialize, Abomonation)]
#[derive(Debug, Clone, Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(where <E::Scalar as PrimeField>::Repr: Abomonation)]
pub struct R1CSShapeSparkCommitment<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(Debug, Clone, 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(Debug, Clone, Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(where <E::Scalar as PrimeField>::Repr: Abomonation)]
pub struct VerifierKey<E: Engine, EE: EvaluationEngineTrait<E>> {
Expand Down
16 changes: 8 additions & 8 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(Debug, Clone, 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(Debug, Clone, 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 @@ -189,7 +189,7 @@ where
// claims from the end of sum-check
let (claim_Az, claim_Bz): (E::Scalar, E::Scalar) = (claims_outer[1], claims_outer[2]);
let claim_Cz = poly_Cz.evaluate(&r_x);
let eval_E = MultilinearPolynomial::new(W.E.clone()).evaluate(&r_x);
let eval_E = MultilinearPolynomial::evaluate_with(&W.E, &r_x);
transcript.absorb(
b"claims_outer",
&[claim_Az, claim_Bz, claim_Cz, eval_E].as_slice(),
Expand Down Expand Up @@ -536,11 +536,11 @@ pub(in crate::spartan) fn batch_eval_verify<E: Engine>(
EqPolynomial::new(r_hi.to_vec()).evaluate(&u.x)
});

evals_r
.zip_eq(evals_batch.iter())
.zip_eq(powers_of_rho.iter())
.map(|((e_i, p_i), rho_i)| e_i * *p_i * rho_i)
.sum()
zip_with!(
(evals_r, evals_batch.iter(), powers_of_rho.iter()),
|e_i, p_i, rho_i| e_i * *p_i * rho_i
)
.sum()
};

if claim_batch_final != claim_batch_final_expected {
Expand Down
6 changes: 3 additions & 3 deletions src/supernova/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use circuit::{
use error::SuperNovaError;

/// A struct that manages all the digests of the primary circuits of a SuperNova instance
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CircuitDigests<E: Engine> {
digests: Vec<E::Scalar>,
}
Expand Down Expand Up @@ -75,7 +75,7 @@ impl<E: Engine> CircuitDigests<E> {
}

/// A vector of [R1CSWithArity] adjoined to a set of [PublicParams]
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct PublicParams<E1, E2, C1, C2>
where
Expand Down Expand Up @@ -107,7 +107,7 @@ where
/// Auxiliary [PublicParams] information about the commitment keys and
/// secondary circuit. This is used as a helper struct when reconstructing
/// [PublicParams] downstream in lurk.
#[derive(Clone, PartialEq, Serialize, Deserialize, Abomonation)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(
where
Expand Down
4 changes: 2 additions & 2 deletions src/supernova/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use serde::{Deserialize, Serialize};
use std::marker::PhantomData;

/// A type that holds the prover key for `CompressedSNARK`
#[derive(Clone, Serialize, Deserialize, Abomonation)]
#[derive(Debug, Clone, Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(
where
Expand All @@ -46,7 +46,7 @@ where
}

/// A type that holds the verifier key for `CompressedSNARK`
#[derive(Clone, Serialize, Deserialize, Abomonation)]
#[derive(Debug, Clone, Serialize, Deserialize, Abomonation)]
#[serde(bound = "")]
#[abomonation_bounds(
where
Expand Down
6 changes: 4 additions & 2 deletions src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ pub trait ROTrait<Base: PrimeField, Scalar> {
type CircuitRO: ROCircuitTrait<Base, Constants = Self::Constants>;

/// A type representing constants/parameters associated with the hash function
type Constants: Default
type Constants: Debug
+ Default
+ Clone
+ PartialEq
+ Send
Expand All @@ -92,7 +93,8 @@ pub trait ROCircuitTrait<Base: PrimeField> {
type NativeRO<T: PrimeField>: ROTrait<Base, T, Constants = Self::Constants>;

/// A type representing constants/parameters associated with the hash function on this Base field
type Constants: Default
type Constants: Debug
+ Default
+ Clone
+ PartialEq
+ Send
Expand Down

1 comment on commit 1f36390

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks

Table of Contents

Overview

This benchmark report shows the Arecibo GPU benchmarks.
NVIDIA L4
Intel(R) Xeon(R) CPU @ 2.20GHz
32 vCPUs
125 GB RAM
Workflow run: https://github.com/lurk-lab/arecibo/actions/runs/7659105151

Benchmark Results

RecursiveSNARK-NIVC-2

ref=5da330e ref=1f36390
Prove-NumCons-6540 52.77 ms (✅ 1.00x) 52.66 ms (✅ 1.00x faster)
Verify-NumCons-6540 32.92 ms (✅ 1.00x) 32.99 ms (✅ 1.00x slower)
Prove-NumCons-1028888 342.46 ms (✅ 1.00x) 341.96 ms (✅ 1.00x faster)
Verify-NumCons-1028888 251.83 ms (✅ 1.00x) 256.53 ms (✅ 1.02x slower)

CompressedSNARK-NIVC-Commitments-2

ref=5da330e ref=1f36390
Prove-NumCons-6540 14.06 s (✅ 1.00x) 14.03 s (✅ 1.00x faster)
Verify-NumCons-6540 77.75 ms (✅ 1.00x) 78.38 ms (✅ 1.01x slower)
Prove-NumCons-1028888 110.17 s (✅ 1.00x) 111.66 s (✅ 1.01x slower)
Verify-NumCons-1028888 773.88 ms (✅ 1.00x) 769.85 ms (✅ 1.01x faster)

Made with criterion-table

Please sign in to comment.