serialize.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, I: IntegerType> Serialize for Integer<E, I> { 22 /// Serializes the integer into a string or as bytes. 23 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { 24 match serializer.is_human_readable() { 25 true => serializer.collect_str(self), 26 false => ToBytesSerializer::serialize(self, serializer), 27 } 28 } 29 } 30 31 impl<'de, E: Environment, I: IntegerType> Deserialize<'de> for Integer<E, I> { 32 /// Deserializes the integer from a string or bytes. 33 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { 34 match deserializer.is_human_readable() { 35 true => FromStr::from_str(&String::deserialize(deserializer)?).map_err(de::Error::custom), 36 false => FromBytesDeserializer::<Self>::deserialize(deserializer, "integer", Self::size_in_bytes()), 37 } 38 } 39 } 40 41 #[cfg(test)] 42 mod tests { 43 use super::*; 44 use alphavm_console_network_environment::Console; 45 46 type CurrentEnvironment = Console; 47 48 const ITERATIONS: u64 = 1000; 49 50 fn check_serde_json<I: IntegerType>(rng: &mut TestRng) -> Result<()> { 51 for _ in 0..ITERATIONS { 52 // Sample a new integer. 53 let expected = Integer::<CurrentEnvironment, I>::new(Uniform::rand(rng)); 54 55 // Serialize 56 let expected_string = &expected.to_string(); 57 let candidate_string = serde_json::to_string(&expected)?; 58 assert_eq!(expected_string, serde_json::Value::from_str(&candidate_string)?.as_str().unwrap()); 59 60 // Deserialize 61 assert_eq!(expected, Integer::from_str(expected_string)?); 62 assert_eq!(expected, serde_json::from_str(&candidate_string)?); 63 } 64 Ok(()) 65 } 66 67 fn check_bincode<I: IntegerType>(rng: &mut TestRng) -> Result<()> { 68 for _ in 0..ITERATIONS { 69 // Sample a new integer. 70 let expected = Integer::<CurrentEnvironment, I>::new(Uniform::rand(rng)); 71 72 // Serialize 73 let expected_bytes = expected.to_bytes_le()?; 74 assert_eq!(&expected_bytes[..], &bincode::serialize(&expected)?[..]); 75 76 // Deserialize 77 assert_eq!(expected, Integer::read_le(&expected_bytes[..])?); 78 assert_eq!(expected, bincode::deserialize(&expected_bytes[..])?); 79 } 80 Ok(()) 81 } 82 83 #[test] 84 fn test_serde_json() -> Result<()> { 85 let mut rng = TestRng::default(); 86 87 check_serde_json::<u8>(&mut rng)?; 88 check_serde_json::<u16>(&mut rng)?; 89 check_serde_json::<u32>(&mut rng)?; 90 check_serde_json::<u64>(&mut rng)?; 91 check_serde_json::<u128>(&mut rng)?; 92 93 check_serde_json::<i8>(&mut rng)?; 94 check_serde_json::<i16>(&mut rng)?; 95 check_serde_json::<i32>(&mut rng)?; 96 check_serde_json::<i64>(&mut rng)?; 97 check_serde_json::<i128>(&mut rng)?; 98 Ok(()) 99 } 100 101 #[test] 102 fn test_bincode() -> Result<()> { 103 let mut rng = TestRng::default(); 104 105 check_bincode::<u8>(&mut rng)?; 106 check_bincode::<u16>(&mut rng)?; 107 check_bincode::<u32>(&mut rng)?; 108 check_bincode::<u64>(&mut rng)?; 109 check_bincode::<u128>(&mut rng)?; 110 111 check_bincode::<i8>(&mut rng)?; 112 check_bincode::<i16>(&mut rng)?; 113 check_bincode::<i32>(&mut rng)?; 114 check_bincode::<i64>(&mut rng)?; 115 check_bincode::<i128>(&mut rng)?; 116 Ok(()) 117 } 118 }