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 PuzzleSolutions<N> { 22 /// Reads the solutions from the buffer. 23 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> { 24 // Read the number of solutions. 25 let num_solutions: u8 = FromBytes::read_le(&mut reader)?; 26 // Ensure the number of solutions is within bounds. 27 if num_solutions as usize > N::MAX_SOLUTIONS { 28 return Err(error("Failed to read solutions: too many solutions")); 29 } 30 // Read the solutions. 31 let mut solutions = Vec::with_capacity(num_solutions as usize); 32 for _ in 0..num_solutions { 33 solutions.push(Solution::read_le(&mut reader)?); 34 } 35 // Return the solutions. 36 Self::new(solutions).map_err(error) 37 } 38 } 39 40 impl<N: Network> ToBytes for PuzzleSolutions<N> { 41 /// Writes the solutions to the buffer. 42 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> { 43 // Write the number of solutions. 44 (u8::try_from(self.solutions.len()).map_err(error)?).write_le(&mut writer)?; 45 // Write the solutions. 46 for solution in self.solutions.values() { 47 solution.write_le(&mut writer)?; 48 } 49 Ok(()) 50 } 51 } 52 53 #[cfg(test)] 54 mod tests { 55 use super::*; 56 57 #[test] 58 fn test_bytes() -> Result<()> { 59 let mut rng = TestRng::default(); 60 61 // Sample random solutions. 62 let expected = crate::solutions::serialize::tests::sample_solutions(&mut rng); 63 64 // Check the byte representation. 65 let expected_bytes = expected.to_bytes_le()?; 66 assert_eq!(expected, PuzzleSolutions::read_le(&expected_bytes[..])?); 67 Ok(()) 68 } 69 }