From 3190c579ae3fd3ddc90bb9fddf1de903dc604563 Mon Sep 17 00:00:00 2001 From: guorong009 Date: Thu, 9 May 2024 22:31:06 +0800 Subject: [PATCH] fix!: remove the "create_proof_custom_with_engine" --- halo2_frontend/src/circuit.rs | 1 + halo2_proofs/src/plonk.rs | 2 +- halo2_proofs/src/plonk/prover.rs | 93 ++++++++---------------- halo2_proofs/tests/compress_selectors.rs | 46 ++++-------- 4 files changed, 47 insertions(+), 95 deletions(-) diff --git a/halo2_frontend/src/circuit.rs b/halo2_frontend/src/circuit.rs index 157127dc02..d50e64d99c 100644 --- a/halo2_frontend/src/circuit.rs +++ b/halo2_frontend/src/circuit.rs @@ -50,6 +50,7 @@ pub fn compile_circuit>( Error, > { let n = 2usize.pow(k); + let mut cs = ConstraintSystem::default(); #[cfg(feature = "circuit-params")] let config = ConcreteCircuit::configure_with_params(&mut cs, circuit.params()); diff --git a/halo2_proofs/src/plonk.rs b/halo2_proofs/src/plonk.rs index 23c5eb0511..41a2feb2d9 100644 --- a/halo2_proofs/src/plonk.rs +++ b/halo2_proofs/src/plonk.rs @@ -15,7 +15,7 @@ mod verifier { use halo2_frontend::circuit::compile_circuit; pub use keygen::{keygen_pk, keygen_pk_custom, keygen_vk, keygen_vk_custom}; -pub use prover::{create_proof, create_proof_custom_with_engine, create_proof_with_engine}; +pub use prover::{create_proof, create_proof_with_engine}; pub use verifier::verify_proof; pub use error::Error; diff --git a/halo2_proofs/src/plonk/prover.rs b/halo2_proofs/src/plonk/prover.rs index 460f9630fa..133df4da40 100644 --- a/halo2_proofs/src/plonk/prover.rs +++ b/halo2_proofs/src/plonk/prover.rs @@ -2,8 +2,8 @@ use crate::plonk::{Error, ErrorBack}; use crate::poly::commitment::{self, CommitmentScheme, Params}; use crate::transcript::{EncodedChallenge, TranscriptWrite}; use halo2_backend::plonk::{prover::Prover, ProvingKey}; -use halo2_frontend::circuit::{compile_circuit, WitnessCalculator}; -use halo2_frontend::plonk::Circuit; +use halo2_frontend::circuit::WitnessCalculator; +use halo2_frontend::plonk::{Circuit, ConstraintSystem}; use halo2_middleware::ff::{FromUniformBytes, WithSmallOrderMulGroup}; use halo2_middleware::zal::{ impls::{PlonkEngine, PlonkEngineConfig}, @@ -37,9 +37,35 @@ pub fn create_proof_with_engine< where Scheme::Scalar: WithSmallOrderMulGroup<3> + FromUniformBytes<64>, { - create_proof_custom_with_engine::( - engine, params, pk, true, circuits, instances, rng, transcript, - ) + if circuits.len() != instances.len() { + return Err(Error::Backend(ErrorBack::InvalidInstances)); + } + + let mut cs = ConstraintSystem::default(); + #[cfg(feature = "circuit-params")] + let config = ConcreteCircuit::configure_with_params(&mut cs, circuits[0].params()); + #[cfg(not(feature = "circuit-params"))] + let config = ConcreteCircuit::configure(&mut cs); + let cs = cs; + + let mut witness_calcs: Vec<_> = circuits + .iter() + .enumerate() + .map(|(i, circuit)| WitnessCalculator::new(params.k(), circuit, &config, &cs, instances[i])) + .collect(); + let mut prover = Prover::::new_with_engine( + engine, params, pk, instances, rng, transcript, + )?; + let mut challenges = HashMap::new(); + let phases = prover.phases().to_vec(); + for phase in phases.iter() { + let mut witnesses = Vec::with_capacity(circuits.len()); + for witness_calc in witness_calcs.iter_mut() { + witnesses.push(witness_calc.calc(*phase, &challenges)?); + } + challenges = prover.commit_phase(*phase, witnesses).unwrap(); + } + Ok(prover.create_proof()?) } /// This creates a proof for the provided `circuit` when given the public @@ -71,60 +97,6 @@ where ) } -/// This creates a proof for the provided `circuit` when given the public -/// parameters `params` and the proving key [`ProvingKey`] that was -/// generated previously for the same circuit. The provided `instances` -/// are zero-padded internally. -/// In addition, this needs the `compress_selectors` field. -#[allow(clippy::too_many_arguments)] -pub fn create_proof_custom_with_engine< - 'params, - Scheme: CommitmentScheme, - P: commitment::Prover<'params, Scheme>, - E: EncodedChallenge, - R: RngCore, - T: TranscriptWrite, - ConcreteCircuit: Circuit, - M: MsmAccel, ->( - engine: PlonkEngine, - params: &'params Scheme::ParamsProver, - pk: &ProvingKey, - compress_selectors: bool, - circuits: &[ConcreteCircuit], - instances: &[&[&[Scheme::Scalar]]], - rng: R, - transcript: &mut T, -) -> Result<(), Error> -where - Scheme::Scalar: WithSmallOrderMulGroup<3> + FromUniformBytes<64>, -{ - if circuits.len() != instances.len() { - return Err(Error::Backend(ErrorBack::InvalidInstances)); - } - - let (_, config, cs) = - compile_circuit::<_, ConcreteCircuit>(params.k(), &circuits[0], compress_selectors)?; - let mut witness_calcs: Vec<_> = circuits - .iter() - .enumerate() - .map(|(i, circuit)| WitnessCalculator::new(params.k(), circuit, &config, &cs, instances[i])) - .collect(); - let mut prover = Prover::::new_with_engine( - engine, params, pk, instances, rng, transcript, - )?; - let mut challenges = HashMap::new(); - let phases = prover.phases().to_vec(); - for phase in phases.iter() { - let mut witnesses = Vec::with_capacity(circuits.len()); - for witness_calc in witness_calcs.iter_mut() { - witnesses.push(witness_calc.calc(*phase, &challenges)?); - } - challenges = prover.commit_phase(*phase, witnesses).unwrap(); - } - Ok(prover.create_proof()?) -} - #[test] fn test_create_proof() { use crate::{ @@ -243,11 +215,10 @@ fn test_create_proof_custom() { let mut transcript = Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]); let engine = PlonkEngineConfig::build_default(); - create_proof_custom_with_engine::, ProverSHPLONK<_>, _, _, _, _, _>( + create_proof_with_engine::, ProverSHPLONK<_>, _, _, _, _, _>( engine, ¶ms, &pk, - compress_selectors, &[MyCircuit, MyCircuit], &[&[], &[]], OsRng, diff --git a/halo2_proofs/tests/compress_selectors.rs b/halo2_proofs/tests/compress_selectors.rs index bb3992f74e..2c246e972b 100644 --- a/halo2_proofs/tests/compress_selectors.rs +++ b/halo2_proofs/tests/compress_selectors.rs @@ -13,8 +13,8 @@ use halo2_backend::transcript::{ use halo2_middleware::zal::impls::{H2cEngine, PlonkEngineConfig}; use halo2_proofs::arithmetic::Field; use halo2_proofs::plonk::{ - create_proof_custom_with_engine, keygen_pk_custom, keygen_vk_custom, verify_proof, Advice, - Assigned, Circuit, Column, ConstraintSystem, Instance, Selector, + create_proof_with_engine, keygen_pk_custom, keygen_vk_custom, verify_proof, Advice, Assigned, + Circuit, Column, ConstraintSystem, Instance, Selector, }; use halo2_proofs::poly::commitment::ParamsProver; use halo2_proofs::poly::kzg::commitment::{KZGCommitmentScheme, ParamsKZG}; @@ -343,7 +343,6 @@ impl Circuit for MyCircuitCircuit { fn test_mycircuit( vk_keygen_compress_selectors: bool, pk_keygen_compress_selectors: bool, - proofgen_compress_selectors: bool, ) -> Result<(), halo2_proofs::plonk::Error> { let engine = PlonkEngineConfig::new() .set_curve::() @@ -373,19 +372,10 @@ fn test_mycircuit( .collect::>()); let mut transcript = Blake2bWrite::<_, G1Affine, Challenge255<_>>::init(vec![]); - create_proof_custom_with_engine::< - KZGCommitmentScheme, - ProverSHPLONK<'_, Bn256>, - _, - _, - _, - _, - _, - >( + create_proof_with_engine::, ProverSHPLONK<'_, Bn256>, _, _, _, _, _>( engine, ¶ms, &pk, - proofgen_compress_selectors, &[circuit], &[instances_slice], &mut rng, @@ -410,34 +400,24 @@ fn test_mycircuit( #[test] fn test_success() { - // keygen & proof generation both WITH compress - assert!(test_mycircuit(true, true, true).is_ok()); + // vk & pk keygen both WITH compress + assert!(test_mycircuit(true, true).is_ok()); - // keygen & proof generation both WITHOUT compress - assert!(test_mycircuit(false, false, false).is_ok()); + // vk & pk keygen both WITHOUT compress + assert!(test_mycircuit(false, false).is_ok()); } #[should_panic] #[test] fn test_failure_1() { - // vk_keygen WITH compress vs pk_keygen WITHOUT compress - assert!(test_mycircuit(false, true, true).is_err()); + // vk keygen WITH compress + // pk keygen WITHOUT compress + assert!(test_mycircuit(false, true).is_err()); } #[test] fn test_failure_2() { - // vk_keygen WITHOUT compress vs pk_keygen WITH compress - assert!(test_mycircuit(true, false, true).is_err()); -} - -#[test] -fn test_failure_3() { - // keygen WITHOUT compress vs proof_gen WITH compress - assert!(test_mycircuit(false, false, true).is_err()); -} - -#[test] -fn test_failure_4() { - // keygen WITH compress vs proof_gen WITHOUT compress - assert!(test_mycircuit(true, true, false).is_err()); + // vk keygen WITHOUT compress + // pk keygen WITH compress + assert!(test_mycircuit(true, false).is_err()); }