snarkvm_console_network_environment/traits/bitwise.rs
1// Copyright (c) 2019-2025 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
16/// Trait for equality comparisons.
17pub trait Equal<Rhs: ?Sized = Self> {
18 type Output;
19
20 /// Returns `true` if `self` and `other` are equal.
21 fn is_equal(&self, other: &Rhs) -> Self::Output;
22
23 /// Returns `true` if `self` and `other` are *not* equal.
24 fn is_not_equal(&self, other: &Rhs) -> Self::Output;
25}
26
27/// Trait for comparator operations.
28pub trait Compare<Rhs: ?Sized = Self> {
29 type Output;
30
31 /// Returns `true` if `self` is less than `other`.
32 fn is_less_than(&self, other: &Rhs) -> Self::Output;
33
34 /// Returns `true` if `self` is greater than `other`.
35 fn is_greater_than(&self, other: &Rhs) -> Self::Output;
36
37 /// Returns `true` if `self` is less than or equal to `other`.
38 fn is_less_than_or_equal(&self, other: &Rhs) -> Self::Output;
39
40 /// Returns `true` if `self` is greater than or equal to `other`.
41 fn is_greater_than_or_equal(&self, other: &Rhs) -> Self::Output;
42}
43
44/// Binary operator for performing `NOT (a AND b)`.
45pub trait Nand<Rhs: ?Sized = Self> {
46 type Output;
47
48 /// Returns `NOT (a AND b)`.
49 fn nand(&self, other: &Rhs) -> Self::Output;
50}
51
52/// Binary operator for performing `(NOT a) AND (NOT b)`.
53pub trait Nor<Rhs: ?Sized = Self> {
54 type Output;
55
56 /// Returns `(NOT a) AND (NOT b)`.
57 fn nor(&self, other: &Rhs) -> Self::Output;
58}
59
60/// Trait for ternary operations.
61pub trait Ternary {
62 type Boolean;
63 type Output;
64
65 /// Returns `first` if `condition` is `true`, otherwise returns `second`.
66 fn ternary(condition: &Self::Boolean, first: &Self, second: &Self) -> Self::Output
67 where
68 Self: Sized;
69}
70
71impl<T: Ternary> Ternary for Box<T> {
72 type Boolean = T::Boolean;
73 type Output = Box<T::Output>;
74
75 /// Returns `first` if `condition` is `true`, otherwise returns `second`.
76 fn ternary(condition: &Self::Boolean, first: &Self, second: &Self) -> Self::Output {
77 Box::new(T::ternary(condition, first, second))
78 }
79}