Skip to content

Commit

Permalink
Add Add/Mul/SubAssign bounds for Integer and implement required traits (
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri authored Jan 6, 2025
1 parent dc7beea commit e2fa911
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 0 deletions.
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

0 comments on commit e2fa911

Please sign in to comment.