Skip to content

Commit

Permalink
Improve InvalidCircuitSize error message
Browse files Browse the repository at this point in the history
Resolves #792
  • Loading branch information
moCello committed Dec 20, 2023
1 parent 1f655c2 commit 48f66ce
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -539,6 +543,7 @@ is necessary since `rkyv/validation` was required as a bound.
<!-- ISSUES -->
[#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
Expand Down
16 changes: 11 additions & 5 deletions src/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ pub trait Composer: Sized + Index<Witness, Output = BlsScalar> {
/// PLONK runtime controller
fn runtime(&mut self) -> &mut Runtime;

/// Initialize the constraint system with dummy gates
/// Initialize the constraint system with the witnesses for 0 and 1 and
/// append two dummy gates
fn initialized() -> Self {
#[allow(deprecated)]
let mut slf = Self::uninitialized();
Expand Down Expand Up @@ -1051,7 +1052,7 @@ pub trait Composer: Sized + Index<Witness, Output = BlsScalar> {
o
}

/// Prove a circuit with a builder initialized with `constraints` capacity.
/// Prove a circuit with a builder initialized with dummy gates
fn prove<C>(constraints: usize, circuit: &C) -> Result<Self, Error>
where
C: Circuit,
Expand All @@ -1060,9 +1061,14 @@ pub trait Composer: Sized + Index<Witness, Output = BlsScalar> {

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);
Expand Down
11 changes: 6 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
15 changes: 6 additions & 9 deletions tests/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,15 @@ 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::<TestSize>(&pp, LABEL)
.expect("It should be possible to compile the prover and verifier");

// Create circuit with more gates
let pi: Vec<BlsScalar> = [BlsScalar::one(); 5].into();
let sum = pi.iter().sum();
let circuit = TestSize::new(pi, sum);
let witnesses: Vec<BlsScalar> = [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"
);
assert!(result.is_err_and(|e| e == Error::InvalidCircuitSize(9, 5)));
}

0 comments on commit 48f66ce

Please sign in to comment.