snarkvm_console_program/data/literal/cast/
integer.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, I: IntegerType> Cast<Address<E>> for Integer<E, I> {
19    /// Casts an `Integer` to an `Address`.
20    ///
21    /// This operation converts the integer to a field element, and then attempts to recover
22    /// the group element by treating the field element as an x-coordinate. The group element
23    /// is then converted to an address.
24    ///
25    /// To cast arbitrary integers to addresses, use `Integer::cast_lossy`.
26    #[inline]
27    fn cast(&self) -> Result<Address<E>> {
28        let field: Field<E> = self.cast()?;
29        field.cast()
30    }
31}
32
33impl<E: Environment, I: IntegerType> Cast<Boolean<E>> for Integer<E, I> {
34    /// Casts an `Integer` to a `Boolean`, if the integer is zero or one.
35    ///
36    /// To cast arbitrary integers to booleans, use `Integer::cast_lossy`.
37    #[inline]
38    fn cast(&self) -> Result<Boolean<E>> {
39        if self.is_zero() {
40            Ok(Boolean::new(false))
41        } else if self.is_one() {
42            Ok(Boolean::new(true))
43        } else {
44            bail!("Failed to convert integer to boolean: integer is not zero or one")
45        }
46    }
47}
48
49impl<E: Environment, I: IntegerType> Cast<Field<E>> for Integer<E, I> {
50    /// Casts an `Integer` to a `Field`.
51    #[inline]
52    fn cast(&self) -> Result<Field<E>> {
53        self.to_field()
54    }
55}
56
57impl<E: Environment, I: IntegerType> Cast<Group<E>> for Integer<E, I> {
58    /// Casts an `Integer` to a `Group`.
59    ///
60    /// This operation converts the integer to a field element, and then attempts to recover
61    /// the group element by treating the field element as an x-coordinate.
62    ///
63    /// To cast arbitrary integers to groups, use `Integer::cast_lossy`.
64    #[inline]
65    fn cast(&self) -> Result<Group<E>> {
66        let field: Field<E> = self.cast()?;
67        field.cast()
68    }
69}
70
71impl<E: Environment, I0: IntegerType, I1: IntegerType + TryFrom<I0>> Cast<Integer<E, I1>> for Integer<E, I0> {
72    /// Casts an `Integer` to another `Integer`, if the conversion is lossless.
73    #[inline]
74    fn cast(&self) -> Result<Integer<E, I1>> {
75        Ok(Integer::<E, I1>::new(match I1::try_from(**self) {
76            Ok(value) => value,
77            Err(_) => bail!("Failed to convert '{}' into '{}'", I0::type_name(), I1::type_name()),
78        }))
79    }
80}
81
82impl<E: Environment, I: IntegerType> Cast<Scalar<E>> for Integer<E, I> {
83    /// Casts an `Integer` to a `Scalar`.
84    #[inline]
85    fn cast(&self) -> Result<Scalar<E>> {
86        let bits_le = self.to_bits_le();
87        Scalar::<E>::from_bits_le(&bits_le)
88    }
89}