Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Add/Mul/SubAssign bounds for Integer #716

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ pub trait Integer:
'static
+ Add<Output = Self>
+ for<'a> Add<&'a Self, Output = Self>
+ AddAssign<Self>
+ for<'a> AddAssign<&'a Self>
+ AddMod<Output = Self>
+ AsRef<[Limb]>
+ BitAnd<Output = Self>
Expand Down Expand Up @@ -121,6 +123,8 @@ pub trait Integer:
+ From<Limb>
+ Mul<Output = Self>
+ for<'a> Mul<&'a Self, Output = Self>
+ MulAssign<Self>
+ for<'a> MulAssign<&'a Self>
+ MulMod<Output = Self>
+ NegMod<Output = Self>
+ Not<Output = Self>
Expand All @@ -140,6 +144,8 @@ pub trait Integer:
+ ShrVartime
+ Sub<Output = Self>
+ for<'a> Sub<&'a Self, Output = Self>
+ SubAssign<Self>
+ for<'a> SubAssign<&'a Self>
+ SubMod<Output = Self>
+ Sync
+ SquareRoot
Expand Down
25 changes: 25 additions & 0 deletions src/uint/boxed/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ impl Add<&BoxedUint> for BoxedUint {
}
}

impl Add<BoxedUint> for &BoxedUint {
type Output = BoxedUint;

fn add(self, rhs: BoxedUint) -> BoxedUint {
Add::add(self, &rhs)
}
}

impl Add<&BoxedUint> for &BoxedUint {
type Output = BoxedUint;

Expand All @@ -75,6 +83,23 @@ impl Add<&BoxedUint> for &BoxedUint {
}
}

impl AddAssign<BoxedUint> for BoxedUint {
fn add_assign(&mut self, rhs: BoxedUint) {
self.add_assign(&rhs)
}
}

impl AddAssign<&BoxedUint> for BoxedUint {
fn add_assign(&mut self, rhs: &BoxedUint) {
let carry = self.adc_assign(rhs, Limb::ZERO);
assert_eq!(
carry.is_zero().unwrap_u8(),
1,
"attempted to add with overflow"
);
}
}

impl<const LIMBS: usize> Add<Uint<LIMBS>> for BoxedUint {
type Output = BoxedUint;

Expand Down
12 changes: 12 additions & 0 deletions src/uint/boxed/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ impl Mul<&BoxedUint> for &BoxedUint {
}
}

impl MulAssign<BoxedUint> for BoxedUint {
fn mul_assign(&mut self, rhs: BoxedUint) {
self.mul_assign(&rhs)
}
}

impl MulAssign<&BoxedUint> for BoxedUint {
fn mul_assign(&mut self, rhs: &BoxedUint) {
*self = self.clone().mul(rhs)
}
}

impl MulAssign<Wrapping<BoxedUint>> for Wrapping<BoxedUint> {
fn mul_assign(&mut self, other: Wrapping<BoxedUint>) {
*self = Wrapping(self.0.wrapping_mul(&other.0));
Expand Down
25 changes: 25 additions & 0 deletions src/uint/boxed/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ impl Sub<&BoxedUint> for BoxedUint {
}
}

impl Sub<BoxedUint> for &BoxedUint {
type Output = BoxedUint;

fn sub(self, rhs: BoxedUint) -> BoxedUint {
Sub::sub(self, &rhs)
}
}

impl Sub<&BoxedUint> for &BoxedUint {
type Output = BoxedUint;

Expand All @@ -82,6 +90,23 @@ impl Sub<&BoxedUint> for &BoxedUint {
}
}

impl SubAssign<BoxedUint> for BoxedUint {
fn sub_assign(&mut self, rhs: BoxedUint) {
self.sub_assign(&rhs)
}
}

impl SubAssign<&BoxedUint> for BoxedUint {
fn sub_assign(&mut self, rhs: &BoxedUint) {
let carry = self.sbb_assign(rhs, Limb::ZERO);
assert_eq!(
carry.is_zero().unwrap_u8(),
1,
"attempted to subtract with underflow"
);
}
}

impl<const LIMBS: usize> Sub<Uint<LIMBS>> for BoxedUint {
type Output = BoxedUint;

Expand Down
12 changes: 12 additions & 0 deletions src/uint/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ impl<const LIMBS: usize, const RHS_LIMBS: usize> Mul<&Uint<RHS_LIMBS>> for &Uint
}
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> MulAssign<Uint<RHS_LIMBS>> for Uint<LIMBS> {
fn mul_assign(&mut self, rhs: Uint<RHS_LIMBS>) {
*self = self.mul(&rhs)
}
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> MulAssign<&Uint<RHS_LIMBS>> for Uint<LIMBS> {
fn mul_assign(&mut self, rhs: &Uint<RHS_LIMBS>) {
*self = self.mul(rhs)
}
}

impl<const LIMBS: usize> MulAssign<Wrapping<Uint<LIMBS>>> for Wrapping<Uint<LIMBS>> {
fn mul_assign(&mut self, other: Wrapping<Uint<LIMBS>>) {
*self = *self * other;
Expand Down
12 changes: 12 additions & 0 deletions src/uint/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ impl<const LIMBS: usize> Sub<&Uint<LIMBS>> for Uint<LIMBS> {
}
}

impl<const LIMBS: usize> SubAssign<Uint<LIMBS>> for Uint<LIMBS> {
fn sub_assign(&mut self, rhs: Uint<LIMBS>) {
*self = self.sub(&rhs)
}
}

impl<const LIMBS: usize> SubAssign<&Uint<LIMBS>> for Uint<LIMBS> {
fn sub_assign(&mut self, rhs: &Uint<LIMBS>) {
*self = self.sub(rhs)
}
}

impl<const LIMBS: usize> SubAssign for Wrapping<Uint<LIMBS>> {
fn sub_assign(&mut self, other: Self) {
*self = *self - other;
Expand Down
Loading