from_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> FromBits for Identifier<N> { 19 /// Initializes a new identifier from a list of little-endian bits *without* trailing zeros. 20 fn from_bits_le(bits_le: &[bool]) -> Result<Self> { 21 // Ensure the number of bits does not exceed the size in bits of the field. 22 // This check is not sufficient to ensure the identifier is of valid size, 23 // the final step checks the byte-aligned field element is within the data capacity. 24 ensure!(bits_le.len() <= Field::<N>::size_in_bits(), "Identifier exceeds the maximum bits allowed"); 25 26 // Convert the bits to bytes, and parse the bytes as a UTF-8 string. 27 let bytes = bits_le.chunks(8).map(u8::from_bits_le).collect::<Result<Vec<u8>>>()?; 28 29 // Recover the identifier length from the bits, by finding the first instance of a `0` byte, 30 // which is the null character '\0' in UTF-8, and an invalid character in an identifier. 31 let num_bytes = match bytes.iter().position(|&byte| byte == 0) { 32 Some(index) => index, // `index` is 0-indexed, and we exclude the null character. 33 None => bytes.len(), // No null character found, so the identifier is the full length. 34 }; 35 36 // Parse the bytes as a UTF-8 string. 37 Self::from_str(str::from_utf8(&bytes[0..num_bytes])?) 38 } 39 40 /// Initializes a new identifier from a list of big-endian bits *without* leading zeros. 41 fn from_bits_be(bits_be: &[bool]) -> Result<Self> { 42 Self::from_bits_le(&bits_be.iter().rev().copied().collect::<Vec<bool>>()) 43 } 44 } 45 46 #[cfg(test)] 47 mod tests { 48 use super::*; 49 use crate::data::identifier::tests::sample_identifier; 50 use alphavm_console_network::MainnetV0; 51 52 type CurrentNetwork = MainnetV0; 53 54 const ITERATIONS: usize = 100; 55 56 #[test] 57 fn test_from_bits_le() -> Result<()> { 58 let mut rng = TestRng::default(); 59 60 for _ in 0..ITERATIONS { 61 // Sample a random fixed-length alphanumeric identifier, that always starts with an alphabetic character. 62 let identifier = sample_identifier::<CurrentNetwork>(&mut rng)?; 63 assert_eq!(identifier, Identifier::from_bits_le(&identifier.to_bits_le())?); 64 } 65 Ok(()) 66 } 67 68 #[test] 69 fn test_from_bits_be() -> Result<()> { 70 let mut rng = TestRng::default(); 71 72 for _ in 0..ITERATIONS { 73 // Sample a random fixed-length alphanumeric identifier, that always starts with an alphabetic character. 74 let identifier = sample_identifier::<CurrentNetwork>(&mut rng)?; 75 assert_eq!(identifier, Identifier::from_bits_be(&identifier.to_bits_be())?); 76 } 77 Ok(()) 78 } 79 }