@@ -28,7 +28,7 @@ mod sub_mod;
2828#[ cfg( feature = "rand_core" ) ]
2929mod rand;
3030
31- use crate :: { Integer , Limb , NonZero , Odd , UintRef , Word , Zero , modular:: BoxedMontyForm } ;
31+ use crate :: { Integer , Limb , NonZero , Odd , Resize , UintRef , Word , Zero , modular:: BoxedMontyForm } ;
3232use alloc:: { boxed:: Box , vec, vec:: Vec } ;
3333use core:: fmt;
3434use subtle:: { Choice , ConditionallySelectable , ConstantTimeEq , CtOption } ;
@@ -199,6 +199,7 @@ impl BoxedUint {
199199 ///
200200 /// Panics if `at_least_bits_precision` is smaller than the current precision.
201201 #[ must_use]
202+ #[ deprecated( since = "0.7.0" , note = "please use `resize` instead" ) ]
202203 pub fn widen ( & self , at_least_bits_precision : u32 ) -> BoxedUint {
203204 assert ! ( at_least_bits_precision >= self . bits_precision( ) ) ;
204205
@@ -211,6 +212,7 @@ impl BoxedUint {
211212 ///
212213 /// Panics if `at_least_bits_precision` is larger than the current precision.
213214 #[ must_use]
215+ #[ deprecated( since = "0.7.0" , note = "please use `resize` instead" ) ]
214216 pub fn shorten ( & self , at_least_bits_precision : u32 ) -> BoxedUint {
215217 assert ! ( at_least_bits_precision <= self . bits_precision( ) ) ;
216218 let mut ret = BoxedUint :: zero_with_precision ( at_least_bits_precision) ;
@@ -271,14 +273,76 @@ impl BoxedUint {
271273 limbs[ i] = Limb :: conditional_select ( & limbs[ i] , & Limb :: ZERO , choice) ;
272274 }
273275 }
276+
277+ /// Returns `true` if the integer's bit size is smaller or equal to `bits`.
278+ pub ( crate ) fn is_within_bits ( & self , bits : u32 ) -> bool {
279+ bits >= self . bits_precision ( ) || bits >= self . bits ( )
280+ }
274281}
275282
276- impl NonZero < BoxedUint > {
277- /// Widen this type's precision to the given number of bits.
278- ///
279- /// See [`BoxedUint::widen`] for more information, including panic conditions.
280- pub fn widen ( & self , bits_precision : u32 ) -> Self {
281- NonZero ( self . 0 . widen ( bits_precision) )
283+ impl Resize for BoxedUint {
284+ type Output = BoxedUint ;
285+
286+ fn resize_unchecked ( self , at_least_bits_precision : u32 ) -> Self :: Output {
287+ let new_len = Self :: limbs_for_precision ( at_least_bits_precision) ;
288+ if new_len == self . limbs . len ( ) {
289+ self
290+ } else {
291+ let mut limbs = self . limbs . into_vec ( ) ;
292+ limbs. resize ( new_len, Limb :: ZERO ) ;
293+ Self :: from ( limbs)
294+ }
295+ }
296+
297+ fn try_resize ( self , at_least_bits_precision : u32 ) -> Option < BoxedUint > {
298+ if self . is_within_bits ( at_least_bits_precision) {
299+ Some ( self . resize_unchecked ( at_least_bits_precision) )
300+ } else {
301+ None
302+ }
303+ }
304+ }
305+
306+ impl Resize for & BoxedUint {
307+ type Output = BoxedUint ;
308+
309+ fn resize_unchecked ( self , at_least_bits_precision : u32 ) -> Self :: Output {
310+ let mut ret = BoxedUint :: zero_with_precision ( at_least_bits_precision) ;
311+ let num_limbs_to_copy = core:: cmp:: min ( ret. limbs . len ( ) , self . limbs . len ( ) ) ;
312+ ret. limbs [ ..num_limbs_to_copy] . copy_from_slice ( & self . limbs [ ..num_limbs_to_copy] ) ;
313+ ret
314+ }
315+
316+ fn try_resize ( self , at_least_bits_precision : u32 ) -> Option < BoxedUint > {
317+ if self . is_within_bits ( at_least_bits_precision) {
318+ Some ( self . resize_unchecked ( at_least_bits_precision) )
319+ } else {
320+ None
321+ }
322+ }
323+ }
324+
325+ impl Resize for NonZero < BoxedUint > {
326+ type Output = Self ;
327+
328+ fn resize_unchecked ( self , at_least_bits_precision : u32 ) -> Self :: Output {
329+ NonZero ( self . 0 . resize_unchecked ( at_least_bits_precision) )
330+ }
331+
332+ fn try_resize ( self , at_least_bits_precision : u32 ) -> Option < Self :: Output > {
333+ self . 0 . try_resize ( at_least_bits_precision) . map ( NonZero )
334+ }
335+ }
336+
337+ impl Resize for & NonZero < BoxedUint > {
338+ type Output = NonZero < BoxedUint > ;
339+
340+ fn resize_unchecked ( self , at_least_bits_precision : u32 ) -> Self :: Output {
341+ NonZero ( ( & self . 0 ) . resize_unchecked ( at_least_bits_precision) )
342+ }
343+
344+ fn try_resize ( self , at_least_bits_precision : u32 ) -> Option < Self :: Output > {
345+ ( & self . 0 ) . try_resize ( at_least_bits_precision) . map ( NonZero )
282346 }
283347}
284348
0 commit comments