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/add.rs | 72 +++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 30 deletions(-) (limited to 'vendor/crypto-bigint/src/uint/add.rs') diff --git a/vendor/crypto-bigint/src/uint/add.rs b/vendor/crypto-bigint/src/uint/add.rs index 2822e9e67..21aa5d578 100644 --- a/vendor/crypto-bigint/src/uint/add.rs +++ b/vendor/crypto-bigint/src/uint/add.rs @@ -1,10 +1,10 @@ -//! [`UInt`] addition operations. +//! [`Uint`] addition operations. -use crate::{Checked, CheckedAdd, Limb, UInt, Wrapping, Zero}; +use crate::{Checked, CheckedAdd, CtChoice, Limb, Uint, Wrapping, Zero}; use core::ops::{Add, AddAssign}; use subtle::CtOption; -impl UInt { +impl Uint { /// Computes `a + b + carry`, returning the result along with the new carry. #[inline(always)] pub const fn adc(&self, rhs: &Self, mut carry: Limb) -> (Self, Limb) { @@ -36,9 +36,21 @@ impl UInt { pub const fn wrapping_add(&self, rhs: &Self) -> Self { self.adc(rhs, Limb::ZERO).0 } + + /// Perform wrapping addition, returning the truthy value as the second element of the tuple + /// if an overflow has occurred. + pub(crate) const fn conditional_wrapping_add( + &self, + rhs: &Self, + choice: CtChoice, + ) -> (Self, CtChoice) { + let actual_rhs = Uint::ct_select(&Uint::ZERO, rhs, choice); + let (sum, carry) = self.adc(&actual_rhs, Limb::ZERO); + (sum, CtChoice::from_lsb(carry.0)) + } } -impl CheckedAdd<&UInt> for UInt { +impl CheckedAdd<&Uint> for Uint { type Output = Self; fn checked_add(&self, rhs: &Self) -> CtOption { @@ -47,54 +59,54 @@ impl CheckedAdd<&UInt> for UInt { } } -impl Add for Wrapping> { +impl Add for Wrapping> { type Output = Self; - fn add(self, rhs: Self) -> Wrapping> { + fn add(self, rhs: Self) -> Wrapping> { Wrapping(self.0.wrapping_add(&rhs.0)) } } -impl Add<&Wrapping>> for Wrapping> { - type Output = Wrapping>; +impl Add<&Wrapping>> for Wrapping> { + type Output = Wrapping>; - fn add(self, rhs: &Wrapping>) -> Wrapping> { + fn add(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_add(&rhs.0)) } } -impl Add>> for &Wrapping> { - type Output = Wrapping>; +impl Add>> for &Wrapping> { + type Output = Wrapping>; - fn add(self, rhs: Wrapping>) -> Wrapping> { + fn add(self, rhs: Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_add(&rhs.0)) } } -impl Add<&Wrapping>> for &Wrapping> { - type Output = Wrapping>; +impl Add<&Wrapping>> for &Wrapping> { + type Output = Wrapping>; - fn add(self, rhs: &Wrapping>) -> Wrapping> { + fn add(self, rhs: &Wrapping>) -> Wrapping> { Wrapping(self.0.wrapping_add(&rhs.0)) } } -impl AddAssign for Wrapping> { +impl AddAssign for Wrapping> { fn add_assign(&mut self, other: Self) { *self = *self + other; } } -impl AddAssign<&Wrapping>> for Wrapping> { +impl AddAssign<&Wrapping>> for Wrapping> { fn add_assign(&mut self, other: &Self) { *self = *self + other; } } -impl Add for Checked> { +impl Add for Checked> { type Output = Self; - fn add(self, rhs: Self) -> Checked> { + fn add(self, rhs: Self) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_add(&rhs))), @@ -102,10 +114,10 @@ impl Add for Checked> { } } -impl Add<&Checked>> for Checked> { - type Output = Checked>; +impl Add<&Checked>> for Checked> { + type Output = Checked>; - fn add(self, rhs: &Checked>) -> Checked> { + fn add(self, rhs: &Checked>) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_add(&rhs))), @@ -113,10 +125,10 @@ impl Add<&Checked>> for Checked> { } } -impl Add>> for &Checked> { - type Output = Checked>; +impl Add>> for &Checked> { + type Output = Checked>; - fn add(self, rhs: Checked>) -> Checked> { + fn add(self, rhs: Checked>) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_add(&rhs))), @@ -124,10 +136,10 @@ impl Add>> for &Checked> { } } -impl Add<&Checked>> for &Checked> { - type Output = Checked>; +impl Add<&Checked>> for &Checked> { + type Output = Checked>; - fn add(self, rhs: &Checked>) -> Checked> { + fn add(self, rhs: &Checked>) -> Checked> { Checked( self.0 .and_then(|lhs| rhs.0.and_then(|rhs| lhs.checked_add(&rhs))), @@ -135,13 +147,13 @@ impl Add<&Checked>> for &Checked> { } } -impl AddAssign for Checked> { +impl AddAssign for Checked> { fn add_assign(&mut self, other: Self) { *self = *self + other; } } -impl AddAssign<&Checked>> for Checked> { +impl AddAssign<&Checked>> for Checked> { fn add_assign(&mut self, other: &Self) { *self = *self + other; } -- cgit v1.2.3