Skip to content

Commit 1276517

Browse files
test UintRef as operator
Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
1 parent b261949 commit 1276517

File tree

4 files changed

+67
-30
lines changed

4 files changed

+67
-30
lines changed

src/uint/boxed/add.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl AddAssign<u128> for BoxedUint {
222222
#[cfg(test)]
223223
#[allow(clippy::unwrap_used)]
224224
mod tests {
225-
use super::{BoxedUint, CheckedAdd, Limb, Wrapping};
225+
use super::{BoxedUint, CheckedAdd, Limb, UintRef, Wrapping};
226226
use crate::Resize;
227227

228228
#[test]
@@ -292,4 +292,14 @@ mod tests {
292292
ret += Wrapping(Limb::ONE);
293293
assert!(ret.0.is_zero_vartime());
294294
}
295+
296+
#[test]
297+
fn add_uintref() {
298+
let a = BoxedUint::from(1234567890u64);
299+
let b = UintRef::new(&[Limb(456), Limb(0)]);
300+
assert_eq!(
301+
a.carrying_add(b, Limb::ZERO).0,
302+
BoxedUint::from(1234568346u64)
303+
);
304+
}
295305
}

src/uint/boxed/mul.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,45 +116,45 @@ impl BoxedUint {
116116
}
117117
}
118118

119-
impl<RHS: AsRef<UintRef>> CheckedMul<RHS> for BoxedUint {
120-
fn checked_mul(&self, rhs: &RHS) -> CtOption<Self> {
119+
impl<Rhs: AsRef<UintRef>> CheckedMul<Rhs> for BoxedUint {
120+
fn checked_mul(&self, rhs: &Rhs) -> CtOption<Self> {
121121
self.checked_mul(rhs)
122122
}
123123
}
124124

125-
impl<RHS: AsRef<UintRef>> Mul<RHS> for BoxedUint {
125+
impl<Rhs: AsRef<UintRef>> Mul<Rhs> for BoxedUint {
126126
type Output = BoxedUint;
127127

128-
fn mul(self, rhs: RHS) -> Self {
128+
fn mul(self, rhs: Rhs) -> Self {
129129
BoxedUint::mul(&self, &rhs)
130130
}
131131
}
132132

133-
impl<RHS: AsRef<UintRef>> Mul<RHS> for &BoxedUint {
133+
impl<Rhs: AsRef<UintRef>> Mul<Rhs> for &BoxedUint {
134134
type Output = BoxedUint;
135135

136-
fn mul(self, rhs: RHS) -> Self::Output {
136+
fn mul(self, rhs: Rhs) -> Self::Output {
137137
BoxedUint::mul(self, rhs)
138138
}
139139
}
140140

141-
impl<RHS: AsRef<UintRef>> MulAssign<RHS> for BoxedUint {
142-
fn mul_assign(&mut self, rhs: RHS) {
141+
impl<Rhs: AsRef<UintRef>> MulAssign<Rhs> for BoxedUint {
142+
fn mul_assign(&mut self, rhs: Rhs) {
143143
*self = BoxedUint::mul(self, rhs);
144144
}
145145
}
146146

147-
impl<RHS: AsRef<UintRef>> MulAssign<RHS> for Wrapping<BoxedUint> {
148-
fn mul_assign(&mut self, other: RHS) {
147+
impl<Rhs: AsRef<UintRef>> MulAssign<Rhs> for Wrapping<BoxedUint> {
148+
fn mul_assign(&mut self, other: Rhs) {
149149
self.0 = self.0.wrapping_mul(other);
150150
}
151151
}
152152

153-
impl<RHS: AsRef<UintRef>> ConcatenatingMul<RHS> for BoxedUint {
153+
impl<Rhs: AsRef<UintRef>> ConcatenatingMul<Rhs> for BoxedUint {
154154
type Output = Self;
155155

156156
#[inline]
157-
fn concatenating_mul(&self, rhs: RHS) -> Self {
157+
fn concatenating_mul(&self, rhs: Rhs) -> Self {
158158
self.mul(&rhs)
159159
}
160160
}
@@ -167,7 +167,7 @@ impl WrappingMul for BoxedUint {
167167

168168
#[cfg(test)]
169169
mod tests {
170-
use crate::{BoxedUint, CheckedMul, ConcatenatingMul, Resize, WrappingMul};
170+
use crate::{BoxedUint, CheckedMul, ConcatenatingMul, Limb, Resize, UintRef, WrappingMul};
171171

172172
#[test]
173173
fn mul_zero_and_one() {
@@ -261,4 +261,11 @@ mod tests {
261261
assert!(m.checked_square().is_none().to_bool());
262262
assert!(CheckedMul::checked_mul(&m, &m).is_none().to_bool());
263263
}
264+
265+
#[test]
266+
fn mul_uintref() {
267+
let a = BoxedUint::from(1234567890u64);
268+
let b = UintRef::new(&[Limb(456), Limb(0)]);
269+
assert_eq!(a * b, BoxedUint::from(562962957840u64));
270+
}
264271
}

src/uint/boxed/pow.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ impl BoxedUint {
209209
}
210210
}
211211

212-
impl<RHS: AsRef<UintRef>> PowBoundedExp<RHS> for Checked<BoxedUint> {
213-
fn pow_bounded_exp(&self, exponent: &RHS, exponent_bits: u32) -> Self {
212+
impl<Rhs: AsRef<UintRef>> PowBoundedExp<Rhs> for Checked<BoxedUint> {
213+
fn pow_bounded_exp(&self, exponent: &Rhs, exponent_bits: u32) -> Self {
214214
let is_some = self.0.is_some();
215215
let pow = self
216216
.0
@@ -220,15 +220,15 @@ impl<RHS: AsRef<UintRef>> PowBoundedExp<RHS> for Checked<BoxedUint> {
220220
}
221221
}
222222

223-
impl<RHS: AsRef<UintRef>> PowBoundedExp<RHS> for Wrapping<BoxedUint> {
224-
fn pow_bounded_exp(&self, exponent: &RHS, exponent_bits: u32) -> Self {
223+
impl<Rhs: AsRef<UintRef>> PowBoundedExp<Rhs> for Wrapping<BoxedUint> {
224+
fn pow_bounded_exp(&self, exponent: &Rhs, exponent_bits: u32) -> Self {
225225
Wrapping(self.0.wrapping_pow_bounded_exp(exponent, exponent_bits))
226226
}
227227
}
228228

229229
#[cfg(test)]
230230
mod tests {
231-
use crate::{BoxedUint, Checked, CtOption, Pow, U128, Wrapping};
231+
use crate::{BoxedUint, Checked, CtOption, Limb, Pow, Resize, U128, UintRef, Wrapping};
232232

233233
#[test]
234234
fn checked_pow_expected() {
@@ -329,4 +329,14 @@ mod tests {
329329
BoxedUint::from_be_hex("002b3854b3dc5d6e0000000000000001", 128).unwrap()
330330
);
331331
}
332+
333+
#[test]
334+
fn pow_uintref() {
335+
let a = BoxedUint::from(1234567890u64).resize_unchecked(128);
336+
let b = UintRef::new(&[Limb(4), Limb(0)]);
337+
assert_eq!(
338+
a.wrapping_pow(b),
339+
BoxedUint::from(2323057227982592441500937982514410000u128)
340+
);
341+
}
332342
}

src/uint/boxed/sub.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,25 @@ impl BoxedUint {
9595
}
9696
}
9797

98-
impl<RHS: AsRef<UintRef>> CheckedSub<RHS> for BoxedUint {
99-
fn checked_sub(&self, rhs: &RHS) -> CtOption<Self> {
98+
impl<Rhs: AsRef<UintRef>> CheckedSub<Rhs> for BoxedUint {
99+
fn checked_sub(&self, rhs: &Rhs) -> CtOption<Self> {
100100
let (result, underflow) = self.underflowing_sub(rhs);
101101
CtOption::new(result, underflow.not())
102102
}
103103
}
104104

105-
impl<RHS: AsRef<UintRef>> Sub<RHS> for BoxedUint {
105+
impl<Rhs: AsRef<UintRef>> Sub<Rhs> for BoxedUint {
106106
type Output = Self;
107107

108-
fn sub(self, rhs: RHS) -> Self {
108+
fn sub(self, rhs: Rhs) -> Self {
109109
Sub::sub(&self, rhs)
110110
}
111111
}
112112

113-
impl<RHS: AsRef<UintRef>> Sub<RHS> for &BoxedUint {
113+
impl<Rhs: AsRef<UintRef>> Sub<Rhs> for &BoxedUint {
114114
type Output = BoxedUint;
115115

116-
fn sub(self, rhs: RHS) -> BoxedUint {
116+
fn sub(self, rhs: Rhs) -> BoxedUint {
117117
let (res, underflow) = self.underflowing_sub(rhs);
118118
assert!(
119119
underflow.not().to_bool(),
@@ -123,8 +123,8 @@ impl<RHS: AsRef<UintRef>> Sub<RHS> for &BoxedUint {
123123
}
124124
}
125125

126-
impl<RHS: AsRef<UintRef>> SubAssign<RHS> for BoxedUint {
127-
fn sub_assign(&mut self, rhs: RHS) {
126+
impl<Rhs: AsRef<UintRef>> SubAssign<Rhs> for BoxedUint {
127+
fn sub_assign(&mut self, rhs: Rhs) {
128128
let underflow = self.underflowing_sub_assign(rhs);
129129
assert!(
130130
underflow.not().to_bool(),
@@ -133,8 +133,8 @@ impl<RHS: AsRef<UintRef>> SubAssign<RHS> for BoxedUint {
133133
}
134134
}
135135

136-
impl<RHS: AsRef<UintRef>> SubAssign<RHS> for Wrapping<BoxedUint> {
137-
fn sub_assign(&mut self, other: RHS) {
136+
impl<Rhs: AsRef<UintRef>> SubAssign<Rhs> for Wrapping<BoxedUint> {
137+
fn sub_assign(&mut self, other: Rhs) {
138138
self.0.wrapping_sub_assign(other);
139139
}
140140
}
@@ -204,7 +204,7 @@ impl SubAssign<u128> for BoxedUint {
204204
#[cfg(test)]
205205
#[allow(clippy::unwrap_used)]
206206
mod tests {
207-
use super::{BoxedUint, CheckedSub, Limb, Wrapping};
207+
use super::{BoxedUint, CheckedSub, Limb, UintRef, Wrapping};
208208
use crate::Resize;
209209

210210
#[test]
@@ -259,4 +259,14 @@ mod tests {
259259
ret -= Wrapping(Limb::ONE);
260260
assert_eq!(ret.0, BoxedUint::max(2 * Limb::BITS));
261261
}
262+
263+
#[test]
264+
fn sub_uintref() {
265+
let a = BoxedUint::from(1234567890u64);
266+
let b = UintRef::new(&[Limb(456), Limb(0)]);
267+
assert_eq!(
268+
a.borrowing_sub(b, Limb::ZERO).0,
269+
BoxedUint::from(1234567434u64)
270+
);
271+
}
262272
}

0 commit comments

Comments
 (0)