snarkvm_circuit_program/data/literal/cast_lossy/
mod.rs1mod boolean;
17mod field;
18mod integer;
19mod scalar;
20
21use crate::data::Literal;
22use console::LiteralType;
23use snarkvm_circuit_algorithms::Elligator2;
24use snarkvm_circuit_network::Aleo;
25use snarkvm_circuit_types::prelude::{
26 Address,
27 Boolean,
28 Environment,
29 Field,
30 FromBits,
31 FromBoolean,
32 FromGroup,
33 Group,
34 Inject,
35 IntegerType,
36 MSB,
37 One,
38 Result,
39 Scalar,
40 Ternary,
41 ToBits,
42 ToField,
43 ToGroup,
44 Zero,
45 bail,
46 integers::Integer,
47};
48
49#[cfg(test)]
50use snarkvm_circuit_types::prelude::{I8, I16, I32, I64, I128, U8, U16, U32, U64, U128};
51
52pub trait CastLossy<T: Sized = Self> {
54 fn cast_lossy(&self) -> T;
59}
60
61impl<A: Aleo> Literal<A> {
62 pub fn cast_lossy(&self, to_type: LiteralType) -> Result<Self> {
74 match self {
75 Self::Address(address) => cast_lossy_group_to_type(&address.to_group(), to_type),
76 Self::Boolean(boolean) => cast_lossy_boolean_to_type(boolean, to_type),
77 Self::Field(field) => cast_lossy_field_to_type(field, to_type),
78 Self::Group(group) => cast_lossy_group_to_type(group, to_type),
79 Self::I8(integer) => cast_lossy_integer_to_type(integer, to_type),
80 Self::I16(integer) => cast_lossy_integer_to_type(integer, to_type),
81 Self::I32(integer) => cast_lossy_integer_to_type(integer, to_type),
82 Self::I64(integer) => cast_lossy_integer_to_type(integer, to_type),
83 Self::I128(integer) => cast_lossy_integer_to_type(integer, to_type),
84 Self::U8(integer) => cast_lossy_integer_to_type(integer, to_type),
85 Self::U16(integer) => cast_lossy_integer_to_type(integer, to_type),
86 Self::U32(integer) => cast_lossy_integer_to_type(integer, to_type),
87 Self::U64(integer) => cast_lossy_integer_to_type(integer, to_type),
88 Self::U128(integer) => cast_lossy_integer_to_type(integer, to_type),
89 Self::Scalar(scalar) => cast_lossy_scalar_to_type(scalar, to_type),
90 Self::Signature(..) => bail!("Cannot cast (lossy) a signature literal to another type."),
91 Self::String(..) => bail!("Cannot cast (lossy) a string literal to another type."),
92 Self::Identifier(..) => bail!("Cannot cast (lossy) an identifier literal to another type."),
93 }
94 }
95}
96
97macro_rules! impl_cast_body {
99 ($type_name:ident, $cast_lossy:ident, $input:expr, $to_type:expr) => {
100 match $to_type {
101 LiteralType::Address => Ok(Literal::Address($input.$cast_lossy())),
102 LiteralType::Boolean => Ok(Literal::Boolean($input.$cast_lossy())),
103 LiteralType::Field => Ok(Literal::Field($input.$cast_lossy())),
104 LiteralType::Group => Ok(Literal::Group($input.$cast_lossy())),
105 LiteralType::I8 => Ok(Literal::I8($input.$cast_lossy())),
106 LiteralType::I16 => Ok(Literal::I16($input.$cast_lossy())),
107 LiteralType::I32 => Ok(Literal::I32($input.$cast_lossy())),
108 LiteralType::I64 => Ok(Literal::I64($input.$cast_lossy())),
109 LiteralType::I128 => Ok(Literal::I128($input.$cast_lossy())),
110 LiteralType::U8 => Ok(Literal::U8($input.$cast_lossy())),
111 LiteralType::U16 => Ok(Literal::U16($input.$cast_lossy())),
112 LiteralType::U32 => Ok(Literal::U32($input.$cast_lossy())),
113 LiteralType::U64 => Ok(Literal::U64($input.$cast_lossy())),
114 LiteralType::U128 => Ok(Literal::U128($input.$cast_lossy())),
115 LiteralType::Scalar => Ok(Literal::Scalar($input.$cast_lossy())),
116 LiteralType::Signature => {
117 bail!(concat!("Cannot cast (lossy) a ", stringify!($type_name), " literal to a signature type."))
118 }
119 LiteralType::String => {
120 bail!(concat!("Cannot cast (lossy) a ", stringify!($type_name), " literal to a string type."))
121 }
122 LiteralType::Identifier => {
123 bail!(concat!("Cannot cast (lossy) a ", stringify!($type_name), " literal to an identifier type."))
124 }
125 }
126 };
127}
128
129fn cast_lossy_boolean_to_type<A: Aleo>(input: &Boolean<A>, to_type: LiteralType) -> Result<Literal<A>> {
131 impl_cast_body!(boolean, cast_lossy, input, to_type)
132}
133
134fn cast_lossy_field_to_type<A: Aleo>(input: &Field<A>, to_type: LiteralType) -> Result<Literal<A>> {
136 impl_cast_body!(field, cast_lossy, input, to_type)
137}
138
139fn cast_lossy_group_to_type<A: Aleo>(input: &Group<A>, to_type: LiteralType) -> Result<Literal<A>> {
141 match to_type {
142 LiteralType::Address => Ok(Literal::Address(Address::from_group(input.clone()))),
143 LiteralType::Group => Ok(Literal::Group(input.clone())),
144 _ => cast_lossy_field_to_type(&input.to_x_coordinate(), to_type),
145 }
146}
147
148fn cast_lossy_integer_to_type<A: Aleo, I: IntegerType>(
150 input: &Integer<A, I>,
151 to_type: LiteralType,
152) -> Result<Literal<A>> {
153 impl_cast_body!(integer, cast_lossy, input, to_type)
154}
155
156fn cast_lossy_scalar_to_type<A: Aleo>(input: &Scalar<A>, to_type: LiteralType) -> Result<Literal<A>> {
158 impl_cast_body!(scalar, cast_lossy, input, to_type)
159}
160
161#[cfg(test)]
162macro_rules! check_cast_lossy {
163 ($fun:ident, $circuit_type:ty, $console_type:ty) => {
164 fn check_cast_lossy<CircuitType, ConsoleType>(mode: Mode, count: UpdatableCount)
165 where
166 CircuitType: Eject,
167 <CircuitType as Eject>::Primitive: Debug + PartialEq<ConsoleType>,
168 ConsoleType: Debug,
169 $console_type: console::CastLossy<ConsoleType>,
170 $circuit_type: crate::CastLossy<CircuitType>,
171 {
172 let rng = &mut TestRng::default();
173 for i in 0..ITERATIONS {
174 let (console_value, circuit_value) = sample_values(i, mode, rng);
176
177 let expected = console_value.$fun();
179
180 Circuit::scope("test", || {
182 let result = circuit_value.$fun();
183 assert_eq!(result.eject_value(), expected);
184 assert!(Circuit::is_satisfied());
185 count.assert_matches(
186 Circuit::num_constants_in_scope(),
187 Circuit::num_public_in_scope(),
188 Circuit::num_private_in_scope(),
189 Circuit::num_constraints_in_scope(),
190 );
191 });
192 Circuit::reset();
193 }
194 }
195 };
196}
197#[cfg(test)]
198pub(super) use check_cast_lossy;