pub enum FieldVar<F>where
F: PrimeField,{
Constant(F),
Var(usize),
Add(Box<FieldVar<F>>, Box<FieldVar<F>>),
Scale(F, Box<FieldVar<F>>),
}
Expand description
A circuit variable represents a field element in the circuit.
Note that a FieldVar
currently represents a small AST that can hide nested additions and multiplications of FieldVar
s.
The reason behind this decision is that converting additions and multiplications directly into constraints is not optimal.
So we most often wait until some additions have been accumulated before reducing them down to constraints.
Note: this design decision also leads to optimization issues,
when we end up cloning and then reducing the same mini-ASTs multiple times.
To force reduction of a FieldVar
(most often before cloning),
the Self::seal function can be used.
Variants§
Constant(F)
A constant (a field element).
Var(usize)
A variable, tracked by a counter.
Add(Box<FieldVar<F>>, Box<FieldVar<F>>)
Addition of two FieldVar
s.
Scale(F, Box<FieldVar<F>>)
Multiplication of a FieldVar
by a constant.
Implementations§
source§impl<F> FieldVar<F>where
F: PrimeField,
impl<F> FieldVar<F>where F: PrimeField,
sourcepub fn eval(&self, state: &RunState<F>) -> F
pub fn eval(&self, state: &RunState<F>) -> F
Evaluate the field element associated to a variable (used during witness generation)
pub fn to_constant_and_terms(&self) -> (Option<F>, Vec<Term<F>>)
pub fn scale(&self, scalar: F) -> Self
pub fn linear_combination(terms: &[ScaledCVar<F>]) -> Self
pub fn sum(vs: &[&Self]) -> Self
pub fn mul( &self, other: &Self, label: Option<Cow<'static, str>>, loc: Cow<'static, str>, cs: &mut RunState<F> ) -> SnarkyResult<Self>
pub fn equal( &self, state: &mut RunState<F>, loc: Cow<'static, str>, other: &FieldVar<F> ) -> SnarkyResult<Boolean<F>>
sourcepub fn seal(
&self,
state: &mut RunState<F>,
loc: Cow<'static, str>
) -> SnarkyResult<Self>
pub fn seal( &self, state: &mut RunState<F>, loc: Cow<'static, str> ) -> SnarkyResult<Self>
Seals the value of a variable.
As a FieldVar
can represent an AST,
it might not be a good idea to clone it and reuse it in several places.
This is because the exact same reduction that will eventually happen on each clone
will end up creating the same set of constraints multiple times in the circuit.
It is useful to call on a variable that represents a long computation that hasn’t been constrained yet (e.g. by an assert call, or a call to a custom gate), before using it further in the circuit.