Skip to main content

snarkvm_console_types_boolean/
to_bits.rs

1// Copyright (c) 2019-2026 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    /// Outputs `self` in a vector.
20    fn write_bits_le(&self, vec: &mut Vec<bool>) {
21        vec.push(**self);
22    }
23
24    /// Outputs `self` in a vector.
25    fn write_bits_be(&self, vec: &mut Vec<bool>) {
26        vec.push(**self);
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use snarkvm_console_network_environment::Console;
34
35    type CurrentEnvironment = Console;
36
37    const ITERATIONS: u64 = 10_000;
38
39    #[test]
40    fn test_to_bits_le() {
41        let mut rng = TestRng::default();
42
43        for _ in 0..ITERATIONS {
44            // Sample a random value.
45            let boolean: Boolean<CurrentEnvironment> = Uniform::rand(&mut rng);
46
47            let candidate = boolean.to_bits_le();
48            assert_eq!(vec![*boolean], candidate);
49            assert_eq!(Boolean::<CurrentEnvironment>::size_in_bits(), candidate.len());
50        }
51    }
52
53    #[test]
54    fn test_to_bits_be() {
55        let mut rng = TestRng::default();
56
57        for _ in 0..ITERATIONS {
58            // Sample a random value.
59            let boolean: Boolean<CurrentEnvironment> = Uniform::rand(&mut rng);
60
61            let candidate = boolean.to_bits_be();
62            assert_eq!(vec![*boolean], candidate);
63            assert_eq!(Boolean::<CurrentEnvironment>::size_in_bits(), candidate.len());
64        }
65    }
66}