snarkvm_circuit_environment/traits/operators.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
// Copyright 2024 Aleo Network Foundation
// This file is part of the snarkVM library.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pub use crate::prelude::num_traits::Pow;
pub use console::traits::{
arithmetic::*,
bitwise::*,
from_bits::{SizeInBits, SizeInDataBits},
};
use crate::BooleanTrait;
/// Unary operator for retrieving the inverse value.
pub trait Inverse {
type Output;
fn inverse(&self) -> Self::Output;
}
/// Unary operator for retrieving the square root of the value.
pub trait SquareRoot {
type Output;
fn square_root(&self) -> Self::Output;
}
///
/// A single-bit binary adder with a carry bit.
///
/// https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder
///
/// sum = (a XOR b) XOR carry
/// carry = a AND b OR carry AND (a XOR b)
/// return (sum, carry)
///
pub trait Adder {
type Carry;
type Sum;
/// Returns the sum of `self` and `other` as a sum bit and carry bit.
fn adder(&self, other: &Self, carry: &Self) -> (Self::Sum, Self::Carry);
}
///
/// A single-bit binary subtractor with a borrow bit.
///
/// https://en.wikipedia.org/wiki/Subtractor#Full_subtractor
///
/// difference = (a XOR b) XOR borrow
/// borrow = ((NOT a) AND b) OR (borrow AND (NOT (a XOR b)))
/// return (difference, borrow)
///
pub trait Subtractor {
type Borrow;
type Difference;
/// Returns the difference of `self` and `other` as a difference bit and borrow bit.
fn subtractor(&self, other: &Self, borrow: &Self) -> (Self::Difference, Self::Borrow);
}
/// Representation of the zero value.
pub trait Zero {
type Boolean: BooleanTrait;
/// Returns a new zero constant.
fn zero() -> Self
where
Self: Sized;
/// Returns `true` if `self` is zero.
fn is_zero(&self) -> Self::Boolean;
}
/// Representation of the one value.
pub trait One {
type Boolean: BooleanTrait;
/// Returns a new one constant.
fn one() -> Self
where
Self: Sized;
/// Returns `true` if `self` is one.
fn is_one(&self) -> Self::Boolean;
}
/// Unary operator for retrieving the most-significant bit.
pub trait MSB {
type Boolean: BooleanTrait;
/// Returns the MSB of the value.
fn msb(&self) -> &Self::Boolean;
}