/ circuit / types / group / src / equal.rs
equal.rs
  1  // Copyright (c) 2025 ADnet Contributors
  2  // This file is part of the AlphaVM library.
  3  
  4  // Licensed under the Apache License, Version 2.0 (the "License");
  5  // you may not use this file except in compliance with the License.
  6  // You may obtain a copy of the License at:
  7  
  8  // http://www.apache.org/licenses/LICENSE-2.0
  9  
 10  // Unless required by applicable law or agreed to in writing, software
 11  // distributed under the License is distributed on an "AS IS" BASIS,
 12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  // See the License for the specific language governing permissions and
 14  // limitations under the License.
 15  
 16  use super::*;
 17  
 18  impl<E: Environment> Equal<Self> for Group<E> {
 19      type Output = Boolean<E>;
 20  
 21      ///
 22      /// Returns `true` if `self` and `other` are equal.
 23      ///
 24      /// This method costs 8 constraints.
 25      ///
 26      fn is_equal(&self, other: &Self) -> Self::Output {
 27          let is_x_eq = self.x.is_equal(&other.x);
 28          let is_y_eq = self.y.is_equal(&other.y);
 29          is_x_eq & is_y_eq
 30      }
 31  
 32      ///
 33      /// Returns `true` if `self` and `other` are *not* equal.
 34      ///
 35      /// This method constructs a boolean that indicates if
 36      /// `self` and `other ` are *not* equal to each other.
 37      ///
 38      /// This method costs 8 constraints.
 39      ///
 40      fn is_not_equal(&self, other: &Self) -> Self::Output {
 41          !self.is_equal(other)
 42      }
 43  }
 44  
 45  #[cfg(test)]
 46  mod tests {
 47      use super::*;
 48      use alphavm_circuit_environment::Circuit;
 49  
 50      const ITERATIONS: u64 = 100;
 51  
 52      #[test]
 53      fn test_is_equal() {
 54          let mut rng = TestRng::default();
 55  
 56          // Constant == Constant
 57          for i in 0..ITERATIONS {
 58              // Sample two random elements.
 59              let a = Uniform::rand(&mut rng);
 60              let b = Uniform::rand(&mut rng);
 61  
 62              let a = Group::<Circuit>::new(Mode::Constant, a);
 63              let b = Group::<Circuit>::new(Mode::Constant, b);
 64  
 65              Circuit::scope(format!("Constant Equals {i}"), || {
 66                  let equals = a.is_equal(&b);
 67                  assert!(!equals.eject_value());
 68                  assert_scope!(2, 0, 0, 0);
 69              });
 70              Circuit::reset();
 71  
 72              Circuit::scope(format!("Constant Not Equals {i}"), || {
 73                  let equals = a.is_not_equal(&b);
 74                  assert!(equals.eject_value());
 75                  assert_scope!(2, 0, 0, 0);
 76              });
 77              Circuit::reset();
 78          }
 79  
 80          // Constant == Public
 81          for i in 0..ITERATIONS {
 82              // Sample two random elements.
 83              let a = Uniform::rand(&mut rng);
 84              let b = Uniform::rand(&mut rng);
 85  
 86              let a = Group::<Circuit>::new(Mode::Constant, a);
 87              let b = Group::<Circuit>::new(Mode::Public, b);
 88  
 89              Circuit::scope(format!("Constant and Public Equals {i}"), || {
 90                  let equals = a.is_equal(&b);
 91                  assert!(!equals.eject_value());
 92                  assert_scope!(0, 0, 5, 5);
 93              });
 94              Circuit::reset();
 95  
 96              Circuit::scope(format!("Constant and Public Not Equals {i}"), || {
 97                  let equals = a.is_not_equal(&b);
 98                  assert!(equals.eject_value());
 99                  assert_scope!(0, 0, 5, 5);
100              });
101              Circuit::reset();
102          }
103  
104          // Public == Constant
105          for i in 0..ITERATIONS {
106              // Sample two random elements.
107              let a = Uniform::rand(&mut rng);
108              let b = Uniform::rand(&mut rng);
109  
110              let a = Group::<Circuit>::new(Mode::Public, a);
111              let b = Group::<Circuit>::new(Mode::Constant, b);
112  
113              Circuit::scope(format!("Public and Constant Equals {i}"), || {
114                  let equals = a.is_equal(&b);
115                  assert!(!equals.eject_value());
116                  assert_scope!(0, 0, 5, 5);
117              });
118              Circuit::reset();
119  
120              Circuit::scope(format!("Public and Constant Not Equals {i}"), || {
121                  let equals = a.is_not_equal(&b);
122                  assert!(equals.eject_value());
123                  assert_scope!(0, 0, 5, 5);
124              });
125              Circuit::reset();
126          }
127  
128          // Public == Public
129          for i in 0..ITERATIONS {
130              // Sample two random elements.
131              let a = Uniform::rand(&mut rng);
132              let b = Uniform::rand(&mut rng);
133  
134              let a = Group::<Circuit>::new(Mode::Public, a);
135              let b = Group::<Circuit>::new(Mode::Public, b);
136  
137              Circuit::scope(format!("Public Equals {i}"), || {
138                  let equals = a.is_equal(&b);
139                  assert!(!equals.eject_value());
140                  assert_scope!(0, 0, 5, 5);
141              });
142              Circuit::reset();
143  
144              Circuit::scope(format!("Public Not Equals {i}"), || {
145                  let equals = a.is_not_equal(&b);
146                  assert!(equals.eject_value());
147                  assert_scope!(0, 0, 5, 5);
148              });
149              Circuit::reset();
150          }
151  
152          // Private == Private
153          for i in 0..ITERATIONS {
154              // Sample two random elements.
155              let a = Uniform::rand(&mut rng);
156              let b = Uniform::rand(&mut rng);
157  
158              let a = Group::<Circuit>::new(Mode::Private, a);
159              let b = Group::<Circuit>::new(Mode::Private, b);
160  
161              Circuit::scope(format!("Private Equals {i}"), || {
162                  let equals = a.is_equal(&b);
163                  assert!(!equals.eject_value());
164                  assert_scope!(0, 0, 5, 5);
165              });
166              Circuit::reset();
167  
168              Circuit::scope(format!("Private Not Equals {i}"), || {
169                  let equals = a.is_not_equal(&b);
170                  assert!(equals.eject_value());
171                  assert_scope!(0, 0, 5, 5);
172              });
173              Circuit::reset();
174          }
175      }
176  }