Skip to content

Commit

Permalink
Remove fs from the parameters of Decider::preprocess (#193)
Browse files Browse the repository at this point in the history
* `Decider::preprocess` now no longer takes `fs` as input

* Format

* Fix examples

* Fix solidity verifiers
  • Loading branch information
winderica authored Jan 10, 2025
1 parent c6f1a24 commit 31fbe72
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 114 deletions.
7 changes: 4 additions & 3 deletions examples/circom_full_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ fn main() -> Result<(), Error> {
let nova_preprocess_params = PreprocessorParam::new(poseidon_config, f_circuit.clone());
let nova_params = N::preprocess(&mut rng, &nova_preprocess_params)?;

// prepare the Decider prover & verifier params
let (decider_pp, decider_vp) =
D::preprocess(&mut rng, (nova_params.clone(), f_circuit.state_len()))?;

// initialize the folding scheme engine, in our case we use Nova
let mut nova = N::init(&nova_params, f_circuit.clone(), z_0)?;

// prepare the Decider prover & verifier params
let (decider_pp, decider_vp) = D::preprocess(&mut rng, nova_params.clone(), nova.clone())?;

// run n steps of the folding iteration
for (i, external_inputs_at_step) in external_inputs.iter().enumerate() {
let start = Instant::now();
Expand Down
7 changes: 4 additions & 3 deletions examples/full_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ fn main() -> Result<(), Error> {
let nova_preprocess_params = PreprocessorParam::new(poseidon_config.clone(), f_circuit);
let nova_params = N::preprocess(&mut rng, &nova_preprocess_params)?;

// prepare the Decider prover & verifier params
let (decider_pp, decider_vp) =
D::preprocess(&mut rng, (nova_params.clone(), f_circuit.state_len()))?;

// initialize the folding scheme engine, in our case we use Nova
let mut nova = N::init(&nova_params, f_circuit, z_0)?;

// prepare the Decider prover & verifier params
let (decider_pp, decider_vp) = D::preprocess(&mut rng, nova_params, nova.clone())?;

// run n steps of the folding iteration
for i in 0..n_steps {
let start = Instant::now();
Expand Down
7 changes: 4 additions & 3 deletions examples/noir_full_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ fn main() -> Result<(), Error> {
let nova_preprocess_params = PreprocessorParam::new(poseidon_config, f_circuit.clone());
let nova_params = N::preprocess(&mut rng, &nova_preprocess_params)?;

// prepare the Decider prover & verifier params
let (decider_pp, decider_vp) =
D::preprocess(&mut rng, (nova_params.clone(), f_circuit.state_len()))?;

// initialize the folding scheme engine, in our case we use Nova
let mut nova = N::init(&nova_params, f_circuit.clone(), z_0)?;

// prepare the Decider prover & verifier params
let (decider_pp, decider_vp) = D::preprocess(&mut rng, nova_params.clone(), nova.clone())?;

// run n steps of the folding iteration
for i in 0..5 {
let start = Instant::now();
Expand Down
7 changes: 4 additions & 3 deletions examples/noname_full_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ fn main() -> Result<(), Error> {
let nova_preprocess_params = PreprocessorParam::new(poseidon_config, f_circuit.clone());
let nova_params = N::preprocess(&mut rng, &nova_preprocess_params)?;

// prepare the Decider prover & verifier params
let (decider_pp, decider_vp) =
D::preprocess(&mut rng, (nova_params.clone(), f_circuit.state_len()))?;

// initialize the folding scheme engine, in our case we use Nova
let mut nova = N::init(&nova_params, f_circuit.clone(), z_0)?;

// prepare the Decider prover & verifier params
let (decider_pp, decider_vp) = D::preprocess(&mut rng, nova_params.clone(), nova.clone())?;

// run n steps of the folding iteration
for (i, external_inputs_at_step) in external_inputs.iter().enumerate() {
let start = Instant::now();
Expand Down
43 changes: 25 additions & 18 deletions folding-schemes/src/folding/hypernova/decider_eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::commitment::{
};
use crate::folding::circuits::decider::DeciderEnabledNIFS;
use crate::folding::nova::decider_eth::VerifierParam;
use crate::folding::traits::WitnessOps;
use crate::folding::traits::{Dummy, WitnessOps};
use crate::frontend::FCircuit;
use crate::{Curve, Error};
use crate::{Decider as DeciderTrait, FoldingScheme};
Expand Down Expand Up @@ -70,7 +70,7 @@ where
crate::folding::hypernova::VerifierParams<C1, C2, CS1, CS2, false>:
From<<FS as FoldingScheme<C1, C2, FC>>::VerifierParam>,
{
type PreprocessorParam = (FS::ProverParam, FS::VerifierParam);
type PreprocessorParam = ((FS::ProverParam, FS::VerifierParam), usize);
type ProverParam = (S::ProvingKey, CS1::ProverParams);
type Proof = Proof<C1, CS1, S>;
type VerifierParam = VerifierParam<C1, CS1::VerifierParams, S::VerifyingKey>;
Expand All @@ -79,30 +79,39 @@ where

fn preprocess(
mut rng: impl RngCore + CryptoRng,
prep_param: Self::PreprocessorParam,
fs: FS,
((pp, vp), state_len): Self::PreprocessorParam,
) -> Result<(Self::ProverParam, Self::VerifierParam), Error> {
let circuit = DeciderEthCircuit::<C1, C2>::try_from(HyperNova::from(fs))?;

// get the Groth16 specific setup for the circuit
let (g16_pk, g16_vk) = S::circuit_specific_setup(circuit, &mut rng)
.map_err(|e| Error::SNARKSetupFail(e.to_string()))?;

// get the FoldingScheme prover & verifier params from HyperNova
#[allow(clippy::type_complexity)]
let hypernova_pp: <HyperNova<C1, C2, FC, CS1, CS2, MU, NU, false> as FoldingScheme<
C1,
C2,
FC,
>>::ProverParam = prep_param.0.into();
#[allow(clippy::type_complexity)]
>>::ProverParam = pp.into();
let hypernova_vp: <HyperNova<C1, C2, FC, CS1, CS2, MU, NU, false> as FoldingScheme<
C1,
C2,
FC,
>>::VerifierParam = prep_param.1.into();
>>::VerifierParam = vp.into();
let pp_hash = hypernova_vp.pp_hash()?;

let s = hypernova_vp.ccs.s;
let t = hypernova_vp.ccs.t;

let circuit = DeciderEthCircuit::<C1, C2>::dummy((
hypernova_vp.ccs,
hypernova_vp.cf_r1cs,
hypernova_pp.cf_cs_pp,
hypernova_pp.poseidon_config,
(s, t, MU, NU),
(),
state_len,
1, // HyperNova's LCCCS contains 1 commitment
));

// get the Groth16 specific setup for the circuit
let (g16_pk, g16_vk) = S::circuit_specific_setup(circuit, &mut rng)
.map_err(|e| Error::SNARKSetupFail(e.to_string()))?;

let pp = (g16_pk, hypernova_pp.cs_pp);

let vp = Self::VerifierParam {
Expand Down Expand Up @@ -265,7 +274,7 @@ pub mod tests {

// prepare the Decider prover & verifier params
let (decider_pp, decider_vp) =
D::preprocess(&mut rng, hypernova_params, hypernova.clone())?;
D::preprocess(&mut rng, (hypernova_params, F_circuit.state_len()))?;

// decider proof generation
let proof = D::prove(rng, decider_pp, hypernova.clone())?;
Expand Down Expand Up @@ -320,13 +329,11 @@ pub mod tests {
let prep_param = PreprocessorParam::new(poseidon_config.clone(), F_circuit);
let hypernova_params = HN::preprocess(&mut rng, &prep_param)?;

let hypernova = HN::init(&hypernova_params, F_circuit, z_0.clone())?;

let mut rng = rand::rngs::OsRng;

// prepare the Decider prover & verifier params
let (decider_pp, decider_vp) =
D::preprocess(&mut rng, hypernova_params.clone(), hypernova.clone())?;
D::preprocess(&mut rng, (hypernova_params.clone(), F_circuit.state_len()))?;

let mut hypernova_pp_serialized = vec![];
hypernova_params
Expand Down
54 changes: 32 additions & 22 deletions folding-schemes/src/folding/nova/decider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ use super::Nova;
use crate::commitment::CommitmentScheme;
use crate::folding::circuits::cyclefold::CycleFoldCommittedInstance;
use crate::folding::circuits::decider::DeciderEnabledNIFS;
use crate::folding::traits::{CommittedInstanceOps, Inputize, InputizeNonNative, WitnessOps};
use crate::folding::traits::{
CommittedInstanceOps, Dummy, Inputize, InputizeNonNative, WitnessOps,
};
use crate::frontend::FCircuit;
use crate::transcript::poseidon::poseidon_canonical_config;
use crate::{Curve, Error};
use crate::{Decider as DeciderTrait, FoldingScheme};

Expand Down Expand Up @@ -117,7 +120,7 @@ where
crate::folding::nova::VerifierParams<C1, C2, CS1, CS2, false>:
From<<FS as FoldingScheme<C1, C2, FC>>::VerifierParam>,
{
type PreprocessorParam = (FS::ProverParam, FS::VerifierParam);
type PreprocessorParam = ((FS::ProverParam, FS::VerifierParam), usize);
type ProverParam =
ProverParam<CS1::ProverParams, S1::ProvingKey, CS2::ProverParams, S2::ProvingKey>;
type Proof = Proof<C1, C2, CS1, CS2, S1, S2>;
Expand All @@ -133,33 +136,39 @@ where

fn preprocess(
mut rng: impl RngCore + CryptoRng,
prep_param: Self::PreprocessorParam,
fs: FS,
((pp, vp), state_len): Self::PreprocessorParam,
) -> Result<(Self::ProverParam, Self::VerifierParam), Error> {
let circuit1 = DeciderCircuit1::<C1, C2>::try_from(Nova::from(fs.clone()))?;
let circuit2 = DeciderCircuit2::<C2>::try_from(Nova::from(fs))?;
// get the FoldingScheme prover & verifier params from Nova
let nova_pp: <Nova<C1, C2, FC, CS1, CS2, false> as FoldingScheme<C1, C2, FC>>::ProverParam =
pp.into();
let nova_vp: <Nova<C1, C2, FC, CS1, CS2, false> as FoldingScheme<
C1,
C2,
FC,
>>::VerifierParam = vp.into();
let pp_hash = nova_vp.pp_hash()?;

let circuit1 = DeciderCircuit1::<C1, C2>::dummy((
nova_vp.r1cs,
&nova_vp.cf_r1cs,
nova_vp.poseidon_config,
(),
(),
state_len,
2, // Nova's running CommittedInstance contains 2 commitments
));
let circuit2 = DeciderCircuit2::<C2>::dummy((
nova_vp.cf_r1cs,
poseidon_canonical_config::<C2::ScalarField>(),
2, // Nova's running CommittedInstance contains 2 commitments
));

// get the Groth16 specific setup for the circuits
let (c1_g16_pk, c1_g16_vk) = S1::circuit_specific_setup(circuit1, &mut rng)
.map_err(|e| Error::SNARKSetupFail(e.to_string()))?;
let (c2_g16_pk, c2_g16_vk) = S2::circuit_specific_setup(circuit2, &mut rng)
.map_err(|e| Error::SNARKSetupFail(e.to_string()))?;

// get the FoldingScheme prover & verifier params from Nova
#[allow(clippy::type_complexity)]
let nova_pp: <Nova<C1, C2, FC, CS1, CS2, false> as FoldingScheme<
C1,
C2,
FC,
>>::ProverParam = prep_param.0.clone().into();
#[allow(clippy::type_complexity)]
let nova_vp: <Nova<C1, C2, FC, CS1, CS2, false> as FoldingScheme<
C1,
C2,
FC,
>>::VerifierParam = prep_param.1.clone().into();

let pp_hash = nova_vp.pp_hash()?;
let pp = Self::ProverParam {
c1_snark_pp: c1_g16_pk,
c1_cs_pp: nova_pp.cs_pp,
Expand Down Expand Up @@ -379,7 +388,8 @@ pub mod tests {

// prepare the Decider prover & verifier params
let start = Instant::now();
let (decider_pp, decider_vp) = D::preprocess(&mut rng, nova_params, nova.clone())?;
let (decider_pp, decider_vp) =
D::preprocess(&mut rng, (nova_params, F_circuit.state_len()))?;
println!("Decider preprocess, {:?}", start.elapsed());

// decider proof generation
Expand Down
65 changes: 35 additions & 30 deletions folding-schemes/src/folding/nova/decider_eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ use core::marker::PhantomData;
pub use super::decider_eth_circuit::DeciderEthCircuit;
use super::decider_eth_circuit::DeciderNovaGadget;
use super::{CommittedInstance, Nova};
use crate::commitment::{
kzg::{Proof as KZGProof, KZG},
pedersen::Params as PedersenParams,
CommitmentScheme,
};
use crate::folding::circuits::decider::DeciderEnabledNIFS;
use crate::folding::traits::{InputizeNonNative, WitnessOps};
use crate::frontend::FCircuit;
use crate::utils::eth::ToEth;
use crate::{
commitment::{
kzg::{Proof as KZGProof, KZG},
pedersen::Params as PedersenParams,
CommitmentScheme,
},
folding::traits::Dummy,
};
use crate::{Curve, Error};
use crate::{Decider as DeciderTrait, FoldingScheme};

Expand Down Expand Up @@ -93,7 +96,7 @@ where
crate::folding::nova::VerifierParams<C1, C2, CS1, CS2, false>:
From<<FS as FoldingScheme<C1, C2, FC>>::VerifierParam>,
{
type PreprocessorParam = (FS::ProverParam, FS::VerifierParam);
type PreprocessorParam = ((FS::ProverParam, FS::VerifierParam), usize);
type ProverParam = (S::ProvingKey, CS1::ProverParams);
type Proof = Proof<C1, CS1, S>;
type VerifierParam = VerifierParam<C1, CS1::VerifierParams, S::VerifyingKey>;
Expand All @@ -102,30 +105,34 @@ where

fn preprocess(
mut rng: impl RngCore + CryptoRng,
prep_param: Self::PreprocessorParam,
fs: FS,
((pp, vp), state_len): Self::PreprocessorParam,
) -> Result<(Self::ProverParam, Self::VerifierParam), Error> {
let circuit = DeciderEthCircuit::<C1, C2>::try_from(Nova::from(fs))?;
// get the FoldingScheme prover & verifier params from Nova
let nova_pp: <Nova<C1, C2, FC, CS1, CS2, false> as FoldingScheme<C1, C2, FC>>::ProverParam =
pp.into();
let nova_vp: <Nova<C1, C2, FC, CS1, CS2, false> as FoldingScheme<
C1,
C2,
FC,
>>::VerifierParam = vp.into();

let pp_hash = nova_vp.pp_hash()?;

let circuit = DeciderEthCircuit::<C1, C2>::dummy((
nova_vp.r1cs,
nova_vp.cf_r1cs,
nova_pp.cf_cs_pp,
nova_pp.poseidon_config,
(),
(),
state_len,
2, // Nova's running CommittedInstance contains 2 commitments
));

// get the Groth16 specific setup for the circuit
let (g16_pk, g16_vk) = S::circuit_specific_setup(circuit, &mut rng)
.map_err(|e| Error::SNARKSetupFail(e.to_string()))?;

// get the FoldingScheme prover & verifier params from Nova
#[allow(clippy::type_complexity)]
let nova_pp: <Nova<C1, C2, FC, CS1, CS2, false> as FoldingScheme<
C1,
C2,
FC,
>>::ProverParam = prep_param.0.clone().into();
#[allow(clippy::type_complexity)]
let nova_vp: <Nova<C1, C2, FC, CS1, CS2, false> as FoldingScheme<
C1,
C2,
FC,
>>::VerifierParam = prep_param.1.clone().into();
let pp_hash = nova_vp.pp_hash()?;

let pp = (g16_pk, nova_pp.cs_pp);
let vp = Self::VerifierParam {
pp_hash,
Expand Down Expand Up @@ -316,7 +323,8 @@ pub mod tests {
println!("Nova initialized, {:?}", start.elapsed());

// prepare the Decider prover & verifier params
let (decider_pp, decider_vp) = D::preprocess(&mut rng, nova_params, nova.clone())?;
let (decider_pp, decider_vp) =
D::preprocess(&mut rng, (nova_params, F_circuit.state_len()))?;

let start = Instant::now();
nova.prove_step(&mut rng, (), None)?;
Expand Down Expand Up @@ -389,12 +397,9 @@ pub mod tests {
let preprocessor_param = PreprocessorParam::new(poseidon_config, F_circuit);
let nova_params = N::preprocess(&mut rng, &preprocessor_param)?;

let start = Instant::now();
let nova = N::init(&nova_params, F_circuit, z_0.clone())?;
println!("Nova initialized, {:?}", start.elapsed());

// prepare the Decider prover & verifier params
let (decider_pp, decider_vp) = D::preprocess(&mut rng, nova_params.clone(), nova.clone())?;
let (decider_pp, decider_vp) =
D::preprocess(&mut rng, (nova_params.clone(), F_circuit.state_len()))?;

// serialize the Nova params. These params are the trusted setup of the commitment schemes used
// (ie. KZG & Pedersen in this case)
Expand Down
Loading

0 comments on commit 31fbe72

Please sign in to comment.