snarkvm_circuit_environment/traits/
operators.rs

1// Copyright 2024-2025 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
16pub use crate::prelude::num_traits::Pow;
17pub use console::traits::{
18    arithmetic::*,
19    bitwise::*,
20    from_bits::{SizeInBits, SizeInDataBits},
21};
22
23use crate::BooleanTrait;
24
25/// Unary operator for retrieving the inverse value.
26pub trait Inverse {
27    type Output;
28
29    fn inverse(&self) -> Self::Output;
30}
31
32/// Unary operator for retrieving the square root of the value.
33pub trait SquareRoot {
34    type Output;
35
36    fn square_root(&self) -> Self::Output;
37}
38
39///
40/// A single-bit binary adder with a carry bit.
41///
42/// https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder
43///
44/// sum = (a XOR b) XOR carry
45/// carry = a AND b OR carry AND (a XOR b)
46/// return (sum, carry)
47///
48pub trait Adder {
49    type Carry;
50    type Sum;
51
52    /// Returns the sum of `self` and `other` as a sum bit and carry bit.
53    fn adder(&self, other: &Self, carry: &Self) -> (Self::Sum, Self::Carry);
54}
55
56///
57/// A single-bit binary subtractor with a borrow bit.
58///
59/// https://en.wikipedia.org/wiki/Subtractor#Full_subtractor
60///
61/// difference = (a XOR b) XOR borrow
62/// borrow = ((NOT a) AND b) OR (borrow AND (NOT (a XOR b)))
63/// return (difference, borrow)
64///
65pub trait Subtractor {
66    type Borrow;
67    type Difference;
68
69    /// Returns the difference of `self` and `other` as a difference bit and borrow bit.
70    fn subtractor(&self, other: &Self, borrow: &Self) -> (Self::Difference, Self::Borrow);
71}
72
73/// Representation of the zero value.
74pub trait Zero {
75    type Boolean: BooleanTrait;
76
77    /// Returns a new zero constant.
78    fn zero() -> Self
79    where
80        Self: Sized;
81
82    /// Returns `true` if `self` is zero.
83    fn is_zero(&self) -> Self::Boolean;
84}
85
86/// Representation of the one value.
87pub trait One {
88    type Boolean: BooleanTrait;
89
90    /// Returns a new one constant.
91    fn one() -> Self
92    where
93        Self: Sized;
94
95    /// Returns `true` if `self` is one.
96    fn is_one(&self) -> Self::Boolean;
97}
98
99/// Unary operator for retrieving the most-significant bit.
100pub trait MSB {
101    type Boolean: BooleanTrait;
102
103    /// Returns the MSB of the value.
104    fn msb(&self) -> &Self::Boolean;
105}