snarkvm_console_types_field/
one.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> One for Field<E> {
19    /// Returns the `1` element of the field.
20    fn one() -> Self {
21        Self::new(E::Field::one())
22    }
23
24    /// Returns `true` if the element is one.
25    fn is_one(&self) -> bool {
26        self.field.is_one()
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 = 100;
38
39    #[test]
40    fn test_one() {
41        let one = Field::<CurrentEnvironment>::one();
42
43        for (index, bit) in one.to_bits_le().iter().enumerate() {
44            match index == 0 {
45                true => assert!(bit),
46                false => assert!(!bit),
47            }
48        }
49    }
50
51    #[test]
52    fn test_is_one() {
53        assert!(Field::<CurrentEnvironment>::one().is_one());
54
55        let mut rng = TestRng::default();
56
57        // Note: This test technically has a `1 / MODULUS` probability of being flaky.
58        for _ in 0..ITERATIONS {
59            let field: Field<CurrentEnvironment> = Uniform::rand(&mut rng);
60            assert!(!field.is_one());
61        }
62    }
63}