Skip to main content

snarkvm_circuit_program/id/
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<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 = 10;
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 = match mode {
66                Mode::Constant => ProgramID::<Circuit>::constant(expected),
67                Mode::Public => ProgramID::<Circuit>::public(expected),
68                Mode::Private => panic!("ProgramID cannot be private"),
69            };
70            assert_eq!(expected.to_bits_le(), candidate.to_bits_le().eject_value());
71        }
72        Ok(())
73    }
74
75    fn check_to_bits_be(mode: Mode) -> Result<()> {
76        for _ in 0..ITERATIONS {
77            // Sample a random fixed-length alphanumeric string, that always starts with an alphabetic character.
78            let expected_name_string = sample_lowercase_console_identifier_as_string::<Circuit>()?;
79            let expected = console::ProgramID::<<Circuit as Environment>::Network>::from_str(&format!(
80                "{expected_name_string}.aleo"
81            ))?;
82
83            let candidate = match mode {
84                Mode::Constant => ProgramID::<Circuit>::constant(expected),
85                Mode::Public => ProgramID::<Circuit>::public(expected),
86                Mode::Private => panic!("ProgramID cannot be private"),
87            };
88            assert_eq!(expected.to_bits_be(), candidate.to_bits_be().eject_value());
89        }
90        Ok(())
91    }
92
93    #[test]
94    fn test_to_bits_le() -> Result<()> {
95        check_to_bits_le(Mode::Constant)?;
96        check_to_bits_le(Mode::Public)?;
97        Ok(())
98    }
99
100    #[test]
101    fn test_to_bits_be() -> Result<()> {
102        check_to_bits_be(Mode::Constant)?;
103        check_to_bits_be(Mode::Public)?;
104        Ok(())
105    }
106}