Skip to main content

snarkvm_circuit_program/data/literal/cast_lossy/
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 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 (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
97/// A helper macro to implement the body of the `cast_lossy` methods.
98macro_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
129/// Casts a boolean literal to the given literal type, with lossy truncation.
130fn 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
134/// Casts a field literal to the given literal type, with lossy truncation.
135fn 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
139/// Casts a group literal to the given literal type, with lossy truncation.
140fn 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
148/// Casts an integer literal to the given literal type, with lossy truncation.
149fn 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
156/// Casts a scalar literal to the given literal type, with lossy truncation.
157fn 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                // Sample a random value.
175                let (console_value, circuit_value) = sample_values(i, mode, rng);
176
177                // Compute the expected result.
178                let expected = console_value.$fun();
179
180                // Run the test.
181                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;