Struct dusk_plonk::constraint_system::TurboComposer
source · [−]pub struct TurboComposer { /* private fields */ }
Expand description
The TurboComposer is the circuit-builder tool that the dusk-plonk
repository provides so that circuit descriptions can be written, stored and
transformed into a Proof
at some point.
A TurboComposer stores all of the circuit information, this one being all of the witness and circuit descriptors info (values, positions in the circuits, gates and Wires that occupy..), the public inputs, the connection relationships between the witnesses and how they’re represented as Wires (so basically the Permutation argument etc..).
The TurboComposer also grants us a way to introduce our secret witnesses in
into the circuit description as well as the public inputs. We can do this
with methods like TurboComposer::append_witness
.
The TurboComposer also contains as associated functions all the necessary
tools to be able to instrument the circuits that the user needs
through the addition of gates. There are functions that may add a single
gate to the circuit as for example TurboComposer::gate_add
and others
that can add several gates to the circuit description such as
TurboComposer::component_select
.
Each gate or group of gates adds a specific functionality or operation to the circuit description, and so, that’s why we can understand the TurboComposer as a builder.
Implementations
sourceimpl TurboComposer
impl TurboComposer
sourcepub const fn constant_zero() -> Witness
pub const fn constant_zero() -> Witness
Returns a Witness
representation of zero.
Every TurboComposer
is initialized with a circuit description
containing a representation of zero. This function will return the
index of that representation.
sourcepub unsafe fn evaluate_witness(&self, witness: &Witness) -> &BlsScalar
pub unsafe fn evaluate_witness(&self, witness: &Witness) -> &BlsScalar
Evaluate the runtime value of a witness
Safety
Witness evaluation inside a gadget isn’t expected and could produce an unsound circuit (different circuit representation for the same code).
Calling this function performs operations outside the circuit.
sourcepub fn public_input_indexes(&self) -> Vec<usize>
pub fn public_input_indexes(&self) -> Vec<usize>
Returns the positions that the Public Inputs occupy in this Composer instance.
sourceimpl TurboComposer
impl TurboComposer
sourcepub fn append_constant<T: Into<BlsScalar>>(&mut self, value: T) -> Witness
pub fn append_constant<T: Into<BlsScalar>>(&mut self, value: T) -> Witness
Constrain a scalar into the circuit description and return an allocated
Witness
with its value
sourcepub fn append_witness<T: Into<BlsScalar>>(&mut self, scalar: T) -> Witness
pub fn append_witness<T: Into<BlsScalar>>(&mut self, scalar: T) -> Witness
Allocate a witness value into the composer and return its index.
sourcepub fn append_public_witness<T: Into<BlsScalar>>(&mut self, scalar: T) -> Witness
pub fn append_public_witness<T: Into<BlsScalar>>(&mut self, scalar: T) -> Witness
Allocate a witness value into the composer and return its index.
Create a public input with the scalar
sourcepub fn append_gate(&mut self, s: Constraint)
pub fn append_gate(&mut self, s: Constraint)
Adds a width-4 poly gate.
The final constraint added will enforce the following:
q_m · a · b + q_l · a + q_r · b + q_o · o + q_4 · d + q_c + PI = 0
.
sourcepub fn assert_equal_constant<C: Into<BlsScalar>>(
&mut self,
a: Witness,
constant: C,
pi: Option<BlsScalar>
)
pub fn assert_equal_constant<C: Into<BlsScalar>>(
&mut self,
a: Witness,
constant: C,
pi: Option<BlsScalar>
)
Constrain a
to be equal to constant + pi
.
constant
will be defined as part of the public circuit description.
sourcepub fn assert_equal(&mut self, a: Witness, b: Witness)
pub fn assert_equal(&mut self, a: Witness, b: Witness)
Asserts a == b
by appending a gate
sourcepub fn component_select(&mut self, bit: Witness, a: Witness, b: Witness) -> Witness
pub fn component_select(&mut self, bit: Witness, a: Witness, b: Witness) -> Witness
Conditionally selects a Witness
based on an input bit.
bit == 1 => a, bit == 0 => b,
bit
is expected to be constrained by
TurboComposer::component_boolean
sourcepub fn component_select_zero(&mut self, bit: Witness, value: Witness) -> Witness
pub fn component_select_zero(&mut self, bit: Witness, value: Witness) -> Witness
Conditionally selects a Witness
based on an input bit.
bit == 1 => value, bit == 0 => 0,
bit
is expected to be constrained by
TurboComposer::component_boolean
sourcepub fn component_select_one(&mut self, bit: Witness, value: Witness) -> Witness
pub fn component_select_one(&mut self, bit: Witness, value: Witness) -> Witness
Conditionally selects a Witness
based on an input bit.
bit == 1 => value, bit == 0 => 1,
bit
is expected to be constrained by
TurboComposer::component_boolean
sourcepub fn component_decomposition<const N: usize>(
&mut self,
scalar: Witness
) -> [Witness; N]
pub fn component_decomposition<const N: usize>(
&mut self,
scalar: Witness
) -> [Witness; N]
Decomposes scalar
into an array truncated to N
bits (max 256).
Asserts the reconstruction of the bits to be equal to scalar
.
Consume 2 · N + 1
gates
sourcepub fn append_dummy_gates(&mut self)
pub fn append_dummy_gates(&mut self)
This function is used to add a blinding factor to the witness polynomials. It essentially adds two dummy gates to the circuit description which are guaranteed to always satisfy the gate equation.
sourceimpl TurboComposer
impl TurboComposer
sourcepub fn component_add_point(
&mut self,
a: WitnessPoint,
b: WitnessPoint
) -> WitnessPoint
pub fn component_add_point(
&mut self,
a: WitnessPoint,
b: WitnessPoint
) -> WitnessPoint
Adds two curve points by consuming 2 gates.
sourceimpl TurboComposer
impl TurboComposer
sourcepub fn component_mul_generator<P: Into<JubJubExtended>>(
&mut self,
jubjub: Witness,
generator: P
) -> WitnessPoint
pub fn component_mul_generator<P: Into<JubJubExtended>>(
&mut self,
jubjub: Witness,
generator: P
) -> WitnessPoint
Evaluate jubjub · Generator
as a WitnessPoint
generator
will be appended to the circuit description as constant
sourceimpl TurboComposer
impl TurboComposer
sourcepub fn component_mul_point(
&mut self,
jubjub: Witness,
point: WitnessPoint
) -> WitnessPoint
pub fn component_mul_point(
&mut self,
jubjub: Witness,
point: WitnessPoint
) -> WitnessPoint
Evaluate jubjub · point
as a WitnessPoint
sourceimpl TurboComposer
impl TurboComposer
sourcepub fn append_point<P: Into<JubJubAffine>>(&mut self, affine: P) -> WitnessPoint
pub fn append_point<P: Into<JubJubAffine>>(&mut self, affine: P) -> WitnessPoint
Appends a point in affine form as WitnessPoint
sourcepub fn append_public_point<P: Into<JubJubAffine>>(
&mut self,
affine: P
) -> WitnessPoint
pub fn append_public_point<P: Into<JubJubAffine>>(
&mut self,
affine: P
) -> WitnessPoint
Appends a point in affine form as WitnessPoint
Creates two public inputs as (x, y)
sourcepub fn append_constant_point<P: Into<JubJubAffine>>(
&mut self,
affine: P
) -> WitnessPoint
pub fn append_constant_point<P: Into<JubJubAffine>>(
&mut self,
affine: P
) -> WitnessPoint
Constrain a point into the circuit description and return an allocated
WitnessPoint
with its coordinates
sourcepub fn append_constant_identity(&mut self) -> WitnessPoint
pub fn append_constant_identity(&mut self) -> WitnessPoint
Create an identity WitnessPoint
constrained by the circuit
description
sourcepub fn assert_equal_public_point<P: Into<JubJubAffine>>(
&mut self,
point: WitnessPoint,
public: P
)
pub fn assert_equal_public_point<P: Into<JubJubAffine>>(
&mut self,
point: WitnessPoint,
public: P
)
Asserts point == public
.
Will add public
affine coordinates (x,y)
as public inputs
sourcepub fn assert_equal_point(&mut self, a: WitnessPoint, b: WitnessPoint)
pub fn assert_equal_point(&mut self, a: WitnessPoint, b: WitnessPoint)
Asserts a == b
by appending two gates
sourcepub fn component_select_point(
&mut self,
a: WitnessPoint,
b: WitnessPoint,
bit: Witness
) -> WitnessPoint
pub fn component_select_point(
&mut self,
a: WitnessPoint,
b: WitnessPoint,
bit: Witness
) -> WitnessPoint
Conditionally selects a WitnessPoint
based on an input bit.
bit == 1 => a, bit == 0 => b,
bit
is expected to be constrained by
TurboComposer::component_boolean
sourcepub fn component_select_identity(
&mut self,
bit: Witness,
a: WitnessPoint
) -> WitnessPoint
pub fn component_select_identity(
&mut self,
bit: Witness,
a: WitnessPoint
) -> WitnessPoint
Conditionally selects identity as WitnessPoint
based on an input
bit.
bit == 1 => a, bit == 0 => identity,
bit
is expected to be constrained by
TurboComposer::component_boolean
sourceimpl TurboComposer
impl TurboComposer
sourceimpl TurboComposer
impl TurboComposer
sourcepub fn component_range(&mut self, witness: Witness, num_bits: usize)
pub fn component_range(&mut self, witness: Witness, num_bits: usize)
Adds a range-constraint gate that checks and constrains a
Witness
to be inside of the range [0,num_bits].
This function adds num_bits/4
gates to the circuit description in
order to add the range constraint.
Panics
This function will panic if the num_bits specified is not even, ie.
num_bits % 2 != 0
.
sourceimpl TurboComposer
impl TurboComposer
sourcepub fn gate_add(&mut self, s: Constraint) -> Witness
pub fn gate_add(&mut self, s: Constraint) -> Witness
Evaluate and return o
by appending a new constraint into the circuit.
Set q_o = (-1)
and override the output of the constraint with:
o := q_l · a + q_r · b + q_4 · d + q_c + PI
sourcepub fn gate_mul(&mut self, s: Constraint) -> Witness
pub fn gate_mul(&mut self, s: Constraint) -> Witness
Evaluate and return o
by appending a new constraint into the circuit.
Set q_o = (-1)
and override the output of the constraint with:
o := q_m · a · b + q_4 · d + q_c + PI
sourceimpl TurboComposer
impl TurboComposer
sourcepub fn component_boolean(&mut self, a: Witness)
pub fn component_boolean(&mut self, a: Witness)
Adds a boolean constraint (also known as binary constraint) where the
gate eq. will enforce that the Witness
received is either 0
or 1
by adding a constraint in the circuit.
Note that using this constraint with whatever Witness
that
is not representing a value equalling 0 or 1, will always force the
equation to fail.
Trait Implementations
sourceimpl Debug for TurboComposer
impl Debug for TurboComposer
Auto Trait Implementations
impl RefUnwindSafe for TurboComposer
impl Send for TurboComposer
impl Sync for TurboComposer
impl Unpin for TurboComposer
impl UnwindSafe for TurboComposer
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more