snarkvm_circuit_program/data/literal/cast/
boolean.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM 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
16use super::*;
17
18impl<E: Environment> Cast<Address<E>> for Boolean<E> {
19    /// Casts a `Boolean` to an `Address`.
20    #[inline]
21    fn cast(&self) -> Address<E> {
22        self.cast_lossy()
23    }
24}
25
26impl<E: Environment> Cast<Boolean<E>> for Boolean<E> {
27    /// Casts a `Boolean` to a `Boolean`.
28    #[inline]
29    fn cast(&self) -> Boolean<E> {
30        self.cast_lossy()
31    }
32}
33
34impl<E: Environment> Cast<Field<E>> for Boolean<E> {
35    /// Casts a `Boolean` to a `Field`.
36    #[inline]
37    fn cast(&self) -> Field<E> {
38        self.cast_lossy()
39    }
40}
41
42impl<E: Environment> Cast<Group<E>> for Boolean<E> {
43    /// Casts a `Boolean` to a `Group`.
44    #[inline]
45    fn cast(&self) -> Group<E> {
46        self.cast_lossy()
47    }
48}
49
50impl<E: Environment, I: IntegerType> Cast<Integer<E, I>> for Boolean<E> {
51    /// Casts a `Boolean` to an `Integer`.
52    #[inline]
53    fn cast(&self) -> Integer<E, I> {
54        self.cast_lossy()
55    }
56}
57
58impl<E: Environment> Cast<Scalar<E>> for Boolean<E> {
59    /// Casts a `Boolean` to a `Scalar`.
60    #[inline]
61    fn cast(&self) -> Scalar<E> {
62        self.cast_lossy()
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use console::Cast as _;
70    use console_root::{network::MainnetV0, prelude::TestRng};
71    use snarkvm_circuit_types::environment::{Circuit, Eject, Inject, Mode, UpdatableCount, count_is};
72
73    use std::fmt::Debug;
74
75    const ITERATIONS: usize = 2;
76
77    fn sample_values(
78        i: usize,
79        mode: Mode,
80        _: &mut TestRng,
81    ) -> (console_root::types::Boolean<MainnetV0>, Boolean<Circuit>) {
82        (console_root::types::Boolean::new(i % 2 == 0), Boolean::new(mode, i % 2 == 0))
83    }
84
85    impl_check_cast!(cast, Boolean<Circuit>, console_root::types::Boolean::<MainnetV0>);
86
87    #[test]
88    fn test_boolean_to_address() {
89        check_cast::<Address<Circuit>, console_root::types::Address<MainnetV0>>(Mode::Constant, count_is!(10, 0, 0, 0));
90        check_cast::<Address<Circuit>, console_root::types::Address<MainnetV0>>(Mode::Public, count_is!(10, 0, 0, 0));
91        check_cast::<Address<Circuit>, console_root::types::Address<MainnetV0>>(Mode::Private, count_is!(10, 0, 0, 0));
92    }
93
94    #[test]
95    fn test_boolean_to_boolean() {
96        check_cast::<Boolean<Circuit>, console_root::types::Boolean<MainnetV0>>(Mode::Constant, count_is!(0, 0, 0, 0));
97        check_cast::<Boolean<Circuit>, console_root::types::Boolean<MainnetV0>>(Mode::Public, count_is!(0, 0, 0, 0));
98        check_cast::<Boolean<Circuit>, console_root::types::Boolean<MainnetV0>>(Mode::Private, count_is!(0, 0, 0, 0));
99    }
100
101    #[test]
102    fn test_boolean_to_field() {
103        check_cast::<Field<Circuit>, console_root::types::Field<MainnetV0>>(Mode::Constant, count_is!(0, 0, 0, 0));
104        check_cast::<Field<Circuit>, console_root::types::Field<MainnetV0>>(Mode::Public, count_is!(0, 0, 0, 0));
105        check_cast::<Field<Circuit>, console_root::types::Field<MainnetV0>>(Mode::Private, count_is!(0, 0, 0, 0));
106    }
107
108    #[test]
109    fn test_boolean_to_group() {
110        check_cast::<Group<Circuit>, console_root::types::Group<MainnetV0>>(Mode::Constant, count_is!(10, 0, 0, 0));
111        check_cast::<Group<Circuit>, console_root::types::Group<MainnetV0>>(Mode::Public, count_is!(10, 0, 0, 0));
112        check_cast::<Group<Circuit>, console_root::types::Group<MainnetV0>>(Mode::Private, count_is!(10, 0, 0, 0));
113    }
114
115    #[test]
116    fn test_boolean_to_i8() {
117        check_cast::<I8<Circuit>, console_root::types::I8<MainnetV0>>(Mode::Constant, count_is!(16, 0, 0, 0));
118        check_cast::<I8<Circuit>, console_root::types::I8<MainnetV0>>(Mode::Public, count_is!(16, 0, 0, 0));
119        check_cast::<I8<Circuit>, console_root::types::I8<MainnetV0>>(Mode::Private, count_is!(16, 0, 0, 0));
120    }
121
122    #[test]
123    fn test_boolean_to_i16() {
124        check_cast::<I16<Circuit>, console_root::types::I16<MainnetV0>>(Mode::Constant, count_is!(32, 0, 0, 0));
125        check_cast::<I16<Circuit>, console_root::types::I16<MainnetV0>>(Mode::Public, count_is!(32, 0, 0, 0));
126        check_cast::<I16<Circuit>, console_root::types::I16<MainnetV0>>(Mode::Private, count_is!(32, 0, 0, 0));
127    }
128
129    #[test]
130    fn test_boolean_to_i32() {
131        check_cast::<I32<Circuit>, console_root::types::I32<MainnetV0>>(Mode::Constant, count_is!(64, 0, 0, 0));
132        check_cast::<I32<Circuit>, console_root::types::I32<MainnetV0>>(Mode::Public, count_is!(64, 0, 0, 0));
133        check_cast::<I32<Circuit>, console_root::types::I32<MainnetV0>>(Mode::Private, count_is!(64, 0, 0, 0));
134    }
135
136    #[test]
137    fn test_boolean_to_i64() {
138        check_cast::<I64<Circuit>, console_root::types::I64<MainnetV0>>(Mode::Constant, count_is!(128, 0, 0, 0));
139        check_cast::<I64<Circuit>, console_root::types::I64<MainnetV0>>(Mode::Public, count_is!(128, 0, 0, 0));
140        check_cast::<I64<Circuit>, console_root::types::I64<MainnetV0>>(Mode::Private, count_is!(128, 0, 0, 0));
141    }
142
143    #[test]
144    fn test_boolean_to_i128() {
145        check_cast::<I128<Circuit>, console_root::types::I128<MainnetV0>>(Mode::Constant, count_is!(256, 0, 0, 0));
146        check_cast::<I128<Circuit>, console_root::types::I128<MainnetV0>>(Mode::Public, count_is!(256, 0, 0, 0));
147        check_cast::<I128<Circuit>, console_root::types::I128<MainnetV0>>(Mode::Private, count_is!(256, 0, 0, 0));
148    }
149
150    #[test]
151    fn test_boolean_to_scalar() {
152        check_cast::<Scalar<Circuit>, console_root::types::Scalar<MainnetV0>>(Mode::Constant, count_is!(2, 0, 0, 0));
153        check_cast::<Scalar<Circuit>, console_root::types::Scalar<MainnetV0>>(Mode::Public, count_is!(2, 0, 0, 0));
154        check_cast::<Scalar<Circuit>, console_root::types::Scalar<MainnetV0>>(Mode::Private, count_is!(2, 0, 0, 0));
155    }
156
157    #[test]
158    fn test_boolean_to_u8() {
159        check_cast::<U8<Circuit>, console_root::types::U8<MainnetV0>>(Mode::Constant, count_is!(16, 0, 0, 0));
160        check_cast::<U8<Circuit>, console_root::types::U8<MainnetV0>>(Mode::Public, count_is!(16, 0, 0, 0));
161        check_cast::<U8<Circuit>, console_root::types::U8<MainnetV0>>(Mode::Private, count_is!(16, 0, 0, 0));
162    }
163
164    #[test]
165    fn test_boolean_to_u16() {
166        check_cast::<U16<Circuit>, console_root::types::U16<MainnetV0>>(Mode::Constant, count_is!(32, 0, 0, 0));
167        check_cast::<U16<Circuit>, console_root::types::U16<MainnetV0>>(Mode::Public, count_is!(32, 0, 0, 0));
168        check_cast::<U16<Circuit>, console_root::types::U16<MainnetV0>>(Mode::Private, count_is!(32, 0, 0, 0));
169    }
170
171    #[test]
172    fn test_boolean_to_u32() {
173        check_cast::<U32<Circuit>, console_root::types::U32<MainnetV0>>(Mode::Constant, count_is!(64, 0, 0, 0));
174        check_cast::<U32<Circuit>, console_root::types::U32<MainnetV0>>(Mode::Public, count_is!(64, 0, 0, 0));
175        check_cast::<U32<Circuit>, console_root::types::U32<MainnetV0>>(Mode::Private, count_is!(64, 0, 0, 0));
176    }
177
178    #[test]
179    fn test_boolean_to_u64() {
180        check_cast::<U64<Circuit>, console_root::types::U64<MainnetV0>>(Mode::Constant, count_is!(128, 0, 0, 0));
181        check_cast::<U64<Circuit>, console_root::types::U64<MainnetV0>>(Mode::Public, count_is!(128, 0, 0, 0));
182        check_cast::<U64<Circuit>, console_root::types::U64<MainnetV0>>(Mode::Private, count_is!(128, 0, 0, 0));
183    }
184
185    #[test]
186    fn test_boolean_to_u128() {
187        check_cast::<U128<Circuit>, console_root::types::U128<MainnetV0>>(Mode::Constant, count_is!(256, 0, 0, 0));
188        check_cast::<U128<Circuit>, console_root::types::U128<MainnetV0>>(Mode::Public, count_is!(256, 0, 0, 0));
189        check_cast::<U128<Circuit>, console_root::types::U128<MainnetV0>>(Mode::Private, count_is!(256, 0, 0, 0));
190    }
191}