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_constant<T, CS: ConstraintSystem<ConstraintF>>(
 12        cs: CS,
 13        t: T,
 14    ) -> Result<Self, SynthesisError>
 15    where
 16        T: Borrow<V>;
 17
 18    fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(cs: CS, f: F) -> Result<Self, SynthesisError>
 19    where
 20        F: FnOnce() -> Result<T, SynthesisError>,
 21        T: Borrow<V>;
 22
 23    fn alloc_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
 24        cs: CS,
 25        f: F,
 26    ) -> Result<Self, SynthesisError>
 27    where
 28        F: FnOnce() -> Result<T, SynthesisError>,
 29        T: Borrow<V>,
 30    {
 31        Self::alloc(cs, f)
 32    }
 33
 34    fn alloc_input<F, T, CS: ConstraintSystem<ConstraintF>>(
 35        cs: CS,
 36        f: F,
 37    ) -> Result<Self, SynthesisError>
 38    where
 39        F: FnOnce() -> Result<T, SynthesisError>,
 40        T: Borrow<V>;
 41
 42    fn alloc_input_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
 43        cs: CS,
 44        f: F,
 45    ) -> Result<Self, SynthesisError>
 46    where
 47        F: FnOnce() -> Result<T, SynthesisError>,
 48        T: Borrow<V>,
 49    {
 50        Self::alloc_input(cs, f)
 51    }
 52}
 53
 54impl<I, ConstraintF: Field, A: AllocGadget<I, ConstraintF>> AllocGadget<[I], ConstraintF>
 55    for Vec<A>
 56{
 57    #[inline]
 58    fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
 59        mut cs: CS,
 60        t: T,
 61    ) -> Result<Self, SynthesisError>
 62    where
 63        T: Borrow<[I]>,
 64    {
 65        let mut vec = Vec::new();
 66        for (i, value) in t.borrow().iter().enumerate() {
 67            vec.push(A::alloc_constant(cs.ns(|| format!("value_{}", i)), value)?);
 68        }
 69        Ok(vec)
 70    }
 71
 72    fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
 73        mut cs: CS,
 74        f: F,
 75    ) -> Result<Self, SynthesisError>
 76    where
 77        F: FnOnce() -> Result<T, SynthesisError>,
 78        T: Borrow<[I]>,
 79    {
 80        let mut vec = Vec::new();
 81        for (i, value) in f()?.borrow().iter().enumerate() {
 82            vec.push(A::alloc(&mut cs.ns(|| format!("value_{}", i)), || {
 83                Ok(value)
 84            })?);
 85        }
 86        Ok(vec)
 87    }
 88
 89    fn alloc_input<F, T, CS: ConstraintSystem<ConstraintF>>(
 90        mut cs: CS,
 91        f: F,
 92    ) -> Result<Self, SynthesisError>
 93    where
 94        F: FnOnce() -> Result<T, SynthesisError>,
 95        T: Borrow<[I]>,
 96    {
 97        let mut vec = Vec::new();
 98        for (i, value) in f()?.borrow().iter().enumerate() {
 99            vec.push(A::alloc_input(
100                &mut cs.ns(|| format!("value_{}", i)),
101                || Ok(value),
102            )?);
103        }
104        Ok(vec)
105    }
106
107    fn alloc_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
108        mut cs: CS,
109        f: F,
110    ) -> Result<Self, SynthesisError>
111    where
112        F: FnOnce() -> Result<T, SynthesisError>,
113        T: Borrow<[I]>,
114    {
115        let mut vec = Vec::new();
116        for (i, value) in f()?.borrow().iter().enumerate() {
117            vec.push(A::alloc_checked(
118                &mut cs.ns(|| format!("value_{}", i)),
119                || Ok(value),
120            )?);
121        }
122        Ok(vec)
123    }
124
125    fn alloc_input_checked<F, T, CS: ConstraintSystem<ConstraintF>>(
126        mut cs: CS,
127        f: F,
128    ) -> Result<Self, SynthesisError>
129    where
130        F: FnOnce() -> Result<T, SynthesisError>,
131        T: Borrow<[I]>,
132    {
133        let mut vec = Vec::new();
134        for (i, value) in f()?.borrow().iter().enumerate() {
135            vec.push(A::alloc_input_checked(
136                &mut cs.ns(|| format!("value_{}", i)),
137                || Ok(value),
138            )?);
139        }
140        Ok(vec)
141    }
142}