snarkvm_circuit_program/data/literal/cast_lossy/
mod.rs

1// Copyright (c) 2019-2025 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 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
52/// Unary operator for casting values of one type to another, with lossy truncation.
53pub trait CastLossy<T: Sized = Self> {
54    /// Casts the value of `self` into a value of type `T`, with lossy truncation.
55    ///
56    /// This method makes a *best-effort* attempt to preserve all bits of information,
57    /// but it is not guaranteed to do so.
58    fn cast_lossy(&self) -> T;
59}
60
61impl<A: Aleo> Literal<A> {
62    /// Casts the literal to the given literal type, with lossy truncation.
63    ///
64    /// This method makes a *best-effort* attempt to preserve all bits of information,
65    /// but it is not guaranteed to do so.
66    ///
67    /// The hierarchy of casting is as follows:
68    ///  - (`Address`, `Group`) <-> `Field` <-> `Scalar` <-> `Integer` <-> `Boolean`
69    ///  - `Signature` (not supported)
70    ///  - `String` (not supported)
71    ///
72    /// Note that casting to left along the hierarchy always preserves information.
73    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 a signature literal to another type."),
91            Self::String(..) => bail!("Cannot cast a string literal to another type."),
92        }
93    }
94}
95
96/// A helper macro to implement the body of the `cast_lossy` methods.
97macro_rules! impl_cast_body {
98    ($type_name:ident, $cast_lossy:ident, $input:expr, $to_type:expr) => {
99        match $to_type {
100            LiteralType::Address => Ok(Literal::Address($input.$cast_lossy())),
101            LiteralType::Boolean => Ok(Literal::Boolean($input.$cast_lossy())),
102            LiteralType::Field => Ok(Literal::Field($input.$cast_lossy())),
103            LiteralType::Group => Ok(Literal::Group($input.$cast_lossy())),
104            LiteralType::I8 => Ok(Literal::I8($input.$cast_lossy())),
105            LiteralType::I16 => Ok(Literal::I16($input.$cast_lossy())),
106            LiteralType::I32 => Ok(Literal::I32($input.$cast_lossy())),
107            LiteralType::I64 => Ok(Literal::I64($input.$cast_lossy())),
108            LiteralType::I128 => Ok(Literal::I128($input.$cast_lossy())),
109            LiteralType::U8 => Ok(Literal::U8($input.$cast_lossy())),
110            LiteralType::U16 => Ok(Literal::U16($input.$cast_lossy())),
111            LiteralType::U32 => Ok(Literal::U32($input.$cast_lossy())),
112            LiteralType::U64 => Ok(Literal::U64($input.$cast_lossy())),
113            LiteralType::U128 => Ok(Literal::U128($input.$cast_lossy())),
114            LiteralType::Scalar => Ok(Literal::Scalar($input.$cast_lossy())),
115            LiteralType::Signature => {
116                bail!(concat!("Cannot cast (lossy) a ", stringify!($type_name), " literal to a signature type."))
117            }
118            LiteralType::String => {
119                bail!(concat!("Cannot cast (lossy) a ", stringify!($type_name), " literal to a string type."))
120            }
121        }
122    };
123}
124
125/// Casts a boolean literal to the given literal type, with lossy truncation.
126fn cast_lossy_boolean_to_type<A: Aleo>(input: &Boolean<A>, to_type: LiteralType) -> Result<Literal<A>> {
127    impl_cast_body!(boolean, cast_lossy, input, to_type)
128}
129
130/// Casts a field literal to the given literal type, with lossy truncation.
131fn cast_lossy_field_to_type<A: Aleo>(input: &Field<A>, to_type: LiteralType) -> Result<Literal<A>> {
132    impl_cast_body!(field, cast_lossy, input, to_type)
133}
134
135/// Casts a group literal to the given literal type, with lossy truncation.
136fn cast_lossy_group_to_type<A: Aleo>(input: &Group<A>, to_type: LiteralType) -> Result<Literal<A>> {
137    match to_type {
138        LiteralType::Address => Ok(Literal::Address(Address::from_group(input.clone()))),
139        LiteralType::Group => Ok(Literal::Group(input.clone())),
140        _ => cast_lossy_field_to_type(&input.to_x_coordinate(), to_type),
141    }
142}
143
144/// Casts an integer literal to the given literal type, with lossy truncation.
145fn cast_lossy_integer_to_type<A: Aleo, I: IntegerType>(
146    input: &Integer<A, I>,
147    to_type: LiteralType,
148) -> Result<Literal<A>> {
149    impl_cast_body!(integer, cast_lossy, input, to_type)
150}
151
152/// Casts a scalar literal to the given literal type, with lossy truncation.
153fn cast_lossy_scalar_to_type<A: Aleo>(input: &Scalar<A>, to_type: LiteralType) -> Result<Literal<A>> {
154    impl_cast_body!(scalar, cast_lossy, input, to_type)
155}
156
157#[cfg(test)]
158macro_rules! check_cast_lossy {
159    ($fun:ident, $circuit_type:ty, $console_type:ty) => {
160        fn check_cast_lossy<CircuitType, ConsoleType>(mode: Mode, count: UpdatableCount)
161        where
162            CircuitType: Eject,
163            <CircuitType as Eject>::Primitive: Debug + PartialEq<ConsoleType>,
164            ConsoleType: Debug,
165            $console_type: console::CastLossy<ConsoleType>,
166            $circuit_type: crate::CastLossy<CircuitType>,
167        {
168            let rng = &mut TestRng::default();
169            for i in 0..ITERATIONS {
170                // Sample a random value.
171                let (console_value, circuit_value) = sample_values(i, mode, rng);
172
173                // Compute the expected result.
174                let expected = console_value.$fun();
175
176                // Run the test.
177                Circuit::scope("test", || {
178                    let result = circuit_value.$fun();
179                    assert_eq!(result.eject_value(), expected);
180                    assert!(Circuit::is_satisfied());
181                    count.assert_matches(
182                        Circuit::num_constants_in_scope(),
183                        Circuit::num_public_in_scope(),
184                        Circuit::num_private_in_scope(),
185                        Circuit::num_constraints_in_scope(),
186                    );
187                });
188                Circuit::reset();
189            }
190        }
191    };
192}
193#[cfg(test)]
194pub(super) use check_cast_lossy;