/ console / program / src / data / identifier / bytes.rs
bytes.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> FromBytes for Identifier<N> {
22      /// Reads in an identifier from a buffer.
23      fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
24          // Read the number of bytes.
25          let size = u8::read_le(&mut reader)?;
26  
27          // Read the identifier bytes.
28          let mut buffer = vec![0u8; size as usize];
29          reader.read_exact(&mut buffer)?;
30  
31          // from_str the identifier.
32          // Note: `Self::from_str` ensures that the identifier string is not empty.
33          Self::from_str(&String::from_utf8(buffer).map_err(|e| error(format!("Failed to decode identifier: {e}")))?)
34              .map_err(|e| error(format!("{e}")))
35      }
36  }
37  
38  impl<N: Network> ToBytes for Identifier<N> {
39      /// Writes an identifier to a buffer.
40      fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
41          // Convert the identifier to a string.
42          let string = self.to_string();
43          if string.len() != self.1 as usize {
44              return Err(error("Identifier length does not match expected size"));
45          }
46  
47          // Ensure identifier fits within the data capacity of the base field.
48          let max_bytes = Field::<N>::size_in_data_bits() / 8; // Note: This intentionally rounds down.
49          if string.len() > max_bytes {
50              return Err(error(format!("Identifier is too large. Identifiers must be <= {max_bytes} bytes long")));
51          }
52  
53          // Write the identifier to a buffer.
54          u8::try_from(string.len()).or_halt_with::<N>("Invalid identifier length").write_le(&mut writer)?;
55          string.as_bytes().write_le(&mut writer)
56      }
57  }
58  
59  #[cfg(test)]
60  mod tests {
61      use super::*;
62      use crate::data::identifier::tests::sample_identifier;
63      use alphavm_console_network::MainnetV0;
64  
65      type CurrentNetwork = MainnetV0;
66  
67      const ITERATIONS: u64 = 1000;
68  
69      #[test]
70      fn test_bytes() -> Result<()> {
71          let mut rng = TestRng::default();
72  
73          for _ in 0..ITERATIONS {
74              // Sample a random fixed-length alphanumeric identifier, that always starts with an alphabetic character.
75              let expected = sample_identifier::<CurrentNetwork>(&mut rng)?;
76  
77              // Check the byte representation.
78              let expected_bytes = expected.to_bytes_le()?;
79              assert_eq!(expected, Identifier::read_le(&expected_bytes[..])?);
80          }
81          Ok(())
82      }
83  
84      #[test]
85      fn test_zero_identifier_fails() {
86          assert!(Identifier::<CurrentNetwork>::read_le(&[0u8; 1][..]).is_err())
87      }
88  }