@@ -157,8 +157,7 @@ impl Mul<&BoxedUint> for &BoxedUint {
157157 type Output = BoxedUint ;
158158
159159 fn mul ( self , rhs : & BoxedUint ) -> Self :: Output {
160- self . checked_mul ( rhs)
161- . expect ( "attempted to multiply with overflow" )
160+ BoxedUint :: mul ( self , rhs)
162161 }
163162}
164163
@@ -212,7 +211,7 @@ impl WrappingMul for BoxedUint {
212211
213212#[ cfg( test) ]
214213mod tests {
215- use crate :: { BoxedUint , Resize } ;
214+ use crate :: { BoxedUint , CheckedMul , ConcatenatingMul , Resize , WrappingMul } ;
216215
217216 #[ test]
218217 fn mul_zero_and_one ( ) {
@@ -258,33 +257,50 @@ mod tests {
258257 for i in 0 ..50 {
259258 let a = BoxedUint :: random_bits ( & mut rng, 4096 ) ;
260259 let b = BoxedUint :: random_bits ( & mut rng, 5000 ) ;
261- let expect = a . mul ( & b ) ;
262- assert_eq ! ( b . mul ( & a ) , expect, "a={a}, b={b}, i={i}" ) ;
260+ let expect = & a * & b ;
261+ assert_eq ! ( & b * a . clone ( ) , expect, "a={a}, b={b}, i={i}" ) ;
263262 assert_eq ! (
264- a. wrapping_mul( & b) ,
263+ ConcatenatingMul :: concatenating_mul( & b, & a) ,
264+ expect,
265+ "a={a}, b={b}, i={i}"
266+ ) ;
267+ assert_eq ! (
268+ WrappingMul :: wrapping_mul( & a, & b) ,
265269 expect. clone( ) . resize_unchecked( a. bits_precision( ) ) ,
266270 "a={a}, b={b}, i={i}"
267271 ) ;
268272 assert_eq ! (
269- b . wrapping_mul( & a) ,
273+ WrappingMul :: wrapping_mul( & b , & a) ,
270274 expect. clone( ) . resize_unchecked( b. bits_precision( ) ) ,
271275 "a={a}, b={b}, i={i}"
272276 ) ;
273277 }
274278 }
275279
276280 #[ test]
277- fn checked_square ( ) {
281+ fn checked_mul_cmp ( ) {
278282 let n = BoxedUint :: max ( 64 )
279283 . resize_unchecked ( 256 )
280284 . wrapping_add ( & BoxedUint :: one ( ) ) ;
281285 let n2 = n. checked_square ( ) ;
282286 assert ! ( n2. is_some( ) . to_bool( ) ) ;
283- let n4 = n2. unwrap ( ) . checked_square ( ) ;
287+ let n2_c = CheckedMul :: checked_mul ( & n, & n) ;
288+ assert_eq ! ( n2. clone( ) . into_option( ) , n2_c. into_option( ) ) ;
289+ let n2 = n2. unwrap ( ) ;
290+
291+ let n4 = n2. checked_square ( ) ;
284292 assert ! ( n4. is_none( ) . to_bool( ) ) ;
285- let z = BoxedUint :: zero_with_precision ( 256 ) . checked_square ( ) ;
286- assert ! ( z. is_some( ) . to_bool( ) ) ;
287- let m = BoxedUint :: max ( 256 ) . checked_square ( ) ;
288- assert ! ( m. is_none( ) . to_bool( ) ) ;
293+ let n4_c = CheckedMul :: checked_mul ( & n2, & n2) ;
294+ assert ! ( n4_c. is_none( ) . to_bool( ) ) ;
295+
296+ let z = BoxedUint :: zero_with_precision ( 256 ) ;
297+ let z2 = z. checked_square ( ) ;
298+ assert ! ( z2. is_some( ) . to_bool( ) ) ;
299+ let z2_c = CheckedMul :: checked_mul ( & z, & z) ;
300+ assert_eq ! ( z2. into_option( ) , z2_c. into_option( ) ) ;
301+
302+ let m = BoxedUint :: max ( 256 ) ;
303+ assert ! ( m. checked_square( ) . is_none( ) . to_bool( ) ) ;
304+ assert ! ( CheckedMul :: checked_mul( & m, & m) . is_none( ) . to_bool( ) ) ;
289305 }
290306}
0 commit comments