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 12, 2023
1 parent e18f02b commit 7657b29
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Improve InvalidCircuitSize error [#792]

## [0.17.0] - 2023-11-1

### Added
Expand Down Expand Up @@ -525,6 +529,7 @@ is necessary since `rkyv/validation` was required as a bound.
- Proof system module.

<!-- ISSUES -->
[#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
13 changes: 9 additions & 4 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 @@ -1061,8 +1062,12 @@ 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);
let description_size = builder.constraints();
if description_size != constraints {
return Err(Error::InvalidCircuitSize(
description_size,
constraints,
));
}

builder.runtime().event(RuntimeEvent::ProofFinished);
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub enum Error {
CircuitAlreadyPreprocessed,
/// This error occurs when the circuit for the proof has a different size
/// than the prover circuit description
InvalidCircuitSize,
InvalidCircuitSize(usize, usize),

// Preprocessing errors
/// This error occurs when an error triggers during the preprocessing
Expand Down Expand Up @@ -127,8 +127,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 size {circuit_size} doesn't match with size of circuit description {description_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 7657b29

Please sign in to comment.