use crate::Vec; use algebra::Field; use core::borrow::Borrow; use r1cs_core::{ConstraintSystem, SynthesisError}; pub trait AllocGadget where Self: Sized, V: ?Sized, { fn alloc_constant>( cs: CS, t: T, ) -> Result where T: Borrow; fn alloc>(cs: CS, f: F) -> Result where F: FnOnce() -> Result, T: Borrow; fn alloc_checked>( cs: CS, f: F, ) -> Result where F: FnOnce() -> Result, T: Borrow, { Self::alloc(cs, f) } fn alloc_input>( cs: CS, f: F, ) -> Result where F: FnOnce() -> Result, T: Borrow; fn alloc_input_checked>( cs: CS, f: F, ) -> Result where F: FnOnce() -> Result, T: Borrow, { Self::alloc_input(cs, f) } } impl> AllocGadget<[I], ConstraintF> for Vec { #[inline] fn alloc_constant>( mut cs: CS, t: T, ) -> Result where T: Borrow<[I]>, { let mut vec = Vec::new(); for (i, value) in t.borrow().iter().enumerate() { vec.push(A::alloc_constant(cs.ns(|| format!("value_{}", i)), value)?); } Ok(vec) } fn alloc>( mut cs: CS, f: F, ) -> Result where F: FnOnce() -> Result, T: Borrow<[I]>, { let mut vec = Vec::new(); for (i, value) in f()?.borrow().iter().enumerate() { vec.push(A::alloc(&mut cs.ns(|| format!("value_{}", i)), || { Ok(value) })?); } Ok(vec) } fn alloc_input>( mut cs: CS, f: F, ) -> Result where F: FnOnce() -> Result, T: Borrow<[I]>, { let mut vec = Vec::new(); for (i, value) in f()?.borrow().iter().enumerate() { vec.push(A::alloc_input( &mut cs.ns(|| format!("value_{}", i)), || Ok(value), )?); } Ok(vec) } fn alloc_checked>( mut cs: CS, f: F, ) -> Result where F: FnOnce() -> Result, T: Borrow<[I]>, { let mut vec = Vec::new(); for (i, value) in f()?.borrow().iter().enumerate() { vec.push(A::alloc_checked( &mut cs.ns(|| format!("value_{}", i)), || Ok(value), )?); } Ok(vec) } fn alloc_input_checked>( mut cs: CS, f: F, ) -> Result where F: FnOnce() -> Result, T: Borrow<[I]>, { let mut vec = Vec::new(); for (i, value) in f()?.borrow().iter().enumerate() { vec.push(A::alloc_input_checked( &mut cs.ns(|| format!("value_{}", i)), || Ok(value), )?); } Ok(vec) } }