snarkvm_circuit_types_boolean/
not.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM 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
16use super::*;
17use std::sync::Arc;
18
19impl<E: Environment> Not for Boolean<E> {
20    type Output = Boolean<E>;
21
22    /// Returns `(NOT a)`.
23    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    /// Returns `(NOT a)`.
32    fn not(self) -> Self::Output {
33        // The `NOT` operation behaves as follows:
34        //     Case 1: If `(self == 0)`, then `(1 - self) == 1`.
35        //     Case 2: If `(self == 1)`, then `(1 - self) == 0`.
36        match self.is_constant() {
37            // Constant case.
38            true => Boolean(E::one() - &self.0),
39            // Public and private cases.
40            // Note: We directly instantiate a public variable to correctly represent a boolean in a linear combination.
41            // For more information, see `LinearCombination::is_boolean_type`.
42            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        // NOT false
84        let expected = true;
85        let candidate_input = Boolean::<Circuit>::new(Mode::Constant, false);
86        check_not("NOT false", expected, candidate_input);
87
88        // NOT true
89        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        // NOT false
97        let expected = true;
98        let candidate_input = Boolean::<Circuit>::new(Mode::Public, false);
99        check_not("NOT false", expected, candidate_input);
100
101        // NOT true
102        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        // NOT false
110        let expected = true;
111        let candidate_input = Boolean::<Circuit>::new(Mode::Private, false);
112        check_not("NOT false", expected, candidate_input);
113
114        // NOT true
115        let expected = false;
116        let candidate_input = Boolean::<Circuit>::new(Mode::Private, true);
117        check_not("NOT true", expected, candidate_input);
118    }
119}