Enum kimchi::snarky::cvar::FieldVar

source ·
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 FieldVars. 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 FieldVars.

§

Scale(F, Box<FieldVar<F>>)

Multiplication of a FieldVar by a constant.

Implementations§

source§

impl<F> FieldVar<F>where F: PrimeField,

source

pub fn constant(c: F) -> Self

Converts a field element c into a FieldVar.

source

pub fn zero() -> Self

Returns the zero constant.

source

pub fn eval(&self, state: &RunState<F>) -> F

Evaluate the field element associated to a variable (used during witness generation)

source

pub fn to_constant_and_terms(&self) -> (Option<F>, Vec<Term<F>>)

source

pub fn scale(&self, scalar: F) -> Self

source

pub fn linear_combination(terms: &[ScaledCVar<F>]) -> Self

source

pub fn sum(vs: &[&Self]) -> Self

source

pub fn mul( &self, other: &Self, label: Option<Cow<'static, str>>, loc: Cow<'static, str>, cs: &mut RunState<F> ) -> SnarkyResult<Self>

source

pub fn equal( &self, state: &mut RunState<F>, loc: Cow<'static, str>, other: &FieldVar<F> ) -> SnarkyResult<Boolean<F>>

source

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.

source§

impl<F> FieldVar<F>where F: PrimeField,

source

pub fn assert_equals( &self, state: &mut RunState<F>, loc: Cow<'static, str>, other: &FieldVar<F> ) -> SnarkyResult<()>

Trait Implementations§

source§

impl<'a, F> Add<&'a FieldVar<F>> for FieldVar<F>where F: PrimeField,

§

type Output = FieldVar<F>

The resulting type after applying the + operator.
source§

fn add(self, other: &Self) -> Self::Output

Performs the + operation. Read more
source§

impl<F> Add<&FieldVar<F>> for &FieldVar<F>where F: PrimeField,

§

type Output = FieldVar<F>

The resulting type after applying the + operator.
source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
source§

impl<F> Add<FieldVar<F>> for &FieldVar<F>where F: PrimeField,

§

type Output = FieldVar<F>

The resulting type after applying the + operator.
source§

fn add(self, other: FieldVar<F>) -> Self::Output

Performs the + operation. Read more
source§

impl<F> Add<FieldVar<F>> for FieldVar<F>where F: PrimeField,

§

type Output = FieldVar<F>

The resulting type after applying the + operator.
source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
source§

impl<F> Clone for FieldVar<F>where F: PrimeField + Clone,

source§

fn clone(&self) -> FieldVar<F>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<F> Debug for FieldVar<F>where F: PrimeField + Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<F> Neg for &FieldVar<F>where F: PrimeField,

§

type Output = FieldVar<F>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<F> Neg for FieldVar<F>where F: PrimeField,

§

type Output = FieldVar<F>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<F> SnarkyCvar for FieldVar<F>where F: PrimeField,

§

type Field = F

source§

fn to_constant_and_terms( &self ) -> (Option<Self::Field>, Vec<(Self::Field, usize)>)

source§

impl<F> SnarkyType<F> for FieldVar<F>where F: PrimeField,

§

type Auxiliary = ()

Some ‘out-of-circuit’ data, which is carried as part of Self. This data isn’t encoded as CVars in the circuit, since the data may be large (e.g. a sparse merkle tree), or may only be used by witness computations / for debugging.
§

type OutOfCircuit = F

The equivalent “out-of-circuit” type. For example, the super::boolean::Boolean snarky type has an out-of-circuit type of bool.
source§

const SIZE_IN_FIELD_ELEMENTS: usize = 1usize

The number of field elements that this type takes.
source§

fn to_cvars(&self) -> (Vec<FieldVar<F>>, Self::Auxiliary)

Returns the circuit variables (and auxiliary data) behind this type.
source§

fn from_cvars_unsafe(cvars: Vec<FieldVar<F>>, _aux: Self::Auxiliary) -> Self

Creates a new instance of this type from the given circuit variables (And some auxiliary data).
source§

fn check( &self, _cs: &mut RunState<F>, _loc: Cow<'static, str> ) -> SnarkyResult<()>

Checks that the circuit variables behind this type are valid. For some definition of valid. For example, a Boolean snarky type would check that the field element representing it is either 0 or 1. The function does this by adding constraints to your constraint system.
source§

fn constraint_system_auxiliary() -> Self::Auxiliary

The “default” value of Self::Auxiliary. This is passed to Self::from_cvars_unsafe when we are not generating a witness, since we have no candidate value to get the auxiliary data from. Note that we use an explicit value here rather than Auxiliary: Default, since the default value for the type may not match the default value we actually want to pass!
source§

fn value_to_field_elements(x: &Self::OutOfCircuit) -> (Vec<F>, Self::Auxiliary)

Converts an out-of-circuit value
source§

fn value_of_field_elements( fields: Vec<F>, _aux: Self::Auxiliary ) -> Self::OutOfCircuit

source§

fn compute<FUNC>( cs: &mut RunState<F>, loc: Cow<'static, str>, to_compute_value: FUNC ) -> SnarkyResult<Self>where FUNC: Fn(&dyn WitnessGeneration<F>) -> Self::OutOfCircuit,

source§

fn read<G>(&self, g: G) -> Self::OutOfCircuitwhere G: WitnessGeneration<F>,

source§

impl<'a, F> Sub<&'a FieldVar<F>> for FieldVar<F>where F: PrimeField,

§

type Output = FieldVar<F>

The resulting type after applying the - operator.
source§

fn sub(self, other: &Self) -> Self::Output

Performs the - operation. Read more
source§

impl<F> Sub<&FieldVar<F>> for &FieldVar<F>where F: PrimeField,

§

type Output = FieldVar<F>

The resulting type after applying the - operator.
source§

fn sub(self, other: Self) -> Self::Output

Performs the - operation. Read more
source§

impl<F> Sub<FieldVar<F>> for &FieldVar<F>where F: PrimeField,

§

type Output = FieldVar<F>

The resulting type after applying the - operator.
source§

fn sub(self, other: FieldVar<F>) -> Self::Output

Performs the - operation. Read more
source§

impl<F> Sub<FieldVar<F>> for FieldVar<F>where F: PrimeField,

§

type Output = FieldVar<F>

The resulting type after applying the - operator.
source§

fn sub(self, other: FieldVar<F>) -> Self::Output

Performs the - operation. Read more

Auto Trait Implementations§

§

impl<F> RefUnwindSafe for FieldVar<F>where F: RefUnwindSafe,

§

impl<F> Send for FieldVar<F>

§

impl<F> Sync for FieldVar<F>

§

impl<F> Unpin for FieldVar<F>where F: Unpin,

§

impl<F> UnwindSafe for FieldVar<F>where F: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V