/ console / program / src / data / identifier / from_bits.rs
from_bits.rs
 1  // Copyright (c) 2025-2026 ACDC Network
 2  // This file is part of the alphavm library.
 3  //
 4  // Alpha Chain | Delta Chain Protocol
 5  // International Monetary Graphite.
 6  //
 7  // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com).
 8  // They built world-class ZK infrastructure. We installed the EASY button.
 9  // Their cryptography: elegant. Our modifications: bureaucracy-compatible.
10  // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours.
11  //
12  // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
13  // All modifications and new work: CC0 1.0 Universal Public Domain Dedication.
14  // No rights reserved. No permission required. No warranty. No refunds.
15  //
16  // https://creativecommons.org/publicdomain/zero/1.0/
17  // SPDX-License-Identifier: CC0-1.0
18  
19  use super::*;
20  
21  impl<N: Network> FromBits for Identifier<N> {
22      /// Initializes a new identifier from a list of little-endian bits *without* trailing zeros.
23      fn from_bits_le(bits_le: &[bool]) -> Result<Self> {
24          // Ensure the number of bits does not exceed the size in bits of the field.
25          // This check is not sufficient to ensure the identifier is of valid size,
26          // the final step checks the byte-aligned field element is within the data capacity.
27          ensure!(bits_le.len() <= Field::<N>::size_in_bits(), "Identifier exceeds the maximum bits allowed");
28  
29          // Convert the bits to bytes, and parse the bytes as a UTF-8 string.
30          let bytes = bits_le.chunks(8).map(u8::from_bits_le).collect::<Result<Vec<u8>>>()?;
31  
32          // Recover the identifier length from the bits, by finding the first instance of a `0` byte,
33          // which is the null character '\0' in UTF-8, and an invalid character in an identifier.
34          let num_bytes = match bytes.iter().position(|&byte| byte == 0) {
35              Some(index) => index, // `index` is 0-indexed, and we exclude the null character.
36              None => bytes.len(),  // No null character found, so the identifier is the full length.
37          };
38  
39          // Parse the bytes as a UTF-8 string.
40          Self::from_str(str::from_utf8(&bytes[0..num_bytes])?)
41      }
42  
43      /// Initializes a new identifier from a list of big-endian bits *without* leading zeros.
44      fn from_bits_be(bits_be: &[bool]) -> Result<Self> {
45          Self::from_bits_le(&bits_be.iter().rev().copied().collect::<Vec<bool>>())
46      }
47  }
48  
49  #[cfg(test)]
50  mod tests {
51      use super::*;
52      use crate::data::identifier::tests::sample_identifier;
53      use alphavm_console_network::MainnetV0;
54  
55      type CurrentNetwork = MainnetV0;
56  
57      const ITERATIONS: usize = 100;
58  
59      #[test]
60      fn test_from_bits_le() -> Result<()> {
61          let mut rng = TestRng::default();
62  
63          for _ in 0..ITERATIONS {
64              // Sample a random fixed-length alphanumeric identifier, that always starts with an alphabetic character.
65              let identifier = sample_identifier::<CurrentNetwork>(&mut rng)?;
66              assert_eq!(identifier, Identifier::from_bits_le(&identifier.to_bits_le())?);
67          }
68          Ok(())
69      }
70  
71      #[test]
72      fn test_from_bits_be() -> Result<()> {
73          let mut rng = TestRng::default();
74  
75          for _ in 0..ITERATIONS {
76              // Sample a random fixed-length alphanumeric identifier, that always starts with an alphabetic character.
77              let identifier = sample_identifier::<CurrentNetwork>(&mut rng)?;
78              assert_eq!(identifier, Identifier::from_bits_be(&identifier.to_bits_be())?);
79          }
80          Ok(())
81      }
82  }