snarkvm_console_types_integers/
from_fields.rs1use super::*;
17
18impl<E: Environment, I: IntegerType> FromFields for Integer<E, I> {
19 type Field = Field<E>;
20
21 fn from_fields(fields: &[Self::Field]) -> Result<Self> {
23 ensure!(fields.len() == 1, "Integer must be recovered from a single field element");
25 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 let expected = Integer::<CurrentEnvironment, I>::rand(&mut rng);
45
46 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}