snarkvm_circuit_program/data/literal/cast/
field.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 Field<E> {
19    /// Casts a `Field` to an `Address`.
20    ///
21    /// This operation attempts to recover the group element from the field element, and then
22    /// constructs an address from the group element.
23    ///
24    /// To cast arbitrary field elements to addresses, use `Field::cast_lossy`.
25    #[inline]
26    fn cast(&self) -> Address<E> {
27        Address::from_field(self.clone())
28    }
29}
30
31impl<E: Environment> Cast<Boolean<E>> for Field<E> {
32    /// Casts a `Field` to a `Boolean`, if the field is zero or one.
33    ///
34    /// To cast arbitrary field elements to booleans, use `Field::cast_lossy`.
35    #[inline]
36    fn cast(&self) -> Boolean<E> {
37        let is_one = self.is_one();
38        E::assert(self.is_zero().bitor(&is_one));
39        is_one
40    }
41}
42
43impl<E: Environment> Cast<Field<E>> for Field<E> {
44    /// Casts a `Field` to a `Field`.
45    #[inline]
46    fn cast(&self) -> Field<E> {
47        self.clone()
48    }
49}
50
51impl<E: Environment> Cast<Group<E>> for Field<E> {
52    /// Casts a `Field` to a `Group`.
53    ///
54    /// This operation attempts to recover the group element from the field element,
55    /// and returns an error if the field element is not a valid x-coordinate.
56    ///
57    /// To cast arbitrary field elements to groups, use `Field::cast_lossy`.
58    #[inline]
59    fn cast(&self) -> Group<E> {
60        Group::from_x_coordinate(self.clone())
61    }
62}
63
64impl<E: Environment, I: IntegerType> Cast<Integer<E, I>> for Field<E> {
65    /// Casts a `Field` to an `Integer`, if the field element is in the integer's range.
66    ///
67    /// To cast arbitrary field elements to integers, use `Field::cast_lossy`.
68    #[inline]
69    fn cast(&self) -> Integer<E, I> {
70        Integer::from_field(self.clone())
71    }
72}
73
74impl<E: Environment> Cast<Scalar<E>> for Field<E> {
75    /// Casts a `Field` to a `Scalar`, if the field element is in the scalar's range.
76    ///
77    /// To cast arbitrary field elements to scalars, use `Field::cast_lossy`.
78    #[inline]
79    fn cast(&self) -> Scalar<E> {
80        Scalar::from_field(self.clone())
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87    use console::Cast as _;
88    use console_root::{
89        network::MainnetV0,
90        prelude::{One, TestRng, Uniform, Zero},
91    };
92    use snarkvm_circuit_types::environment::{Circuit, Eject, Inject, Mode, UpdatableCount, count_is, count_less_than};
93
94    use std::fmt::Debug;
95
96    const ITERATIONS: usize = 100;
97
98    fn sample_values(
99        i: usize,
100        mode: Mode,
101        rng: &mut TestRng,
102    ) -> (console_root::types::Field<MainnetV0>, Field<Circuit>) {
103        let console_value = match i {
104            0 => console_root::types::Field::<MainnetV0>::zero(),
105            1 => console_root::types::Field::<MainnetV0>::one(),
106            _ => Uniform::rand(rng),
107        };
108        let circuit_value = Field::<Circuit>::new(mode, console_value);
109        (console_value, circuit_value)
110    }
111
112    impl_check_cast!(cast, Field<Circuit>, console_root::types::Field::<MainnetV0>);
113
114    #[test]
115    fn test_field_to_address() {
116        check_cast::<Address<Circuit>, console_root::types::Address<MainnetV0>>(
117            Mode::Constant,
118            count_less_than!(11, 0, 0, 0),
119        );
120        check_cast::<Address<Circuit>, console_root::types::Address<MainnetV0>>(Mode::Public, count_is!(4, 0, 13, 13));
121        check_cast::<Address<Circuit>, console_root::types::Address<MainnetV0>>(Mode::Private, count_is!(4, 0, 13, 13));
122    }
123
124    #[test]
125    fn test_field_to_boolean() {
126        check_cast::<Boolean<Circuit>, console_root::types::Boolean<MainnetV0>>(Mode::Constant, count_is!(2, 0, 0, 0));
127        check_cast::<Boolean<Circuit>, console_root::types::Boolean<MainnetV0>>(Mode::Public, count_is!(0, 0, 5, 6));
128        check_cast::<Boolean<Circuit>, console_root::types::Boolean<MainnetV0>>(Mode::Private, count_is!(0, 0, 5, 6));
129    }
130
131    #[test]
132    fn test_field_to_field() {
133        check_cast::<Field<Circuit>, console_root::types::Field<MainnetV0>>(Mode::Constant, count_is!(0, 0, 0, 0));
134        check_cast::<Field<Circuit>, console_root::types::Field<MainnetV0>>(Mode::Public, count_is!(0, 0, 0, 0));
135        check_cast::<Field<Circuit>, console_root::types::Field<MainnetV0>>(Mode::Private, count_is!(0, 0, 0, 0));
136    }
137
138    #[test]
139    fn test_field_to_group() {
140        check_cast::<Group<Circuit>, console_root::types::Group<MainnetV0>>(
141            Mode::Constant,
142            count_less_than!(11, 0, 0, 0),
143        );
144        check_cast::<Group<Circuit>, console_root::types::Group<MainnetV0>>(Mode::Public, count_is!(4, 0, 13, 13));
145        check_cast::<Group<Circuit>, console_root::types::Group<MainnetV0>>(Mode::Private, count_is!(4, 0, 13, 13));
146    }
147
148    #[test]
149    fn test_field_to_i8() {
150        check_cast::<I8<Circuit>, console_root::types::I8<MainnetV0>>(Mode::Constant, count_is!(8, 0, 0, 0));
151        check_cast::<I8<Circuit>, console_root::types::I8<MainnetV0>>(Mode::Public, count_is!(0, 0, 8, 9));
152        check_cast::<I8<Circuit>, console_root::types::I8<MainnetV0>>(Mode::Private, count_is!(0, 0, 8, 9));
153    }
154
155    #[test]
156    fn test_field_to_i16() {
157        check_cast::<I16<Circuit>, console_root::types::I16<MainnetV0>>(Mode::Constant, count_is!(16, 0, 0, 0));
158        check_cast::<I16<Circuit>, console_root::types::I16<MainnetV0>>(Mode::Public, count_is!(0, 0, 16, 17));
159        check_cast::<I16<Circuit>, console_root::types::I16<MainnetV0>>(Mode::Private, count_is!(0, 0, 16, 17));
160    }
161
162    #[test]
163    fn test_field_to_i32() {
164        check_cast::<I32<Circuit>, console_root::types::I32<MainnetV0>>(Mode::Constant, count_is!(32, 0, 0, 0));
165        check_cast::<I32<Circuit>, console_root::types::I32<MainnetV0>>(Mode::Public, count_is!(0, 0, 32, 33));
166        check_cast::<I32<Circuit>, console_root::types::I32<MainnetV0>>(Mode::Private, count_is!(0, 0, 32, 33));
167    }
168
169    #[test]
170    fn test_field_to_i64() {
171        check_cast::<I64<Circuit>, console_root::types::I64<MainnetV0>>(Mode::Constant, count_is!(64, 0, 0, 0));
172        check_cast::<I64<Circuit>, console_root::types::I64<MainnetV0>>(Mode::Public, count_is!(0, 0, 64, 65));
173        check_cast::<I64<Circuit>, console_root::types::I64<MainnetV0>>(Mode::Private, count_is!(0, 0, 64, 65));
174    }
175
176    #[test]
177    fn test_field_to_i128() {
178        check_cast::<I128<Circuit>, console_root::types::I128<MainnetV0>>(Mode::Constant, count_is!(128, 0, 0, 0));
179        check_cast::<I128<Circuit>, console_root::types::I128<MainnetV0>>(Mode::Public, count_is!(0, 0, 128, 129));
180        check_cast::<I128<Circuit>, console_root::types::I128<MainnetV0>>(Mode::Private, count_is!(0, 0, 128, 129));
181    }
182
183    #[test]
184    fn test_field_to_scalar() {
185        check_cast::<Scalar<Circuit>, console_root::types::Scalar<MainnetV0>>(Mode::Constant, count_is!(253, 0, 0, 0));
186        check_cast::<Scalar<Circuit>, console_root::types::Scalar<MainnetV0>>(Mode::Public, count_is!(0, 0, 755, 759));
187        check_cast::<Scalar<Circuit>, console_root::types::Scalar<MainnetV0>>(Mode::Private, count_is!(0, 0, 755, 759));
188    }
189
190    #[test]
191    fn test_field_to_u8() {
192        check_cast::<U8<Circuit>, console_root::types::U8<MainnetV0>>(Mode::Constant, count_is!(8, 0, 0, 0));
193        check_cast::<U8<Circuit>, console_root::types::U8<MainnetV0>>(Mode::Public, count_is!(0, 0, 8, 9));
194        check_cast::<U8<Circuit>, console_root::types::U8<MainnetV0>>(Mode::Private, count_is!(0, 0, 8, 9));
195    }
196
197    #[test]
198    fn test_field_to_u16() {
199        check_cast::<U16<Circuit>, console_root::types::U16<MainnetV0>>(Mode::Constant, count_is!(16, 0, 0, 0));
200        check_cast::<U16<Circuit>, console_root::types::U16<MainnetV0>>(Mode::Public, count_is!(0, 0, 16, 17));
201        check_cast::<U16<Circuit>, console_root::types::U16<MainnetV0>>(Mode::Private, count_is!(0, 0, 16, 17));
202    }
203
204    #[test]
205    fn test_field_to_u32() {
206        check_cast::<U32<Circuit>, console_root::types::U32<MainnetV0>>(Mode::Constant, count_is!(32, 0, 0, 0));
207        check_cast::<U32<Circuit>, console_root::types::U32<MainnetV0>>(Mode::Public, count_is!(0, 0, 32, 33));
208        check_cast::<U32<Circuit>, console_root::types::U32<MainnetV0>>(Mode::Private, count_is!(0, 0, 32, 33));
209    }
210
211    #[test]
212    fn test_field_to_u64() {
213        check_cast::<U64<Circuit>, console_root::types::U64<MainnetV0>>(Mode::Constant, count_is!(64, 0, 0, 0));
214        check_cast::<U64<Circuit>, console_root::types::U64<MainnetV0>>(Mode::Public, count_is!(0, 0, 64, 65));
215        check_cast::<U64<Circuit>, console_root::types::U64<MainnetV0>>(Mode::Private, count_is!(0, 0, 64, 65));
216    }
217
218    #[test]
219    fn test_field_to_u128() {
220        check_cast::<U128<Circuit>, console_root::types::U128<MainnetV0>>(Mode::Constant, count_is!(128, 0, 0, 0));
221        check_cast::<U128<Circuit>, console_root::types::U128<MainnetV0>>(Mode::Public, count_is!(0, 0, 128, 129));
222        check_cast::<U128<Circuit>, console_root::types::U128<MainnetV0>>(Mode::Private, count_is!(0, 0, 128, 129));
223    }
224}