snarkvm_console_types_integers/
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, I: IntegerType> ToBits for Integer<E, I> {
19    /// Outputs the little-endian bit representation of `self` *without* trailing zeros.
20    fn write_bits_le(&self, vec: &mut Vec<bool>) {
21        (**self).write_bits_le(vec);
22    }
23
24    /// Outputs the big-endian bit representation of `self` *without* leading zeros.
25    fn write_bits_be(&self, vec: &mut Vec<bool>) {
26        (**self).write_bits_be(vec);
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    fn check_to_bits_le<I: IntegerType>(rng: &mut TestRng) {
40        for _ in 0..ITERATIONS {
41            // Sample a random value.
42            let integer: Integer<CurrentEnvironment, I> = Uniform::rand(rng);
43
44            let candidate = integer.to_bits_le();
45            assert_eq!(Integer::<CurrentEnvironment, I>::size_in_bits(), candidate.len());
46
47            for (expected, candidate) in (*integer).to_bits_le().iter().zip_eq(&candidate) {
48                assert_eq!(expected, candidate);
49            }
50        }
51    }
52
53    fn check_to_bits_be<I: IntegerType>(rng: &mut TestRng) {
54        for _ in 0..ITERATIONS {
55            // Sample a random value.
56            let integer: Integer<CurrentEnvironment, I> = Uniform::rand(rng);
57
58            let candidate = integer.to_bits_be();
59            assert_eq!(Integer::<CurrentEnvironment, I>::size_in_bits(), candidate.len());
60
61            for (expected, candidate) in (*integer).to_bits_be().iter().zip_eq(&candidate) {
62                assert_eq!(expected, candidate);
63            }
64        }
65    }
66
67    #[test]
68    fn test_to_bits_le() {
69        let mut rng = TestRng::default();
70
71        check_to_bits_le::<u8>(&mut rng);
72        check_to_bits_le::<u16>(&mut rng);
73        check_to_bits_le::<u32>(&mut rng);
74        check_to_bits_le::<u64>(&mut rng);
75        check_to_bits_le::<u128>(&mut rng);
76
77        check_to_bits_le::<i8>(&mut rng);
78        check_to_bits_le::<i16>(&mut rng);
79        check_to_bits_le::<i32>(&mut rng);
80        check_to_bits_le::<i64>(&mut rng);
81        check_to_bits_le::<i128>(&mut rng);
82    }
83
84    #[test]
85    fn test_to_bits_be() {
86        let mut rng = TestRng::default();
87
88        check_to_bits_be::<u8>(&mut rng);
89        check_to_bits_be::<u16>(&mut rng);
90        check_to_bits_be::<u32>(&mut rng);
91        check_to_bits_be::<u64>(&mut rng);
92        check_to_bits_be::<u128>(&mut rng);
93
94        check_to_bits_be::<i8>(&mut rng);
95        check_to_bits_be::<i16>(&mut rng);
96        check_to_bits_be::<i32>(&mut rng);
97        check_to_bits_be::<i64>(&mut rng);
98        check_to_bits_be::<i128>(&mut rng);
99    }
100}