Skip to content

Commit

Permalink
migrate tests to use result instead of unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaucube committed Dec 2, 2024
1 parent 2a5e812 commit f13a8fd
Show file tree
Hide file tree
Showing 57 changed files with 1,189 additions and 1,267 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ jobs:
feature_set: [basic, wasm]
include:
- feature_set: basic
features: --features default,light-test
features: --features default
# We only want to test `frontends` package with `wasm` feature.
- feature_set: wasm
features: -p frontends --features wasm,parallel --target wasm32-unknown-unknown
Expand Down
33 changes: 14 additions & 19 deletions examples/circom_full_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use folding_schemes::{
},
frontend::FCircuit,
transcript::poseidon::poseidon_canonical_config,
Decider, FoldingScheme,
Decider, Error, FoldingScheme,
};
use solidity_verifiers::{
evm::{compile_solidity, Evm},
Expand All @@ -38,7 +38,7 @@ use solidity_verifiers::{
NovaCycleFoldVerifierKey,
};

fn main() {
fn main() -> Result<(), Error> {
// set the initial state
let z_0 = vec![Fr::from(3_u32)];

Expand All @@ -65,7 +65,7 @@ fn main() {
);

let f_circuit_params = (r1cs_path.into(), wasm_path.into(), 1, 2);
let f_circuit = CircomFCircuit::<Fr>::new(f_circuit_params).unwrap();
let f_circuit = CircomFCircuit::<Fr>::new(f_circuit_params)?;

pub type N =
Nova<G1, GVar, G2, GVar2, CircomFCircuit<Fr>, KZG<'static, Bn254>, Pedersen<G2>, false>;
Expand All @@ -86,20 +86,18 @@ fn main() {

// prepare the Nova prover & verifier params
let nova_preprocess_params = PreprocessorParam::new(poseidon_config, f_circuit.clone());
let nova_params = N::preprocess(&mut rng, &nova_preprocess_params).unwrap();
let nova_params = N::preprocess(&mut rng, &nova_preprocess_params)?;

// initialize the folding scheme engine, in our case we use Nova
let mut nova = N::init(&nova_params, f_circuit.clone(), z_0).unwrap();
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()).unwrap();
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();
nova.prove_step(rng, external_inputs_at_step.clone(), None)
.unwrap();
nova.prove_step(rng, external_inputs_at_step.clone(), None)?;
println!("Nova::prove_step {}: {:?}", i, start.elapsed());
}

Expand All @@ -108,11 +106,10 @@ fn main() {
N::verify(
nova_params.1, // Nova's verifier params
ivc_proof,
)
.unwrap();
)?;

let start = Instant::now();
let proof = D::prove(rng, decider_pp, nova.clone()).unwrap();
let proof = D::prove(rng, decider_pp, nova.clone())?;
println!("generated Decider proof: {:?}", start.elapsed());

let verified = D::verify(
Expand All @@ -123,8 +120,7 @@ fn main() {
&nova.U_i.get_commitments(),
&nova.u_i.get_commitments(),
&proof,
)
.unwrap();
)?;
assert!(verified);
println!("Decider proof verification: {}", verified);

Expand All @@ -140,8 +136,7 @@ fn main() {
&nova.U_i,
&nova.u_i,
proof,
)
.unwrap();
)?;

// prepare the setup params for the solidity verifier
let nova_cyclefold_vk = NovaCycleFoldVerifierKey::from((decider_vp, f_circuit.state_len()));
Expand All @@ -162,9 +157,9 @@ fn main() {
fs::write(
"./examples/nova-verifier.sol",
decider_solidity_code.clone(),
)
.unwrap();
fs::write("./examples/solidity-calldata.calldata", calldata.clone()).unwrap();
)?;
fs::write("./examples/solidity-calldata.calldata", calldata.clone())?;
let s = solidity_verifiers::utils::get_formatted_calldata(calldata.clone());
fs::write("./examples/solidity-calldata.inputs", s.join(",\n")).expect("");
Ok(())
}
26 changes: 12 additions & 14 deletions examples/full_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ impl<F: PrimeField> FCircuit<F> for CubicFCircuit<F> {
}
}

fn main() {
fn main() -> Result<(), Error> {
let n_steps = 5;
// set the initial state
let z_0 = vec![Fr::from(3_u32)];

let f_circuit = CubicFCircuit::<Fr>::new(()).unwrap();
let f_circuit = CubicFCircuit::<Fr>::new(())?;

pub type N =
Nova<G1, GVar, G2, GVar2, CubicFCircuit<Fr>, KZG<'static, Bn254>, Pedersen<G2>, false>;
Expand All @@ -103,23 +103,23 @@ fn main() {

// prepare the Nova prover & verifier params
let nova_preprocess_params = PreprocessorParam::new(poseidon_config.clone(), f_circuit);
let nova_params = N::preprocess(&mut rng, &nova_preprocess_params).unwrap();
let nova_params = N::preprocess(&mut rng, &nova_preprocess_params)?;

// initialize the folding scheme engine, in our case we use Nova
let mut nova = N::init(&nova_params, f_circuit, z_0).unwrap();
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()).unwrap();
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();
nova.prove_step(rng, vec![], None).unwrap();
nova.prove_step(rng, vec![], None)?;
println!("Nova::prove_step {}: {:?}", i, start.elapsed());
}

let start = Instant::now();
let proof = D::prove(rng, decider_pp, nova.clone()).unwrap();
let proof = D::prove(rng, decider_pp, nova.clone())?;
println!("generated Decider proof: {:?}", start.elapsed());

let verified = D::verify(
Expand All @@ -130,8 +130,7 @@ fn main() {
&nova.U_i.get_commitments(),
&nova.u_i.get_commitments(),
&proof,
)
.unwrap();
)?;
assert!(verified);
println!("Decider proof verification: {}", verified);

Expand All @@ -147,8 +146,7 @@ fn main() {
&nova.U_i,
&nova.u_i,
proof,
)
.unwrap();
)?;

// prepare the setup params for the solidity verifier
let nova_cyclefold_vk = NovaCycleFoldVerifierKey::from((decider_vp, f_circuit.state_len()));
Expand All @@ -169,9 +167,9 @@ fn main() {
fs::write(
"./examples/nova-verifier.sol",
decider_solidity_code.clone(),
)
.unwrap();
fs::write("./examples/solidity-calldata.calldata", calldata.clone()).unwrap();
)?;
fs::write("./examples/solidity-calldata.calldata", calldata.clone())?;
let s = solidity_verifiers::utils::get_formatted_calldata(calldata.clone());
fs::write("./examples/solidity-calldata.inputs", s.join(",\n")).expect("");
Ok(())
}
30 changes: 15 additions & 15 deletions examples/multi_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ pub mod tests {

// test to check that the MultiInputsFCircuit computes the same values inside and outside the circuit
#[test]
fn test_f_circuit() {
fn test_f_circuit() -> Result<(), Error> {
let cs = ConstraintSystem::<Fr>::new_ref();

let circuit = MultiInputsFCircuit::<Fr>::new(()).unwrap();
let circuit = MultiInputsFCircuit::<Fr>::new(())?;
let z_i = vec![
Fr::from(1_u32),
Fr::from(1_u32),
Expand All @@ -100,18 +100,18 @@ pub mod tests {
Fr::from(1_u32),
];

let z_i1 = circuit.step_native(0, z_i.clone(), vec![]).unwrap();
let z_i1 = circuit.step_native(0, z_i.clone(), vec![])?;

let z_iVar = Vec::<FpVar<Fr>>::new_witness(cs.clone(), || Ok(z_i)).unwrap();
let computed_z_i1Var = circuit
.generate_step_constraints(cs.clone(), 0, z_iVar.clone(), vec![])
.unwrap();
assert_eq!(computed_z_i1Var.value().unwrap(), z_i1);
let z_iVar = Vec::<FpVar<Fr>>::new_witness(cs.clone(), || Ok(z_i))?;
let computed_z_i1Var =
circuit.generate_step_constraints(cs.clone(), 0, z_iVar.clone(), vec![])?;
assert_eq!(computed_z_i1Var.value()?, z_i1);
Ok(())
}
}

/// cargo run --release --example multi_inputs
fn main() {
fn main() -> Result<(), Error> {
let num_steps = 10;
let initial_state = vec![
Fr::from(1_u32),
Expand All @@ -121,7 +121,7 @@ fn main() {
Fr::from(1_u32),
];

let F_circuit = MultiInputsFCircuit::<Fr>::new(()).unwrap();
let F_circuit = MultiInputsFCircuit::<Fr>::new(())?;

let poseidon_config = poseidon_canonical_config::<Fr>();
let mut rng = rand::rngs::OsRng;
Expand All @@ -142,15 +142,15 @@ fn main() {

println!("Prepare Nova ProverParams & VerifierParams");
let nova_preprocess_params = PreprocessorParam::new(poseidon_config, F_circuit);
let nova_params = N::preprocess(&mut rng, &nova_preprocess_params).unwrap();
let nova_params = N::preprocess(&mut rng, &nova_preprocess_params)?;

println!("Initialize FoldingScheme");
let mut folding_scheme = N::init(&nova_params, F_circuit, initial_state.clone()).unwrap();
let mut folding_scheme = N::init(&nova_params, F_circuit, initial_state.clone())?;

// compute a step of the IVC
for i in 0..num_steps {
let start = Instant::now();
folding_scheme.prove_step(rng, vec![], None).unwrap();
folding_scheme.prove_step(rng, vec![], None)?;
println!("Nova::prove_step {}: {:?}", i, start.elapsed());
}

Expand All @@ -159,6 +159,6 @@ fn main() {
N::verify(
nova_params.1, // Nova's verifier params
ivc_proof,
)
.unwrap();
)?;
Ok(())
}
33 changes: 14 additions & 19 deletions examples/noir_full_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use folding_schemes::{
},
frontend::FCircuit,
transcript::poseidon::poseidon_canonical_config,
Decider, FoldingScheme,
Decider, Error, FoldingScheme,
};
use std::{path::Path, time::Instant};

Expand All @@ -37,7 +37,7 @@ use solidity_verifiers::{
NovaCycleFoldVerifierKey,
};

fn main() {
fn main() -> Result<(), Error> {
// set the initial state
let z_0 = vec![Fr::from(1)];

Expand All @@ -47,8 +47,7 @@ fn main() {
.into(),
1,
0,
))
.unwrap();
))?;

pub type N = Nova<G1, GVar, G2, GVar2, NoirFCircuit<Fr>, KZG<'static, Bn254>, Pedersen<G2>>;
pub type D = DeciderEth<
Expand All @@ -68,31 +67,29 @@ fn main() {

// prepare the Nova prover & verifier params
let nova_preprocess_params = PreprocessorParam::new(poseidon_config, f_circuit.clone());
let nova_params = N::preprocess(&mut rng, &nova_preprocess_params).unwrap();
let nova_params = N::preprocess(&mut rng, &nova_preprocess_params)?;

// initialize the folding scheme engine, in our case we use Nova
let mut nova = N::init(&nova_params, f_circuit.clone(), z_0).unwrap();
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()).unwrap();
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();
nova.prove_step(rng, vec![], None).unwrap();
nova.prove_step(rng, vec![], None)?;
println!("Nova::prove_step {}: {:?}", i, start.elapsed());
}
// verify the last IVC proof
let ivc_proof = nova.ivc_proof();
N::verify(
nova_params.1, // Nova's verifier params
ivc_proof,
)
.unwrap();
)?;

let start = Instant::now();
let proof = D::prove(rng, decider_pp, nova.clone()).unwrap();
let proof = D::prove(rng, decider_pp, nova.clone())?;
println!("generated Decider proof: {:?}", start.elapsed());

let verified = D::verify(
Expand All @@ -103,8 +100,7 @@ fn main() {
&nova.U_i.get_commitments(),
&nova.u_i.get_commitments(),
&proof,
)
.unwrap();
)?;
assert!(verified);
println!("Decider proof verification: {}", verified);

Expand All @@ -120,8 +116,7 @@ fn main() {
&nova.U_i,
&nova.u_i,
proof,
)
.unwrap();
)?;

// prepare the setup params for the solidity verifier
let nova_cyclefold_vk = NovaCycleFoldVerifierKey::from((decider_vp, f_circuit.state_len()));
Expand All @@ -142,9 +137,9 @@ fn main() {
fs::write(
"./examples/nova-verifier.sol",
decider_solidity_code.clone(),
)
.unwrap();
fs::write("./examples/solidity-calldata.calldata", calldata.clone()).unwrap();
)?;
fs::write("./examples/solidity-calldata.calldata", calldata.clone())?;
let s = solidity_verifiers::utils::get_formatted_calldata(calldata.clone());
fs::write("./examples/solidity-calldata.inputs", s.join(",\n")).expect("");
Ok(())
}
Loading

0 comments on commit f13a8fd

Please sign in to comment.