use super::*;
impl<E: Environment> Equal<Self> for Boolean<E> {
type Output = Boolean<E>;
fn is_equal(&self, other: &Self) -> Self::Output {
!self.is_not_equal(other)
}
fn is_not_equal(&self, other: &Self) -> Self::Output {
self ^ other
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_circuit_environment::Circuit;
fn check_is_equal(
name: &str,
expected: bool,
a: Boolean<Circuit>,
b: Boolean<Circuit>,
num_constants: u64,
num_public: u64,
num_private: u64,
num_constraints: u64,
) {
Circuit::scope(name, || {
let candidate = a.is_equal(&b);
assert_eq!(expected, candidate.eject_value(), "({} == {})", a.eject_value(), b.eject_value());
assert_scope!(num_constants, num_public, num_private, num_constraints);
});
}
#[test]
fn test_constant_equals_constant() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Constant, false);
check_is_equal("false == false", expected, a, b, 0, 0, 0, 0);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Constant, true);
check_is_equal("false == true", expected, a, b, 0, 0, 0, 0);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Constant, true);
let b = Boolean::<Circuit>::new(Mode::Constant, false);
check_is_equal("true == false", expected, a, b, 0, 0, 0, 0);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Constant, true);
let b = Boolean::<Circuit>::new(Mode::Constant, true);
check_is_equal("true == true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_constant_equals_public() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Public, false);
check_is_equal("false == false", expected, a, b, 0, 0, 0, 0);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Public, true);
check_is_equal("false == true", expected, a, b, 0, 0, 0, 0);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Constant, true);
let b = Boolean::<Circuit>::new(Mode::Public, false);
check_is_equal("true == false", expected, a, b, 0, 0, 0, 0);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Constant, true);
let b = Boolean::<Circuit>::new(Mode::Public, true);
check_is_equal("true == true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_constant_equal_private() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Private, false);
check_is_equal("false == false", expected, a, b, 0, 0, 0, 0);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Private, true);
check_is_equal("false == true", expected, a, b, 0, 0, 0, 0);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Constant, true);
let b = Boolean::<Circuit>::new(Mode::Private, false);
check_is_equal("true == false", expected, a, b, 0, 0, 0, 0);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Constant, true);
let b = Boolean::<Circuit>::new(Mode::Private, true);
check_is_equal("true == true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_public_equals_constant() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Constant, false);
check_is_equal("false == false", expected, a, b, 0, 0, 0, 0);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Constant, true);
check_is_equal("false == true", expected, a, b, 0, 0, 0, 0);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Public, true);
let b = Boolean::<Circuit>::new(Mode::Constant, false);
check_is_equal("true == false", expected, a, b, 0, 0, 0, 0);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Public, true);
let b = Boolean::<Circuit>::new(Mode::Constant, true);
check_is_equal("true == true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_public_equals_public() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Public, false);
check_is_equal("false == false", expected, a, b, 0, 0, 1, 1);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Public, true);
check_is_equal("false == true", expected, a, b, 0, 0, 1, 1);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Public, true);
let b = Boolean::<Circuit>::new(Mode::Public, false);
check_is_equal("true == false", expected, a, b, 0, 0, 1, 1);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Public, true);
let b = Boolean::<Circuit>::new(Mode::Public, true);
check_is_equal("true == true", expected, a, b, 0, 0, 1, 1);
}
#[test]
fn test_public_equals_private() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Private, false);
check_is_equal("false == false", expected, a, b, 0, 0, 1, 1);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Private, true);
check_is_equal("false == true", expected, a, b, 0, 0, 1, 1);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Public, true);
let b = Boolean::<Circuit>::new(Mode::Private, false);
check_is_equal("true == false", expected, a, b, 0, 0, 1, 1);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Public, true);
let b = Boolean::<Circuit>::new(Mode::Private, true);
check_is_equal("true == true", expected, a, b, 0, 0, 1, 1);
}
#[test]
fn test_private_equals_constant() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Private, false);
let b = Boolean::<Circuit>::new(Mode::Constant, false);
check_is_equal("false == false", expected, a, b, 0, 0, 0, 0);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Private, false);
let b = Boolean::<Circuit>::new(Mode::Constant, true);
check_is_equal("false == true", expected, a, b, 0, 0, 0, 0);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Private, true);
let b = Boolean::<Circuit>::new(Mode::Constant, false);
check_is_equal("true == false", expected, a, b, 0, 0, 0, 0);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Private, true);
let b = Boolean::<Circuit>::new(Mode::Constant, true);
check_is_equal("true == true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_private_equal_public() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Private, false);
let b = Boolean::<Circuit>::new(Mode::Public, false);
check_is_equal("false == false", expected, a, b, 0, 0, 1, 1);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Private, false);
let b = Boolean::<Circuit>::new(Mode::Public, true);
check_is_equal("false == true", expected, a, b, 0, 0, 1, 1);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Private, true);
let b = Boolean::<Circuit>::new(Mode::Public, false);
check_is_equal("true == false", expected, a, b, 0, 0, 1, 1);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Private, true);
let b = Boolean::<Circuit>::new(Mode::Public, true);
check_is_equal("true == true", expected, a, b, 0, 0, 1, 1);
}
#[test]
fn test_private_equals_private() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Private, false);
let b = Boolean::<Circuit>::new(Mode::Private, false);
check_is_equal("false == false", expected, a, b, 0, 0, 1, 1);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Private, false);
let b = Boolean::<Circuit>::new(Mode::Private, true);
check_is_equal("false == true", expected, a, b, 0, 0, 1, 1);
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Private, true);
let b = Boolean::<Circuit>::new(Mode::Private, false);
check_is_equal("true == false", expected, a, b, 0, 0, 1, 1);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Private, true);
let b = Boolean::<Circuit>::new(Mode::Private, true);
check_is_equal("true == true", expected, a, b, 0, 0, 1, 1);
}
}