Skip to content

Commit

Permalink
feat: Refactor to use default_commitment_key_hint across codebase
Browse files Browse the repository at this point in the history
- Introduced a new function `default_commitment_key_hint` in `nova_snark` package to provide a clearer default sizing hint for the commitment key,
- Replaced hard-coded zero values throughout the codebase with calls to `default_commitment_key_hint` function,
  • Loading branch information
huitseeker committed Nov 3, 2023
1 parent 574e249 commit a3fdbe6
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 27 deletions.
5 changes: 3 additions & 2 deletions benches/compute-digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ff::PrimeField;
use nova_snark::{
traits::{
circuit::{StepCircuit, TrivialCircuit},
snark::default_commitment_key_hint,
Group,
},
PublicParams,
Expand All @@ -30,8 +31,8 @@ fn bench_compute_digest(c: &mut Criterion) {
PublicParams::<G1, G2, C1, C2>::setup(
black_box(&C1::new(10)),
black_box(&C2::default()),
black_box(&(|_| 0)),
black_box(&(|_| 0)),
black_box(&*default_commitment_key_hint()),
black_box(&*default_commitment_key_hint()),
)
})
});
Expand Down
8 changes: 7 additions & 1 deletion benches/recursive-snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ff::PrimeField;
use nova_snark::{
traits::{
circuit::{StepCircuit, TrivialCircuit},
snark::default_commitment_key_hint,
Group,
},
PublicParams, RecursiveSNARK,
Expand Down Expand Up @@ -56,7 +57,12 @@ fn bench_recursive_snark(c: &mut Criterion) {
let c_secondary = TrivialCircuit::default();

// Produce public parameters
let pp = PublicParams::<G1, G2, C1, C2>::setup(&c_primary, &c_secondary, &(|_| 0), &(|_| 0));
let pp = PublicParams::<G1, G2, C1, C2>::setup(
&c_primary,
&c_secondary,
&*default_commitment_key_hint(),
&*default_commitment_key_hint(),
);

// Bench time to produce a recursive SNARK;
// we execute a certain number of warm-up steps since executing
Expand Down
8 changes: 7 additions & 1 deletion benches/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use ff::{PrimeField, PrimeFieldBits};
use nova_snark::{
traits::{
circuit::{StepCircuit, TrivialCircuit},
snark::default_commitment_key_hint,
Group,
},
PublicParams, RecursiveSNARK,
Expand Down Expand Up @@ -155,7 +156,12 @@ fn bench_recursive_snark(c: &mut Criterion) {

// Produce public parameters
let ttc = TrivialCircuit::default();
let pp = PublicParams::<G1, G2, C1, C2>::setup(&circuit_primary, &ttc, &(|_| 0), &(|_| 0));
let pp = PublicParams::<G1, G2, C1, C2>::setup(
&circuit_primary,
&ttc,
&*default_commitment_key_hint(),
&*default_commitment_key_hint(),
);

let circuit_secondary = TrivialCircuit::default();
let z0_primary = vec![<G1 as Group>::Scalar::from(2u64)];
Expand Down
8 changes: 7 additions & 1 deletion examples/minroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use flate2::{write::ZlibEncoder, Compression};
use nova_snark::{
traits::{
circuit::{StepCircuit, TrivialCircuit},
snark::default_commitment_key_hint,
Group,
},
CompressedSNARK, PublicParams, RecursiveSNARK,
Expand Down Expand Up @@ -161,7 +162,12 @@ fn main() {
G2,
MinRootCircuit<<G1 as Group>::Scalar>,
TrivialCircuit<<G2 as Group>::Scalar>,
>::setup(&circuit_primary, &circuit_secondary, &(|_| 0), &(|_| 0));
>::setup(
&circuit_primary,
&circuit_secondary,
&*default_commitment_key_hint(),
&*default_commitment_key_hint(),
);
println!("PublicParams::setup, took {:?} ", start.elapsed());

println!(
Expand Down
4 changes: 2 additions & 2 deletions src/bellpepper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod tests {
shape_cs::ShapeCS,
solver::SatisfyingAssignment,
},
traits::Group,
traits::{snark::default_commitment_key_hint, Group},
};
use bellpepper_core::{num::AllocatedNum, ConstraintSystem};
use ff::PrimeField;
Expand Down Expand Up @@ -47,7 +47,7 @@ mod tests {
// First create the shape
let mut cs: ShapeCS<G> = ShapeCS::new();
synthesize_alloc_bit(&mut cs);
let (shape, ck) = cs.r1cs_shape(&(|_| 0));
let (shape, ck) = cs.r1cs_shape(&*default_commitment_key_hint());

// Now get the assignment
let mut cs: SatisfyingAssignment<G> = SatisfyingAssignment::new();
Expand Down
5 changes: 3 additions & 2 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ mod tests {

use crate::constants::{BN_LIMB_WIDTH, BN_N_LIMBS};
use crate::provider;
use crate::traits::snark::default_commitment_key_hint;
use crate::{
bellpepper::r1cs::{NovaShape, NovaWitness},
gadgets::utils::scalar_as_base,
Expand All @@ -392,7 +393,7 @@ mod tests {
NovaAugmentedCircuit::new(primary_params, None, &tc1, ro_consts1.clone());
let mut cs: TestShapeCS<G1> = TestShapeCS::new();
let _ = circuit1.synthesize(&mut cs);
let (shape1, ck1) = cs.r1cs_shape(&(|_| 0));
let (shape1, ck1) = cs.r1cs_shape(&*default_commitment_key_hint());
assert_eq!(cs.num_constraints(), num_constraints_primary);

let tc2 = TrivialCircuit::default();
Expand All @@ -401,7 +402,7 @@ mod tests {
NovaAugmentedCircuit::new(secondary_params, None, &tc2, ro_consts2.clone());
let mut cs: TestShapeCS<G2> = TestShapeCS::new();
let _ = circuit2.synthesize(&mut cs);
let (shape2, ck2) = cs.r1cs_shape(&(|_| 0));
let (shape2, ck2) = cs.r1cs_shape(&*default_commitment_key_hint());
assert_eq!(cs.num_constraints(), num_constraints_secondary);

// Execute the base case for the primary
Expand Down
17 changes: 10 additions & 7 deletions src/gadgets/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,14 +748,17 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::bellpepper::{
r1cs::{NovaShape, NovaWitness},
{solver::SatisfyingAssignment, test_shape_cs::TestShapeCS},
};
use crate::provider::{
bn256_grumpkin::{bn256, grumpkin},
secp_secq::{secp256k1, secq256k1},
};
use crate::{
bellpepper::{
r1cs::{NovaShape, NovaWitness},
{solver::SatisfyingAssignment, test_shape_cs::TestShapeCS},
},
traits::snark::default_commitment_key_hint,
};
use ff::{Field, PrimeFieldBits};
use pasta_curves::{arithmetic::CurveAffine, group::Curve, pallas, vesta};
use rand::rngs::OsRng;
Expand Down Expand Up @@ -1000,7 +1003,7 @@ mod tests {
let mut cs: TestShapeCS<G2> = TestShapeCS::new();
let _ = synthesize_smul::<G1, _>(cs.namespace(|| "synthesize"));
println!("Number of constraints: {}", cs.num_constraints());
let (shape, ck) = cs.r1cs_shape(&(|_| 0));
let (shape, ck) = cs.r1cs_shape(&*default_commitment_key_hint());

// Then the satisfying assignment
let mut cs: SatisfyingAssignment<G2> = SatisfyingAssignment::new();
Expand Down Expand Up @@ -1056,7 +1059,7 @@ mod tests {
let mut cs: TestShapeCS<G2> = TestShapeCS::new();
let _ = synthesize_add_equal::<G1, _>(cs.namespace(|| "synthesize add equal"));
println!("Number of constraints: {}", cs.num_constraints());
let (shape, ck) = cs.r1cs_shape(&(|_| 0));
let (shape, ck) = cs.r1cs_shape(&*default_commitment_key_hint());

// Then the satisfying assignment
let mut cs: SatisfyingAssignment<G2> = SatisfyingAssignment::new();
Expand Down Expand Up @@ -1116,7 +1119,7 @@ mod tests {
let mut cs: TestShapeCS<G2> = TestShapeCS::new();
let _ = synthesize_add_negation::<G1, _>(cs.namespace(|| "synthesize add equal"));
println!("Number of constraints: {}", cs.num_constraints());
let (shape, ck) = cs.r1cs_shape(&(|_| 0));
let (shape, ck) = cs.r1cs_shape(&*default_commitment_key_hint());

// Then the satisfying assignment
let mut cs: SatisfyingAssignment<G2> = SatisfyingAssignment::new();
Expand Down
40 changes: 33 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ where
///
/// Public parameters set up a number of bases for the homomorphic commitment scheme of Nova.
///
/// Some final ocmpressing SNARKs, like variants of Spartan, use computation commitments that require
/// Some final compressing SNARKs, like variants of Spartan, use computation commitments that require
/// larger sizes for these parameters. These SNARKs provide a hint for these values by
/// implementing `RelaxedR1CSSNARKTrait::commitment_key_floor()`, which can be passed to this function.
///
/// If you're not using such a SNARK, pass `&(|_| 0)` instead.
/// If you're not using such a SNARK, pass `nova_snark::traits::snark::default_commitment_key_hint()` instead.
///
/// # Arguments
///
Expand Down Expand Up @@ -866,6 +866,7 @@ mod tests {
use crate::provider::pedersen::CommitmentKeyExtTrait;
use crate::provider::secp_secq::{secp256k1, secq256k1};
use crate::traits::evaluation::EvaluationEngineTrait;
use crate::traits::snark::default_commitment_key_hint;
use core::fmt::Write;

use super::*;
Expand Down Expand Up @@ -1024,7 +1025,12 @@ mod tests {
G2,
TrivialCircuit<<G1 as Group>::Scalar>,
TrivialCircuit<<G2 as Group>::Scalar>,
>::setup(&test_circuit1, &test_circuit2, &(|_| 0), &(|_| 0));
>::setup(
&test_circuit1,
&test_circuit2,
&*default_commitment_key_hint(),
&*default_commitment_key_hint(),
);

let num_steps = 1;

Expand Down Expand Up @@ -1076,7 +1082,12 @@ mod tests {
G2,
TrivialCircuit<<G1 as Group>::Scalar>,
CubicCircuit<<G2 as Group>::Scalar>,
>::setup(&circuit_primary, &circuit_secondary, &(|_| 0), &(|_| 0));
>::setup(
&circuit_primary,
&circuit_secondary,
&*default_commitment_key_hint(),
&*default_commitment_key_hint(),
);

let num_steps = 3;

Expand Down Expand Up @@ -1156,7 +1167,12 @@ mod tests {
G2,
TrivialCircuit<<G1 as Group>::Scalar>,
CubicCircuit<<G2 as Group>::Scalar>,
>::setup(&circuit_primary, &circuit_secondary, &(|_| 0), &(|_| 0));
>::setup(
&circuit_primary,
&circuit_secondary,
&*default_commitment_key_hint(),
&*default_commitment_key_hint(),
);

let num_steps = 3;

Expand Down Expand Up @@ -1414,7 +1430,12 @@ mod tests {
G2,
FifthRootCheckingCircuit<<G1 as Group>::Scalar>,
TrivialCircuit<<G2 as Group>::Scalar>,
>::setup(&circuit_primary, &circuit_secondary, &(|_| 0), &(|_| 0));
>::setup(
&circuit_primary,
&circuit_secondary,
&*default_commitment_key_hint(),
&*default_commitment_key_hint(),
);

let num_steps = 3;

Expand Down Expand Up @@ -1489,7 +1510,12 @@ mod tests {
G2,
TrivialCircuit<<G1 as Group>::Scalar>,
CubicCircuit<<G2 as Group>::Scalar>,
>::setup(&test_circuit1, &test_circuit2, &(|_| 0), &(|_| 0));
>::setup(
&test_circuit1,
&test_circuit2,
&*default_commitment_key_hint(),
&*default_commitment_key_hint(),
);

let num_steps = 1;

Expand Down
8 changes: 5 additions & 3 deletions src/nifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ impl<G: Group> NIFS<G> {
#[cfg(test)]
mod tests {
use super::*;
use crate::{r1cs::SparseMatrix, r1cs::R1CS, traits::Group};
use crate::{
r1cs::SparseMatrix, r1cs::R1CS, traits::snark::default_commitment_key_hint, traits::Group,
};
use ::bellpepper_core::{num::AllocatedNum, ConstraintSystem, SynthesisError};
use ff::{Field, PrimeField};
use rand::rngs::OsRng;
Expand Down Expand Up @@ -166,7 +168,7 @@ mod tests {
// First create the shape
let mut cs: TestShapeCS<G> = TestShapeCS::new();
let _ = synthesize_tiny_r1cs_bellpepper(&mut cs, None);
let (shape, ck) = cs.r1cs_shape(&(|_| 0));
let (shape, ck) = cs.r1cs_shape(&*default_commitment_key_hint());
let ro_consts =
<<G as Group>::RO as ROTrait<<G as Group>::Base, <G as Group>::Scalar>>::Constants::default();

Expand Down Expand Up @@ -327,7 +329,7 @@ mod tests {
};

// generate generators and ro constants
let ck = R1CS::<G>::commitment_key(&S, &(|_| 0));
let ck = R1CS::<G>::commitment_key(&S, &*default_commitment_key_hint());
let ro_consts =
<<G as Group>::RO as ROTrait<<G as Group>::Base, <G as Group>::Scalar>>::Constants::default();

Expand Down
11 changes: 10 additions & 1 deletion src/traits/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ use crate::{

use serde::{Deserialize, Serialize};

/// Public parameter creation takes a size hint. This size hint carries the particular requirements of
/// the final compressing SNARK the user expected to use with these public parameters, and the below
/// is a sensible default, which is to not require any more bases then the usual (maximum of the number of
/// variables and constraints of the involved R1CS circuit).
pub fn default_commitment_key_hint<G: Group>() -> Box<dyn for<'a> Fn(&'a R1CSShape<G>) -> usize> {
// The default is to not put an additional floor on the size of the commitment key
Box::new(|_shape: &R1CSShape<G>| 0)
}

/// A trait that defines the behavior of a `zkSNARK`
pub trait RelaxedR1CSSNARKTrait<G: Group>:
Send + Sync + Serialize + for<'de> Deserialize<'de>
Expand All @@ -24,7 +33,7 @@ pub trait RelaxedR1CSSNARKTrait<G: Group>:
/// be at least as large as this hint.
fn commitment_key_floor() -> Box<dyn for<'a> Fn(&'a R1CSShape<G>) -> usize> {
// The default is to not put an additional floor on the size of the commitment key
Box::new(|_shape: &R1CSShape<G>| 0)
default_commitment_key_hint()
}

/// Produces the keys for the prover and the verifier
Expand Down

0 comments on commit a3fdbe6

Please sign in to comment.