/ console / types / string / src / 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<E: Environment> FromBytes for StringType<E> {
22      /// Reads the string from a buffer.
23      #[inline]
24      fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
25          // Read the number of bytes.
26          let num_bytes = u16::read_le(&mut reader)?;
27          // Ensure the number of bytes is within the allowed bounds.
28          if num_bytes as u32 > E::MAX_STRING_BYTES {
29              return Err(error(format!("String literal exceeds maximum length of {} bytes.", E::MAX_STRING_BYTES)));
30          }
31          // Read the bytes.
32          let mut bytes = vec![0u8; num_bytes as usize];
33          reader.read_exact(&mut bytes)?;
34          // Return the string.
35          Ok(Self::new(&String::from_utf8(bytes).map_err(error)?))
36      }
37  }
38  
39  impl<E: Environment> ToBytes for StringType<E> {
40      /// Writes the string to a buffer.
41      #[inline]
42      fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
43          // Ensure the number of bytes is within the allowed bounds.
44          if self.string.len() > E::MAX_STRING_BYTES as usize {
45              return Err(error(format!("String literal exceeds maximum length of {} bytes.", E::MAX_STRING_BYTES)));
46          }
47          // Write the number of bytes.
48          u16::try_from(self.string.len()).or_halt_with::<E>("String exceeds u16::MAX bytes").write_le(&mut writer)?;
49          // Write the bytes.
50          self.string.as_bytes().write_le(&mut writer)
51      }
52  }
53  
54  #[cfg(test)]
55  mod tests {
56      use super::*;
57      use alphavm_console_network_environment::Console;
58  
59      type CurrentEnvironment = Console;
60  
61      const ITERATIONS: u64 = 10_000;
62  
63      #[test]
64      fn test_bytes() -> Result<()> {
65          let mut rng = TestRng::default();
66  
67          for _ in 0..ITERATIONS {
68              // Sample a new string.
69              let expected = StringType::<CurrentEnvironment>::rand(&mut rng);
70  
71              // Check the byte representation.
72              let expected_bytes = expected.to_bytes_le()?;
73              assert_eq!(expected, StringType::read_le(&expected_bytes[..])?);
74          }
75          Ok(())
76      }
77  }