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

- 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.
  • Loading branch information
huitseeker committed Nov 3, 2023
1 parent dc482ef commit 474da9c
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 48 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>::setup(
&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>::setup(
&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
4 changes: 2 additions & 2 deletions benches/compute-digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,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(None),
black_box(None),
black_box(&(|_| 0)),
black_box(&(|_| 0)),
)
})
});
Expand Down
2 changes: 1 addition & 1 deletion benches/recursive-snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ 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, None, None);
let pp = PublicParams::<G1, G2, C1, C2>::setup(&c_primary, &c_secondary, &(|_| 0), &(|_| 0));

// Bench time to produce a recursive SNARK;
// we execute a certain number of warm-up steps since executing
Expand Down
2 changes: 1 addition & 1 deletion benches/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ 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, None, None);
let pp = PublicParams::<G1, G2, C1, C2>::setup(&circuit_primary, &ttc, &(|_| 0), &(|_| 0));

let circuit_secondary = TrivialCircuit::default();
let z0_primary = vec![<G1 as Group>::Scalar::from(2u64)];
Expand Down
2 changes: 1 addition & 1 deletion examples/minroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fn main() {
G2,
MinRootCircuit<<G1 as Group>::Scalar>,
TrivialCircuit<<G2 as Group>::Scalar>,
>::setup(&circuit_primary, &circuit_secondary, None, None);
>::setup(&circuit_primary, &circuit_secondary, &(|_| 0), &(|_| 0));
println!("PublicParams::setup, took {:?} ", start.elapsed());

println!(
Expand Down
2 changes: 1 addition & 1 deletion src/bellpepper/mod.rs
Original file line number Diff line number Diff line change
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(None);
let (shape, ck) = cs.r1cs_shape(&(|_| 0));

// Now get the assignment
let mut cs: SatisfyingAssignment<G> = SatisfyingAssignment::new();
Expand Down
10 changes: 5 additions & 5 deletions src/bellpepper/r1cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ 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(&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(&self, ck_hint: &CommitmentKeyHint<G>) -> (R1CSShape<G>, CommitmentKey<G>);
}

impl<G: Group> NovaWitness<G> for SatisfyingAssignment<G> {
Expand Down Expand Up @@ -55,7 +55,7 @@ macro_rules! impl_nova_shape {
{
fn r1cs_shape(
&self,
optfn: Option<CommitmentKeyHint<G>>,
ck_hint: &CommitmentKeyHint<G>,
) -> (R1CSShape<G>, CommitmentKey<G>) {
let mut A = SparseMatrix::<G::Scalar>::empty();
let mut B = SparseMatrix::<G::Scalar>::empty();
Expand Down Expand Up @@ -84,7 +84,7 @@ macro_rules! impl_nova_shape {

// Don't count One as an input for shape's purposes.
let S = R1CSShape::new(num_constraints, num_vars, num_inputs - 1, A, B, C).unwrap();
let ck = R1CS::<G>::commitment_key(&S, optfn);
let ck = R1CS::<G>::commitment_key(&S, ck_hint);

(S, ck)
}
Expand Down
4 changes: 2 additions & 2 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,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(None);
let (shape1, ck1) = cs.r1cs_shape(&(|_| 0));
assert_eq!(cs.num_constraints(), num_constraints_primary);

let tc2 = TrivialCircuit::default();
Expand All @@ -401,7 +401,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(None);
let (shape2, ck2) = cs.r1cs_shape(&(|_| 0));
assert_eq!(cs.num_constraints(), num_constraints_secondary);

// Execute the base case for the primary
Expand Down
6 changes: 3 additions & 3 deletions src/gadgets/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,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(None);
let (shape, ck) = cs.r1cs_shape(&(|_| 0));

// Then the satisfying assignment
let mut cs: SatisfyingAssignment<G2> = SatisfyingAssignment::new();
Expand Down Expand Up @@ -1056,7 +1056,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(None);
let (shape, ck) = cs.r1cs_shape(&(|_| 0));

// Then the satisfying assignment
let mut cs: SatisfyingAssignment<G2> = SatisfyingAssignment::new();
Expand Down Expand Up @@ -1116,7 +1116,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(None);
let (shape, ck) = cs.r1cs_shape(&(|_| 0));

// Then the satisfying assignment
let mut cs: SatisfyingAssignment<G2> = SatisfyingAssignment::new();
Expand Down
41 changes: 22 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,21 @@ where
///
/// # Note
///
/// Some SNARKs, like variants of Spartan, use computation commitments that require
/// larger sizes for some parameters. These SNARKs provide a hint for these values by
/// 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
/// 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 `None` instead.
///
/// If you're not using such a SNARK, pass `&(|_| 0)` instead.
///
/// # Arguments
///
/// * `c_primary`: The primary circuit of type `C1`.
/// * `c_secondary`: The secondary circuit of type `C2`.
/// * `optfn1`: An optional `CommitmentKeyHint` for `G1`, which is a function that provides a hint
/// * `optfn1`: A `CommitmentKeyHint` for `G1`, which is a function that provides a hint
/// for the number of generators required in the commitment scheme for the primary circuit.
/// * `optfn2`: An optional `CommitmentKeyHint` for `G2`, similar to `optfn1`, but for the secondary circuit.
/// * `optfn2`: A `CommitmentKeyHint` for `G2`, similar to `optfn1`, but for the secondary circuit.
///
/// # Example
///
Expand All @@ -128,17 +131,17 @@ where
///
/// let circuit1 = TrivialCircuit::<<G1 as Group>::Scalar>::default();
/// let circuit2 = TrivialCircuit::<<G2 as Group>::Scalar>::default();
/// // Only relevant for a SNARK using computational commitments, pass None otherwise.
/// let pp_hint1 = Some(SPrime::<G1>::commitment_key_floor());
/// let pp_hint2 = Some(SPrime::<G2>::commitment_key_floor());
/// // Only relevant for a SNARK using computational commitments, pass &(|_| 0) otherwise.
/// let pp_hint1 = &*SPrime::<G1>::commitment_key_floor();
/// let pp_hint2 = &*SPrime::<G2>::commitment_key_floor();
///
/// let pp = PublicParams::setup(&circuit1, &circuit2, pp_hint1, pp_hint2);
/// ```
pub fn setup(
c_primary: &C1,
c_secondary: &C2,
optfn1: Option<CommitmentKeyHint<G1>>,
optfn2: Option<CommitmentKeyHint<G2>>,
optfn1: &CommitmentKeyHint<G1>,
optfn2: &CommitmentKeyHint<G2>,
) -> Self {
let augmented_circuit_params_primary =
NovaAugmentedCircuitParams::new(BN_LIMB_WIDTH, BN_N_LIMBS, true);
Expand Down Expand Up @@ -940,8 +943,8 @@ mod tests {
<G2::CE as CommitmentEngineTrait<G2>>::CommitmentKey: CommitmentKeyExtTrait<G2>,
{
// this tests public parameters with a size specifically intended for a spark-compressed SNARK
let pp_hint1 = Some(SPrime::<G1, EE<G1>>::commitment_key_floor());
let pp_hint2 = Some(SPrime::<G2, EE<G2>>::commitment_key_floor());
let pp_hint1 = &*SPrime::<G1, EE<G1>>::commitment_key_floor();
let pp_hint2 = &*SPrime::<G2, EE<G2>>::commitment_key_floor();
let pp = PublicParams::<G1, G2, T1, T2>::setup(circuit1, circuit2, pp_hint1, pp_hint2);

let digest_str = pp
Expand Down Expand Up @@ -1021,7 +1024,7 @@ mod tests {
G2,
TrivialCircuit<<G1 as Group>::Scalar>,
TrivialCircuit<<G2 as Group>::Scalar>,
>::setup(&test_circuit1, &test_circuit2, None, None);
>::setup(&test_circuit1, &test_circuit2, &(|_| 0), &(|_| 0));

let num_steps = 1;

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

let num_steps = 3;

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

let num_steps = 3;

Expand Down Expand Up @@ -1245,8 +1248,8 @@ mod tests {
>::setup(
&circuit_primary,
&circuit_secondary,
Some(SPrime::<G1, E1>::commitment_key_floor()),
Some(SPrime::<G2, E2>::commitment_key_floor()),
&*SPrime::<G1, E1>::commitment_key_floor(),
&*SPrime::<G2, E2>::commitment_key_floor(),
);

let num_steps = 3;
Expand Down Expand Up @@ -1411,7 +1414,7 @@ mod tests {
G2,
FifthRootCheckingCircuit<<G1 as Group>::Scalar>,
TrivialCircuit<<G2 as Group>::Scalar>,
>::setup(&circuit_primary, &circuit_secondary, None, None);
>::setup(&circuit_primary, &circuit_secondary, &(|_| 0), &(|_| 0));

let num_steps = 3;

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

let num_steps = 1;

Expand Down
4 changes: 2 additions & 2 deletions src/nifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,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(None);
let (shape, ck) = cs.r1cs_shape(&(|_| 0));
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 +327,7 @@ mod tests {
};

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

Expand Down
11 changes: 5 additions & 6 deletions src/r1cs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub struct RelaxedR1CSInstance<G: Group> {
pub(crate) u: G::Scalar,
}

pub type CommitmentKeyHint<G> = Box<dyn Fn(&R1CSShape<G>) -> usize>;
pub type CommitmentKeyHint<G> = dyn Fn(&R1CSShape<G>) -> usize;

impl<G: Group> R1CS<G> {
/// Generates public parameters for a Rank-1 Constraint System (R1CS).
Expand All @@ -89,17 +89,16 @@ impl<G: Group> R1CS<G> {
/// # Arguments
///
/// * `S`: The shape of the R1CS matrices.
/// * `commitment_key_hint`: An optional function that provides a floor for the number of
/// generators. A good function to provide is the commitment_key_floor field in the trait `RelaxedR1CSSNARKTrait`.
/// If no floot function is provided, the default number of generators will be max(S.num_cons, S.num_vars).
/// * `commitment_key_hint`: A function that provides a floor for the number of generators. A good function
/// to provide is the commitment_key_floor field defined in the trait `RelaxedR1CSSNARKTrait`.
///
pub fn commitment_key(
S: &R1CSShape<G>,
commitment_key_floor: Option<CommitmentKeyHint<G>>,
commitment_key_floor: &CommitmentKeyHint<G>,
) -> CommitmentKey<G> {
let num_cons = S.num_cons;
let num_vars = S.num_vars;
let generators_hint = commitment_key_floor.map(|f| f(S)).unwrap_or(0);
let generators_hint = commitment_key_floor(S);
G::CE::setup(b"ck", max(max(num_cons, num_vars), generators_hint))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/spartan/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<G: Group, S: RelaxedR1CSSNARKTrait<G>, C: StepCircuit<G::Scalar>> DirectSNA
let mut cs: ShapeCS<G> = ShapeCS::new();
let _ = circuit.synthesize(&mut cs);

let (shape, ck) = cs.r1cs_shape(Some(S::commitment_key_floor()));
let (shape, ck) = cs.r1cs_shape(&*S::commitment_key_floor());

let (pk, vk) = S::setup(&ck, &shape)?;

Expand Down

0 comments on commit 474da9c

Please sign in to comment.