snarkvm_console_types_integers/
bitwise.rsuse super::*;
impl<E: Environment, I: IntegerType> Equal for Integer<E, I> {
type Output = Boolean<E>;
fn is_equal(&self, other: &Self) -> Self::Output {
Boolean::new(self == other)
}
fn is_not_equal(&self, other: &Self) -> Self::Output {
Boolean::new(self != other)
}
}
impl<E: Environment, I: IntegerType> Compare<Self> for Integer<E, I> {
type Output = Boolean<E>;
fn is_less_than(&self, other: &Self) -> Self::Output {
Boolean::new(self.integer < other.integer)
}
fn is_greater_than(&self, other: &Self) -> Self::Output {
other.is_less_than(self)
}
fn is_less_than_or_equal(&self, other: &Self) -> Self::Output {
other.is_greater_than_or_equal(self)
}
fn is_greater_than_or_equal(&self, other: &Self) -> Self::Output {
!self.is_less_than(other)
}
}
impl<E: Environment, I: IntegerType> Not for Integer<E, I> {
type Output = Self;
#[inline]
fn not(self) -> Self::Output {
Integer::new(self.integer.not())
}
}
impl<E: Environment, I: IntegerType> BitAnd for Integer<E, I> {
type Output = Self;
#[inline]
fn bitand(self, other: Self) -> Self::Output {
Integer::new(self.integer & other.integer)
}
}
impl<E: Environment, I: IntegerType> BitAnd<&Integer<E, I>> for Integer<E, I> {
type Output = Self;
#[inline]
fn bitand(self, other: &Integer<E, I>) -> Self::Output {
Integer::new(self.integer & other.integer)
}
}
impl<E: Environment, I: IntegerType> BitAndAssign for Integer<E, I> {
#[inline]
fn bitand_assign(&mut self, other: Self) {
self.integer = self.integer & other.integer;
}
}
impl<E: Environment, I: IntegerType> BitOr for Integer<E, I> {
type Output = Self;
#[inline]
fn bitor(self, other: Self) -> Self::Output {
Integer::new(self.integer | other.integer)
}
}
impl<E: Environment, I: IntegerType> BitOr<&Integer<E, I>> for Integer<E, I> {
type Output = Self;
#[inline]
fn bitor(self, other: &Integer<E, I>) -> Self::Output {
Integer::new(self.integer | other.integer)
}
}
impl<E: Environment, I: IntegerType> BitOrAssign for Integer<E, I> {
#[inline]
fn bitor_assign(&mut self, other: Self) {
self.integer = self.integer | other.integer;
}
}
impl<E: Environment, I: IntegerType> BitXor for Integer<E, I> {
type Output = Self;
#[inline]
fn bitxor(self, other: Self) -> Self::Output {
Integer::new(self.integer ^ other.integer)
}
}
impl<E: Environment, I: IntegerType> BitXor<&Integer<E, I>> for Integer<E, I> {
type Output = Self;
#[inline]
fn bitxor(self, other: &Integer<E, I>) -> Self::Output {
Integer::new(self.integer ^ other.integer)
}
}
impl<E: Environment, I: IntegerType> BitXorAssign for Integer<E, I> {
#[inline]
fn bitxor_assign(&mut self, other: Self) {
self.integer = self.integer ^ other.integer;
}
}
impl<E: Environment, I: IntegerType, M: Magnitude> Shl<Integer<E, M>> for Integer<E, I> {
type Output = Self;
#[inline]
fn shl(self, n: Integer<E, M>) -> Self::Output {
match self.integer.checked_shl(&n.integer.to_u32().unwrap()) {
Some(shifted) => Integer::new(shifted),
None => E::halt(format!("Failed to shift {self} left by {n} bits")),
}
}
}
impl<E: Environment, I: IntegerType, M: Magnitude> Shl<&Integer<E, M>> for Integer<E, I> {
type Output = Self;
#[inline]
fn shl(self, n: &Integer<E, M>) -> Self::Output {
match self.integer.checked_shl(&n.integer.to_u32().unwrap()) {
Some(shifted) => Integer::new(shifted),
None => E::halt(format!("Failed to shift {self} left by {n} bits")),
}
}
}
impl<E: Environment, I: IntegerType, M: Magnitude> ShlChecked<Integer<E, M>> for Integer<E, I> {
type Output = Self;
#[inline]
fn shl_checked(&self, n: &Integer<E, M>) -> Self::Output {
match self.integer.checked_shl(&n.integer.to_u32().unwrap()) {
Some(shifted) => Integer::new(shifted),
None => E::halt(format!("Failed to shift {self} left by {n} bits")),
}
}
}
impl<E: Environment, I: IntegerType, M: Magnitude> ShlWrapped<Integer<E, M>> for Integer<E, I> {
type Output = Self;
#[inline]
fn shl_wrapped(&self, n: &Integer<E, M>) -> Self::Output {
Integer::new(self.integer.wrapping_shl(n.integer.to_u32().unwrap()))
}
}
impl<E: Environment, I: IntegerType, M: Magnitude> ShlAssign<Integer<E, M>> for Integer<E, I> {
#[inline]
fn shl_assign(&mut self, n: Integer<E, M>) {
match self.integer.checked_shl(&n.integer.to_u32().unwrap()) {
Some(shifted) => {
self.integer = shifted;
}
None => E::halt(format!("Failed to shift {self} left by {n} bits")),
}
}
}
impl<E: Environment, I: IntegerType, M: Magnitude> Shr<Integer<E, M>> for Integer<E, I> {
type Output = Self;
#[inline]
fn shr(self, n: Integer<E, M>) -> Self::Output {
match self.integer.checked_shr(n.integer.to_u32().unwrap()) {
Some(shifted) => Integer::new(shifted),
None => E::halt(format!("Failed to shift {self} right by {n} bits")),
}
}
}
impl<E: Environment, I: IntegerType, M: Magnitude> Shr<&Integer<E, M>> for Integer<E, I> {
type Output = Self;
#[inline]
fn shr(self, n: &Integer<E, M>) -> Self::Output {
match self.integer.checked_shr(n.integer.to_u32().unwrap()) {
Some(shifted) => Integer::new(shifted),
None => E::halt(format!("Failed to shift {self} right by {n} bits")),
}
}
}
impl<E: Environment, I: IntegerType, M: Magnitude> ShrChecked<Integer<E, M>> for Integer<E, I> {
type Output = Self;
#[inline]
fn shr_checked(&self, n: &Integer<E, M>) -> Self::Output {
match self.integer.checked_shr(n.integer.to_u32().unwrap()) {
Some(shifted) => Integer::new(shifted),
None => E::halt(format!("Failed to shift {self} right by {n} bits")),
}
}
}
impl<E: Environment, I: IntegerType, M: Magnitude> ShrWrapped<Integer<E, M>> for Integer<E, I> {
type Output = Self;
#[inline]
fn shr_wrapped(&self, n: &Integer<E, M>) -> Self::Output {
Integer::new(self.integer.wrapping_shr(n.integer.to_u32().unwrap()))
}
}
impl<E: Environment, I: IntegerType, M: Magnitude> ShrAssign<Integer<E, M>> for Integer<E, I> {
#[inline]
fn shr_assign(&mut self, n: Integer<E, M>) {
match self.integer.checked_shr(n.integer.to_u32().unwrap()) {
Some(shifted) => {
self.integer = shifted;
}
None => E::halt(format!("Failed to shift {self} right by {n} bits")),
}
}
}
impl<E: Environment, I: IntegerType> Ternary for Integer<E, I> {
type Boolean = Boolean<E>;
type Output = Self;
fn ternary(condition: &Self::Boolean, first: &Self, second: &Self) -> Self::Output {
match **condition {
true => *first,
false => *second,
}
}
}