to_bits.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the alphavm library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use super::*; 17 18 impl<N: Network> ToBits for Identifier<N> { 19 /// Returns the little-endian bits of the identifier. 20 fn write_bits_le(&self, vec: &mut Vec<bool>) { 21 (&self).write_bits_le(vec); 22 } 23 24 /// Returns the big-endian bits of the identifier. 25 fn write_bits_be(&self, vec: &mut Vec<bool>) { 26 (&self).write_bits_be(vec); 27 } 28 } 29 30 impl<N: Network> ToBits for &Identifier<N> { 31 /// Returns the little-endian bits of the identifier. 32 fn write_bits_le(&self, vec: &mut Vec<bool>) { 33 let initial_len = vec.len(); 34 self.0.write_bits_le(vec); 35 vec.truncate(initial_len + 8 * self.1 as usize); 36 } 37 38 /// Returns the big-endian bits of the identifier. 39 fn write_bits_be(&self, vec: &mut Vec<bool>) { 40 let initial_len = vec.len(); 41 self.write_bits_le(vec); 42 vec[initial_len..].reverse(); 43 } 44 } 45 46 #[cfg(test)] 47 mod tests { 48 use super::*; 49 use crate::data::identifier::tests::sample_identifier_as_string; 50 use alphavm_console_network::MainnetV0; 51 52 type CurrentNetwork = MainnetV0; 53 54 const ITERATIONS: usize = 100; 55 56 #[test] 57 fn test_to_bits_le() -> Result<()> { 58 let mut rng = TestRng::default(); 59 60 for _ in 0..ITERATIONS { 61 // Sample a random fixed-length alphanumeric string, that always starts with an alphabetic character. 62 let expected_string = sample_identifier_as_string::<CurrentNetwork>(&mut rng)?; 63 // Recover the field element from the bits. 64 let expected_field = Field::<CurrentNetwork>::from_bits_le(&expected_string.to_bits_le())?; 65 66 let candidate = Identifier::<CurrentNetwork>::from_str(&expected_string)?; 67 assert_eq!(expected_field, candidate.0); 68 assert_eq!(expected_field.to_bits_le()[..expected_string.len() * 8], candidate.to_bits_le()); 69 } 70 Ok(()) 71 } 72 73 #[test] 74 fn test_to_bits_be() -> Result<()> { 75 let mut rng = TestRng::default(); 76 77 for _ in 0..ITERATIONS { 78 // Sample a random fixed-length alphanumeric string, that always starts with an alphabetic character. 79 let expected_string = sample_identifier_as_string::<CurrentNetwork>(&mut rng)?; 80 // Recover the field element from the bits. 81 let expected_field = Field::<CurrentNetwork>::from_bits_le(&expected_string.to_bits_le())?; 82 83 let candidate = Identifier::<CurrentNetwork>::from_str(&expected_string)?; 84 assert_eq!(expected_field, candidate.0); 85 assert_eq!( 86 expected_field.to_bits_le()[..expected_string.len() * 8].iter().rev().copied().collect::<Vec<_>>(), 87 candidate.to_bits_be() 88 ); 89 } 90 Ok(()) 91 } 92 }