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.

119 lines
3.1 KiB

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