snarkvm_circuit_types_boolean/helpers/
to_bits.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::*;
17
18impl<E: Environment> ToBits for Boolean<E> {
19    type Boolean = Boolean<E>;
20
21    /// Outputs `self` as a single-element vector.
22    fn write_bits_le(&self, vec: &mut Vec<Self::Boolean>) {
23        (&self).write_bits_le(vec);
24    }
25
26    /// Outputs `self` as a single-element vector.
27    fn write_bits_be(&self, vec: &mut Vec<Self::Boolean>) {
28        (&self).write_bits_be(vec);
29    }
30}
31
32impl<E: Environment> ToBits for &Boolean<E> {
33    type Boolean = Boolean<E>;
34
35    /// Outputs `self` as a single-element vector.
36    fn write_bits_le(&self, vec: &mut Vec<Self::Boolean>) {
37        vec.push((*self).clone());
38    }
39
40    /// Outputs `self` as a single-element vector.
41    fn write_bits_be(&self, vec: &mut Vec<Self::Boolean>) {
42        vec.push((*self).clone());
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49    use snarkvm_circuit_environment::Circuit;
50
51    fn check_to_bits_le(name: &str, expected: &[bool], candidate: &Boolean<Circuit>) {
52        Circuit::scope(name, || {
53            let candidate = candidate.to_bits_le();
54            assert_eq!(1, candidate.len());
55            for (expected_bit, candidate_bit) in expected.iter().zip_eq(candidate.iter()) {
56                assert_eq!(*expected_bit, candidate_bit.eject_value());
57            }
58            assert_scope!(0, 0, 0, 0);
59        });
60    }
61
62    fn check_to_bits_be(name: &str, expected: &[bool], candidate: Boolean<Circuit>) {
63        Circuit::scope(name, || {
64            let candidate = candidate.to_bits_be();
65            assert_eq!(1, candidate.len());
66            for (expected_bit, candidate_bit) in expected.iter().zip_eq(candidate.iter()) {
67                assert_eq!(*expected_bit, candidate_bit.eject_value());
68            }
69            assert_scope!(0, 0, 0, 0);
70        });
71    }
72
73    #[test]
74    fn test_to_bits_constant() {
75        let candidate = Boolean::<Circuit>::new(Mode::Constant, true);
76        check_to_bits_le("Constant", &[true], &candidate);
77        check_to_bits_be("Constant", &[true], candidate);
78
79        let candidate = Boolean::<Circuit>::new(Mode::Constant, false);
80        check_to_bits_le("Constant", &[false], &candidate);
81        check_to_bits_be("Constant", &[false], candidate);
82    }
83
84    #[test]
85    fn test_to_bits_public() {
86        let candidate = Boolean::<Circuit>::new(Mode::Public, true);
87        check_to_bits_le("Public", &[true], &candidate);
88        check_to_bits_be("Public", &[true], candidate);
89
90        let candidate = Boolean::<Circuit>::new(Mode::Public, false);
91        check_to_bits_le("Public", &[false], &candidate);
92        check_to_bits_be("Public", &[false], candidate);
93    }
94
95    #[test]
96    fn test_to_bits_private() {
97        let candidate = Boolean::<Circuit>::new(Mode::Private, true);
98        check_to_bits_le("Private", &[true], &candidate);
99        check_to_bits_be("Private", &[true], candidate);
100
101        let candidate = Boolean::<Circuit>::new(Mode::Private, false);
102        check_to_bits_le("Private", &[false], &candidate);
103        check_to_bits_be("Private", &[false], candidate);
104    }
105}