snarkvm_console_program/data/record/
equal.rs

1// Copyright 2024 Aleo Network Foundation
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, Private: Visibility<Boolean = Boolean<N>>> Eq for Record<N, Private> {}
19
20impl<N: Network, Private: Visibility<Boolean = Boolean<N>>> PartialEq for Record<N, Private> {
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, Private: Visibility<Boolean = Boolean<N>>> Equal<Self> for Record<N, Private> {
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        // Ensure the `data` lengths are equal.
33        if self.data.len() != other.data.len() {
34            return Boolean::new(false);
35        }
36
37        // Check the `owner`, and `nonce`.
38        if !(*self.owner.is_equal(&other.owner) && *self.nonce.is_equal(&other.nonce)) {
39            return Boolean::new(false);
40        }
41
42        // Recursively check each entry for equality.
43        if self
44            .data
45            .iter()
46            .zip_eq(other.data.iter())
47            .all(|((name_a, entry_a), (name_b, entry_b))| *name_a.is_equal(name_b) && *entry_a.is_equal(entry_b))
48        {
49            Boolean::new(true)
50        } else {
51            Boolean::new(false)
52        }
53    }
54
55    /// Returns `true` if `self` and `other` are *not* equal.
56    fn is_not_equal(&self, other: &Self) -> Self::Output {
57        !self.is_equal(other)
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64    use snarkvm_console_network::MainnetV0;
65
66    type CurrentNetwork = MainnetV0;
67
68    fn sample_record() -> Record<CurrentNetwork, Plaintext<CurrentNetwork>> {
69        Record::<CurrentNetwork, Plaintext<CurrentNetwork>>::from_str(
70            r"{
71    owner: aleo14tlamssdmg3d0p5zmljma573jghe2q9n6wz29qf36re2glcedcpqfg4add.private,
72    a: true.private,
73    b: 123456789field.public,
74    c: 0group.private,
75    d: {
76        e: true.private,
77        f: 123456789field.private,
78        g: 0group.private
79    },
80    _nonce: 0group.public
81}",
82        )
83        .unwrap()
84    }
85
86    fn sample_mismatched_record() -> Record<CurrentNetwork, Plaintext<CurrentNetwork>> {
87        Record::<CurrentNetwork, Plaintext<CurrentNetwork>>::from_str(
88            r"{
89    owner: aleo14tlamssdmg3d0p5zmljma573jghe2q9n6wz29qf36re2glcedcpqfg4add.private,
90    a: true.public,
91    b: 123456789field.public,
92    c: 0group.private,
93    d: {
94        e: true.private,
95        f: 123456789field.private,
96        g: 0group.private
97    },
98    _nonce: 0group.public
99}",
100        )
101        .unwrap()
102    }
103
104    fn check_is_equal() {
105        // Sample the record.
106        let record = sample_record();
107        let mismatched_record = sample_mismatched_record();
108
109        let candidate = record.is_equal(&record);
110        assert!(*candidate);
111
112        let candidate = record.is_equal(&mismatched_record);
113        assert!(!*candidate);
114    }
115
116    fn check_is_not_equal() {
117        // Sample the record.
118        let record = sample_record();
119        let mismatched_record = sample_mismatched_record();
120
121        let candidate = record.is_not_equal(&mismatched_record);
122        assert!(*candidate);
123
124        let candidate = record.is_not_equal(&record);
125        assert!(!*candidate);
126    }
127
128    #[test]
129    fn test_is_equal() {
130        check_is_equal()
131    }
132
133    #[test]
134    fn test_is_not_equal() {
135        check_is_not_equal()
136    }
137}