use super::*;
impl<E: Environment> BitAnd<Boolean<E>> for Boolean<E> {
type Output = Boolean<E>;
fn bitand(self, other: Boolean<E>) -> Self::Output {
self & &other
}
}
impl<E: Environment> BitAnd<Boolean<E>> for &Boolean<E> {
type Output = Boolean<E>;
fn bitand(self, other: Boolean<E>) -> Self::Output {
self & &other
}
}
impl<E: Environment> BitAnd<&Boolean<E>> for Boolean<E> {
type Output = Boolean<E>;
fn bitand(self, other: &Boolean<E>) -> Self::Output {
&self & other
}
}
impl<E: Environment> BitAnd<&Boolean<E>> for &Boolean<E> {
type Output = Boolean<E>;
fn bitand(self, other: &Boolean<E>) -> Self::Output {
let mut output = self.clone();
output &= other;
output
}
}
impl<E: Environment> BitAndAssign<Boolean<E>> for Boolean<E> {
fn bitand_assign(&mut self, other: Boolean<E>) {
*self &= &other;
}
}
impl<E: Environment> BitAndAssign<&Boolean<E>> for Boolean<E> {
fn bitand_assign(&mut self, other: &Boolean<E>) {
*self =
if self.is_constant() {
match self.eject_value() {
true => other.clone(),
false => self.clone(),
}
}
else if other.is_constant() {
match other.eject_value() {
true => self.clone(),
false => other.clone(),
}
}
else {
let output = Boolean(
E::new_variable(Mode::Private, match self.eject_value() & other.eject_value() {
true => E::BaseField::one(),
false => E::BaseField::zero(),
})
.into(),
);
E::enforce(|| (&*self, other, &output));
output
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_circuit_environment::Circuit;
fn check_and(
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 & &b;
assert_eq!(expected, candidate.eject_value(), "({} AND {})", a.eject_value(), b.eject_value());
assert_scope!(num_constants, num_public, num_private, num_constraints);
});
}
#[test]
fn test_constant_and_constant() {
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Constant, false);
check_and("false AND 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_and("false AND 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_and("true AND 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_and("true AND true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_constant_and_public() {
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Public, false);
check_and("false AND 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_and("false AND 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_and("true AND 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_and("true AND true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_constant_and_private() {
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Private, false);
check_and("false AND 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_and("false AND 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_and("true AND 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_and("true AND true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_public_and_constant() {
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Constant, false);
check_and("false AND 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_and("false AND 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_and("true AND 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_and("true AND true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_public_and_public() {
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Public, false);
check_and("false AND 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_and("false AND 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_and("true AND 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_and("true AND true", expected, a, b, 0, 0, 1, 1);
}
#[test]
fn test_public_and_private() {
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Private, false);
check_and("false AND 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_and("false AND 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_and("true AND 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_and("true AND true", expected, a, b, 0, 0, 1, 1);
}
#[test]
fn test_private_and_constant() {
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Private, false);
let b = Boolean::<Circuit>::new(Mode::Constant, false);
check_and("false AND 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_and("false AND 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_and("true AND 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_and("true AND true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_private_and_public() {
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Private, false);
check_and("false AND 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_and("false AND 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_and("true AND 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_and("true AND true", expected, a, b, 0, 0, 1, 1);
}
#[test]
fn test_private_and_private() {
let expected = false;
let a = Boolean::<Circuit>::new(Mode::Private, false);
let b = Boolean::<Circuit>::new(Mode::Private, false);
check_and("false AND 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_and("false AND 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_and("true AND 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_and("true AND true", expected, a, b, 0, 0, 1, 1);
}
}