Skip to main content

snarkvm_console_program/data/literal/cast/
mod.rs

1// Copyright (c) 2019-2026 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
16mod boolean;
17mod field;
18mod identifier;
19mod integer;
20mod scalar;
21
22use crate::{Literal, LiteralType};
23use snarkvm_console_network::Network;
24use snarkvm_console_types::{Boolean, integers::Integer, prelude::*};
25
26/// Unary operator for casting values of one type to another.
27pub trait Cast<T: Sized = Self> {
28    /// Casts the value of `self` into a value of type `T`.
29    ///
30    /// This method checks that the cast does not lose any bits of information,
31    /// and returns an error if it does.
32    fn cast(&self) -> Result<T>;
33}
34
35impl<N: Network> Literal<N> {
36    /// Casts the literal to the given literal type.
37    ///
38    /// This method checks that the cast does not lose any bits of information,
39    /// and returns an error if it does.
40    ///
41    /// The hierarchy of casting is as follows:
42    ///  - (`Address`, `Group`) <-> `Field` <-> `Scalar` <-> `Integer` <-> `Boolean`
43    ///  - `Identifier` <-> `Field`
44    ///  - `Signature` (not supported)
45    ///  - `String` (not supported)
46    ///
47    /// Note that casting to left along the hierarchy always preserves information.
48    pub fn cast(&self, to_type: LiteralType) -> Result<Self> {
49        match self {
50            Self::Address(address) => cast_group_to_type(address.to_group(), to_type),
51            Self::Boolean(boolean) => cast_boolean_to_type(boolean, to_type),
52            Self::Field(field) => cast_field_to_type(field, to_type),
53            Self::Group(group) => cast_group_to_type(group, to_type),
54            Self::I8(integer) => cast_integer_to_type(integer, to_type),
55            Self::I16(integer) => cast_integer_to_type(integer, to_type),
56            Self::I32(integer) => cast_integer_to_type(integer, to_type),
57            Self::I64(integer) => cast_integer_to_type(integer, to_type),
58            Self::I128(integer) => cast_integer_to_type(integer, to_type),
59            Self::U8(integer) => cast_integer_to_type(integer, to_type),
60            Self::U16(integer) => cast_integer_to_type(integer, to_type),
61            Self::U32(integer) => cast_integer_to_type(integer, to_type),
62            Self::U64(integer) => cast_integer_to_type(integer, to_type),
63            Self::U128(integer) => cast_integer_to_type(integer, to_type),
64            Self::Scalar(scalar) => cast_scalar_to_type(scalar, to_type),
65            Self::Signature(..) => bail!("Cannot cast a signature literal to another type."),
66            Self::String(..) => bail!("Cannot cast a string literal to another type."),
67            Self::Identifier(identifier) => cast_identifier_literal_to_type(identifier, to_type),
68        }
69    }
70}
71
72/// A helper macro to implement the body of the `cast` methods.
73macro_rules! impl_cast_body {
74    ($type_name:ident, $cast:ident, $input:expr, $to_type:expr) => {
75        match $to_type {
76            LiteralType::Address => Ok(Literal::Address($input.$cast()?)),
77            LiteralType::Boolean => Ok(Literal::Boolean($input.$cast()?)),
78            LiteralType::Field => Ok(Literal::Field($input.$cast()?)),
79            LiteralType::Group => Ok(Literal::Group($input.$cast()?)),
80            LiteralType::I8 => Ok(Literal::I8($input.$cast()?)),
81            LiteralType::I16 => Ok(Literal::I16($input.$cast()?)),
82            LiteralType::I32 => Ok(Literal::I32($input.$cast()?)),
83            LiteralType::I64 => Ok(Literal::I64($input.$cast()?)),
84            LiteralType::I128 => Ok(Literal::I128($input.$cast()?)),
85            LiteralType::U8 => Ok(Literal::U8($input.$cast()?)),
86            LiteralType::U16 => Ok(Literal::U16($input.$cast()?)),
87            LiteralType::U32 => Ok(Literal::U32($input.$cast()?)),
88            LiteralType::U64 => Ok(Literal::U64($input.$cast()?)),
89            LiteralType::U128 => Ok(Literal::U128($input.$cast()?)),
90            LiteralType::Scalar => Ok(Literal::Scalar($input.$cast()?)),
91            LiteralType::Signature => {
92                bail!(concat!("Cannot cast a ", stringify!($type_name), " literal to a signature type."))
93            }
94            LiteralType::String => {
95                bail!(concat!("Cannot cast a ", stringify!($type_name), " literal to a string type."))
96            }
97            LiteralType::Identifier => Ok(Literal::Identifier(Box::new($input.$cast()?))),
98        }
99    };
100}
101
102/// Casts a boolean literal to the given literal type.
103fn cast_boolean_to_type<N: Network>(input: &Boolean<N>, to_type: LiteralType) -> Result<Literal<N>> {
104    impl_cast_body!(boolean, cast, input, to_type)
105}
106
107/// Casts a field literal to the given literal type.
108fn cast_field_to_type<N: Network>(input: &Field<N>, to_type: LiteralType) -> Result<Literal<N>> {
109    impl_cast_body!(field, cast, input, to_type)
110}
111
112/// Casts a group literal to the given literal type.
113fn cast_group_to_type<N: Network>(input: &Group<N>, to_type: LiteralType) -> Result<Literal<N>> {
114    match to_type {
115        LiteralType::Address => Ok(Literal::Address(Address::new(*input))),
116        LiteralType::Group => Ok(Literal::Group(*input)),
117        _ => cast_field_to_type(&input.to_x_coordinate(), to_type),
118    }
119}
120
121/// Casts an integer literal to the given literal type.
122fn cast_integer_to_type<N: Network, I: IntegerType>(
123    input: &integers::Integer<N, I>,
124    to_type: LiteralType,
125) -> Result<Literal<N>>
126where
127    i8: TryFrom<I>,
128    i16: TryFrom<I>,
129    i32: TryFrom<I>,
130    i64: TryFrom<I>,
131    i128: TryFrom<I>,
132    u8: TryFrom<I>,
133    u16: TryFrom<I>,
134    u32: TryFrom<I>,
135    u64: TryFrom<I>,
136    u128: TryFrom<I>,
137{
138    impl_cast_body!(integer, cast, input, to_type)
139}
140
141/// Casts a scalar literal to the given literal type.
142fn cast_scalar_to_type<N: Network>(input: &Scalar<N>, to_type: LiteralType) -> Result<Literal<N>> {
143    impl_cast_body!(scalar, cast, input, to_type)
144}
145
146/// Casts an identifier literal to the given literal type.
147fn cast_identifier_literal_to_type<N: Network>(
148    input: &IdentifierLiteral<N>,
149    to_type: LiteralType,
150) -> Result<Literal<N>> {
151    impl_cast_body!(identifier, cast, input, to_type)
152}