From 9835e2ae736235810b4ea1c162ca5e65c547e770 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 18 May 2024 04:49:50 +0200 Subject: Merging upstream version 1.71.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/crypto-bigint/src/uint/sub.rs | 74 +++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 31 deletions(-) (limited to 'vendor/crypto-bigint/src/uint/sub.rs') diff --git a/vendor/crypto-bigint/src/uint/sub.rs b/vendor/crypto-bigint/src/uint/sub.rs index 102f6b978..c39e54922 100644 --- a/vendor/crypto-bigint/src/uint/sub.rs +++ b/vendor/crypto-bigint/src/uint/sub.rs @@ -1,11 +1,11 @@ -//! [`UInt`] addition operations. +//! [`Uint`] addition operations. -use super::UInt; -use crate::{Checked, CheckedSub, Limb, Wrapping, Zero}; +use super::Uint; +use crate::{Checked, CheckedSub, CtChoice, Limb, Wrapping, Zero}; use core::ops::{Sub, SubAssign}; use subtle::CtOption; -impl UInt { +impl Uint { /// Computes `a - (b + borrow)`, returning the result along with the new borrow. #[inline(always)] pub const fn sbb(&self, rhs: &Self, mut borrow: Limb) -> (Self, Limb) { @@ -38,9 +38,21 @@ impl UInt { pub const fn wrapping_sub(&self, rhs: &Self) -> Self { self.sbb(rhs, Limb::ZERO).0 } + + /// Perform wrapping subtraction, returning the truthy value as the second element of the tuple + /// if an underflow has occurred. + pub(crate) const fn conditional_wrapping_sub( + &self, + rhs: &Self, + choice: CtChoice, + ) -> (Self, CtChoice) { + let actual_rhs = Uint::ct_select(&Uint::ZERO, rhs, choice); + let (res, borrow) = self.sbb(&actual_rhs, Limb::ZERO); + (res, CtChoice::from_mask(borrow.0)) + } } -impl CheckedSub<&UInt> for UInt { +impl CheckedSub<&Uint> for Uint { type Output = Self; fn checked_sub(&self, rhs: &Self) -> CtOption { @@ -49,54 +61,54 @@ impl CheckedSub<&UInt> for UInt { } } -impl Sub for Wrapping> { +impl Sub for Wrapping> { type Output = Self; - fn sub(self, rhs: Self) -> Wrapping> { + fn sub(self, rhs: Self) -> Wrapping> { Wrapping(self.0.wrapping_sub(&rhs.0)) } } -impl Sub<&Wrapping>> for Wrapping> { - type Output = Wrapping>; +impl Sub<&Wrapping>> for Wrapping> { + type Output = Wrapping>; - fn sub(self, rhs: &Wrapping>) -> Wrapping> { + fn sub(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_sub(&rhs.0)) } } -impl Sub>> for &Wrapping> { - type Output = Wrapping>; +impl Sub>> for &Wrapping> { + type Output = Wrapping>; - fn sub(self, rhs: Wrapping>) -> Wrapping> { + fn sub(self, rhs: Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_sub(&rhs.0)) } } -impl Sub<&Wrapping>> for &Wrapping> { - type Output = Wrapping>; +impl Sub<&Wrapping>> for &Wrapping> { + type Output = Wrapping>; - fn sub(self, rhs: &Wrapping>) -> Wrapping> { + fn sub(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_sub(&rhs.0)) } } -impl SubAssign for Wrapping> { +impl SubAssign for Wrapping> { fn sub_assign(&mut self, other: Self) { *self = *self - other; } } -impl SubAssign<&Wrapping>> for Wrapping> { +impl SubAssign<&Wrapping>> for Wrapping> { fn sub_assign(&mut self, other: &Self) { *self = *self - other; } } -impl Sub for Checked> { +impl Sub for Checked> { type Output = Self; - fn sub(self, rhs: Self) -> Checked> { + fn sub(self, rhs: Self) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_sub(&rhs))), @@ -104,10 +116,10 @@ impl Sub for Checked> { } } -impl Sub<&Checked>> for Checked> { - type Output = Checked>; +impl Sub<&Checked>> for Checked> { + type Output = Checked>; - fn sub(self, rhs: &Checked>) -> Checked> { + fn sub(self, rhs: &Checked>) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_sub(&rhs))), @@ -115,10 +127,10 @@ impl Sub<&Checked>> for Checked> { } } -impl Sub>> for &Checked> { - type Output = Checked>; +impl Sub>> for &Checked> { + type Output = Checked>; - fn sub(self, rhs: Checked>) -> Checked> { + fn sub(self, rhs: Checked>) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_sub(&rhs))), @@ -126,10 +138,10 @@ impl Sub>> for &Checked> { } } -impl Sub<&Checked>> for &Checked> { - type Output = Checked>; +impl Sub<&Checked>> for &Checked> { + type Output = Checked>; - fn sub(self, rhs: &Checked>) -> Checked> { + fn sub(self, rhs: &Checked>) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_sub(&rhs))), @@ -137,13 +149,13 @@ impl Sub<&Checked>> for &Checked> { } } -impl SubAssign for Checked> { +impl SubAssign for Checked> { fn sub_assign(&mut self, other: Self) { *self = *self - other; } } -impl SubAssign<&Checked>> for Checked> { +impl SubAssign<&Checked>> for Checked> { fn sub_assign(&mut self, other: &Self) { *self = *self - other; } -- cgit v1.2.3