Skip to content

Commit

Permalink
refactor: Refactor public parameters setup to remove optionality of c…
Browse files Browse the repository at this point in the history
…ommitment key size hint (#95)

* refactor: Refactor public parameters setup to remove optionality of commitment key size hint

- Converted `CommitmentKeyHint<G>` from a boxed dynamic trait object to a direct dynamic trait object in `r1cs/mod.rs`.
- Changed the `commitment_key` function to always require a commitment key floor, eliminating the need for default behavior when a floor function isn't provided.
- Updated the `r1cs_shape` function across various files to take in a `CommitmentKeyHint` instead of it being optional and introduce a closure as an argument.
- Relevant modifications and updates were made in the `r1cs_shape` and `commitment_key` function calls within the test functions for various modules.
- Ported use of commitment key hint to Supernova, closing #53.
- This PR puts Arecibo in line with microsoft/Nova#203

* doc: document Supernova Public Parameters

- Enhanced the `PublicParams` struct within `./src/supernova/mod.rs` with detailed comments

* feat: Refactor to use `default_commitment_key_hint` across codebase

- 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 authored Nov 3, 2023
1 parent 16cfdff commit 5854fff
Show file tree
Hide file tree
Showing 18 changed files with 192 additions and 75 deletions.
8 changes: 4 additions & 4 deletions benches/compressed-snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ fn bench_compressed_snark(c: &mut Criterion) {
let pp = PublicParams::<G1, G2, C1, C2>::new(
&c_primary,
&c_secondary,
Some(S1::commitment_key_floor()),
Some(S2::commitment_key_floor()),
&*S1::commitment_key_floor(),
&*S2::commitment_key_floor(),
);

// Produce prover and verifier keys for CompressedSNARK
Expand Down Expand Up @@ -156,8 +156,8 @@ fn bench_compressed_snark_with_computational_commitments(c: &mut Criterion) {
let pp = PublicParams::<G1, G2, C1, C2>::new(
&c_primary,
&c_secondary,
Some(SS1::commitment_key_floor()),
Some(SS2::commitment_key_floor()),
&*SS1::commitment_key_floor(),
&*SS2::commitment_key_floor(),
);
// Produce prover and verifier keys for CompressedSNARK
let (pk, vk) = CompressedSNARK::<_, _, _, _, SS1, SS2>::setup(&pp).unwrap();
Expand Down
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>::new(
black_box(&C1::new(10)),
black_box(&C2::default()),
black_box(None),
black_box(None),
black_box(&*default_commitment_key_hint()),
black_box(&*default_commitment_key_hint()),
)
})
});
Expand Down
13 changes: 11 additions & 2 deletions benches/recursive-snark-supernova.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use nova_snark::{
supernova::{PublicParams, RecursiveSNARK},
traits::{
circuit_supernova::{StepCircuit, TrivialTestCircuit},
snark::default_commitment_key_hint,
Group,
},
};
Expand Down Expand Up @@ -104,7 +105,11 @@ fn bench_one_augmented_circuit_recursive_snark(c: &mut Criterion) {

let bench: NonUniformBench<G1, G2, TrivialTestCircuit<<G2 as Group>::Scalar>> =
NonUniformBench::new(1, num_cons);
let pp = PublicParams::new(&bench);
let pp = PublicParams::new(
&bench,
&*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 Expand Up @@ -206,7 +211,11 @@ fn bench_two_augmented_circuit_recursive_snark(c: &mut Criterion) {

let bench: NonUniformBench<G1, G2, TrivialTestCircuit<<G2 as Group>::Scalar>> =
NonUniformBench::new(2, num_cons);
let pp = PublicParams::new(&bench);
let pp = PublicParams::new(
&bench,
&*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/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>::new(&c_primary, &c_secondary, None, None);
let pp = PublicParams::<G1, G2, C1, C2>::new(
&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>::new(&circuit_primary, &ttc, None, None);
let pp = PublicParams::<G1, G2, C1, C2>::new(
&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 @@ -169,7 +170,12 @@ fn main() {
G2,
MinRootCircuit<<G1 as Group>::Scalar>,
TrivialCircuit<<G2 as Group>::Scalar>,
>::new(&circuit_primary, &circuit_secondary, None, None);
>::new(
&circuit_primary,
&circuit_secondary,
&*default_commitment_key_hint(),
&*default_commitment_key_hint(),
);
println!("PublicParams::setup, took {:?} ", start.elapsed());

println!(
Expand Down
15 changes: 13 additions & 2 deletions examples/minroot_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use ff::PrimeField;
use nova_snark::{
traits::{
circuit::{StepCircuit, TrivialCircuit},
snark::default_commitment_key_hint,
Group,
},
PublicParams,
Expand Down Expand Up @@ -167,7 +168,12 @@ fn main() {
G2,
MinRootCircuit<<G1 as Group>::Scalar>,
TrivialCircuit<<G2 as Group>::Scalar>,
>::new(&circuit_primary, &circuit_secondary, None, None);
>::new(
&circuit_primary,
&circuit_secondary,
&*default_commitment_key_hint(),
&*default_commitment_key_hint(),
);
println!("PublicParams::setup, took {:?} ", start.elapsed());
encode(&pp, &mut file).unwrap()
};
Expand All @@ -193,7 +199,12 @@ fn main() {
G2,
MinRootCircuit<<G1 as Group>::Scalar>,
TrivialCircuit<<G2 as Group>::Scalar>,
>::new(&circuit_primary, &circuit_secondary, None, None);
>::new(
&circuit_primary,
&circuit_secondary,
&*default_commitment_key_hint(),
&*default_commitment_key_hint(),
);
assert!(result.clone() == pp, "not equal!");
assert!(remaining.is_empty());
} else {
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, SynthesisError};
use ff::PrimeField;
Expand Down Expand Up @@ -50,7 +50,7 @@ mod tests {
// First create the shape
let mut cs: ShapeCS<G> = ShapeCS::new();
let _ = synthesize_alloc_bit(&mut cs);
let (shape, ck) = cs.r1cs_shape_and_key(None);
let (shape, ck) = cs.r1cs_shape_and_key(&*default_commitment_key_hint());

// Now get the assignment
let mut cs: SatisfyingAssignment<G> = SatisfyingAssignment::new();
Expand Down
12 changes: 4 additions & 8 deletions src/bellpepper/r1cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@ pub trait NovaWitness<G: Group> {
/// `NovaShape` provides methods for acquiring `R1CSShape` and `CommitmentKey` from implementers.
pub trait NovaShape<G: Group> {
/// Return an appropriate `R1CSShape` and `CommitmentKey` structs.
/// Optionally, a `CommitmentKeyHint` can be provided to help guide the
/// construction of the `CommitmentKey`. This parameter is documented in
/// `r1cs::R1CS::commitment_key`.
fn r1cs_shape_and_key(
&self,
optfn: Option<CommitmentKeyHint<G>>,
) -> (R1CSShape<G>, CommitmentKey<G>) {
/// A `CommitmentKeyHint` should be provided to help guide the construction of the `CommitmentKey`.
/// This parameter is documented in `r1cs::R1CS::commitment_key`.
fn r1cs_shape_and_key(&self, ck_hint: &CommitmentKeyHint<G>) -> (R1CSShape<G>, CommitmentKey<G>) {
let S = self.r1cs_shape();
let ck = commitment_key(&S, optfn);
let ck = commitment_key(&S, ck_hint);

(S, ck)
}
Expand Down
5 changes: 3 additions & 2 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,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 @@ -393,7 +394,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_and_key(None);
let (shape1, ck1) = cs.r1cs_shape_and_key(&*default_commitment_key_hint());
assert_eq!(cs.num_constraints(), num_constraints_primary);

let tc2 = TrivialCircuit::default();
Expand All @@ -402,7 +403,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_and_key(None);
let (shape2, ck2) = cs.r1cs_shape_and_key(&*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 @@ -1001,7 +1004,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_and_key(None);
let (shape, ck) = cs.r1cs_shape_and_key(&*default_commitment_key_hint());

// Then the satisfying assignment
let mut cs: SatisfyingAssignment<G2> = SatisfyingAssignment::new();
Expand Down Expand Up @@ -1057,7 +1060,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_and_key(None);
let (shape, ck) = cs.r1cs_shape_and_key(&*default_commitment_key_hint());

// Then the satisfying assignment
let mut cs: SatisfyingAssignment<G2> = SatisfyingAssignment::new();
Expand Down Expand Up @@ -1117,7 +1120,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_and_key(None);
let (shape, ck) = cs.r1cs_shape_and_key(&*default_commitment_key_hint());

// Then the satisfying assignment
let mut cs: SatisfyingAssignment<G2> = SatisfyingAssignment::new();
Expand Down
Loading

0 comments on commit 5854fff

Please sign in to comment.