snarkvm_console_program/data/literal/cast_lossy/
mod.rs1mod boolean;
17mod field;
18mod integer;
19mod scalar;
20
21use crate::{Literal, LiteralType};
22use snarkvm_console_algorithms::Elligator2;
23use snarkvm_console_network::Network;
24use snarkvm_console_types::{Boolean, integers::Integer, prelude::*};
25
26pub trait CastLossy<T: Sized = Self> {
28 fn cast_lossy(&self) -> T;
33}
34
35impl<N: Network> Literal<N> {
36 pub fn cast_lossy(&self, to_type: LiteralType) -> Result<Self> {
48 match self {
49 Self::Address(address) => cast_lossy_group_to_type(address.to_group(), to_type),
50 Self::Boolean(boolean) => cast_lossy_boolean_to_type(boolean, to_type),
51 Self::Field(field) => cast_lossy_field_to_type(field, to_type),
52 Self::Group(group) => cast_lossy_group_to_type(group, to_type),
53 Self::I8(integer) => cast_lossy_integer_to_type(integer, to_type),
54 Self::I16(integer) => cast_lossy_integer_to_type(integer, to_type),
55 Self::I32(integer) => cast_lossy_integer_to_type(integer, to_type),
56 Self::I64(integer) => cast_lossy_integer_to_type(integer, to_type),
57 Self::I128(integer) => cast_lossy_integer_to_type(integer, to_type),
58 Self::U8(integer) => cast_lossy_integer_to_type(integer, to_type),
59 Self::U16(integer) => cast_lossy_integer_to_type(integer, to_type),
60 Self::U32(integer) => cast_lossy_integer_to_type(integer, to_type),
61 Self::U64(integer) => cast_lossy_integer_to_type(integer, to_type),
62 Self::U128(integer) => cast_lossy_integer_to_type(integer, to_type),
63 Self::Scalar(scalar) => cast_lossy_scalar_to_type(scalar, to_type),
64 Self::Signature(..) => bail!("Cannot cast a signature literal to another type."),
65 Self::String(..) => bail!("Cannot cast a string literal to another type."),
66 Self::Identifier(..) => bail!("Cannot cast an identifier literal to another type."),
67 }
68 }
69}
70
71macro_rules! impl_cast_lossy_body {
73 ($type_name:ident, $cast_lossy:ident, $input:expr, $to_type:expr) => {
74 match $to_type {
75 LiteralType::Address => Ok(Literal::Address($input.$cast_lossy())),
76 LiteralType::Boolean => Ok(Literal::Boolean($input.$cast_lossy())),
77 LiteralType::Field => Ok(Literal::Field($input.$cast_lossy())),
78 LiteralType::Group => Ok(Literal::Group($input.$cast_lossy())),
79 LiteralType::I8 => Ok(Literal::I8($input.$cast_lossy())),
80 LiteralType::I16 => Ok(Literal::I16($input.$cast_lossy())),
81 LiteralType::I32 => Ok(Literal::I32($input.$cast_lossy())),
82 LiteralType::I64 => Ok(Literal::I64($input.$cast_lossy())),
83 LiteralType::I128 => Ok(Literal::I128($input.$cast_lossy())),
84 LiteralType::U8 => Ok(Literal::U8($input.$cast_lossy())),
85 LiteralType::U16 => Ok(Literal::U16($input.$cast_lossy())),
86 LiteralType::U32 => Ok(Literal::U32($input.$cast_lossy())),
87 LiteralType::U64 => Ok(Literal::U64($input.$cast_lossy())),
88 LiteralType::U128 => Ok(Literal::U128($input.$cast_lossy())),
89 LiteralType::Scalar => Ok(Literal::Scalar($input.$cast_lossy())),
90 LiteralType::Signature => {
91 bail!(concat!("Cannot cast (lossy) a ", stringify!($type_name), " literal to a signature type."))
92 }
93 LiteralType::String => {
94 bail!(concat!("Cannot cast (lossy) a ", stringify!($type_name), " literal to a string type."))
95 }
96 LiteralType::Identifier => {
97 bail!(concat!("Cannot cast (lossy) a ", stringify!($type_name), " literal to an identifier type."))
98 }
99 }
100 };
101}
102
103fn cast_lossy_boolean_to_type<N: Network>(input: &Boolean<N>, to_type: LiteralType) -> Result<Literal<N>> {
105 impl_cast_lossy_body!(boolean, cast_lossy, input, to_type)
106}
107
108fn cast_lossy_field_to_type<N: Network>(input: &Field<N>, to_type: LiteralType) -> Result<Literal<N>> {
110 impl_cast_lossy_body!(field, cast_lossy, input, to_type)
111}
112
113fn cast_lossy_group_to_type<N: Network>(input: &Group<N>, to_type: LiteralType) -> Result<Literal<N>> {
115 match to_type {
116 LiteralType::Address => Ok(Literal::Address(Address::new(*input))),
117 LiteralType::Group => Ok(Literal::Group(*input)),
118 _ => cast_lossy_field_to_type(&input.to_x_coordinate(), to_type),
119 }
120}
121
122fn cast_lossy_integer_to_type<N: Network, I>(input: &Integer<N, I>, to_type: LiteralType) -> Result<Literal<N>>
124where
125 I: AsPrimitive<u8>
126 + AsPrimitive<u16>
127 + AsPrimitive<u32>
128 + AsPrimitive<u64>
129 + AsPrimitive<u128>
130 + AsPrimitive<i8>
131 + AsPrimitive<i16>
132 + AsPrimitive<i32>
133 + AsPrimitive<i64>
134 + AsPrimitive<i128>
135 + IntegerType,
136{
137 impl_cast_lossy_body!(integer, cast_lossy, input, to_type)
138}
139
140fn cast_lossy_scalar_to_type<N: Network>(input: &Scalar<N>, to_type: LiteralType) -> Result<Literal<N>> {
142 impl_cast_lossy_body!(scalar, cast_lossy, input, to_type)
143}