@@ -6,10 +6,10 @@ use num::{BigInt, bigint::Sign};
66#[ cfg( any( test, feature = "fuzzing" ) ) ]
77use proptest:: strategy:: BoxedStrategy ;
88use rand:: {
9- Rng ,
10- distributions :: {
11- Distribution , Standard ,
12- uniform:: { SampleUniform , UniformSampler } ,
9+ Rng , RngExt ,
10+ distr :: {
11+ Distribution , StandardUniform ,
12+ uniform:: { Error as UniformError , SampleBorrow , SampleUniform , UniformSampler } ,
1313 } ,
1414} ;
1515use std:: {
@@ -308,9 +308,7 @@ impl U256 {
308308
309309 /// U256 to 32 little endian bytes
310310 pub fn to_le_bytes ( self ) -> [ u8 ; U256_NUM_BYTES ] {
311- let mut bytes = [ 0u8 ; U256_NUM_BYTES ] ;
312- self . 0 . to_little_endian ( & mut bytes) ;
313- bytes
311+ self . 0 . to_little_endian ( )
314312 }
315313
316314 /// Leading zeros of the number
@@ -553,11 +551,11 @@ impl TryFrom<U256> for u128 {
553551 }
554552}
555553
556- impl Distribution < U256 > for Standard {
554+ impl Distribution < U256 > for StandardUniform {
557555 #[ inline]
558556 fn sample < R : Rng + ?Sized > ( & self , rng : & mut R ) -> U256 {
559557 let mut dest = [ 0 ; U256_NUM_BYTES ] ;
560- rng. fill_bytes ( & mut dest) ;
558+ rng. fill ( & mut dest) ;
561559 U256 :: from_le_bytes ( & dest)
562560 }
563561}
@@ -578,28 +576,29 @@ impl SampleUniform for U256 {
578576impl UniformSampler for UniformU256 {
579577 type X = U256 ;
580578
581- fn new < B1 , B2 > ( low : B1 , high : B2 ) -> Self
579+ fn new < B1 , B2 > ( low : B1 , high : B2 ) -> Result < Self , UniformError >
582580 where
583- B1 : rand :: distributions :: uniform :: SampleBorrow < Self :: X > + Sized ,
584- B2 : rand :: distributions :: uniform :: SampleBorrow < Self :: X > + Sized ,
581+ B1 : SampleBorrow < Self :: X > + Sized ,
582+ B2 : SampleBorrow < Self :: X > + Sized ,
585583 {
586584 let low = * low. borrow ( ) ;
587585 let high = * high. borrow ( ) ;
588- assert ! ( low < high, "Uniform::new called with `low >= high`" ) ;
586+ if low >= high {
587+ return Err ( UniformError :: EmptyRange ) ;
588+ }
589589 UniformSampler :: new_inclusive ( low, high - U256 :: one ( ) )
590590 }
591591
592- fn new_inclusive < B1 , B2 > ( low : B1 , high : B2 ) -> Self
592+ fn new_inclusive < B1 , B2 > ( low : B1 , high : B2 ) -> Result < Self , UniformError >
593593 where
594- B1 : rand :: distributions :: uniform :: SampleBorrow < Self :: X > + Sized ,
595- B2 : rand :: distributions :: uniform :: SampleBorrow < Self :: X > + Sized ,
594+ B1 : SampleBorrow < Self :: X > + Sized ,
595+ B2 : SampleBorrow < Self :: X > + Sized ,
596596 {
597597 let low = * low. borrow ( ) ;
598598 let high = * high. borrow ( ) ;
599- assert ! (
600- low <= high,
601- "Uniform::new_inclusive called with `low > high`"
602- ) ;
599+ if low > high {
600+ return Err ( UniformError :: EmptyRange ) ;
601+ }
603602 let unsigned_max = U256 :: max_value ( ) ;
604603
605604 let range = high. wrapping_sub ( low) . wrapping_add ( U256 :: one ( ) ) ;
@@ -610,11 +609,11 @@ impl UniformSampler for UniformU256 {
610609 U256 :: zero ( )
611610 } ;
612611
613- UniformU256 {
612+ Ok ( UniformU256 {
614613 low,
615614 range,
616615 z : ints_to_reject,
617- }
616+ } )
618617 }
619618
620619 fn sample < R : rand:: Rng + ?Sized > ( & self , rng : & mut R ) -> Self :: X {
@@ -623,59 +622,64 @@ impl UniformSampler for UniformU256 {
623622 let unsigned_max = U256 :: max_value ( ) ;
624623 let zone = unsigned_max - self . z ;
625624 loop {
626- let v: U256 = rng. r#gen ( ) ;
625+ let v: U256 = rng. random ( ) ;
627626 let ( hi, lo) = v. wmul ( range) ;
628627 if lo <= zone {
629628 return self . low . wrapping_add ( hi) ;
630629 }
631630 }
632631 } else {
633632 // Sample from the entire integer range.
634- rng. r#gen ( )
633+ rng. random ( )
635634 }
636635 }
637636
638- fn sample_single < R : rand:: Rng + ?Sized , B1 , B2 > ( low : B1 , high : B2 , rng : & mut R ) -> Self :: X
637+ fn sample_single < R : rand:: Rng + ?Sized , B1 , B2 > (
638+ low : B1 ,
639+ high : B2 ,
640+ rng : & mut R ,
641+ ) -> Result < Self :: X , UniformError >
639642 where
640- B1 : rand :: distributions :: uniform :: SampleBorrow < Self :: X > + Sized ,
641- B2 : rand :: distributions :: uniform :: SampleBorrow < Self :: X > + Sized ,
643+ B1 : SampleBorrow < Self :: X > + Sized ,
644+ B2 : SampleBorrow < Self :: X > + Sized ,
642645 {
643646 let low = * low. borrow ( ) ;
644647 let high = * high. borrow ( ) ;
645- assert ! ( low < high, "UniformSampler::sample_single: low >= high" ) ;
648+ if low >= high {
649+ return Err ( UniformError :: EmptyRange ) ;
650+ }
646651 Self :: sample_single_inclusive ( low, high - U256 :: one ( ) , rng)
647652 }
648653
649654 fn sample_single_inclusive < R : rand:: Rng + ?Sized , B1 , B2 > (
650655 low : B1 ,
651656 high : B2 ,
652657 rng : & mut R ,
653- ) -> Self :: X
658+ ) -> Result < Self :: X , UniformError >
654659 where
655- B1 : rand :: distributions :: uniform :: SampleBorrow < Self :: X > + Sized ,
656- B2 : rand :: distributions :: uniform :: SampleBorrow < Self :: X > + Sized ,
660+ B1 : SampleBorrow < Self :: X > + Sized ,
661+ B2 : SampleBorrow < Self :: X > + Sized ,
657662 {
658663 let low = * low. borrow ( ) ;
659664 let high = * high. borrow ( ) ;
660- assert ! (
661- low <= high,
662- "UniformSampler::sample_single_inclusive: low > high"
663- ) ;
665+ if low > high {
666+ return Err ( UniformError :: EmptyRange ) ;
667+ }
664668 let range = high. wrapping_sub ( low) . wrapping_add ( U256 :: one ( ) ) ;
665669 // If the above resulted in wrap-around to 0, the range is U256::MIN..=U256::MAX,
666670 // and any integer will do.
667671 if range == U256 :: zero ( ) {
668- return rng. r#gen ( ) ;
672+ return Ok ( rng. random ( ) ) ;
669673 }
670674 // conservative but fast approximation. `- 1` is necessary to allow the
671675 // same comparison without bias.
672676 let zone = ( range << range. leading_zeros ( ) ) . wrapping_sub ( U256 :: one ( ) ) ;
673677
674678 loop {
675- let v: U256 = rng. r#gen ( ) ;
679+ let v: U256 = rng. random ( ) ;
676680 let ( hi, lo) = v. wmul ( range) ;
677681 if lo <= zone {
678- return low. wrapping_add ( hi) ;
682+ return Ok ( low. wrapping_add ( hi) ) ;
679683 }
680684 }
681685 }
0 commit comments