snarkvm_circuit_program/data/dynamic/future/
equal.rs1use super::*;
17
18impl<A: Aleo> Equal<Self> for DynamicFuture<A> {
19 type Output = Boolean<A>;
20
21 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 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 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 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 let a = sample_dynamic_future(mode, rng);
67 let b = sample_dynamic_future(mode, rng);
68
69 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 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 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 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}