Skip to main content

snarkvm_circuit_program/data/dynamic/future/
equal.rs

1// Copyright (c) 2019-2026 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<A: Aleo> Equal<Self> for DynamicFuture<A> {
19    type Output = Boolean<A>;
20
21    /// Returns `true` if `self` and `other` are equal.
22    fn is_equal(&self, other: &Self) -> Self::Output {
23        self.program_name.is_equal(&other.program_name)
24            & self.program_network.is_equal(&other.program_network)
25            & self.function_name.is_equal(&other.function_name)
26            & self.checksum.is_equal(&other.checksum)
27    }
28
29    /// Returns `true` if `self` and `other` are *not* equal.
30    fn is_not_equal(&self, other: &Self) -> Self::Output {
31        !self.is_equal(other)
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use crate::Circuit;
39    use snarkvm_circuit_types::environment::{Eject, Inject, Mode, assert_scope};
40    use snarkvm_utilities::{TestRng, Uniform};
41
42    type CurrentNetwork = <Circuit as Environment>::Network;
43
44    /// Creates a sample dynamic future for testing.
45    fn sample_dynamic_future(mode: Mode, rng: &mut TestRng) -> DynamicFuture<Circuit> {
46        let program_name = console::Field::<CurrentNetwork>::rand(rng);
47        let program_network = console::Field::<CurrentNetwork>::rand(rng);
48        let function_name = console::Field::<CurrentNetwork>::rand(rng);
49        let root = console::Field::<CurrentNetwork>::rand(rng);
50        let console_future =
51            console::DynamicFuture::new_unchecked(program_name, program_network, function_name, root, None);
52        DynamicFuture::new(mode, console_future)
53    }
54
55    /// Tests equality operations for the given mode.
56    /// - `equal_counts`: (constants, public, private, constraints) for self-comparison
57    /// - `unequal_counts`: (constants, public, private, constraints) for different-value comparison
58    fn check_equality(
59        mode: Mode,
60        equal_counts: (u64, u64, u64, u64),
61        unequal_counts: (u64, u64, u64, u64),
62    ) -> Result<(), console::Error> {
63        let rng = &mut TestRng::default();
64
65        // Sample two distinct dynamic futures.
66        let a = sample_dynamic_future(mode, rng);
67        let b = sample_dynamic_future(mode, rng);
68
69        // Test is_equal on self (should be true).
70        Circuit::scope(format!("{mode} is_equal(self)"), || {
71            assert!(a.is_equal(&a).eject_value());
72            assert_scope!(equal_counts.0, equal_counts.1, equal_counts.2, equal_counts.3);
73        });
74        Circuit::reset();
75
76        // Test is_equal on different values (should be false).
77        Circuit::scope(format!("{mode} is_equal(other)"), || {
78            assert!(!a.is_equal(&b).eject_value());
79            assert_scope!(unequal_counts.0, unequal_counts.1, unequal_counts.2, unequal_counts.3);
80        });
81        Circuit::reset();
82
83        // Test is_not_equal on self (should be false).
84        Circuit::scope(format!("{mode} is_not_equal(self)"), || {
85            assert!(!a.is_not_equal(&a).eject_value());
86            assert_scope!(equal_counts.0, equal_counts.1, equal_counts.2, equal_counts.3);
87        });
88        Circuit::reset();
89
90        // Test is_not_equal on different values (should be true).
91        Circuit::scope(format!("{mode} is_not_equal(other)"), || {
92            assert!(a.is_not_equal(&b).eject_value());
93            assert_scope!(unequal_counts.0, unequal_counts.1, unequal_counts.2, unequal_counts.3);
94        });
95        Circuit::reset();
96
97        Ok(())
98    }
99
100    #[test]
101    fn test_equality_constant() -> Result<(), console::Error> {
102        check_equality(Mode::Constant, (4, 0, 0, 0), (4, 0, 0, 0))
103    }
104
105    #[test]
106    fn test_equality_public() -> Result<(), console::Error> {
107        check_equality(Mode::Public, (4, 0, 7, 11), (0, 0, 11, 11))
108    }
109
110    #[test]
111    fn test_equality_private() -> Result<(), console::Error> {
112        check_equality(Mode::Private, (4, 0, 7, 11), (0, 0, 11, 11))
113    }
114}