Have prove return Result (#100)

* Have `prove` return `Result`

To address that TODO.

* PR feedback
This commit is contained in:
Daniel Lubarov 2021-07-18 23:14:48 -07:00 committed by GitHub
parent 9c17a00c00
commit 0a5d46bfa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 67 additions and 48 deletions

View File

@ -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::<CrandallField, 4>();
bench_prove::<CrandallField, 4>()
}
fn bench_prove<F: Field + Extendable<D>, const D: usize>() {
fn bench_prove<F: Field + Extendable<D>, const D: usize>() -> Result<()> {
let config = CircuitConfig {
num_wires: 134,
num_routed_wires: 27,
@ -49,8 +50,8 @@ fn bench_prove<F: Field + Extendable<D>, 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)
}

View File

@ -77,7 +77,7 @@ pub struct CircuitData<F: Extendable<D>, const D: usize> {
}
impl<F: Extendable<D>, const D: usize> CircuitData<F, D> {
pub fn prove(&self, inputs: PartialWitness<F>) -> Proof<F, D> {
pub fn prove(&self, inputs: PartialWitness<F>) -> Result<Proof<F, D>> {
prove(&self.prover_only, &self.common, inputs)
}
@ -99,7 +99,7 @@ pub struct ProverCircuitData<F: Extendable<D>, const D: usize> {
}
impl<F: Extendable<D>, const D: usize> ProverCircuitData<F, D> {
pub fn prove(&self, inputs: PartialWitness<F>) -> Proof<F, D> {
pub fn prove(&self, inputs: PartialWitness<F>) -> Result<Proof<F, D>> {
prove(&self.prover_only, &self.common, inputs)
}
}

View File

@ -435,6 +435,8 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
#[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)
}
}

View File

@ -73,6 +73,8 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
}
#[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(())
}
}

View File

@ -59,6 +59,8 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
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)
}
}

View File

@ -113,6 +113,8 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
#[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]

View File

@ -37,6 +37,8 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
#[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::<F, 4>::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)
}
}

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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<F: Extendable<D>, const D: usize>(
prover_data: &ProverOnlyCircuitData<F, D>,
common_data: &CommonCircuitData<F, D>,
inputs: PartialWitness<F>,
) -> Proof<F, D> {
) -> Result<Proof<F, D>> {
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<F: Extendable<D>, 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<F: Extendable<D>, 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<F: Extendable<D>, 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.

View File

@ -107,6 +107,8 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
#[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::<F, D>::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)
}
}

View File

@ -170,6 +170,8 @@ impl<const D: usize> ReducingFactorTarget<D> {
#[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)
}
}