snarkvm_console_program/data/literal/cast/
field.rs

1// Copyright 2024 Aleo Network Foundation
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) -> Result<Address<E>> {
27        Address::from_field(self)
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) -> Result<Boolean<E>> {
37        if self.is_zero() {
38            Ok(Boolean::new(false))
39        } else if self.is_one() {
40            Ok(Boolean::new(true))
41        } else {
42            bail!("Failed to convert field to boolean: field element is not zero or one")
43        }
44    }
45}
46
47impl<E: Environment> Cast<Field<E>> for Field<E> {
48    /// Casts a `Field` to a `Field`.
49    #[inline]
50    fn cast(&self) -> Result<Field<E>> {
51        Ok(*self)
52    }
53}
54
55impl<E: Environment> Cast<Group<E>> for Field<E> {
56    /// Casts a `Field` to a `Group`.
57    ///
58    /// This operation attempts to recover the group element from the field element,
59    /// and returns an error if the field element is not a valid x-coordinate.
60    ///
61    /// To cast arbitrary field elements to groups, use `Field::cast_lossy`.
62    #[inline]
63    fn cast(&self) -> Result<Group<E>> {
64        Group::from_field(self)
65    }
66}
67
68impl<E: Environment, I: IntegerType> Cast<Integer<E, I>> for Field<E> {
69    /// Casts a `Field` to an `Integer`, if the field element is in the integer's range.
70    ///
71    /// To cast arbitrary field elements to integers, use `Field::cast_lossy`.
72    #[inline]
73    fn cast(&self) -> Result<Integer<E, I>> {
74        Integer::from_field(self)
75    }
76}
77
78impl<E: Environment> Cast<Scalar<E>> for Field<E> {
79    /// Casts a `Field` to a `Scalar`, if the field element is in the scalar's range.
80    ///
81    /// To cast arbitrary field elements to scalars, use `Field::cast_lossy`.
82    #[inline]
83    fn cast(&self) -> Result<Scalar<E>> {
84        Scalar::from_field(self)
85    }
86}