Skip to main content

snarkvm_circuit_program/data/dynamic/record/
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 DynamicRecord<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        // Check the `owner`, `root`, `nonce`, and `version`.
24        self.owner.is_equal(&other.owner)
25            & self.root.is_equal(&other.root)
26            & self.nonce.is_equal(&other.nonce)
27            & self.version.is_equal(&other.version)
28    }
29
30    /// Returns `true` if `self` and `other` are *not* equal.
31    fn is_not_equal(&self, other: &Self) -> Self::Output {
32        !self.is_equal(other)
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use crate::Circuit;
40    use snarkvm_circuit_types::environment::{Eject, Inject, Mode, assert_scope};
41    use snarkvm_utilities::{TestRng, Uniform};
42
43    type CurrentNetwork = <Circuit as Environment>::Network;
44
45    /// Creates a sample dynamic record for testing.
46    fn sample_dynamic_record(mode: Mode, rng: &mut TestRng) -> DynamicRecord<Circuit> {
47        let owner = console::Address::<CurrentNetwork>::rand(rng);
48        let root = console::Field::<CurrentNetwork>::rand(rng);
49        let nonce = console::Group::<CurrentNetwork>::rand(rng);
50        let version = console::U8::<CurrentNetwork>::rand(rng);
51        let console_record = console::DynamicRecord::new_unchecked(owner, root, nonce, version, None);
52        DynamicRecord::new(mode, console_record)
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<()> {
63        let rng = &mut TestRng::default();
64
65        // Sample two distinct dynamic records.
66        let a = sample_dynamic_record(mode, rng);
67        let b = sample_dynamic_record(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<()> {
102        check_equality(Mode::Constant, (6, 0, 11, 17), (0, 0, 17, 17))
103    }
104
105    #[test]
106    fn test_equality_public() -> Result<()> {
107        check_equality(Mode::Public, (6, 0, 11, 17), (0, 0, 17, 17))
108    }
109
110    #[test]
111    fn test_equality_private() -> Result<()> {
112        check_equality(Mode::Private, (6, 0, 11, 17), (0, 0, 17, 17))
113    }
114}