from_bits.rs
1 // Copyright (c) 2025 ADnet Contributors 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<E: Environment> FromBits for Boolean<E> { 19 type Boolean = Boolean<E>; 20 21 /// Returns a boolean circuit given a mode and single boolean. 22 fn from_bits_le(bits_le: &[Self::Boolean]) -> Self { 23 // Ensure there is exactly one boolean in the list of booleans. 24 match bits_le.len() == 1 { 25 true => bits_le[0].clone(), 26 false => E::halt(format!("Attempted to instantiate a boolean with {} bits", bits_le.len())), 27 } 28 } 29 30 /// Returns a boolean circuit given a mode and single boolean. 31 fn from_bits_be(bits_be: &[Self::Boolean]) -> Self { 32 // Ensure there is exactly one boolean in the list of booleans. 33 match bits_be.len() == 1 { 34 true => bits_be[0].clone(), 35 false => E::halt(format!("Attempted to instantiate a boolean with {} bits", bits_be.len())), 36 } 37 } 38 } 39 40 #[cfg(test)] 41 mod tests { 42 use super::*; 43 use alphavm_circuit_environment::Circuit; 44 45 fn check_from_bits_le( 46 name: &str, 47 expected: bool, 48 candidate: &Boolean<Circuit>, 49 num_constants: u64, 50 num_public: u64, 51 num_private: u64, 52 num_constraints: u64, 53 ) { 54 Circuit::scope(name, || { 55 let candidate = Boolean::from_bits_le(&[(*candidate).clone()]); 56 assert_eq!(expected, candidate.eject_value()); 57 assert_scope!(num_constants, num_public, num_private, num_constraints); 58 }); 59 Circuit::reset(); 60 } 61 62 fn check_from_bits_be( 63 name: &str, 64 expected: bool, 65 candidate: &Boolean<Circuit>, 66 num_constants: u64, 67 num_public: u64, 68 num_private: u64, 69 num_constraints: u64, 70 ) { 71 Circuit::scope(name, || { 72 let candidate = Boolean::from_bits_be(&[(*candidate).clone()]); 73 assert_eq!(expected, candidate.eject_value()); 74 assert_scope!(num_constants, num_public, num_private, num_constraints); 75 }); 76 Circuit::reset(); 77 } 78 79 #[test] 80 fn test_from_bits_constant() { 81 let candidate = Boolean::<Circuit>::new(Mode::Constant, true); 82 check_from_bits_le("Constant", true, &candidate, 0, 0, 0, 0); 83 check_from_bits_be("Constant", true, &candidate, 0, 0, 0, 0); 84 85 let candidate = Boolean::<Circuit>::new(Mode::Constant, false); 86 check_from_bits_le("Constant", false, &candidate, 0, 0, 0, 0); 87 check_from_bits_be("Constant", false, &candidate, 0, 0, 0, 0); 88 } 89 90 #[test] 91 fn test_from_bits_public() { 92 let candidate = Boolean::<Circuit>::new(Mode::Public, true); 93 check_from_bits_le("Public", true, &candidate, 0, 0, 0, 0); 94 check_from_bits_be("Public", true, &candidate, 0, 0, 0, 0); 95 96 let candidate = Boolean::<Circuit>::new(Mode::Public, false); 97 check_from_bits_le("Public", false, &candidate, 0, 0, 0, 0); 98 check_from_bits_be("Public", false, &candidate, 0, 0, 0, 0); 99 } 100 101 #[test] 102 fn test_from_bits_private() { 103 let candidate = Boolean::<Circuit>::new(Mode::Private, true); 104 check_from_bits_le("Private", true, &candidate, 0, 0, 0, 0); 105 check_from_bits_be("Private", true, &candidate, 0, 0, 0, 0); 106 107 let candidate = Boolean::<Circuit>::new(Mode::Private, false); 108 check_from_bits_le("Private", false, &candidate, 0, 0, 0, 0); 109 check_from_bits_be("Private", false, &candidate, 0, 0, 0, 0); 110 } 111 }