snarkvm_console_types_group/
to_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> ToField for Group<E> {
19    type Field = Field<E>;
20
21    /// Returns the group as a field element.
22    fn to_field(&self) -> Result<Self::Field> {
23        Ok(self.to_x_coordinate())
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    #[test]
37    fn test_to_field() -> Result<()> {
38        let mut rng = TestRng::default();
39
40        for _ in 0..ITERATIONS {
41            // Sample a random value.
42            let group: Group<CurrentEnvironment> = Uniform::rand(&mut rng);
43
44            let candidate = group.to_field()?;
45
46            let expected = group.to_bits_le();
47            for (index, candidate_bit) in candidate.to_bits_le().iter().enumerate() {
48                assert_eq!(expected[index], *candidate_bit);
49            }
50        }
51        Ok(())
52    }
53}