Skip to main content

snarkvm_console_types_integers/
from_fields.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
16use super::*;
17
18impl<E: Environment, I: IntegerType> FromFields for Integer<E, I> {
19    type Field = Field<E>;
20
21    /// Initializes a new integer by recovering the **x-coordinate** of an affine group from a field element.
22    fn from_fields(fields: &[Self::Field]) -> Result<Self> {
23        // Ensure there is exactly one field element in the vector.
24        ensure!(fields.len() == 1, "Integer must be recovered from a single field element");
25        // Recover the integer from the field element.
26        Self::from_field(&fields[0])
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use snarkvm_console_network_environment::Console;
34
35    type CurrentEnvironment = Console;
36
37    const ITERATIONS: u64 = 10_000;
38
39    fn check_from_fields<I: IntegerType>() -> Result<()> {
40        let mut rng = TestRng::default();
41
42        for _ in 0..ITERATIONS {
43            // Sample a random integer.
44            let expected = Integer::<CurrentEnvironment, I>::rand(&mut rng);
45
46            // Perform the operation.
47            let candidate = Integer::from_fields(&expected.to_fields()?)?;
48            assert_eq!(expected, candidate);
49        }
50        Ok(())
51    }
52
53    #[test]
54    fn test_u8_from_fields() -> Result<()> {
55        type I = u8;
56        check_from_fields::<I>()
57    }
58
59    #[test]
60    fn test_i8_from_fields() -> Result<()> {
61        type I = i8;
62        check_from_fields::<I>()
63    }
64
65    #[test]
66    fn test_u16_from_fields() -> Result<()> {
67        type I = u16;
68        check_from_fields::<I>()
69    }
70
71    #[test]
72    fn test_i16_from_fields() -> Result<()> {
73        type I = i16;
74        check_from_fields::<I>()
75    }
76
77    #[test]
78    fn test_u32_from_fields() -> Result<()> {
79        type I = u32;
80        check_from_fields::<I>()
81    }
82
83    #[test]
84    fn test_i32_from_fields() -> Result<()> {
85        type I = i32;
86        check_from_fields::<I>()
87    }
88
89    #[test]
90    fn test_u64_from_fields() -> Result<()> {
91        type I = u64;
92        check_from_fields::<I>()
93    }
94
95    #[test]
96    fn test_i64_from_fields() -> Result<()> {
97        type I = i64;
98        check_from_fields::<I>()
99    }
100
101    #[test]
102    fn test_u128_from_fields() -> Result<()> {
103        type I = u128;
104        check_from_fields::<I>()
105    }
106
107    #[test]
108    fn test_i128_from_fields() -> Result<()> {
109        type I = i128;
110        check_from_fields::<I>()
111    }
112}