From 0a5d46bfa981253ac85fe83dfc620058bad7c515 Mon Sep 17 00:00:00 2001 From: Daniel Lubarov Date: Sun, 18 Jul 2021 23:14:48 -0700 Subject: [PATCH] Have `prove` return `Result` (#100) * Have `prove` return `Result` To address that TODO. * PR feedback --- src/bin/bench_recursion.rs | 11 ++++++----- src/circuit_data.rs | 4 ++-- src/gadgets/arithmetic_extension.rs | 8 +++++--- src/gadgets/insert.rs | 13 ++++++++----- src/gadgets/interpolation.rs | 14 ++++++++------ src/gadgets/rotate.rs | 8 +++++--- src/gadgets/split_base.rs | 8 +++++--- src/gates/gmimc.rs | 8 +++++--- src/merkle_proofs.rs | 2 +- src/prover.rs | 11 ++++++----- src/recursive_verifier.rs | 12 +++++++----- src/util/scaling.rs | 16 +++++++++------- 12 files changed, 67 insertions(+), 48 deletions(-) diff --git a/src/bin/bench_recursion.rs b/src/bin/bench_recursion.rs index 51f93025..05bbe52f 100644 --- a/src/bin/bench_recursion.rs +++ b/src/bin/bench_recursion.rs @@ -1,3 +1,4 @@ +use anyhow::Result; use env_logger::Env; use log::info; use plonky2::circuit_builder::CircuitBuilder; @@ -8,17 +9,17 @@ use plonky2::field::field::Field; use plonky2::fri::FriConfig; use plonky2::witness::PartialWitness; -fn main() { +fn main() -> Result<()> { // Set the default log filter. This can be overridden using the `RUST_LOG` environment variable, // e.g. `RUST_LOG=debug`. // We default to debug for now, since there aren't many logs anyway, but we should probably // change this to info or warn later. env_logger::Builder::from_env(Env::default().default_filter_or("debug")).init(); - bench_prove::(); + bench_prove::() } -fn bench_prove, const D: usize>() { +fn bench_prove, const D: usize>() -> Result<()> { let config = CircuitConfig { num_wires: 134, num_routed_wires: 27, @@ -49,8 +50,8 @@ fn bench_prove, const D: usize>() { let circuit = builder.build(); let inputs = PartialWitness::new(); - let proof = circuit.prove(inputs); + let proof = circuit.prove(inputs)?; let proof_bytes = serde_cbor::to_vec(&proof).unwrap(); info!("Proof length: {} bytes", proof_bytes.len()); - circuit.verify(proof).unwrap(); + circuit.verify(proof) } diff --git a/src/circuit_data.rs b/src/circuit_data.rs index ac3ede94..3874f25b 100644 --- a/src/circuit_data.rs +++ b/src/circuit_data.rs @@ -77,7 +77,7 @@ pub struct CircuitData, const D: usize> { } impl, const D: usize> CircuitData { - pub fn prove(&self, inputs: PartialWitness) -> Proof { + pub fn prove(&self, inputs: PartialWitness) -> Result> { prove(&self.prover_only, &self.common, inputs) } @@ -99,7 +99,7 @@ pub struct ProverCircuitData, const D: usize> { } impl, const D: usize> ProverCircuitData { - pub fn prove(&self, inputs: PartialWitness) -> Proof { + pub fn prove(&self, inputs: PartialWitness) -> Result> { prove(&self.prover_only, &self.common, inputs) } } diff --git a/src/gadgets/arithmetic_extension.rs b/src/gadgets/arithmetic_extension.rs index d8a38ea9..dfe63653 100644 --- a/src/gadgets/arithmetic_extension.rs +++ b/src/gadgets/arithmetic_extension.rs @@ -435,6 +435,8 @@ impl, const D: usize> CircuitBuilder { #[cfg(test)] mod tests { + use anyhow::Result; + use crate::circuit_builder::CircuitBuilder; use crate::circuit_data::CircuitConfig; use crate::field::crandall_field::CrandallField; @@ -444,7 +446,7 @@ mod tests { use crate::witness::PartialWitness; #[test] - fn test_div_extension() { + fn test_div_extension() -> Result<()> { type F = CrandallField; type FF = QuarticCrandallField; const D: usize = 4; @@ -465,8 +467,8 @@ mod tests { builder.assert_equal_extension(zt, comp_zt_unsafe); let data = builder.build(); - let proof = data.prove(PartialWitness::new()); + let proof = data.prove(PartialWitness::new())?; - verify(proof, &data.verifier_only, &data.common).unwrap(); + verify(proof, &data.verifier_only, &data.common) } } diff --git a/src/gadgets/insert.rs b/src/gadgets/insert.rs index 734b6c03..5424b59b 100644 --- a/src/gadgets/insert.rs +++ b/src/gadgets/insert.rs @@ -73,6 +73,8 @@ impl, const D: usize> CircuitBuilder { } #[cfg(test)] mod tests { + use anyhow::Result; + use super::*; use crate::circuit_data::CircuitConfig; use crate::field::crandall_field::CrandallField; @@ -91,7 +93,7 @@ mod tests { res } - fn test_insert_given_len(len_log: usize) { + fn test_insert_given_len(len_log: usize) -> Result<()> { type F = CrandallField; type FF = QuarticCrandallField; let len = 1 << len_log; @@ -115,15 +117,16 @@ mod tests { } let data = builder.build(); - let proof = data.prove(PartialWitness::new()); + let proof = data.prove(PartialWitness::new())?; - verify(proof, &data.verifier_only, &data.common).unwrap(); + verify(proof, &data.verifier_only, &data.common) } #[test] - fn test_insert() { + fn test_insert() -> Result<()> { for len_log in 1..3 { - test_insert_given_len(len_log); + test_insert_given_len(len_log)?; } + Ok(()) } } diff --git a/src/gadgets/interpolation.rs b/src/gadgets/interpolation.rs index a531515c..26e049d2 100644 --- a/src/gadgets/interpolation.rs +++ b/src/gadgets/interpolation.rs @@ -59,6 +59,8 @@ impl, const D: usize> CircuitBuilder { mod tests { use std::convert::TryInto; + use anyhow::Result; + use super::*; use crate::circuit_data::CircuitConfig; use crate::field::crandall_field::CrandallField; @@ -70,7 +72,7 @@ mod tests { use crate::witness::PartialWitness; #[test] - fn test_interpolate() { + fn test_interpolate() -> Result<()> { type F = CrandallField; type FF = QuarticCrandallField; let config = CircuitConfig::large_config(); @@ -103,13 +105,13 @@ mod tests { builder.assert_equal_extension(eval, true_eval_target); let data = builder.build(); - let proof = data.prove(PartialWitness::new()); + let proof = data.prove(PartialWitness::new())?; - verify(proof, &data.verifier_only, &data.common).unwrap(); + verify(proof, &data.verifier_only, &data.common) } #[test] - fn test_interpolate2() { + fn test_interpolate2() -> Result<()> { type F = CrandallField; type FF = QuarticCrandallField; let config = CircuitConfig::large_config(); @@ -137,8 +139,8 @@ mod tests { builder.assert_equal_extension(eval, true_eval_target); let data = builder.build(); - let proof = data.prove(PartialWitness::new()); + let proof = data.prove(PartialWitness::new())?; - verify(proof, &data.verifier_only, &data.common).unwrap(); + verify(proof, &data.verifier_only, &data.common) } } diff --git a/src/gadgets/rotate.rs b/src/gadgets/rotate.rs index 61c1d2cc..67b53c9c 100644 --- a/src/gadgets/rotate.rs +++ b/src/gadgets/rotate.rs @@ -113,6 +113,8 @@ impl, const D: usize> CircuitBuilder { #[cfg(test)] mod tests { + use anyhow::Result; + use super::*; use crate::circuit_data::CircuitConfig; use crate::field::crandall_field::CrandallField; @@ -130,7 +132,7 @@ mod tests { res } - fn test_rotate_given_len(len: usize) { + fn test_rotate_given_len(len: usize) -> Result<()> { type F = CrandallField; type FF = QuarticCrandallField; let config = CircuitConfig::large_config(); @@ -150,9 +152,9 @@ mod tests { } let data = builder.build(); - let proof = data.prove(PartialWitness::new()); + let proof = data.prove(PartialWitness::new())?; - verify(proof, &data.verifier_only, &data.common).unwrap(); + verify(proof, &data.verifier_only, &data.common) } #[test] diff --git a/src/gadgets/split_base.rs b/src/gadgets/split_base.rs index a3177a1f..1223170e 100644 --- a/src/gadgets/split_base.rs +++ b/src/gadgets/split_base.rs @@ -37,6 +37,8 @@ impl, const D: usize> CircuitBuilder { #[cfg(test)] mod tests { + use anyhow::Result; + use super::*; use crate::circuit_data::CircuitConfig; use crate::field::crandall_field::CrandallField; @@ -45,7 +47,7 @@ mod tests { use crate::witness::PartialWitness; #[test] - fn test_split_base() { + fn test_split_base() -> Result<()> { type F = CrandallField; let config = CircuitConfig::large_config(); let mut builder = CircuitBuilder::::new(config); @@ -67,8 +69,8 @@ mod tests { builder.assert_leading_zeros(xt, 64 - 9); let data = builder.build(); - let proof = data.prove(PartialWitness::new()); + let proof = data.prove(PartialWitness::new())?; - verify(proof, &data.verifier_only, &data.common).unwrap(); + verify(proof, &data.verifier_only, &data.common) } } diff --git a/src/gates/gmimc.rs b/src/gates/gmimc.rs index 60ad3cd5..316ec524 100644 --- a/src/gates/gmimc.rs +++ b/src/gates/gmimc.rs @@ -315,6 +315,8 @@ mod tests { use std::convert::TryInto; use std::sync::Arc; + use anyhow::Result; + use crate::circuit_builder::CircuitBuilder; use crate::circuit_data::CircuitConfig; use crate::field::crandall_field::CrandallField; @@ -402,7 +404,7 @@ mod tests { } #[test] - fn test_evals() { + fn test_evals() -> Result<()> { type F = CrandallField; type FF = QuarticCrandallField; const R: usize = 101; @@ -439,8 +441,8 @@ mod tests { } let data = builder.build(); - let proof = data.prove(pw); + let proof = data.prove(pw)?; - verify(proof, &data.verifier_only, &data.common).unwrap(); + verify(proof, &data.verifier_only, &data.common) } } diff --git a/src/merkle_proofs.rs b/src/merkle_proofs.rs index 7b7cb67a..a366ae90 100644 --- a/src/merkle_proofs.rs +++ b/src/merkle_proofs.rs @@ -200,7 +200,7 @@ mod tests { builder.verify_merkle_proof(data, i_c, root_t, &proof_t); let data = builder.build(); - let proof = data.prove(pw); + let proof = data.prove(pw)?; verify(proof, &data.verifier_only, &data.common) } diff --git a/src/prover.rs b/src/prover.rs index ce83f0a5..5174362d 100644 --- a/src/prover.rs +++ b/src/prover.rs @@ -1,5 +1,6 @@ use std::time::Instant; +use anyhow::Result; use log::info; use rayon::prelude::*; @@ -22,7 +23,7 @@ pub(crate) fn prove, const D: usize>( prover_data: &ProverOnlyCircuitData, common_data: &CommonCircuitData, inputs: PartialWitness, -) -> Proof { +) -> Result> { let fri_config = &common_data.config.fri_config; let config = &common_data.config; let num_wires = config.num_wires; @@ -46,8 +47,7 @@ pub(crate) fn prove, const D: usize>( timed!( partial_witness - .check_copy_constraints(&prover_data.copy_constraints, &prover_data.gate_instances) - .unwrap(), // TODO: Change return value to `Result` and use `?` here. + .check_copy_constraints(&prover_data.copy_constraints, &prover_data.gate_instances)?, "to check copy constraints" ); @@ -135,6 +135,7 @@ pub(crate) fn prove, const D: usize>( .into_par_iter() .flat_map(|mut quotient_poly| { quotient_poly.trim(); + // TODO: Return Result instead of panicking. quotient_poly.pad(quotient_degree).expect( "Quotient has failed, the vanishing polynomial is not divisible by `Z_H", ); @@ -178,13 +179,13 @@ pub(crate) fn prove, const D: usize>( start_proof_gen.elapsed().as_secs_f32() ); - Proof { + Ok(Proof { wires_root: wires_commitment.merkle_tree.root, plonk_zs_partial_products_root: zs_partial_products_commitment.merkle_tree.root, quotient_polys_root: quotient_polys_commitment.merkle_tree.root, openings, opening_proof, - } + }) } /// Compute the partial products used in the `Z` polynomials. diff --git a/src/recursive_verifier.rs b/src/recursive_verifier.rs index d9310b04..346ba3e8 100644 --- a/src/recursive_verifier.rs +++ b/src/recursive_verifier.rs @@ -107,6 +107,8 @@ impl, const D: usize> CircuitBuilder { #[cfg(test)] mod tests { + use anyhow::Result; + use super::*; use crate::field::crandall_field::CrandallField; use crate::fri::FriConfig; @@ -314,7 +316,7 @@ mod tests { #[test] #[ignore] - fn test_recursive_verifier() { + fn test_recursive_verifier() -> Result<()> { env_logger::init(); type F = CrandallField; const D: usize = 4; @@ -340,12 +342,12 @@ mod tests { } let data = builder.build(); ( - data.prove(PartialWitness::new()), + data.prove(PartialWitness::new())?, data.verifier_only, data.common, ) }; - verify(proof.clone(), &vd, &cd).unwrap(); + verify(proof.clone(), &vd, &cd)?; let mut builder = CircuitBuilder::::new(config.clone()); let mut pw = PartialWitness::new(); @@ -360,8 +362,8 @@ mod tests { builder.add_recursive_verifier(pt, &config, &inner_data, &cd); let data = builder.build(); - let recursive_proof = data.prove(pw); + let recursive_proof = data.prove(pw)?; - verify(recursive_proof, &data.verifier_only, &data.common).unwrap(); + verify(recursive_proof, &data.verifier_only, &data.common) } } diff --git a/src/util/scaling.rs b/src/util/scaling.rs index e2cedfb8..be339784 100644 --- a/src/util/scaling.rs +++ b/src/util/scaling.rs @@ -170,6 +170,8 @@ impl ReducingFactorTarget { #[cfg(test)] mod tests { + use anyhow::Result; + use super::*; use crate::circuit_data::CircuitConfig; use crate::field::crandall_field::CrandallField; @@ -177,7 +179,7 @@ mod tests { use crate::verifier::verify; use crate::witness::PartialWitness; - fn test_reduce_gadget(n: usize) { + fn test_reduce_gadget(n: usize) -> Result<()> { type F = CrandallField; type FF = QuarticCrandallField; const D: usize = 4; @@ -202,18 +204,18 @@ mod tests { builder.assert_equal_extension(manual_reduce, circuit_reduce); let data = builder.build(); - let proof = data.prove(PartialWitness::new()); + let proof = data.prove(PartialWitness::new())?; - verify(proof, &data.verifier_only, &data.common).unwrap(); + verify(proof, &data.verifier_only, &data.common) } #[test] - fn test_reduce_gadget_even() { - test_reduce_gadget(10); + fn test_reduce_gadget_even() -> Result<()> { + test_reduce_gadget(10) } #[test] - fn test_reduce_gadget_odd() { - test_reduce_gadget(11); + fn test_reduce_gadget_odd() -> Result<()> { + test_reduce_gadget(11) } }