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.

131 lines
4.2 KiB

  1. use crate::{prelude::*, Vec};
  2. use algebra::Field;
  3. use r1cs_core::SynthesisError;
  4. pub trait EqGadget<F: Field> {
  5. /// Output a `Boolean` value representing whether `self.value() == other.value()`.
  6. fn is_eq(&self, other: &Self) -> Result<Boolean<F>, SynthesisError>;
  7. /// Output a `Boolean` value representing whether `self.value() != other.value()`.
  8. fn is_neq(&self, other: &Self) -> Result<Boolean<F>, SynthesisError> {
  9. Ok(self.is_eq(other)?.not())
  10. }
  11. /// If `should_enforce == true`, enforce that `self` and `other` are equal; else,
  12. /// enforce a vacuously true statement.
  13. fn conditional_enforce_equal(
  14. &self,
  15. other: &Self,
  16. should_enforce: &Boolean<F>,
  17. ) -> Result<(), SynthesisError> {
  18. self.is_eq(&other)?
  19. .conditional_enforce_equal(&Boolean::constant(true), should_enforce)
  20. }
  21. /// Enforce that `self` and `other` are equal.
  22. fn enforce_equal(&self, other: &Self) -> Result<(), SynthesisError> {
  23. self.conditional_enforce_equal(other, &Boolean::constant(true))
  24. }
  25. /// If `should_enforce == true`, enforce that `self` and `other` are not equal; else,
  26. /// enforce a vacuously true statement.
  27. fn conditional_enforce_not_equal(
  28. &self,
  29. other: &Self,
  30. should_enforce: &Boolean<F>,
  31. ) -> Result<(), SynthesisError> {
  32. self.is_neq(&other)?
  33. .conditional_enforce_equal(&Boolean::constant(true), should_enforce)
  34. }
  35. /// Enforce that `self` and `other` are not equal.
  36. fn enforce_not_equal(&self, other: &Self) -> Result<(), SynthesisError> {
  37. self.conditional_enforce_not_equal(other, &Boolean::constant(true))
  38. }
  39. }
  40. impl<T: EqGadget<F> + R1CSVar<F>, F: Field> EqGadget<F> for [T] {
  41. fn is_eq(&self, other: &Self) -> Result<Boolean<F>, SynthesisError> {
  42. assert_eq!(self.len(), other.len());
  43. assert!(!self.is_empty());
  44. let mut results = Vec::with_capacity(self.len());
  45. for (a, b) in self.iter().zip(other) {
  46. results.push(a.is_eq(b)?);
  47. }
  48. Boolean::kary_and(&results)
  49. }
  50. fn conditional_enforce_equal(
  51. &self,
  52. other: &Self,
  53. condition: &Boolean<F>,
  54. ) -> Result<(), SynthesisError> {
  55. assert_eq!(self.len(), other.len());
  56. for (a, b) in self.iter().zip(other) {
  57. a.conditional_enforce_equal(b, condition)?;
  58. }
  59. Ok(())
  60. }
  61. fn conditional_enforce_not_equal(
  62. &self,
  63. other: &Self,
  64. should_enforce: &Boolean<F>,
  65. ) -> Result<(), SynthesisError> {
  66. assert_eq!(self.len(), other.len());
  67. let some_are_different = self.is_neq(other)?;
  68. if let Some(cs) = some_are_different.cs().or(should_enforce.cs()) {
  69. cs.enforce_constraint(
  70. some_are_different.lc(),
  71. should_enforce.lc(),
  72. should_enforce.lc(),
  73. )
  74. } else {
  75. // `some_are_different` and `should_enforce` are both constants
  76. assert!(some_are_different.value().unwrap());
  77. Ok(())
  78. }
  79. }
  80. }
  81. pub trait OrEqualsGadget<ConstraintF: Field>
  82. where
  83. Self: Sized,
  84. {
  85. /// If `should_enforce == true`, enforce that `self` equals
  86. /// (a) `first` (if `cond` is `true`)
  87. /// (b) `second` (if `cond` is `false`)
  88. fn conditional_enforce_equal_or(
  89. &self,
  90. cond: &Boolean<ConstraintF>,
  91. first: &Self,
  92. second: &Self,
  93. should_enforce: &Boolean<ConstraintF>,
  94. ) -> Result<(), SynthesisError>;
  95. fn enforce_equal_or(
  96. &self,
  97. cond: &Boolean<ConstraintF>,
  98. first: &Self,
  99. second: &Self,
  100. ) -> Result<(), SynthesisError> {
  101. self.conditional_enforce_equal_or(cond, first, second, &Boolean::Constant(true))
  102. }
  103. }
  104. impl<ConstraintF, T> OrEqualsGadget<ConstraintF> for T
  105. where
  106. ConstraintF: Field,
  107. T: Sized + EqGadget<ConstraintF> + CondSelectGadget<ConstraintF>,
  108. {
  109. fn conditional_enforce_equal_or(
  110. &self,
  111. cond: &Boolean<ConstraintF>,
  112. first: &Self,
  113. second: &Self,
  114. should_enforce: &Boolean<ConstraintF>,
  115. ) -> Result<(), SynthesisError> {
  116. let match_opt = cond.select(first, second)?;
  117. self.conditional_enforce_equal(&match_opt, should_enforce)
  118. }
  119. }