snarkvm_circuit_types_boolean/
not.rs1use super::*;
17use std::sync::Arc;
18
19impl<E: Environment> Not for Boolean<E> {
20 type Output = Boolean<E>;
21
22 fn not(self) -> Self::Output {
24 (&self).not()
25 }
26}
27
28impl<E: Environment> Not for &Boolean<E> {
29 type Output = Boolean<E>;
30
31 fn not(self) -> Self::Output {
33 match self.is_constant() {
37 true => Boolean(E::one() - &self.0),
39 false => Boolean(Variable::Public(Arc::new((0, E::BaseField::one()))) - &self.0),
43 }
44 }
45}
46
47impl<E: Environment> Metrics<dyn Not<Output = Boolean<E>>> for Boolean<E> {
48 type Case = Mode;
49
50 fn count(_case: &Self::Case) -> Count {
51 Count::is(0, 0, 0, 0)
52 }
53}
54
55impl<E: Environment> OutputMode<dyn Not<Output = Boolean<E>>> for Boolean<E> {
56 type Case = Mode;
57
58 fn output_mode(case: &Self::Case) -> Mode {
59 match case {
60 Mode::Constant => Mode::Constant,
61 _ => Mode::Private,
62 }
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69 use snarkvm_circuit_environment::Circuit;
70
71 fn check_not(name: &str, expected: bool, candidate_input: Boolean<Circuit>) {
72 Circuit::scope(name, || {
73 let mode = candidate_input.mode();
74 let candidate_output = !candidate_input;
75 assert_eq!(expected, candidate_output.eject_value());
76 assert_count!(Not(Boolean) => Boolean, &mode);
77 assert_output_mode!(Not(Boolean) => Boolean, &mode, candidate_output);
78 });
79 }
80
81 #[test]
82 fn test_not_constant() {
83 let expected = true;
85 let candidate_input = Boolean::<Circuit>::new(Mode::Constant, false);
86 check_not("NOT false", expected, candidate_input);
87
88 let expected = false;
90 let candidate_input = Boolean::<Circuit>::new(Mode::Constant, true);
91 check_not("NOT true", expected, candidate_input);
92 }
93
94 #[test]
95 fn test_not_public() {
96 let expected = true;
98 let candidate_input = Boolean::<Circuit>::new(Mode::Public, false);
99 check_not("NOT false", expected, candidate_input);
100
101 let expected = false;
103 let candidate_input = Boolean::<Circuit>::new(Mode::Public, true);
104 check_not("NOT true", expected, candidate_input);
105 }
106
107 #[test]
108 fn test_not_private() {
109 let expected = true;
111 let candidate_input = Boolean::<Circuit>::new(Mode::Private, false);
112 check_not("NOT false", expected, candidate_input);
113
114 let expected = false;
116 let candidate_input = Boolean::<Circuit>::new(Mode::Private, true);
117 check_not("NOT true", expected, candidate_input);
118 }
119}