snarkvm_console_types_boolean/
from_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> FromBits for Boolean<E> {
19    /// Initializes a new boolean by extracting the first bit from a list of length 1.
20    fn from_bits_le(bits_le: &[bool]) -> Result<Self> {
21        match bits_le.len().is_one() {
22            true => Ok(Boolean::new(bits_le[0])),
23            false => bail!("Boolean::from_bits_le expects a list of one boolean, found {}", bits_le.len()),
24        }
25    }
26
27    /// Initializes a new boolean by extracting the first bit from a list of length 1.
28    fn from_bits_be(bits_be: &[bool]) -> Result<Self> {
29        match bits_be.len().is_one() {
30            true => Ok(Boolean::new(bits_be[0])),
31            false => bail!("Boolean::from_bits_be expects a list of one boolean, found {}", bits_be.len()),
32        }
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use snarkvm_console_network_environment::Console;
40
41    type CurrentEnvironment = Console;
42
43    const ITERATIONS: usize = 100;
44
45    fn check_from_bits_le() -> Result<()> {
46        let mut rng = TestRng::default();
47
48        for i in 1..ITERATIONS {
49            // Sample a random element.
50            let expected: Boolean<CurrentEnvironment> = Uniform::rand(&mut rng);
51            let given_bits = expected.to_bits_le();
52            assert_eq!(Boolean::<CurrentEnvironment>::size_in_bits(), given_bits.len());
53
54            let candidate = Boolean::<CurrentEnvironment>::from_bits_le(&given_bits)?;
55            assert_eq!(expected, candidate);
56
57            // Add excess zero bits.
58            let candidate = [given_bits, vec![false; i]].concat();
59            assert!(Boolean::<CurrentEnvironment>::from_bits_le(&candidate).is_err());
60        }
61        Ok(())
62    }
63
64    fn check_from_bits_be() -> Result<()> {
65        let mut rng = TestRng::default();
66
67        for i in 1..ITERATIONS {
68            // Sample a random element.
69            let expected: Boolean<CurrentEnvironment> = Uniform::rand(&mut rng);
70            let given_bits = expected.to_bits_be();
71            assert_eq!(Boolean::<CurrentEnvironment>::size_in_bits(), given_bits.len());
72
73            let candidate = Boolean::<CurrentEnvironment>::from_bits_be(&given_bits)?;
74            assert_eq!(expected, candidate);
75
76            // Add excess zero bits.
77            let candidate = [vec![false; i], given_bits].concat();
78            assert!(Boolean::<CurrentEnvironment>::from_bits_be(&candidate).is_err());
79        }
80        Ok(())
81    }
82
83    #[test]
84    fn test_from_bits_le() -> Result<()> {
85        check_from_bits_le()
86    }
87
88    #[test]
89    fn test_from_bits_be() -> Result<()> {
90        check_from_bits_be()
91    }
92}