arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
ark-r1cs-std/r1cs-std/src/alloc.rs
r1cs-std/src/alloc.rsRaw
  1use crate::Vec;
  2use algebra::Field;
  3use core::borrow::Borrow;
  4use r1cs_core::{ConstraintSystem, SynthesisError};
  5
  6pub trait AllocGadget<V, ConstraintF: Field>
  7where
  8    Self: Sized,
  9    V: ?Sized,
 10{
 11    fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(cs: CS, f: F) -> Result<Self, SynthesisError>
 12    where
 13        F: FnOnce() -> Result<T, SynthesisError>,
 14        T: Borrow<V>;
 15
 16    fn alloc_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
 17        cs: CS,
 18        f: F,
 19    ) -> Result<Self, SynthesisError>
 20    where
 21        F: FnOnce() -> Result<T, SynthesisError>,
 22        T: Borrow<V>,
 23    {
 24        Self::alloc(cs, f)
 25    }
 26
 27    fn alloc_input<F, T, CS: ConstraintSystem<ConstraintF>>(
 28        cs: CS,
 29        f: F,
 30    ) -> Result<Self, SynthesisError>
 31    where
 32        F: FnOnce() -> Result<T, SynthesisError>,
 33        T: Borrow<V>;
 34
 35    fn alloc_input_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
 36        cs: CS,
 37        f: F,
 38    ) -> Result<Self, SynthesisError>
 39    where
 40        F: FnOnce() -> Result<T, SynthesisError>,
 41        T: Borrow<V>,
 42    {
 43        Self::alloc_input(cs, f)
 44    }
 45}
 46
 47impl<I, ConstraintF: Field, A: AllocGadget<I, ConstraintF>> AllocGadget<[I], ConstraintF>
 48    for Vec<A>
 49{
 50    fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
 51        mut cs: CS,
 52        f: F,
 53    ) -> Result<Self, SynthesisError>
 54    where
 55        F: FnOnce() -> Result<T, SynthesisError>,
 56        T: Borrow<[I]>,
 57    {
 58        let mut vec = Vec::new();
 59        for (i, value) in f()?.borrow().iter().enumerate() {
 60            vec.push(A::alloc(&mut cs.ns(|| format!("value_{}", i)), || {
 61                Ok(value)
 62            })?);
 63        }
 64        Ok(vec)
 65    }
 66
 67    fn alloc_input<F, T, CS: ConstraintSystem<ConstraintF>>(
 68        mut cs: CS,
 69        f: F,
 70    ) -> Result<Self, SynthesisError>
 71    where
 72        F: FnOnce() -> Result<T, SynthesisError>,
 73        T: Borrow<[I]>,
 74    {
 75        let mut vec = Vec::new();
 76        for (i, value) in f()?.borrow().iter().enumerate() {
 77            vec.push(A::alloc_input(
 78                &mut cs.ns(|| format!("value_{}", i)),
 79                || Ok(value),
 80            )?);
 81        }
 82        Ok(vec)
 83    }
 84
 85    fn alloc_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
 86        mut cs: CS,
 87        f: F,
 88    ) -> Result<Self, SynthesisError>
 89    where
 90        F: FnOnce() -> Result<T, SynthesisError>,
 91        T: Borrow<[I]>,
 92    {
 93        let mut vec = Vec::new();
 94        for (i, value) in f()?.borrow().iter().enumerate() {
 95            vec.push(A::alloc_checked(
 96                &mut cs.ns(|| format!("value_{}", i)),
 97                || Ok(value),
 98            )?);
 99        }
100        Ok(vec)
101    }
102
103    fn alloc_input_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
104        mut cs: CS,
105        f: F,
106    ) -> Result<Self, SynthesisError>
107    where
108        F: FnOnce() -> Result<T, SynthesisError>,
109        T: Borrow<[I]>,
110    {
111        let mut vec = Vec::new();
112        for (i, value) in f()?.borrow().iter().enumerate() {
113            vec.push(A::alloc_input_checked(
114                &mut cs.ns(|| format!("value_{}", i)),
115                || Ok(value),
116            )?);
117        }
118        Ok(vec)
119    }
120}