snarkvm_circuit_program/data/identifier/
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<A: Aleo> ToBits for Identifier<A> {
19    type Boolean = Boolean<A>;
20
21    /// Returns the little-endian bits of the identifier.
22    fn write_bits_le(&self, vec: &mut Vec<Self::Boolean>) {
23        (&self).write_bits_le(vec);
24    }
25
26    /// Returns the big-endian bits of the identifier.
27    fn write_bits_be(&self, vec: &mut Vec<Self::Boolean>) {
28        (&self).write_bits_be(vec);
29    }
30}
31
32impl<A: Aleo> ToBits for &Identifier<A> {
33    type Boolean = Boolean<A>;
34
35    /// Returns the little-endian bits of the identifier.
36    fn write_bits_le(&self, vec: &mut Vec<Self::Boolean>) {
37        let initial_len = vec.len();
38        self.0.write_bits_le(vec);
39        vec.truncate(initial_len + 8 * self.1 as usize);
40    }
41
42    /// Returns the big-endian bits of the identifier.
43    fn write_bits_be(&self, vec: &mut Vec<Self::Boolean>) {
44        let initial_len = vec.len();
45        self.write_bits_le(vec);
46        vec[initial_len..].reverse();
47    }
48}
49
50#[cfg(all(test, feature = "console"))]
51mod tests {
52    use super::*;
53    use crate::{Circuit, data::identifier::tests::sample_console_identifier};
54
55    use anyhow::Result;
56
57    const ITERATIONS: u64 = 100;
58
59    fn check_to_bits_le(num_constants: u64, num_public: u64, num_private: u64, num_constraints: u64) -> Result<()> {
60        for _ in 0..ITERATIONS {
61            // Initialize the console identifier.
62            let console_identifier = sample_console_identifier::<Circuit>()?;
63            // Initialize the circuit identifier.
64            let circuit_identifier = Identifier::<Circuit>::new(Mode::Constant, console_identifier);
65
66            Circuit::scope("Identifier ToBits", || {
67                let candidate = circuit_identifier.to_bits_le();
68                assert_eq!(Mode::Constant, candidate.eject_mode());
69                assert_eq!(console_identifier.to_bits_le(), candidate.eject_value());
70                assert_scope!(num_constants, num_public, num_private, num_constraints);
71            });
72            Circuit::reset();
73        }
74        Ok(())
75    }
76
77    fn check_to_bits_be(num_constants: u64, num_public: u64, num_private: u64, num_constraints: u64) -> Result<()> {
78        for _ in 0..ITERATIONS {
79            // Initialize the console identifier.
80            let console_identifier = sample_console_identifier::<Circuit>()?;
81            // Initialize the circuit identifier.
82            let circuit_identifier = Identifier::<Circuit>::new(Mode::Constant, console_identifier);
83
84            Circuit::scope("Identifier ToBits", || {
85                let candidate = circuit_identifier.to_bits_be();
86                assert_eq!(Mode::Constant, candidate.eject_mode());
87                assert_eq!(console_identifier.to_bits_be(), candidate.eject_value());
88                assert_scope!(num_constants, num_public, num_private, num_constraints);
89            });
90            Circuit::reset();
91        }
92        Ok(())
93    }
94
95    #[test]
96    fn test_to_bits_le() -> Result<()> {
97        check_to_bits_le(0, 0, 0, 0)
98    }
99
100    #[test]
101    fn test_to_bits_be() -> Result<()> {
102        check_to_bits_be(0, 0, 0, 0)
103    }
104}