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

Add Constraint and Selector types #606

Merged
merged 1 commit into from
Oct 8, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add back benchmarks to the crate. [#555](https://github.com/dusk-network/plonk/issue/555)
- Add `Witness` by removing `AllocatedScalar`. [#588](https://github.com/dusk-network/plonk/issue/588)
- Add `Constraint` for circuit description. [#608](https://github.com/dusk-network/plonk/issue/608)

### Changed

Expand Down
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,37 @@ impl Circuit for TestCircuit {
) -> Result<(), Error> {
let a = composer.append_witness(self.a);
let b = composer.append_witness(self.b);

// Make first constraint a + b = c
let constraint = Constraint::new()
.left(1)
.right(1)
.public(-self.c);

composer.append_gate(
a,
b,
composer.constant_zero(),
composer.constant_zero(),
BlsScalar::zero(),
BlsScalar::one(),
BlsScalar::one(),
BlsScalar::zero(),
BlsScalar::zero(),
BlsScalar::zero(),
Some(-self.c),
constraint,
);

// Check that a and b are in range
composer.component_range(a, 1 << 6);
composer.component_range(b, 1 << 5);

// Make second constraint a * b = d
let constraint = Constraint::new()
.mul(1)
.output(1)
.public(-self.d);

composer.append_gate(
a,
b,
composer.constant_zero(),
composer.constant_zero(),
BlsScalar::one(),
BlsScalar::zero(),
BlsScalar::zero(),
BlsScalar::one(),
BlsScalar::zero(),
BlsScalar::zero(),
Some(-self.d),
constraint,
);

let e = composer.append_witness(self.e);
Expand Down
52 changes: 23 additions & 29 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,36 +155,37 @@ impl VerifierData {
/// let zero = composer.constant_zero();
/// let a = composer.append_witness(self.a);
/// let b = composer.append_witness(self.b);
///
/// // Make first constraint a + b = c
/// let constraint = Constraint::new()
/// .left(1)
/// .right(1)
/// .public(-self.c);
///
/// composer.append_gate(
/// a,
/// b,
/// zero,
/// zero,
/// BlsScalar::zero(),
/// BlsScalar::one(),
/// BlsScalar::one(),
/// BlsScalar::zero(),
/// BlsScalar::zero(),
/// BlsScalar::zero(),
/// Some(-self.c),
/// constraint,
/// );
///
/// // Check that a and b are in range
/// composer.component_range(a, 1 << 6);
/// composer.component_range(b, 1 << 5);
///
/// // Make second constraint a * b = d
/// let constraint = Constraint::new()
/// .mul(1)
/// .output(1)
/// .public(-self.d);
///
/// composer.append_gate(
/// a,
/// b,
/// zero,
/// zero,
/// BlsScalar::one(),
/// BlsScalar::zero(),
/// BlsScalar::zero(),
/// BlsScalar::one(),
/// BlsScalar::zero(),
/// BlsScalar::zero(),
/// Some(-self.d),
/// constraint,
/// );
///
/// let e = composer.append_witness(self.e);
Expand Down Expand Up @@ -359,7 +360,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::constraint_system::TurboComposer;
use crate::constraint_system::{Constraint, TurboComposer};
use crate::proof_system::ProverKey;

type Result<T> = std::result::Result<T, TestError>;
Expand Down Expand Up @@ -406,36 +407,29 @@ mod tests {
) -> std::result::Result<(), Error> {
let a = composer.append_witness(self.a);
let b = composer.append_witness(self.b);

// Make first constraint a + b = c
let constraint = Constraint::new().left(1).right(1).public(-self.c);
composer.append_gate(
a,
b,
composer.constant_zero(),
composer.constant_zero(),
BlsScalar::zero(),
BlsScalar::one(),
BlsScalar::one(),
BlsScalar::zero(),
BlsScalar::zero(),
BlsScalar::zero(),
Some(-self.c),
constraint,
);

// Check that a and b are in range
composer.component_range(a, 1 << 6);
composer.component_range(b, 1 << 5);

// Make second constraint a * b = d
let constraint = Constraint::new().mul(1).output(1).public(-self.d);
composer.append_gate(
a,
b,
composer.constant_zero(),
composer.constant_zero(),
BlsScalar::one(),
BlsScalar::zero(),
BlsScalar::zero(),
BlsScalar::one(),
BlsScalar::zero(),
BlsScalar::zero(),
Some(-self.d),
constraint,
);

let e = composer.append_witness(self.e);
Expand Down
3 changes: 3 additions & 0 deletions src/constraint_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
//! build, preprocess circuits.

pub(crate) mod composer;
pub(crate) mod constraint;
pub(crate) mod ecc;
pub(crate) mod logic;
pub(crate) mod range;
pub(crate) mod witness;

pub(crate) use constraint::Selector;
pub(crate) use witness::WireData;

mod arithmetic;
Expand All @@ -25,5 +27,6 @@ mod boolean;
pub(crate) mod helper;

pub use composer::TurboComposer;
pub use constraint::Constraint;
pub use ecc::WitnessPoint;
pub use witness::Witness;
Loading