to_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> ToBits for Identifier<N> { 22 /// Returns the little-endian bits of the identifier. 23 fn write_bits_le(&self, vec: &mut Vec<bool>) { 24 (&self).write_bits_le(vec); 25 } 26 27 /// Returns the big-endian bits of the identifier. 28 fn write_bits_be(&self, vec: &mut Vec<bool>) { 29 (&self).write_bits_be(vec); 30 } 31 } 32 33 impl<N: Network> ToBits for &Identifier<N> { 34 /// Returns the little-endian bits of the identifier. 35 fn write_bits_le(&self, vec: &mut Vec<bool>) { 36 let initial_len = vec.len(); 37 self.0.write_bits_le(vec); 38 vec.truncate(initial_len + 8 * self.1 as usize); 39 } 40 41 /// Returns the big-endian bits of the identifier. 42 fn write_bits_be(&self, vec: &mut Vec<bool>) { 43 let initial_len = vec.len(); 44 self.write_bits_le(vec); 45 vec[initial_len..].reverse(); 46 } 47 } 48 49 #[cfg(test)] 50 mod tests { 51 use super::*; 52 use crate::data::identifier::tests::sample_identifier_as_string; 53 use alphavm_console_network::MainnetV0; 54 55 type CurrentNetwork = MainnetV0; 56 57 const ITERATIONS: usize = 100; 58 59 #[test] 60 fn test_to_bits_le() -> Result<()> { 61 let mut rng = TestRng::default(); 62 63 for _ in 0..ITERATIONS { 64 // Sample a random fixed-length alphanumeric string, that always starts with an alphabetic character. 65 let expected_string = sample_identifier_as_string::<CurrentNetwork>(&mut rng)?; 66 // Recover the field element from the bits. 67 let expected_field = Field::<CurrentNetwork>::from_bits_le(&expected_string.to_bits_le())?; 68 69 let candidate = Identifier::<CurrentNetwork>::from_str(&expected_string)?; 70 assert_eq!(expected_field, candidate.0); 71 assert_eq!(expected_field.to_bits_le()[..expected_string.len() * 8], candidate.to_bits_le()); 72 } 73 Ok(()) 74 } 75 76 #[test] 77 fn test_to_bits_be() -> Result<()> { 78 let mut rng = TestRng::default(); 79 80 for _ in 0..ITERATIONS { 81 // Sample a random fixed-length alphanumeric string, that always starts with an alphabetic character. 82 let expected_string = sample_identifier_as_string::<CurrentNetwork>(&mut rng)?; 83 // Recover the field element from the bits. 84 let expected_field = Field::<CurrentNetwork>::from_bits_le(&expected_string.to_bits_le())?; 85 86 let candidate = Identifier::<CurrentNetwork>::from_str(&expected_string)?; 87 assert_eq!(expected_field, candidate.0); 88 assert_eq!( 89 expected_field.to_bits_le()[..expected_string.len() * 8].iter().rev().copied().collect::<Vec<_>>(), 90 candidate.to_bits_be() 91 ); 92 } 93 Ok(()) 94 } 95 }