use super::*;
impl<E: Environment> Nand<Self> for Boolean<E> {
type Output = Boolean<E>;
fn nand(&self, other: &Self) -> Self::Output {
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, E::one() - &output.0));
output
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_circuit_environment::Circuit;
fn check_nand(
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.nand(&b);
assert_eq!(expected, candidate.eject_value(), "({} NAND {})", a.eject_value(), b.eject_value());
assert_scope!(num_constants, num_public, num_private, num_constraints);
});
}
#[test]
fn test_constant_nand_constant() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Constant, false);
check_nand("false NAND false", expected, a, b, 0, 0, 0, 0);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Constant, true);
check_nand("false NAND true", 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, false);
check_nand("true NAND false", 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, true);
check_nand("true NAND true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_constant_nand_public() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Public, false);
check_nand("false NAND false", expected, a, b, 0, 0, 0, 0);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Public, true);
check_nand("false NAND true", 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, false);
check_nand("true NAND false", 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, true);
check_nand("true NAND true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_constant_nand_private() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Private, false);
check_nand("false NAND false", expected, a, b, 0, 0, 0, 0);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Constant, false);
let b = Boolean::<Circuit>::new(Mode::Private, true);
check_nand("false NAND true", 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, false);
check_nand("true NAND false", 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, true);
check_nand("true NAND true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_public_nand_constant() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Constant, false);
check_nand("false NAND false", expected, a, b, 0, 0, 0, 0);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Constant, true);
check_nand("false NAND true", 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, false);
check_nand("true NAND false", 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, true);
check_nand("true NAND true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_public_nand_public() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Public, false);
check_nand("false NAND false", expected, a, b, 0, 0, 1, 1);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Public, true);
check_nand("false NAND true", 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, false);
check_nand("true NAND false", 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, true);
check_nand("true NAND true", expected, a, b, 0, 0, 1, 1);
}
#[test]
fn test_public_nand_private() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Private, false);
check_nand("false NAND false", expected, a, b, 0, 0, 1, 1);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Public, false);
let b = Boolean::<Circuit>::new(Mode::Private, true);
check_nand("false NAND true", 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, false);
check_nand("true NAND false", 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, true);
check_nand("true NAND true", expected, a, b, 0, 0, 1, 1);
}
#[test]
fn test_private_nand_constant() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Private, false);
let b = Boolean::<Circuit>::new(Mode::Constant, false);
check_nand("false NAND false", expected, a, b, 0, 0, 0, 0);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Private, false);
let b = Boolean::<Circuit>::new(Mode::Constant, true);
check_nand("false NAND true", 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, false);
check_nand("true NAND false", 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, true);
check_nand("true NAND true", expected, a, b, 0, 0, 0, 0);
}
#[test]
fn test_private_nand_public() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Private, false);
let b = Boolean::<Circuit>::new(Mode::Public, false);
check_nand("false NAND false", expected, a, b, 0, 0, 1, 1);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Private, false);
let b = Boolean::<Circuit>::new(Mode::Public, true);
check_nand("false NAND true", 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, false);
check_nand("true NAND false", 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, true);
check_nand("true NAND true", expected, a, b, 0, 0, 1, 1);
}
#[test]
fn test_private_nand_private() {
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Private, false);
let b = Boolean::<Circuit>::new(Mode::Private, false);
check_nand("false NAND false", expected, a, b, 0, 0, 1, 1);
let expected = true;
let a = Boolean::<Circuit>::new(Mode::Private, false);
let b = Boolean::<Circuit>::new(Mode::Private, true);
check_nand("false NAND true", 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, false);
check_nand("true NAND false", 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, true);
check_nand("true NAND true", expected, a, b, 0, 0, 1, 1);
}
}