snarkvm_console_network_environment/traits/
arithmetic.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
16use anyhow::Result;
17
18/// Binary operator for adding two values, enforcing an overflow never occurs.
19pub trait AddChecked<Rhs: ?Sized = Self> {
20    type Output;
21
22    fn add_checked(&self, rhs: &Rhs) -> Self::Output;
23}
24
25/// Binary operator for adding two values, bounding the sum to `MAX` if an overflow occurs.
26pub trait AddSaturating<Rhs: ?Sized = Self> {
27    type Output;
28
29    fn add_saturating(&self, rhs: &Rhs) -> Self::Output;
30}
31
32/// Binary operator for adding two values, wrapping the sum if an overflow occurs.
33pub trait AddWrapped<Rhs: ?Sized = Self> {
34    type Output;
35
36    fn add_wrapped(&self, rhs: &Rhs) -> Self::Output;
37}
38
39/// Binary operator for dividing two values, without checking specific conditions.
40pub trait DivUnchecked<Rhs: ?Sized = Self> {
41    type Output;
42
43    fn div_unchecked(&self, rhs: &Rhs) -> Self::Output;
44}
45
46/// Binary operator for dividing two values, enforcing an overflow never occurs.
47pub trait DivChecked<Rhs: ?Sized = Self> {
48    type Output;
49
50    fn div_checked(&self, rhs: &Rhs) -> Self::Output;
51}
52
53/// Binary operator for dividing two values, bounding the quotient to `MAX` or `MIN` if an overflow occurs.
54pub trait DivSaturating<Rhs: ?Sized = Self> {
55    type Output;
56
57    fn div_saturating(&self, rhs: &Rhs) -> Self::Output;
58}
59
60/// Binary operator for dividing two values, wrapping the quotient if an overflow occurs.
61pub trait DivWrapped<Rhs: ?Sized = Self> {
62    type Output;
63
64    fn div_wrapped(&self, rhs: &Rhs) -> Self::Output;
65}
66
67/// Binary operator for modding two values.
68pub trait Modulo<Rhs: ?Sized = Self> {
69    type Output;
70
71    fn modulo(&self, rhs: &Rhs) -> Self::Output;
72}
73
74/// Binary operator for multiplying two values, enforcing an overflow never occurs.
75pub trait MulChecked<Rhs: ?Sized = Self> {
76    type Output;
77
78    fn mul_checked(&self, rhs: &Rhs) -> Self::Output;
79}
80
81/// Binary operator for multiplying two values, bounding the product to `MAX` if an overflow occurs.
82pub trait MulSaturating<Rhs: ?Sized = Self> {
83    type Output;
84
85    fn mul_saturating(&self, rhs: &Rhs) -> Self::Output;
86}
87
88/// Binary operator for multiplying two values, wrapping the product if an overflow occurs.
89pub trait MulWrapped<Rhs: ?Sized = Self> {
90    type Output;
91
92    fn mul_wrapped(&self, rhs: &Rhs) -> Self::Output;
93}
94
95/// Binary operator for exponentiating two values, enforcing an overflow never occurs.
96pub trait PowChecked<Rhs: ?Sized = Self> {
97    type Output;
98
99    fn pow_checked(&self, rhs: &Rhs) -> Self::Output;
100}
101
102/// Binary operator for exponentiating two values, wrapping the result if an overflow occurs.
103pub trait PowWrapped<Rhs: ?Sized = Self> {
104    type Output;
105
106    fn pow_wrapped(&self, rhs: &Rhs) -> Self::Output;
107}
108
109/// Binary operator for dividing two values and returning the remainder, enforcing an overflow never occurs.
110pub trait RemChecked<Rhs: ?Sized = Self> {
111    type Output;
112
113    fn rem_checked(&self, rhs: &Rhs) -> Self::Output;
114}
115
116/// Binary operator for dividing two values, bounding the remainder to `MAX` or `MIN` if an overflow occurs.
117pub trait RemSaturating<Rhs: ?Sized = Self> {
118    type Output;
119
120    fn rem_saturating(&self, rhs: &Rhs) -> Self::Output;
121}
122
123/// Binary operator for dividing two values, wrapping the remainder if an overflow occurs.
124pub trait RemWrapped<Rhs: ?Sized = Self> {
125    type Output;
126
127    fn rem_wrapped(&self, rhs: &Rhs) -> Self::Output;
128}
129
130/// Binary operator for left shifting a value, checking that the rhs is less than the number
131/// of bits in self.
132pub trait ShlChecked<Rhs: ?Sized = Self> {
133    type Output;
134
135    fn shl_checked(&self, rhs: &Rhs) -> Self::Output;
136}
137
138/// Binary operator for left shifting a value, safely continuing past the number of bits in self.
139pub trait ShlWrapped<Rhs: ?Sized = Self> {
140    type Output;
141
142    fn shl_wrapped(&self, rhs: &Rhs) -> Self::Output;
143}
144
145/// Binary operator for right shifting a value, checking that the rhs is less than the number
146/// of bits in self.
147pub trait ShrChecked<Rhs: ?Sized = Self> {
148    type Output;
149
150    fn shr_checked(&self, rhs: &Rhs) -> Self::Output;
151}
152
153/// Binary operator for right shifting a value, safely continuing past the number of bits in self.
154pub trait ShrWrapped<Rhs: ?Sized = Self> {
155    type Output;
156
157    fn shr_wrapped(&self, rhs: &Rhs) -> Self::Output;
158}
159
160/// Binary operator for subtracting two values, enforcing an underflow never occurs.
161pub trait SubChecked<Rhs: ?Sized = Self> {
162    type Output;
163
164    fn sub_checked(&self, rhs: &Rhs) -> Self::Output;
165}
166
167/// Binary operator for subtracting two values, bounding the difference to `MIN` if an underflow occurs.
168pub trait SubSaturating<Rhs: ?Sized = Self> {
169    type Output;
170
171    fn sub_saturating(&self, rhs: &Rhs) -> Self::Output;
172}
173
174/// Binary operator for subtracting two values, wrapping the difference if an underflow occurs.
175pub trait SubWrapped<Rhs: ?Sized = Self> {
176    type Output;
177
178    fn sub_wrapped(&self, rhs: &Rhs) -> Self::Output;
179}
180
181/// Unary operator for retrieving the absolute value, enforcing an overflow never occurs.
182pub trait AbsChecked {
183    type Output;
184
185    fn abs_checked(self) -> Self::Output;
186}
187
188/// Unary operator for retrieving the absolute value, bounding the difference to `MAX` if an overflow occurs.
189pub trait AbsSaturating {
190    type Output;
191
192    fn abs_saturating(self) -> Self::Output;
193}
194
195/// Unary operator for retrieving the absolute value, wrapping the result if an overflow occurs.
196pub trait AbsWrapped {
197    type Output;
198
199    fn abs_wrapped(self) -> Self::Output;
200}
201
202/// Unary operator for retrieving the doubled value.
203pub trait Double {
204    type Output;
205
206    fn double(&self) -> Self::Output;
207}
208
209/// Unary operator for retrieving the inverse value.
210pub trait Inverse {
211    type Output;
212
213    fn inverse(&self) -> Result<Self::Output>;
214}
215
216/// Unary operator for retrieving the squared value.
217pub trait Square {
218    type Output;
219
220    fn square(&self) -> Self::Output;
221}
222
223/// Unary operator for retrieving the square root of the value.
224pub trait SquareRoot {
225    type Output;
226
227    fn square_root(&self) -> Result<Self::Output>;
228}