diff --git a/CHANGELOG.md b/CHANGELOG.md index 70aab304..776f0dde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix inconsistency in gate ordering of arithmetic verifier key [#797] - Fix leading coefficients might be zero [#796] +### Changed + +- Improve InvalidCircuitSize error [#792] + ## [0.18.0] - 2023-12-13 ### Changed @@ -539,6 +543,7 @@ is necessary since `rkyv/validation` was required as a bound. [#797]: https://github.com/dusk-network/plonk/issues/797 [#796]: https://github.com/dusk-network/plonk/issues/796 +[#792]: https://github.com/dusk-network/plonk/issues/792 [#784]: https://github.com/dusk-network/plonk/issues/784 [#773]: https://github.com/dusk-network/plonk/issues/773 [#774]: https://github.com/dusk-network/plonk/issues/774 diff --git a/src/composer.rs b/src/composer.rs index 3e9805eb..36ddf429 100644 --- a/src/composer.rs +++ b/src/composer.rs @@ -81,7 +81,8 @@ pub trait Composer: Sized + Index { /// PLONK runtime controller fn runtime(&mut self) -> &mut Runtime; - /// Initialize the constraint system with dummy gates + /// Initialize the constraint system with the constants for 0 and 1 and + /// append two dummy gates fn initialized() -> Self { #[allow(deprecated)] let mut slf = Self::uninitialized(); @@ -1051,7 +1052,7 @@ pub trait Composer: Sized + Index { o } - /// Prove a circuit with a builder initialized with `constraints` capacity. + /// Prove a circuit with a builder initialized with dummy gates fn prove(constraints: usize, circuit: &C) -> Result where C: Circuit, @@ -1060,9 +1061,14 @@ pub trait Composer: Sized + Index { circuit.circuit(&mut builder)?; - // assert that the circuit has the expected amount of constraints - if builder.constraints() != constraints { - return Err(Error::InvalidCircuitSize); + // assert that the circuit has the same amount of constraints as the + // circuit description + let description_size = builder.constraints(); + if description_size != constraints { + return Err(Error::InvalidCircuitSize( + description_size, + constraints, + )); } builder.runtime().event(RuntimeEvent::ProofFinished); diff --git a/src/error.rs b/src/error.rs index e669590e..d3b0d675 100644 --- a/src/error.rs +++ b/src/error.rs @@ -35,9 +35,10 @@ pub enum Error { /// This error occurs when the Prover structure already contains a /// preprocessed circuit inside, but you call preprocess again. CircuitAlreadyPreprocessed, - /// This error occurs when the circuit for the proof has a different size - /// than the prover circuit description - InvalidCircuitSize, + /// This error occurs when the circuit description has a different amount + /// of gates than the circuit for the proof creation. + /// The order: (description_size, circuit_size) + InvalidCircuitSize(usize, usize), // Preprocessing errors /// This error occurs when an error triggers during the preprocessing @@ -127,8 +128,8 @@ impl std::fmt::Display for Error { Self::CircuitAlreadyPreprocessed => { write!(f, "circuit has already been preprocessed") } - Self::InvalidCircuitSize => { - write!(f, "circuit size doesn't match with circuit description") + Self::InvalidCircuitSize(description_size, circuit_size) => { + write!(f, "circuit description has a different amount of gates than the circuit for the proof creation: description size = {description_size}, circuit size = {circuit_size}") } Self::DegreeIsZero => { write!(f, "cannot create PublicParameters with max degree 0") diff --git a/tests/size.rs b/tests/error_size.rs similarity index 76% rename from tests/size.rs rename to tests/error_size.rs index e8c9c3f7..558e7bab 100644 --- a/tests/size.rs +++ b/tests/error_size.rs @@ -47,18 +47,20 @@ fn size() { let pp = PublicParameters::setup(CAPACITY, rng) .expect("Creation of public parameter shouldn't fail"); - // compiling the default version of TestSize, which only one gate: sum = 0 + // compiling the default version of TestSize circuit, with only one gate in + // addition to the 4 dummy gates let (prover, _verifier) = Compiler::compile::(&pp, LABEL) .expect("It should be possible to compile the prover and verifier"); // Create circuit with more gates - let pi: Vec = [BlsScalar::one(); 5].into(); - let sum = pi.iter().sum(); - let circuit = TestSize::new(pi, sum); + let witnesses: Vec = [BlsScalar::one(); 4].into(); + let sum = witnesses.iter().sum(); + let circuit = TestSize::new(witnesses, sum); let result = prover.prove(rng, &circuit); - assert_eq!( - result, - Err(Error::InvalidCircuitSize), - "proof creation for different sized circuit shouldn't be possible" - ); + let empty_circuit_size = Builder::initialized().constraints(); + assert!(result.is_err_and(|e| e + == Error::InvalidCircuitSize( + empty_circuit_size + 5, + empty_circuit_size + 1 + ))); }