snarkvm_circuit_program/data/literal/cast/
scalar.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
16use super::*;
17
18impl<E: Environment> Cast<Address<E>> for Scalar<E> {
19    /// Casts a `Scalar` to an `Address`.
20    ///
21    /// This operation converts the scalar to a field element, and then attempts to recover
22    /// the group element by treating the field element as an x-coordinate. The group element
23    /// is then converted to an address.
24    ///
25    /// To cast arbitrary scalars to addresses, use `Scalar::cast_lossy`.
26    #[inline]
27    fn cast(&self) -> Address<E> {
28        let field: Field<E> = self.cast();
29        Address::from_field(field)
30    }
31}
32
33impl<E: Environment> Cast<Boolean<E>> for Scalar<E> {
34    /// Casts a `Scalar` to a `Boolean`, if the scalar is zero or one.
35    ///
36    /// To cast arbitrary scalars to booleans, use `Scalar::cast_lossy`.
37    #[inline]
38    fn cast(&self) -> Boolean<E> {
39        let is_one = self.is_one();
40        E::assert(self.is_zero().bitor(&is_one));
41        is_one
42    }
43}
44
45impl<E: Environment> Cast<Group<E>> for Scalar<E> {
46    /// Casts a `Scalar` to a `Group`.
47    ///
48    /// This operation converts the scalar to a field element, and then attempts to recover
49    /// the group element by treating the field element as an x-coordinate.
50    ///
51    /// To cast arbitrary scalars to groups, use `Scalar::cast_lossy`.
52    #[inline]
53    fn cast(&self) -> Group<E> {
54        let field: Field<E> = self.cast();
55        Group::from_x_coordinate(field)
56    }
57}
58
59impl<E: Environment> Cast<Field<E>> for Scalar<E> {
60    /// Casts a `Scalar` to a `Field`.
61    #[inline]
62    fn cast(&self) -> Field<E> {
63        self.to_field()
64    }
65}
66
67impl<E: Environment, I: IntegerType> Cast<Integer<E, I>> for Scalar<E> {
68    /// Casts a `Scalar` to an `Integer`, if the scalar is in the range of the integer.
69    ///
70    /// To cast arbitrary scalars to integers, via truncation, use `Scalar::cast_lossy`.
71    #[inline]
72    fn cast(&self) -> Integer<E, I> {
73        let bits_le = self.to_bits_le();
74        Integer::<E, I>::from_bits_le(&bits_le)
75    }
76}
77
78impl<E: Environment> Cast<Scalar<E>> for Scalar<E> {
79    /// Casts a `Scalar` to a `Scalar`.
80    #[inline]
81    fn cast(&self) -> Scalar<E> {
82        self.clone()
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89    use console::Cast as _;
90    use console_root::{
91        network::MainnetV0,
92        prelude::{One, TestRng, Uniform, Zero},
93    };
94    use snarkvm_circuit_types::environment::{Circuit, Eject, Inject, Mode, UpdatableCount, count_is, count_less_than};
95
96    use std::fmt::Debug;
97
98    const ITERATIONS: usize = 100;
99
100    fn sample_values(
101        i: usize,
102        mode: Mode,
103        rng: &mut TestRng,
104    ) -> (console_root::types::Scalar<MainnetV0>, Scalar<Circuit>) {
105        let console_value = match i {
106            0 => console_root::types::Scalar::<MainnetV0>::zero(),
107            1 => console_root::types::Scalar::<MainnetV0>::one(),
108            _ => Uniform::rand(rng),
109        };
110        let circuit_value = Scalar::<Circuit>::new(mode, console_value);
111        (console_value, circuit_value)
112    }
113
114    impl_check_cast!(cast, Scalar<Circuit>, console_root::types::Scalar::<MainnetV0>);
115
116    #[test]
117    fn test_scalar_to_address() {
118        check_cast::<Address<Circuit>, console_root::types::Address<MainnetV0>>(
119            Mode::Constant,
120            count_less_than!(11, 0, 0, 0),
121        );
122        check_cast::<Address<Circuit>, console_root::types::Address<MainnetV0>>(Mode::Public, count_is!(4, 0, 13, 13));
123        check_cast::<Address<Circuit>, console_root::types::Address<MainnetV0>>(Mode::Private, count_is!(4, 0, 13, 13));
124    }
125
126    #[test]
127    fn test_scalar_to_boolean() {
128        check_cast::<Boolean<Circuit>, console_root::types::Boolean<MainnetV0>>(Mode::Constant, count_is!(4, 0, 0, 0));
129        check_cast::<Boolean<Circuit>, console_root::types::Boolean<MainnetV0>>(Mode::Public, count_is!(2, 0, 5, 6));
130        check_cast::<Boolean<Circuit>, console_root::types::Boolean<MainnetV0>>(Mode::Private, count_is!(2, 0, 5, 6));
131    }
132
133    #[test]
134    fn test_scalar_to_field() {
135        check_cast::<Field<Circuit>, console_root::types::Field<MainnetV0>>(Mode::Constant, count_is!(0, 0, 0, 0));
136        check_cast::<Field<Circuit>, console_root::types::Field<MainnetV0>>(Mode::Public, count_is!(0, 0, 0, 0));
137        check_cast::<Field<Circuit>, console_root::types::Field<MainnetV0>>(Mode::Private, count_is!(0, 0, 0, 0));
138    }
139
140    #[test]
141    fn test_scalar_to_group() {
142        check_cast::<Group<Circuit>, console_root::types::Group<MainnetV0>>(
143            Mode::Constant,
144            count_less_than!(11, 0, 0, 0),
145        );
146        check_cast::<Group<Circuit>, console_root::types::Group<MainnetV0>>(Mode::Public, count_is!(4, 0, 13, 13));
147        check_cast::<Group<Circuit>, console_root::types::Group<MainnetV0>>(Mode::Private, count_is!(4, 0, 13, 13));
148    }
149
150    #[test]
151    fn test_scalar_to_i8() {
152        check_cast::<I8<Circuit>, console_root::types::I8<MainnetV0>>(Mode::Constant, count_is!(251, 0, 0, 0));
153        check_cast::<I8<Circuit>, console_root::types::I8<MainnetV0>>(Mode::Public, count_is!(0, 0, 501, 504));
154        check_cast::<I8<Circuit>, console_root::types::I8<MainnetV0>>(Mode::Private, count_is!(0, 0, 501, 504));
155    }
156
157    #[test]
158    fn test_scalar_to_i16() {
159        check_cast::<I16<Circuit>, console_root::types::I16<MainnetV0>>(Mode::Constant, count_is!(251, 0, 0, 0));
160        check_cast::<I16<Circuit>, console_root::types::I16<MainnetV0>>(Mode::Public, count_is!(0, 0, 501, 504));
161        check_cast::<I16<Circuit>, console_root::types::I16<MainnetV0>>(Mode::Private, count_is!(0, 0, 501, 504));
162    }
163
164    #[test]
165    fn test_scalar_to_i32() {
166        check_cast::<I32<Circuit>, console_root::types::I32<MainnetV0>>(Mode::Constant, count_is!(251, 0, 0, 0));
167        check_cast::<I32<Circuit>, console_root::types::I32<MainnetV0>>(Mode::Public, count_is!(0, 0, 501, 504));
168        check_cast::<I32<Circuit>, console_root::types::I32<MainnetV0>>(Mode::Private, count_is!(0, 0, 501, 504));
169    }
170
171    #[test]
172    fn test_scalar_to_i64() {
173        check_cast::<I64<Circuit>, console_root::types::I64<MainnetV0>>(Mode::Constant, count_is!(251, 0, 0, 0));
174        check_cast::<I64<Circuit>, console_root::types::I64<MainnetV0>>(Mode::Public, count_is!(0, 0, 501, 504));
175        check_cast::<I64<Circuit>, console_root::types::I64<MainnetV0>>(Mode::Private, count_is!(0, 0, 501, 504));
176    }
177
178    #[test]
179    fn test_scalar_to_i128() {
180        check_cast::<I128<Circuit>, console_root::types::I128<MainnetV0>>(Mode::Constant, count_is!(251, 0, 0, 0));
181        check_cast::<I128<Circuit>, console_root::types::I128<MainnetV0>>(Mode::Public, count_is!(0, 0, 501, 504));
182        check_cast::<I128<Circuit>, console_root::types::I128<MainnetV0>>(Mode::Private, count_is!(0, 0, 501, 504));
183    }
184
185    #[test]
186    fn test_scalar_to_scalar() {
187        check_cast::<Scalar<Circuit>, console_root::types::Scalar<MainnetV0>>(Mode::Constant, count_is!(0, 0, 0, 0));
188        check_cast::<Scalar<Circuit>, console_root::types::Scalar<MainnetV0>>(Mode::Public, count_is!(0, 0, 0, 0));
189        check_cast::<Scalar<Circuit>, console_root::types::Scalar<MainnetV0>>(Mode::Private, count_is!(0, 0, 0, 0));
190    }
191
192    #[test]
193    fn test_scalar_to_u8() {
194        check_cast::<U8<Circuit>, console_root::types::U8<MainnetV0>>(Mode::Constant, count_is!(251, 0, 0, 0));
195        check_cast::<U8<Circuit>, console_root::types::U8<MainnetV0>>(Mode::Public, count_is!(0, 0, 501, 504));
196        check_cast::<U8<Circuit>, console_root::types::U8<MainnetV0>>(Mode::Private, count_is!(0, 0, 501, 504));
197    }
198
199    #[test]
200    fn test_scalar_to_u16() {
201        check_cast::<U16<Circuit>, console_root::types::U16<MainnetV0>>(Mode::Constant, count_is!(251, 0, 0, 0));
202        check_cast::<U16<Circuit>, console_root::types::U16<MainnetV0>>(Mode::Public, count_is!(0, 0, 501, 504));
203        check_cast::<U16<Circuit>, console_root::types::U16<MainnetV0>>(Mode::Private, count_is!(0, 0, 501, 504));
204    }
205
206    #[test]
207    fn test_scalar_to_u32() {
208        check_cast::<U32<Circuit>, console_root::types::U32<MainnetV0>>(Mode::Constant, count_is!(251, 0, 0, 0));
209        check_cast::<U32<Circuit>, console_root::types::U32<MainnetV0>>(Mode::Public, count_is!(0, 0, 501, 504));
210        check_cast::<U32<Circuit>, console_root::types::U32<MainnetV0>>(Mode::Private, count_is!(0, 0, 501, 504));
211    }
212
213    #[test]
214    fn test_scalar_to_u64() {
215        check_cast::<U64<Circuit>, console_root::types::U64<MainnetV0>>(Mode::Constant, count_is!(251, 0, 0, 0));
216        check_cast::<U64<Circuit>, console_root::types::U64<MainnetV0>>(Mode::Public, count_is!(0, 0, 501, 504));
217        check_cast::<U64<Circuit>, console_root::types::U64<MainnetV0>>(Mode::Private, count_is!(0, 0, 501, 504));
218    }
219
220    #[test]
221    fn test_scalar_to_u128() {
222        check_cast::<U128<Circuit>, console_root::types::U128<MainnetV0>>(Mode::Constant, count_is!(251, 0, 0, 0));
223        check_cast::<U128<Circuit>, console_root::types::U128<MainnetV0>>(Mode::Public, count_is!(0, 0, 501, 504));
224        check_cast::<U128<Circuit>, console_root::types::U128<MainnetV0>>(Mode::Private, count_is!(0, 0, 501, 504));
225    }
226}