Skip to main content

Var

Struct Var 

Source
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>

Source

pub fn witness( ctx: Context<'ctx, F>, init: impl for<'a> FnOnce(Values<'a, F>) -> F, ) -> Self

Allocate a fresh witness.

In prover mode, init receives the values assigned so far, and must return the value of this witness. In verifier mode, init does not run.

init must not use the Context: doing so deadlocks. See Values.

Source

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.

Source

pub fn constant(ctx: Context<'ctx, F>, value: F) -> Self

Create a var with a fixed, public value.

Source

pub fn assert_eq(&self, other: &Self)
where F: Clone + PartialEq,

Assert that this var equals other.

The constraint must hold for the circuit to be satisfied.

§Panics

Panics if both vars are native and their values differ, since there is no circuit to record the failure in.

Source§

impl<'ctx, F: Clone> Var<'ctx, F>

Source

pub fn value(&self, values: Values<'_, F>) -> F

The value of this var, under a prover-mode assignment.

Trait Implementations§

Source§

impl<'ctx, F: Additive> Add<&Var<'ctx, F>> for Var<'ctx, F>

Source§

type Output = Var<'ctx, F>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Self) -> Self

Performs the + operation. Read more
Source§

impl<'ctx, F: Additive> AddAssign<&Var<'ctx, F>> for Var<'ctx, F>

Source§

fn add_assign(&mut self, rhs: &Self)

Performs the += operation. Read more
Source§

impl<'ctx, F: Additive + Ring> Additive for Var<'ctx, F>

Source§

fn zero() -> Self

The neutral element for addition.
Source§

fn double(&mut self)

Add an element to itself. Read more
Source§

fn scale(&self, bits_le: &[u64]) -> Self

Scale this number by a positive integer. Read more
Source§

impl<'ctx, F: Clone> Clone for Var<'ctx, F>

Source§

fn clone(&self) -> Var<'ctx, F>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<F: Debug> Debug for Var<'_, F>

Source§

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

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

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§

type Output = Var<'ctx, F>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &Self) -> Self

Performs the / operation. Read more
Source§

impl<'ctx, F: Field> DivAssign<&Var<'ctx, F>> for Var<'ctx, F>

Source§

fn div_assign(&mut self, rhs: &Self)

Performs the /= operation. Read more
Source§

impl<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.

Source§

fn inv(&self) -> Self

The multiplicative inverse of an element. Read more
Source§

impl<'ctx, F: Multiplicative> Mul<&Var<'ctx, F>> for Var<'ctx, F>

Source§

type Output = Var<'ctx, F>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Self) -> Self

Performs the * operation. Read more
Source§

impl<'ctx, F: Multiplicative> MulAssign<&Var<'ctx, F>> for Var<'ctx, F>

Source§

fn mul_assign(&mut self, rhs: &Self)

Performs the *= operation. Read more
Source§

impl<'ctx, F: Multiplicative> Multiplicative for Var<'ctx, F>

Source§

fn square(&mut self)

Multiply an element with itself. Read more
Source§

impl<'ctx, F: Additive + Ring> Neg for Var<'ctx, F>

Source§

type Output = Var<'ctx, F>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl<'ctx, F: Object> Object for Var<'ctx, F>

Source§

impl<F: PartialEq> PartialEq for Var<'_, F>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'ctx, F: Ring> Ring for Var<'ctx, F>

Source§

fn one() -> Self

The neutral element for multiplication. Read more
Source§

fn exp(&self, bits_le: &[u64]) -> Self

Exponentiate this number by a positive integer. Read more
Source§

impl<'ctx, F: Additive + Ring> Sub<&Var<'ctx, F>> for Var<'ctx, F>

Source§

type Output = Var<'ctx, F>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Self) -> Self

Performs the - operation. Read more
Source§

impl<'ctx, F: Additive + Ring> SubAssign<&Var<'ctx, F>> for Var<'ctx, F>

Source§

fn sub_assign(&mut self, rhs: &Self)

Performs the -= operation. Read more

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>
where F: Sync + Send,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, Rhs, Output> GroupOps<Rhs, Output> for T
where T: Add<Rhs, Output = Output> + Sub<Rhs, Output = Output> + AddAssign<Rhs> + SubAssign<Rhs>,

Source§

impl<T, Rhs, Output> GroupOpsOwned<Rhs, Output> for T
where T: for<'r> GroupOps<&'r Rhs, Output>,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where 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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, Rhs, Output> ScalarMul<Rhs, Output> for T
where T: Mul<Rhs, Output = Output> + MulAssign<Rhs>,

Source§

impl<T, Rhs, Output> ScalarMulOwned<Rhs, Output> for T
where T: for<'r> ScalarMul<&'r Rhs, Output>,

Source§

impl<R> Space<R> for R

Source§

fn msm(points: &[Self], scalars: &[R], _strategy: &impl Strategy) -> Self

Calculate sum_i points[i] * scalars[i]. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more