1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use super::*;
impl<A: Aleo> FromBits for Identifier<A> {
    type Boolean = Boolean<A>;
    fn from_bits_le(bits_le: &[Self::Boolean]) -> Self {
        debug_assert!(bits_le.len() <= A::BaseField::size_in_bits(), "Identifier exceeds the maximum bits allowed");
        let field = Field::from_bits_le(bits_le);
        let num_bytes = match console::Identifier::<A::Network>::from_bits_le(&bits_le.eject_value()) {
            Ok(console_identifier) => console_identifier.size_in_bits() / 8,
            Err(error) => A::halt(format!("Failed to recover an identifier from bits: {error}")),
        };
        let max_bytes = A::BaseField::size_in_data_bits() / 8; match num_bytes as usize <= max_bytes {
            true => Self(field, num_bytes),
            false => A::halt("Identifier exceeds the maximum capacity allowed"),
        }
    }
    fn from_bits_be(bits_be: &[Self::Boolean]) -> Self {
        Self::from_bits_le(bits_be.iter().rev().cloned().collect::<Vec<_>>().as_slice())
    }
}
#[cfg(all(test, console))]
mod tests {
    use super::*;
    use crate::{data::identifier::tests::sample_console_identifier, Circuit};
    use anyhow::Result;
    const ITERATIONS: u64 = 100;
    fn check_from_bits_le(num_constants: u64, num_public: u64, num_private: u64, num_constraints: u64) -> Result<()> {
        for _ in 0..ITERATIONS {
            let console_identifier = sample_console_identifier::<Circuit>()?;
            let circuit_bits: Vec<_> = Inject::constant(console_identifier.to_bits_le());
            Circuit::scope("Identifier FromBits", || {
                let candidate = Identifier::<Circuit>::from_bits_le(&circuit_bits);
                assert_eq!(Mode::Constant, candidate.eject_mode());
                assert_eq!(console_identifier, candidate.eject_value());
                assert_scope!(num_constants, num_public, num_private, num_constraints);
            });
            Circuit::reset();
        }
        Ok(())
    }
    fn check_from_bits_be(num_constants: u64, num_public: u64, num_private: u64, num_constraints: u64) -> Result<()> {
        for _ in 0..ITERATIONS {
            let console_identifier = sample_console_identifier::<Circuit>()?;
            let circuit_bits: Vec<_> = Inject::constant(console_identifier.to_bits_be());
            Circuit::scope("Identifier FromBits", || {
                let candidate = Identifier::<Circuit>::from_bits_be(&circuit_bits);
                assert_eq!(Mode::Constant, candidate.eject_mode());
                assert_eq!(console_identifier, candidate.eject_value());
                assert_scope!(num_constants, num_public, num_private, num_constraints);
            });
            Circuit::reset();
        }
        Ok(())
    }
    #[test]
    fn test_from_bits_le() -> Result<()> {
        check_from_bits_le(0, 0, 0, 0)
    }
    #[test]
    fn test_from_bits_be() -> Result<()> {
        check_from_bits_be(0, 0, 0, 0)
    }
}