Skip to content

Commit

Permalink
Add Selector for gate API polynomials
Browse files Browse the repository at this point in the history
`Selector` will introduce a fail-safe type that can be used to build
custom constraints.

It will replace any composer argument that takes the circuit
description/selector polynomials.
  • Loading branch information
vlopes11 committed Oct 7, 2021
1 parent 481b119 commit 30a49d5
Show file tree
Hide file tree
Showing 12 changed files with 421 additions and 529 deletions.
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 selector = Selector::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),
selector,
);

// 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 selector = Selector::new()
.multiplication(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),
selector,
);

let e = composer.append_witness(self.e);
Expand Down
53 changes: 24 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 selector = Selector::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),
/// selector,
/// );
///
/// // 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 selector = Selector::new()
/// .multiplication(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),
/// selector,
/// );
///
/// 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::{Selector, TurboComposer};
use crate::proof_system::ProverKey;

type Result<T> = std::result::Result<T, TestError>;
Expand Down Expand Up @@ -406,36 +407,30 @@ 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 selector = Selector::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),
selector,
);

// 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 selector =
Selector::new().multiplication(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),
selector,
);

let e = composer.append_witness(self.e);
Expand Down
2 changes: 2 additions & 0 deletions src/constraint_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(crate) mod composer;
pub(crate) mod ecc;
pub(crate) mod logic;
pub(crate) mod range;
pub(crate) mod selector;
pub(crate) mod witness;

pub(crate) use witness::WireData;
Expand All @@ -26,4 +27,5 @@ pub(crate) mod helper;

pub use composer::TurboComposer;
pub use ecc::WitnessPoint;
pub use selector::{Selector, SelectorRepr};
pub use witness::Witness;
Loading

0 comments on commit 30a49d5

Please sign in to comment.