Skip to content

Commit

Permalink
fix!: remove the "create_proof_custom_with_engine"
Browse files Browse the repository at this point in the history
  • Loading branch information
guorong009 committed May 9, 2024
1 parent b8aa324 commit 3190c57
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 95 deletions.
1 change: 1 addition & 0 deletions halo2_frontend/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub fn compile_circuit<F: Field, ConcreteCircuit: Circuit<F>>(
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());
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
93 changes: 32 additions & 61 deletions halo2_proofs/src/plonk/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -37,9 +37,35 @@ pub fn create_proof_with_engine<
where
Scheme::Scalar: WithSmallOrderMulGroup<3> + FromUniformBytes<64>,
{
create_proof_custom_with_engine::<Scheme, P, E, R, T, ConcreteCircuit, M>(
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::<Scheme, P, _, _, _, _>::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
Expand Down Expand Up @@ -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<Scheme::Curve>,
R: RngCore,
T: TranscriptWrite<Scheme::Curve, E>,
ConcreteCircuit: Circuit<Scheme::Scalar>,
M: MsmAccel<Scheme::Curve>,
>(
engine: PlonkEngine<Scheme::Curve, M>,
params: &'params Scheme::ParamsProver,
pk: &ProvingKey<Scheme::Curve>,
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::<Scheme, P, _, _, _, _>::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::{
Expand Down Expand Up @@ -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::<KZGCommitmentScheme<_>, ProverSHPLONK<_>, _, _, _, _, _>(
create_proof_with_engine::<KZGCommitmentScheme<_>, ProverSHPLONK<_>, _, _, _, _, _>(
engine,
&params,
&pk,
compress_selectors,
&[MyCircuit, MyCircuit],
&[&[], &[]],
OsRng,
Expand Down
46 changes: 13 additions & 33 deletions halo2_proofs/tests/compress_selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -343,7 +343,6 @@ impl<F: Field> Circuit<F> for MyCircuitCircuit<F> {
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::<G1Affine>()
Expand Down Expand Up @@ -373,19 +372,10 @@ fn test_mycircuit(
.collect::<Vec<_>>());

let mut transcript = Blake2bWrite::<_, G1Affine, Challenge255<_>>::init(vec![]);
create_proof_custom_with_engine::<
KZGCommitmentScheme<Bn256>,
ProverSHPLONK<'_, Bn256>,
_,
_,
_,
_,
_,
>(
create_proof_with_engine::<KZGCommitmentScheme<Bn256>, ProverSHPLONK<'_, Bn256>, _, _, _, _, _>(
engine,
&params,
&pk,
proofgen_compress_selectors,
&[circuit],
&[instances_slice],
&mut rng,
Expand All @@ -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());
}

0 comments on commit 3190c57

Please sign in to comment.