ark_r1cs_std::alloc

Trait AllocVar

Source
pub trait AllocVar<V: ?Sized, F: Field>: Sized {
    // Required method
    fn new_variable<T: Borrow<V>>(
        cs: impl Into<Namespace<F>>,
        f: impl FnOnce() -> Result<T, SynthesisError>,
        mode: AllocationMode,
    ) -> Result<Self, SynthesisError>;

    // Provided methods
    fn new_constant(
        cs: impl Into<Namespace<F>>,
        t: impl Borrow<V>,
    ) -> Result<Self, SynthesisError> { ... }
    fn new_input<T: Borrow<V>>(
        cs: impl Into<Namespace<F>>,
        f: impl FnOnce() -> Result<T, SynthesisError>,
    ) -> Result<Self, SynthesisError> { ... }
    fn new_witness<T: Borrow<V>>(
        cs: impl Into<Namespace<F>>,
        f: impl FnOnce() -> Result<T, SynthesisError>,
    ) -> Result<Self, SynthesisError> { ... }
    fn new_variable_with_inferred_mode<T: Borrow<V>>(
        cs: impl Into<Namespace<F>>,
        f: impl FnOnce() -> Result<T, SynthesisError>,
    ) -> Result<Self, SynthesisError> { ... }
}
Expand description

Specifies how variables of type Self should be allocated in a ConstraintSystem.

Required Methods§

Source

fn new_variable<T: Borrow<V>>( cs: impl Into<Namespace<F>>, f: impl FnOnce() -> Result<T, SynthesisError>, mode: AllocationMode, ) -> Result<Self, SynthesisError>

Allocates a new variable of type Self in the ConstraintSystem cs. The mode of allocation is decided by mode.

Provided Methods§

Source

fn new_constant( cs: impl Into<Namespace<F>>, t: impl Borrow<V>, ) -> Result<Self, SynthesisError>

Allocates a new constant of type Self in the ConstraintSystem cs.

This should not allocate any new variables or constraints in cs.

Source

fn new_input<T: Borrow<V>>( cs: impl Into<Namespace<F>>, f: impl FnOnce() -> Result<T, SynthesisError>, ) -> Result<Self, SynthesisError>

Allocates a new public input of type Self in the ConstraintSystem cs.

Source

fn new_witness<T: Borrow<V>>( cs: impl Into<Namespace<F>>, f: impl FnOnce() -> Result<T, SynthesisError>, ) -> Result<Self, SynthesisError>

Allocates a new private witness of type Self in the ConstraintSystem cs.

Source

fn new_variable_with_inferred_mode<T: Borrow<V>>( cs: impl Into<Namespace<F>>, f: impl FnOnce() -> Result<T, SynthesisError>, ) -> Result<Self, SynthesisError>

Allocates a new constant or private witness of type Self in the ConstraintSystem cs with the allocation mode inferred from cs. A constant is allocated if cs is None, and a private witness is allocated otherwise.

A common use case is the creation of non-deterministic advice (a.k.a. hints) in the circuit, where this method can avoid boilerplate code while allowing optimization on circuit size.

For example, to compute x_var / y_var where y_var is a non-zero variable, one can write:

use ark_ff::PrimeField;
use ark_r1cs_std::{alloc::AllocVar, fields::{fp::FpVar, FieldVar}, R1CSVar};
use ark_relations::r1cs::SynthesisError;

fn div<F: PrimeField>(x_var: &FpVar<F>, y_var: &FpVar<F>) -> Result<FpVar<F>, SynthesisError> {
  let cs = x_var.cs().or(y_var.cs());
  let z_var = FpVar::new_variable_with_inferred_mode(cs, || Ok(x_var.value()? / y_var.value()?))?;
  z_var.mul_equals(y_var, x_var)?;
  Ok(z_var)
}

In this example, if either x_var or y_var is a witness variable, then z_var is also a witness variable. On the other hand, z_var is a constant if both x_var and y_var are constants (i.e., cs is None), and future operations on z_var do not generate any constraints.

(Note that we use division as an example for simplicity. You may call x_var.mul_by_inverse(y_var)? directly, which internally works similarly to the above code.)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<F: Field> AllocVar<(), F> for ()

Dummy impl for ().

Source§

fn new_variable<T: Borrow<()>>( _cs: impl Into<Namespace<F>>, _f: impl FnOnce() -> Result<T, SynthesisError>, _mode: AllocationMode, ) -> Result<Self, SynthesisError>

Source§

impl<I, F: Field, A: AllocVar<I, F>> AllocVar<[I], F> for Vec<A>

This blanket implementation just allocates variables in Self element by element.

Source§

fn new_variable<T: Borrow<[I]>>( cs: impl Into<Namespace<F>>, f: impl FnOnce() -> Result<T, SynthesisError>, mode: AllocationMode, ) -> Result<Self, SynthesisError>

Source§

impl<I, F: Field, A: AllocVar<I, F>, const N: usize> AllocVar<[I; N], F> for [A; N]

This blanket implementation just allocates variables in Self element by element.

Source§

fn new_variable<T: Borrow<[I; N]>>( cs: impl Into<Namespace<F>>, f: impl FnOnce() -> Result<T, SynthesisError>, mode: AllocationMode, ) -> Result<Self, SynthesisError>

Source§

impl<I, F: Field, A: AllocVar<I, F>, const N: usize> AllocVar<[I], F> for [A; N]

This blanket implementation just allocates variables in Self element by element.

Source§

fn new_variable<T: Borrow<[I]>>( cs: impl Into<Namespace<F>>, f: impl FnOnce() -> Result<T, SynthesisError>, mode: AllocationMode, ) -> Result<Self, SynthesisError>

Implementors§

Source§

impl<BF, P> AllocVar<CubicExtField<P>, <P as CubicExtConfig>::BasePrimeField> for CubicExtVar<BF, P>
where BF: FieldVar<P::BaseField, P::BasePrimeField>, for<'a> &'a BF: FieldOpsBounds<'a, P::BaseField, BF>, P: CubicExtVarConfig<BF>,

Source§

impl<BF, P> AllocVar<QuadExtField<P>, <P as QuadExtConfig>::BasePrimeField> for QuadExtVar<BF, P>
where BF: FieldVar<P::BaseField, P::BasePrimeField>, for<'b> &'b BF: FieldOpsBounds<'b, P::BaseField, BF>, P: QuadExtVarConfig<BF>,

Source§

impl<F: PrimeField> AllocVar<F, F> for FpVar<F>

Source§

impl<F: PrimeField> AllocVar<F, F> for AllocatedFp<F>

Source§

impl<F: Field> AllocVar<bool, F> for Boolean<F>

Source§

impl<F: Field> AllocVar<bool, F> for AllocatedBool<F>

Source§

impl<P, F> AllocVar<Affine<P>, <<P as CurveConfig>::BaseField as Field>::BasePrimeField> for ProjectiveVar<P, F>

Source§

impl<P, F> AllocVar<Projective<P>, <<P as CurveConfig>::BaseField as Field>::BasePrimeField> for ProjectiveVar<P, F>

Source§

impl<P, F> AllocVar<Affine<P>, <<P as CurveConfig>::BaseField as Field>::BasePrimeField> for AffineVar<P, F>

Source§

impl<P, F> AllocVar<Projective<P>, <<P as CurveConfig>::BaseField as Field>::BasePrimeField> for AffineVar<P, F>

Source§

impl<P: Bls12Config> AllocVar<G1Prepared<P>, <P as Bls12Config>::Fp> for ark_r1cs_std::groups::curves::short_weierstrass::bls12::G1PreparedVar<P>

Source§

impl<P: Bls12Config> AllocVar<G2Prepared<P>, <P as Bls12Config>::Fp> for ark_r1cs_std::groups::curves::short_weierstrass::bls12::G2PreparedVar<P>

Source§

impl<P: MNT4Config> AllocVar<G1Prepared<P>, <P as MNT4Config>::Fp> for ark_r1cs_std::groups::curves::short_weierstrass::mnt4::G1PreparedVar<P>

Source§

impl<P: MNT4Config> AllocVar<G2Prepared<P>, <P as MNT4Config>::Fp> for ark_r1cs_std::groups::curves::short_weierstrass::mnt4::G2PreparedVar<P>

Source§

impl<P: MNT6Config> AllocVar<G1Prepared<P>, <P as MNT6Config>::Fp> for ark_r1cs_std::groups::curves::short_weierstrass::mnt6::G1PreparedVar<P>

Source§

impl<P: MNT6Config> AllocVar<G2Prepared<P>, <P as MNT6Config>::Fp> for ark_r1cs_std::groups::curves::short_weierstrass::mnt6::G2PreparedVar<P>

Source§

impl<TargetF: PrimeField, BaseF: PrimeField> AllocVar<TargetF, BaseF> for EmulatedFpVar<TargetF, BaseF>

Source§

impl<TargetF: PrimeField, BaseF: PrimeField> AllocVar<TargetF, BaseF> for AllocatedEmulatedFpVar<TargetF, BaseF>

Source§

impl<const N: usize, T: PrimUInt, ConstraintF: Field> AllocVar<T, ConstraintF> for UInt<N, T, ConstraintF>