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.

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