pub struct Var<'ctx, F> { /* private fields */ }Expand description
A value in a circuit being built.
Vars are created with Self::witness and Self::constant, combined
with the usual arithmetic operators, and constrained with
Self::assert_eq. Vars implement the algebra traits from
commonware_math, so code written against Ring or Field runs
unchanged over circuit values.
Values produced by Additive::zero and Ring::one are “native”:
they live outside the circuit until combined with a circuit value. This
is visible in two places: equality compares what vars refer to, not what
they evaluate to (a native var is never equal to a circuit-backed var,
even when their values agree), and Self::assert_eq panics on two
unequal native vars. Generic code that branches on equality may
therefore behave differently over vars than over plain values.
Implementations§
Source§impl<'ctx, F> Var<'ctx, F>
impl<'ctx, F> Var<'ctx, F>
Sourcepub fn witness(
ctx: Context<'ctx, F>,
init: impl for<'a> FnOnce(Values<'a, F>) -> F,
) -> Self
pub fn witness( ctx: Context<'ctx, F>, init: impl for<'a> FnOnce(Values<'a, F>) -> F, ) -> Self
Sourcepub const fn native(value: F) -> Self
pub const fn native(value: F) -> Self
Create a “native” var holding a value outside any circuit.
Like the vars produced by Additive::zero and Ring::one, a
native var lives outside the circuit until it is combined with a
circuit-backed var, at which point it is folded in as a constant. This
is convenient for fixed constants (such as curve parameters) that are
the same in every circuit and so do not need a Context to create.
Trait Implementations§
Source§impl<'ctx, F: Additive> AddAssign<&Var<'ctx, F>> for Var<'ctx, F>
impl<'ctx, F: Additive> AddAssign<&Var<'ctx, F>> for Var<'ctx, F>
Source§fn add_assign(&mut self, rhs: &Self)
fn add_assign(&mut self, rhs: &Self)
+= operation. Read moreSource§impl<'ctx, F: Field> Div<&Var<'ctx, F>> for Var<'ctx, F>
Division by rhs, computed as a single multiplication constraint.
impl<'ctx, F: Field> Div<&Var<'ctx, F>> for Var<'ctx, F>
Division by rhs, computed as a single multiplication constraint.
Rather than inverting rhs and multiplying (which costs two
multiplications), the prover supplies the quotient q = self / rhs as a
witness and the circuit constrains q * rhs == self.
§Caveats
Like Field::inv on a circuit-backed var, this deviates from the
inv(0) = 0 field contract: dividing by a circuit-backed rhs of zero
adds an unsatisfiable constraint when self != 0. Worse, 0 / 0
constrains q * 0 == 0, which holds for any q, leaving the quotient
unconstrained. Only use / where rhs is known to be nonzero.
Source§impl<'ctx, F: Field> DivAssign<&Var<'ctx, F>> for Var<'ctx, F>
impl<'ctx, F: Field> DivAssign<&Var<'ctx, F>> for Var<'ctx, F>
Source§fn div_assign(&mut self, rhs: &Self)
fn div_assign(&mut self, rhs: &Self)
/= operation. Read moreimpl<F: Eq> Eq for Var<'_, F>
Source§impl<'ctx, F: Field> Field for Var<'ctx, F>
Unlike the Field::inv contract, inverting a circuit-backed zero does
not produce zero: it adds an unsatisfiable constraint to the circuit.
impl<'ctx, F: Field> Field for Var<'ctx, F>
Unlike the Field::inv contract, inverting a circuit-backed zero does
not produce zero: it adds an unsatisfiable constraint to the circuit.
Source§impl<'ctx, F: Multiplicative> MulAssign<&Var<'ctx, F>> for Var<'ctx, F>
impl<'ctx, F: Multiplicative> MulAssign<&Var<'ctx, F>> for Var<'ctx, F>
Source§fn mul_assign(&mut self, rhs: &Self)
fn mul_assign(&mut self, rhs: &Self)
*= operation. Read moreSource§impl<'ctx, F: Multiplicative> Multiplicative for Var<'ctx, F>
impl<'ctx, F: Multiplicative> Multiplicative for Var<'ctx, F>
impl<'ctx, F: Object> Object for Var<'ctx, F>
Auto Trait Implementations§
impl<'ctx, F> !RefUnwindSafe for Var<'ctx, F>
impl<'ctx, F> !UnwindSafe for Var<'ctx, F>
impl<'ctx, F> Freeze for Var<'ctx, F>where
F: Freeze,
impl<'ctx, F> Send for Var<'ctx, F>where
F: Send,
impl<'ctx, F> Sync for Var<'ctx, F>
impl<'ctx, F> Unpin for Var<'ctx, F>where
F: Unpin,
impl<'ctx, F> UnsafeUnpin for Var<'ctx, F>where
F: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
impl<T, Rhs, Output> GroupOps<Rhs, Output> for T
impl<T, Rhs, Output> GroupOpsOwned<Rhs, Output> for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more