1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;

impl<E: Environment> CastLossy<Address<E>> for Scalar<E> {
    /// Casts a `Scalar` to an `Address`.
    ///
    /// This operation converts the scalar into a field element, and then attempts to recover
    /// the group element to construct the address. See the documentation of `Field::cast_lossy`
    /// on the `Group` type for more details.
    #[inline]
    fn cast_lossy(&self) -> Address<E> {
        let field: Field<E> = self.cast_lossy();
        field.cast_lossy()
    }
}

impl<E: Environment> CastLossy<Boolean<E>> for Scalar<E> {
    /// Casts a `Scalar` to a `Boolean`, with lossy truncation.
    /// This operation returns the least significant bit of the field.
    #[inline]
    fn cast_lossy(&self) -> Boolean<E> {
        let bits_le = self.to_bits_le();
        debug_assert!(!bits_le.is_empty(), "An integer must have at least one bit");
        Boolean::new(bits_le[0])
    }
}

impl<E: Environment> CastLossy<Group<E>> for Scalar<E> {
    /// Casts a `Scalar` to a `Group`.
    ///
    /// This operation converts the scalar into a field element, and then attempts to recover
    /// the group element. See the documentation of `Field::cast_lossy` on the `Group` type
    /// for more details.
    #[inline]
    fn cast_lossy(&self) -> Group<E> {
        let field: Field<E> = self.cast_lossy();
        field.cast_lossy()
    }
}

impl<E: Environment> CastLossy<Field<E>> for Scalar<E> {
    /// Casts a `Scalar` to a `Field`.
    /// This operation is **always** lossless.
    #[inline]
    fn cast_lossy(&self) -> Field<E> {
        let result = self.to_field();
        debug_assert!(result.is_ok(), "A scalar should always be able to be converted to a field");
        result.unwrap()
    }
}

impl<E: Environment, I: IntegerType> CastLossy<Integer<E, I>> for Scalar<E> {
    /// Casts a `Scalar` to an `Integer`, with lossy truncation.
    #[inline]
    fn cast_lossy(&self) -> Integer<E, I> {
        // Note: We are reconstituting the integer from the scalar field.
        // This is safe as the number of bits in the integer is less than the scalar field modulus,
        // and thus will always fit within a single scalar field element.
        debug_assert!(I::BITS < Scalar::<E>::size_in_bits() as u64);

        // Truncate the field to the size of the integer domain.
        // Slicing here is safe as the base field is larger than the integer domain.
        let result = Integer::<E, I>::from_bits_le(&self.to_bits_le()[..usize::try_from(I::BITS).unwrap()]);
        debug_assert!(result.is_ok(), "A lossy integer should always be able to be constructed from scalar bits");
        result.unwrap()
    }
}

impl<E: Environment> CastLossy<Scalar<E>> for Scalar<E> {
    /// Casts a `Scalar` to a `Scalar`.
    /// This is an identity cast, so it is **always** lossless.
    #[inline]
    fn cast_lossy(&self) -> Scalar<E> {
        *self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    type CurrentEnvironment = Console;

    const ITERATIONS: u64 = 10_000;

    #[test]
    fn test_scalar_to_address() {
        let rng = &mut TestRng::default();

        let scalar = Scalar::<CurrentEnvironment>::one();
        let address: Address<CurrentEnvironment> = scalar.cast_lossy();
        assert_eq!(address, Address::new(Group::generator()));
        assert_eq!(address.to_group(), &Group::generator());

        let scalar = Scalar::<CurrentEnvironment>::zero();
        let address: Address<CurrentEnvironment> = scalar.cast_lossy();
        assert_eq!(address, Address::zero());
        assert_eq!(address.to_group(), &Group::zero());

        for _ in 0..ITERATIONS {
            // Sample a random scalar.
            let scalar = Scalar::<CurrentEnvironment>::rand(rng);
            // Perform the operation.
            let candidate = scalar.cast_lossy();
            // Compare the result against the group element. (This is the most we can do.)
            let expected: Group<CurrentEnvironment> = scalar.cast_lossy();
            assert_eq!(Address::new(expected), candidate);
        }
    }

    #[test]
    fn test_scalar_to_boolean() {
        let rng = &mut TestRng::default();

        let scalar = Scalar::<CurrentEnvironment>::one();
        let boolean: Boolean<CurrentEnvironment> = scalar.cast_lossy();
        assert_eq!(boolean, Boolean::new(true));

        let scalar = Scalar::<CurrentEnvironment>::zero();
        let boolean: Boolean<CurrentEnvironment> = scalar.cast_lossy();
        assert_eq!(boolean, Boolean::new(false));

        for _ in 0..ITERATIONS {
            // Sample a random scalar.
            let scalar = Scalar::<CurrentEnvironment>::rand(rng);
            // Perform the operation.
            let candidate = scalar.cast_lossy();
            // Compare the result against the least significant bit of the scalar.
            let expected = Boolean::new(scalar.to_bits_be().pop().unwrap());
            assert_eq!(expected, candidate);
        }
    }

    #[test]
    fn test_scalar_to_field() {
        let rng = &mut TestRng::default();

        for _ in 0..ITERATIONS {
            // Sample a random scalar.
            let scalar = Scalar::<CurrentEnvironment>::rand(rng);
            // Perform the operation.
            let candidate = scalar.cast_lossy();
            assert_eq!(scalar.to_field().unwrap(), candidate);
        }
    }

    #[test]
    fn test_scalar_to_group() {
        let rng = &mut TestRng::default();

        let scalar = Scalar::<CurrentEnvironment>::one();
        let group: Group<CurrentEnvironment> = scalar.cast_lossy();
        assert_eq!(group, Group::generator());

        let scalar = Scalar::<CurrentEnvironment>::zero();
        let group: Group<CurrentEnvironment> = scalar.cast_lossy();
        assert_eq!(group, Group::zero());

        for _ in 0..ITERATIONS {
            // Sample a random scalar.
            let scalar = Scalar::<CurrentEnvironment>::rand(rng);
            // Perform the operation.
            let candidate: Group<CurrentEnvironment> = scalar.cast_lossy();
            // Compare the result against the address. (This is the most we can do.)
            let expected: Address<CurrentEnvironment> = scalar.cast_lossy();
            assert_eq!(expected.to_group(), &candidate);
        }
    }

    #[test]
    fn test_scalar_to_scalar() {
        let rng = &mut TestRng::default();

        for _ in 0..ITERATIONS {
            // Sample a random scalar.
            let scalar = Scalar::<CurrentEnvironment>::rand(rng);
            // Perform the operation.
            let candidate: Scalar<CurrentEnvironment> = scalar.cast_lossy();
            assert_eq!(scalar, candidate);
        }
    }

    macro_rules! check_scalar_to_integer {
        ($type:ty) => {
            let rng = &mut TestRng::default();

            let scalar = Scalar::<CurrentEnvironment>::one();
            let integer: Integer<CurrentEnvironment, $type> = scalar.cast_lossy();
            assert_eq!(integer, Integer::<CurrentEnvironment, $type>::one());

            let scalar = Scalar::<CurrentEnvironment>::zero();
            let integer: Integer<CurrentEnvironment, $type> = scalar.cast_lossy();
            assert_eq!(integer, Integer::<CurrentEnvironment, $type>::zero());

            for _ in 0..ITERATIONS {
                // Sample a random scalar.
                let scalar = Scalar::<CurrentEnvironment>::rand(rng);
                // Perform the operation.
                let candidate: Integer<CurrentEnvironment, $type> = scalar.cast_lossy();
                // Compare the result against the least significant bits of the scalar.
                let expected = Integer::<CurrentEnvironment, $type>::from_bits_le(
                    &scalar.to_bits_le()[..usize::try_from(<$type>::BITS).unwrap()],
                )
                .unwrap();
                assert_eq!(expected, candidate);
            }
        };
    }

    #[test]
    fn test_scalar_to_i8() {
        check_scalar_to_integer!(i8);
    }

    #[test]
    fn test_scalar_to_i16() {
        check_scalar_to_integer!(i16);
    }

    #[test]
    fn test_scalar_to_i32() {
        check_scalar_to_integer!(i32);
    }

    #[test]
    fn test_scalar_to_i64() {
        check_scalar_to_integer!(i64);
    }

    #[test]
    fn test_scalar_to_i128() {
        check_scalar_to_integer!(i128);
    }

    #[test]
    fn test_scalar_to_u8() {
        check_scalar_to_integer!(u8);
    }

    #[test]
    fn test_scalar_to_u16() {
        check_scalar_to_integer!(u16);
    }

    #[test]
    fn test_scalar_to_u32() {
        check_scalar_to_integer!(u32);
    }

    #[test]
    fn test_scalar_to_u64() {
        check_scalar_to_integer!(u64);
    }

    #[test]
    fn test_scalar_to_u128() {
        check_scalar_to_integer!(u128);
    }
}