snarkvm_circuit_program/id/
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 ProgramID<A> {
19    type Boolean = Boolean<A>;
20
21    /// Returns the little-endian bits of the program ID.
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 program ID.
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 &ProgramID<A> {
33    type Boolean = Boolean<A>;
34
35    /// Returns the little-endian bits of the program ID.
36    fn write_bits_le(&self, vec: &mut Vec<Self::Boolean>) {
37        self.name().write_bits_le(vec);
38        self.network().write_bits_le(vec);
39    }
40
41    /// Returns the big-endian bits of the program ID.
42    fn write_bits_be(&self, vec: &mut Vec<Self::Boolean>) {
43        self.name().write_bits_be(vec);
44        self.network().write_bits_be(vec);
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    use crate::{Circuit, data::identifier::tests::sample_lowercase_console_identifier_as_string};
52
53    use anyhow::Result;
54
55    const ITERATIONS: usize = 100;
56
57    fn check_to_bits_le(mode: Mode) -> Result<()> {
58        for _ in 0..ITERATIONS {
59            // Sample a random fixed-length alphanumeric string, that always starts with an alphabetic character.
60            let expected_name_string = sample_lowercase_console_identifier_as_string::<Circuit>()?;
61            let expected = console::ProgramID::<<Circuit as Environment>::Network>::from_str(&format!(
62                "{expected_name_string}.aleo"
63            ))?;
64
65            let candidate = ProgramID::<Circuit>::new(mode, expected);
66            assert_eq!(expected.to_bits_le(), candidate.to_bits_le().eject_value());
67        }
68        Ok(())
69    }
70
71    fn check_to_bits_be(mode: Mode) -> Result<()> {
72        for _ in 0..ITERATIONS {
73            // Sample a random fixed-length alphanumeric string, that always starts with an alphabetic character.
74            let expected_name_string = sample_lowercase_console_identifier_as_string::<Circuit>()?;
75            let expected = console::ProgramID::<<Circuit as Environment>::Network>::from_str(&format!(
76                "{expected_name_string}.aleo"
77            ))?;
78
79            let candidate = ProgramID::<Circuit>::new(mode, expected);
80            assert_eq!(expected.to_bits_be(), candidate.to_bits_be().eject_value());
81        }
82        Ok(())
83    }
84
85    #[test]
86    fn test_to_bits_le() -> Result<()> {
87        check_to_bits_le(Mode::Constant)?;
88        check_to_bits_le(Mode::Public)?;
89        check_to_bits_le(Mode::Private)?;
90        Ok(())
91    }
92
93    #[test]
94    fn test_to_bits_be() -> Result<()> {
95        check_to_bits_be(Mode::Constant)?;
96        check_to_bits_be(Mode::Public)?;
97        check_to_bits_be(Mode::Private)?;
98        Ok(())
99    }
100}