snarkvm_console_types_field/
zero.rs

1// Copyright 2024 Aleo Network Foundation
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> Zero for Field<E> {
19    /// Returns the `0` element of the field.
20    fn zero() -> Self {
21        Self::new(E::Field::zero())
22    }
23
24    /// Returns `true` if the element is zero.
25    fn is_zero(&self) -> bool {
26        self.field.is_zero()
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_zero() {
41        let zero = Field::<CurrentEnvironment>::zero();
42
43        for bit in zero.to_bits_le().iter() {
44            assert!(!bit)
45        }
46    }
47
48    #[test]
49    fn test_is_zero() {
50        assert!(Field::<CurrentEnvironment>::zero().is_zero());
51
52        let mut rng = TestRng::default();
53
54        // Note: This test technically has a `1 / MODULUS` probability of being flaky.
55        for _ in 0..ITERATIONS {
56            let field: Field<CurrentEnvironment> = Uniform::rand(&mut rng);
57            assert!(!field.is_zero());
58        }
59    }
60}