snarkvm_console_program/data/literal/
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> Eq for Literal<N> {}
19
20impl<N: Network> PartialEq for Literal<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> core::hash::Hash for Literal<N> {
28    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
29        match self {
30            Self::Address(a) => a.hash(state),
31            Self::Boolean(a) => a.hash(state),
32            Self::Field(a) => a.hash(state),
33            Self::Group(a) => a.hash(state),
34            Self::I8(a) => a.hash(state),
35            Self::I16(a) => a.hash(state),
36            Self::I32(a) => a.hash(state),
37            Self::I64(a) => a.hash(state),
38            Self::I128(a) => a.hash(state),
39            Self::U8(a) => a.hash(state),
40            Self::U16(a) => a.hash(state),
41            Self::U32(a) => a.hash(state),
42            Self::U64(a) => a.hash(state),
43            Self::U128(a) => a.hash(state),
44            Self::Scalar(a) => a.hash(state),
45            Self::Signature(a) => a.hash(state),
46            Self::String(a) => a.hash(state),
47        }
48    }
49}
50
51impl<N: Network> Equal for Literal<N> {
52    type Output = Boolean<N>;
53
54    /// Returns `true` if `self` and `other` are equal.
55    fn is_equal(&self, other: &Self) -> Self::Output {
56        match (self, other) {
57            (Self::Address(a), Self::Address(b)) => a.is_equal(b),
58            (Self::Boolean(a), Self::Boolean(b)) => a.is_equal(b),
59            (Self::Field(a), Self::Field(b)) => a.is_equal(b),
60            (Self::Group(a), Self::Group(b)) => a.is_equal(b),
61            (Self::I8(a), Self::I8(b)) => a.is_equal(b),
62            (Self::I16(a), Self::I16(b)) => a.is_equal(b),
63            (Self::I32(a), Self::I32(b)) => a.is_equal(b),
64            (Self::I64(a), Self::I64(b)) => a.is_equal(b),
65            (Self::I128(a), Self::I128(b)) => a.is_equal(b),
66            (Self::U8(a), Self::U8(b)) => a.is_equal(b),
67            (Self::U16(a), Self::U16(b)) => a.is_equal(b),
68            (Self::U32(a), Self::U32(b)) => a.is_equal(b),
69            (Self::U64(a), Self::U64(b)) => a.is_equal(b),
70            (Self::U128(a), Self::U128(b)) => a.is_equal(b),
71            (Self::Scalar(a), Self::Scalar(b)) => a.is_equal(b),
72            (Self::Signature(a), Self::Signature(b)) => a.is_equal(b),
73            (Self::String(a), Self::String(b)) => a.is_equal(b),
74            _ => Boolean::new(false),
75        }
76    }
77
78    /// Returns `true` if `self` and `other` are *not* equal.
79    fn is_not_equal(&self, other: &Self) -> Self::Output {
80        match (self, other) {
81            (Self::Address(a), Self::Address(b)) => a.is_not_equal(b),
82            (Self::Boolean(a), Self::Boolean(b)) => a.is_not_equal(b),
83            (Self::Field(a), Self::Field(b)) => a.is_not_equal(b),
84            (Self::Group(a), Self::Group(b)) => a.is_not_equal(b),
85            (Self::I8(a), Self::I8(b)) => a.is_not_equal(b),
86            (Self::I16(a), Self::I16(b)) => a.is_not_equal(b),
87            (Self::I32(a), Self::I32(b)) => a.is_not_equal(b),
88            (Self::I64(a), Self::I64(b)) => a.is_not_equal(b),
89            (Self::I128(a), Self::I128(b)) => a.is_not_equal(b),
90            (Self::U8(a), Self::U8(b)) => a.is_not_equal(b),
91            (Self::U16(a), Self::U16(b)) => a.is_not_equal(b),
92            (Self::U32(a), Self::U32(b)) => a.is_not_equal(b),
93            (Self::U64(a), Self::U64(b)) => a.is_not_equal(b),
94            (Self::U128(a), Self::U128(b)) => a.is_not_equal(b),
95            (Self::Scalar(a), Self::Scalar(b)) => a.is_not_equal(b),
96            (Self::Signature(a), Self::Signature(b)) => a.is_not_equal(b),
97            (Self::String(a), Self::String(b)) => a.is_not_equal(b),
98            _ => Boolean::new(true),
99        }
100    }
101}