Skip to content

Commit e2fa911

Browse files
authored
Add Add/Mul/SubAssign bounds for Integer and implement required traits (#716)
1 parent dc7beea commit e2fa911

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed

src/traits.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ pub trait Integer:
8282
'static
8383
+ Add<Output = Self>
8484
+ for<'a> Add<&'a Self, Output = Self>
85+
+ AddAssign<Self>
86+
+ for<'a> AddAssign<&'a Self>
8587
+ AddMod<Output = Self>
8688
+ AsRef<[Limb]>
8789
+ BitAnd<Output = Self>
@@ -121,6 +123,8 @@ pub trait Integer:
121123
+ From<Limb>
122124
+ Mul<Output = Self>
123125
+ for<'a> Mul<&'a Self, Output = Self>
126+
+ MulAssign<Self>
127+
+ for<'a> MulAssign<&'a Self>
124128
+ MulMod<Output = Self>
125129
+ NegMod<Output = Self>
126130
+ Not<Output = Self>
@@ -140,6 +144,8 @@ pub trait Integer:
140144
+ ShrVartime
141145
+ Sub<Output = Self>
142146
+ for<'a> Sub<&'a Self, Output = Self>
147+
+ SubAssign<Self>
148+
+ for<'a> SubAssign<&'a Self>
143149
+ SubMod<Output = Self>
144150
+ Sync
145151
+ SquareRoot

src/uint/boxed/add.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ impl Add<&BoxedUint> for BoxedUint {
6666
}
6767
}
6868

69+
impl Add<BoxedUint> for &BoxedUint {
70+
type Output = BoxedUint;
71+
72+
fn add(self, rhs: BoxedUint) -> BoxedUint {
73+
Add::add(self, &rhs)
74+
}
75+
}
76+
6977
impl Add<&BoxedUint> for &BoxedUint {
7078
type Output = BoxedUint;
7179

@@ -75,6 +83,23 @@ impl Add<&BoxedUint> for &BoxedUint {
7583
}
7684
}
7785

86+
impl AddAssign<BoxedUint> for BoxedUint {
87+
fn add_assign(&mut self, rhs: BoxedUint) {
88+
self.add_assign(&rhs)
89+
}
90+
}
91+
92+
impl AddAssign<&BoxedUint> for BoxedUint {
93+
fn add_assign(&mut self, rhs: &BoxedUint) {
94+
let carry = self.adc_assign(rhs, Limb::ZERO);
95+
assert_eq!(
96+
carry.is_zero().unwrap_u8(),
97+
1,
98+
"attempted to add with overflow"
99+
);
100+
}
101+
}
102+
78103
impl<const LIMBS: usize> Add<Uint<LIMBS>> for BoxedUint {
79104
type Output = BoxedUint;
80105

src/uint/boxed/mul.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ impl Mul<&BoxedUint> for &BoxedUint {
102102
}
103103
}
104104

105+
impl MulAssign<BoxedUint> for BoxedUint {
106+
fn mul_assign(&mut self, rhs: BoxedUint) {
107+
self.mul_assign(&rhs)
108+
}
109+
}
110+
111+
impl MulAssign<&BoxedUint> for BoxedUint {
112+
fn mul_assign(&mut self, rhs: &BoxedUint) {
113+
*self = self.clone().mul(rhs)
114+
}
115+
}
116+
105117
impl MulAssign<Wrapping<BoxedUint>> for Wrapping<BoxedUint> {
106118
fn mul_assign(&mut self, other: Wrapping<BoxedUint>) {
107119
*self = Wrapping(self.0.wrapping_mul(&other.0));

src/uint/boxed/sub.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ impl Sub<&BoxedUint> for BoxedUint {
7373
}
7474
}
7575

76+
impl Sub<BoxedUint> for &BoxedUint {
77+
type Output = BoxedUint;
78+
79+
fn sub(self, rhs: BoxedUint) -> BoxedUint {
80+
Sub::sub(self, &rhs)
81+
}
82+
}
83+
7684
impl Sub<&BoxedUint> for &BoxedUint {
7785
type Output = BoxedUint;
7886

@@ -82,6 +90,23 @@ impl Sub<&BoxedUint> for &BoxedUint {
8290
}
8391
}
8492

93+
impl SubAssign<BoxedUint> for BoxedUint {
94+
fn sub_assign(&mut self, rhs: BoxedUint) {
95+
self.sub_assign(&rhs)
96+
}
97+
}
98+
99+
impl SubAssign<&BoxedUint> for BoxedUint {
100+
fn sub_assign(&mut self, rhs: &BoxedUint) {
101+
let carry = self.sbb_assign(rhs, Limb::ZERO);
102+
assert_eq!(
103+
carry.is_zero().unwrap_u8(),
104+
1,
105+
"attempted to subtract with underflow"
106+
);
107+
}
108+
}
109+
85110
impl<const LIMBS: usize> Sub<Uint<LIMBS>> for BoxedUint {
86111
type Output = BoxedUint;
87112

src/uint/mul.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,18 @@ impl<const LIMBS: usize, const RHS_LIMBS: usize> Mul<&Uint<RHS_LIMBS>> for &Uint
244244
}
245245
}
246246

247+
impl<const LIMBS: usize, const RHS_LIMBS: usize> MulAssign<Uint<RHS_LIMBS>> for Uint<LIMBS> {
248+
fn mul_assign(&mut self, rhs: Uint<RHS_LIMBS>) {
249+
*self = self.mul(&rhs)
250+
}
251+
}
252+
253+
impl<const LIMBS: usize, const RHS_LIMBS: usize> MulAssign<&Uint<RHS_LIMBS>> for Uint<LIMBS> {
254+
fn mul_assign(&mut self, rhs: &Uint<RHS_LIMBS>) {
255+
*self = self.mul(rhs)
256+
}
257+
}
258+
247259
impl<const LIMBS: usize> MulAssign<Wrapping<Uint<LIMBS>>> for Wrapping<Uint<LIMBS>> {
248260
fn mul_assign(&mut self, other: Wrapping<Uint<LIMBS>>) {
249261
*self = *self * other;

src/uint/sub.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ impl<const LIMBS: usize> Sub<&Uint<LIMBS>> for Uint<LIMBS> {
5959
}
6060
}
6161

62+
impl<const LIMBS: usize> SubAssign<Uint<LIMBS>> for Uint<LIMBS> {
63+
fn sub_assign(&mut self, rhs: Uint<LIMBS>) {
64+
*self = self.sub(&rhs)
65+
}
66+
}
67+
68+
impl<const LIMBS: usize> SubAssign<&Uint<LIMBS>> for Uint<LIMBS> {
69+
fn sub_assign(&mut self, rhs: &Uint<LIMBS>) {
70+
*self = self.sub(rhs)
71+
}
72+
}
73+
6274
impl<const LIMBS: usize> SubAssign for Wrapping<Uint<LIMBS>> {
6375
fn sub_assign(&mut self, other: Self) {
6476
*self = *self - other;

0 commit comments

Comments
 (0)