Skip to main content

snarkvm_console_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<N: Network> Eq for DynamicRecord<N> {}
19
20impl<N: Network> PartialEq for DynamicRecord<N> {
21    /// Returns `true` if `self` and `other` are equal.
22    fn eq(&self, other: &Self) -> bool {
23        *self.is_equal(other)
24    }
25}
26
27impl<N: Network> Equal<Self> for DynamicRecord<N> {
28    type Output = Boolean<N>;
29
30    /// Returns `true` if `self` and `other` are equal.
31    fn is_equal(&self, other: &Self) -> Self::Output {
32        // Check the `owner`, `root`, `nonce`, and `version`.
33        self.owner.is_equal(&other.owner)
34            & self.root.is_equal(&other.root)
35            & self.nonce.is_equal(&other.nonce)
36            & self.version.is_equal(&other.version)
37    }
38
39    /// Returns `true` if `self` and `other` are *not* equal.
40    fn is_not_equal(&self, other: &Self) -> Self::Output {
41        !self.is_equal(other)
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48    use snarkvm_console_network::MainnetV0;
49
50    use std::str::FromStr;
51
52    type CurrentNetwork = MainnetV0;
53
54    fn sample_record() -> DynamicRecord<CurrentNetwork> {
55        DynamicRecord::from_record(
56            &Record::<CurrentNetwork, Plaintext<CurrentNetwork>>::from_str(
57                r"{
58    owner: aleo14tlamssdmg3d0p5zmljma573jghe2q9n6wz29qf36re2glcedcpqfg4add.private,
59    a: true.private,
60    b: 123456789field.public,
61    c: 0group.private,
62    d: {
63        e: true.private,
64        f: 123456789field.private,
65        g: 0group.private
66    },
67    _nonce: 0group.public,
68    _version: 0u8.public
69}",
70            )
71            .unwrap(),
72        )
73        .unwrap()
74    }
75
76    fn sample_mismatched_record() -> DynamicRecord<CurrentNetwork> {
77        DynamicRecord::from_record(
78            &Record::<CurrentNetwork, Plaintext<CurrentNetwork>>::from_str(
79                r"{
80    owner: aleo14tlamssdmg3d0p5zmljma573jghe2q9n6wz29qf36re2glcedcpqfg4add.private,
81    a: true.public,
82    b: 123456789field.public,
83    c: 0group.private,
84    d: {
85        e: true.private,
86        f: 123456789field.private,
87        g: 0group.private
88    },
89    _nonce: 0group.public,
90    _version: 0u8.public
91}",
92            )
93            .unwrap(),
94        )
95        .unwrap()
96    }
97
98    fn check_is_equal() {
99        // Sample the record.
100        let record = sample_record();
101        let mismatched_record = sample_mismatched_record();
102
103        let candidate = record.is_equal(&record);
104        assert!(*candidate);
105
106        let candidate = record.is_equal(&mismatched_record);
107        assert!(!*candidate);
108    }
109
110    fn check_is_not_equal() {
111        // Sample the record.
112        let record = sample_record();
113        let mismatched_record = sample_mismatched_record();
114
115        let candidate = record.is_not_equal(&mismatched_record);
116        assert!(*candidate);
117
118        let candidate = record.is_not_equal(&record);
119        assert!(!*candidate);
120    }
121
122    #[test]
123    fn test_is_equal() {
124        check_is_equal()
125    }
126
127    #[test]
128    fn test_is_not_equal() {
129        check_is_not_equal()
130    }
131}