/ console / types / scalar / src / zero.rs
zero.rs
 1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
 2  // This file is part of the alphavm 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  
16  use super::*;
17  
18  impl<E: Environment> Zero for Scalar<E> {
19      /// Returns the `0` element of the scalar.
20      fn zero() -> Self {
21          Self::new(E::Scalar::zero())
22      }
23  
24      /// Returns `true` if the element is zero.
25      fn is_zero(&self) -> bool {
26          self.scalar.is_zero()
27      }
28  }
29  
30  #[cfg(test)]
31  mod tests {
32      use super::*;
33      use alphavm_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 = Scalar::<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!(Scalar::<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 scalar: Scalar<CurrentEnvironment> = Uniform::rand(&mut rng);
57              assert!(!scalar.is_zero());
58          }
59      }
60  }