snarkvm_console_types_group/
from_field.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> FromField for Group<E> {
19    type Field = Field<E>;
20
21    /// Initializes a new group by recovering the **x-coordinate** of an affine group from a field element.
22    fn from_field(field: &Self::Field) -> Result<Self> {
23        Group::from_x_coordinate(*field)
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30    use snarkvm_console_network_environment::Console;
31
32    type CurrentEnvironment = Console;
33
34    const ITERATIONS: u64 = 10_000;
35
36    fn check_from_field() -> Result<()> {
37        let mut rng = TestRng::default();
38
39        for _ in 0..ITERATIONS {
40            // Sample a random value.
41            let expected = Group::<CurrentEnvironment>::new(Uniform::rand(&mut rng));
42            let candidate = Group::<CurrentEnvironment>::from_field(&expected.to_field()?)?;
43            assert_eq!(expected, candidate);
44        }
45        Ok(())
46    }
47
48    #[test]
49    fn test_from_field() -> Result<()> {
50        check_from_field()
51    }
52}