Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve InvalidCircuitSize error message #793

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)));
}
Loading