from_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<E: Environment> FromBits for Boolean<E> { 22 /// Initializes a new boolean by extracting the first bit from a list of length 1. 23 fn from_bits_le(bits_le: &[bool]) -> Result<Self> { 24 match bits_le.len().is_one() { 25 true => Ok(Boolean::new(bits_le[0])), 26 false => bail!("Boolean::from_bits_le expects a list of one boolean, found {}", bits_le.len()), 27 } 28 } 29 30 /// Initializes a new boolean by extracting the first bit from a list of length 1. 31 fn from_bits_be(bits_be: &[bool]) -> Result<Self> { 32 match bits_be.len().is_one() { 33 true => Ok(Boolean::new(bits_be[0])), 34 false => bail!("Boolean::from_bits_be expects a list of one boolean, found {}", bits_be.len()), 35 } 36 } 37 } 38 39 #[cfg(test)] 40 mod tests { 41 use super::*; 42 use alphavm_console_network_environment::Console; 43 44 type CurrentEnvironment = Console; 45 46 const ITERATIONS: usize = 100; 47 48 fn check_from_bits_le() -> Result<()> { 49 let mut rng = TestRng::default(); 50 51 for i in 1..ITERATIONS { 52 // Sample a random element. 53 let expected: Boolean<CurrentEnvironment> = Uniform::rand(&mut rng); 54 let given_bits = expected.to_bits_le(); 55 assert_eq!(Boolean::<CurrentEnvironment>::size_in_bits(), given_bits.len()); 56 57 let candidate = Boolean::<CurrentEnvironment>::from_bits_le(&given_bits)?; 58 assert_eq!(expected, candidate); 59 60 // Add excess zero bits. 61 let candidate = [given_bits, vec![false; i]].concat(); 62 assert!(Boolean::<CurrentEnvironment>::from_bits_le(&candidate).is_err()); 63 } 64 Ok(()) 65 } 66 67 fn check_from_bits_be() -> Result<()> { 68 let mut rng = TestRng::default(); 69 70 for i in 1..ITERATIONS { 71 // Sample a random element. 72 let expected: Boolean<CurrentEnvironment> = Uniform::rand(&mut rng); 73 let given_bits = expected.to_bits_be(); 74 assert_eq!(Boolean::<CurrentEnvironment>::size_in_bits(), given_bits.len()); 75 76 let candidate = Boolean::<CurrentEnvironment>::from_bits_be(&given_bits)?; 77 assert_eq!(expected, candidate); 78 79 // Add excess zero bits. 80 let candidate = [vec![false; i], given_bits].concat(); 81 assert!(Boolean::<CurrentEnvironment>::from_bits_be(&candidate).is_err()); 82 } 83 Ok(()) 84 } 85 86 #[test] 87 fn test_from_bits_le() -> Result<()> { 88 check_from_bits_le() 89 } 90 91 #[test] 92 fn test_from_bits_be() -> Result<()> { 93 check_from_bits_be() 94 } 95 }