Skip to content

Commit

Permalink
Merge pull request #793 from dusk-network/mocello/size_error
Browse files Browse the repository at this point in the history
Improve InvalidCircuitSize error message
  • Loading branch information
moCello authored Dec 20, 2023
2 parents 1f655c2 + a7270b0 commit 97240ce
Show file tree
Hide file tree
Showing 4 changed files with 33 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 constants 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
20 changes: 11 additions & 9 deletions tests/size.rs → tests/error_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<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"
);
let empty_circuit_size = Builder::initialized().constraints();
assert!(result.is_err_and(|e| e
== Error::InvalidCircuitSize(
empty_circuit_size + 5,
empty_circuit_size + 1
)));
}

0 comments on commit 97240ce

Please sign in to comment.