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.

123 lines
5.0 KiB

  1. use crate::{prelude::*, Vec};
  2. use algebra::Field;
  3. use r1cs_core::SynthesisError;
  4. /// Specifies how to generate constraints that check for equality for two variables of type `Self`.
  5. pub trait EqGadget<F: Field> {
  6. /// Output a `Boolean` value representing whether `self.value() == other.value()`.
  7. fn is_eq(&self, other: &Self) -> Result<Boolean<F>, SynthesisError>;
  8. /// Output a `Boolean` value representing whether `self.value() != other.value()`.
  9. ///
  10. /// By default, this is defined as `self.is_eq(other)?.not()`.
  11. fn is_neq(&self, other: &Self) -> Result<Boolean<F>, SynthesisError> {
  12. Ok(self.is_eq(other)?.not())
  13. }
  14. /// If `should_enforce == true`, enforce that `self` and `other` are equal; else,
  15. /// enforce a vacuously true statement.
  16. ///
  17. /// A safe default implementation is provided that generates the following constraints:
  18. /// `self.is_eq(other)?.conditional_enforce_equal(&Boolean::TRUE, should_enforce)`.
  19. ///
  20. /// More efficient specialized implementation may be possible; implementors
  21. /// are encouraged to carefully analyze the efficiency and safety of these.
  22. #[tracing::instrument(target = "r1cs", skip(self, other))]
  23. fn conditional_enforce_equal(
  24. &self,
  25. other: &Self,
  26. should_enforce: &Boolean<F>,
  27. ) -> Result<(), SynthesisError> {
  28. self.is_eq(&other)?
  29. .conditional_enforce_equal(&Boolean::constant(true), should_enforce)
  30. }
  31. /// Enforce that `self` and `other` are equal.
  32. ///
  33. /// A safe default implementation is provided that generates the following constraints:
  34. /// `self.conditional_enforce_equal(other, &Boolean::TRUE)`.
  35. ///
  36. /// More efficient specialized implementation may be possible; implementors
  37. /// are encouraged to carefully analyze the efficiency and safety of these.
  38. #[tracing::instrument(target = "r1cs", skip(self, other))]
  39. fn enforce_equal(&self, other: &Self) -> Result<(), SynthesisError> {
  40. self.conditional_enforce_equal(other, &Boolean::constant(true))
  41. }
  42. /// If `should_enforce == true`, enforce that `self` and `other` are *not* equal; else,
  43. /// enforce a vacuously true statement.
  44. ///
  45. /// A safe default implementation is provided that generates the following constraints:
  46. /// `self.is_neq(other)?.conditional_enforce_equal(&Boolean::TRUE, should_enforce)`.
  47. ///
  48. /// More efficient specialized implementation may be possible; implementors
  49. /// are encouraged to carefully analyze the efficiency and safety of these.
  50. #[tracing::instrument(target = "r1cs", skip(self, other))]
  51. fn conditional_enforce_not_equal(
  52. &self,
  53. other: &Self,
  54. should_enforce: &Boolean<F>,
  55. ) -> Result<(), SynthesisError> {
  56. self.is_neq(&other)?
  57. .conditional_enforce_equal(&Boolean::constant(true), should_enforce)
  58. }
  59. /// Enforce that `self` and `other` are *not* equal.
  60. ///
  61. /// A safe default implementation is provided that generates the following constraints:
  62. /// `self.conditional_enforce_not_equal(other, &Boolean::TRUE)`.
  63. ///
  64. /// More efficient specialized implementation may be possible; implementors
  65. /// are encouraged to carefully analyze the efficiency and safety of these.
  66. #[tracing::instrument(target = "r1cs", skip(self, other))]
  67. fn enforce_not_equal(&self, other: &Self) -> Result<(), SynthesisError> {
  68. self.conditional_enforce_not_equal(other, &Boolean::constant(true))
  69. }
  70. }
  71. impl<T: EqGadget<F> + R1CSVar<F>, F: Field> EqGadget<F> for [T] {
  72. #[tracing::instrument(target = "r1cs", skip(self, other))]
  73. fn is_eq(&self, other: &Self) -> Result<Boolean<F>, SynthesisError> {
  74. assert_eq!(self.len(), other.len());
  75. assert!(!self.is_empty());
  76. let mut results = Vec::with_capacity(self.len());
  77. for (a, b) in self.iter().zip(other) {
  78. results.push(a.is_eq(b)?);
  79. }
  80. Boolean::kary_and(&results)
  81. }
  82. #[tracing::instrument(target = "r1cs", skip(self, other))]
  83. fn conditional_enforce_equal(
  84. &self,
  85. other: &Self,
  86. condition: &Boolean<F>,
  87. ) -> Result<(), SynthesisError> {
  88. assert_eq!(self.len(), other.len());
  89. for (a, b) in self.iter().zip(other) {
  90. a.conditional_enforce_equal(b, condition)?;
  91. }
  92. Ok(())
  93. }
  94. #[tracing::instrument(target = "r1cs", skip(self, other))]
  95. fn conditional_enforce_not_equal(
  96. &self,
  97. other: &Self,
  98. should_enforce: &Boolean<F>,
  99. ) -> Result<(), SynthesisError> {
  100. assert_eq!(self.len(), other.len());
  101. let some_are_different = self.is_neq(other)?;
  102. if [&some_are_different, should_enforce].is_constant() {
  103. assert!(some_are_different.value().unwrap());
  104. Ok(())
  105. } else {
  106. let cs = [&some_are_different, should_enforce].cs();
  107. cs.enforce_constraint(
  108. some_are_different.lc(),
  109. should_enforce.lc(),
  110. should_enforce.lc(),
  111. )
  112. }
  113. }
  114. }