You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.6 KiB

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