File tree Expand file tree Collapse file tree 6 files changed +16
-37
lines changed Expand file tree Collapse file tree 6 files changed +16
-37
lines changed Original file line number Diff line number Diff line change @@ -78,34 +78,17 @@ impl BoxedUint {
78
78
. fold ( Choice :: from ( 1 ) , |acc, limb| acc & limb. is_zero ( ) )
79
79
}
80
80
81
- /// Create a new [`BoxedUint`] with the given number of bits of precision.
82
- ///
83
- /// Returns `None` if the number of bits is not a multiple of the
84
- /// [`Limb`] size.
85
- pub fn new ( bits_precision : usize ) -> Option < Self > {
86
- if bits_precision == 0 || bits_precision % Limb :: BITS != 0 {
87
- return None ;
88
- }
89
-
90
- let nlimbs = bits_precision / Limb :: BITS ;
91
-
92
- Some ( Self {
93
- limbs : vec ! [ Limb :: ZERO ; nlimbs] . into ( ) ,
94
- } )
95
- }
96
-
97
81
/// Get the maximum value for a given number of bits of precision.
98
82
///
99
- /// Returns `None` if the number of bits is not a multiple of the
100
- /// [`Limb`] size.
101
- pub fn max ( bits_precision : usize ) -> Option < Self > {
102
- let mut ret = Self :: new ( bits_precision) ?;
103
-
104
- for limb in & mut * ret. limbs {
105
- * limb = Limb :: MAX ;
106
- }
83
+ /// Panics if the precision is not a multiple of [`Limb::BITS`].
84
+ pub fn max ( bits_precision : usize ) -> Self {
85
+ assert_eq ! (
86
+ bits_precision % Limb :: BITS ,
87
+ 0 ,
88
+ "precision is not a multiple of limb size"
89
+ ) ;
107
90
108
- Some ( ret )
91
+ vec ! [ Limb :: MAX ; bits_precision / Limb :: BITS ] . into ( )
109
92
}
110
93
111
94
/// Create a [`BoxedUint`] from an array of [`Word`]s (i.e. word-sized unsigned
Original file line number Diff line number Diff line change @@ -39,9 +39,7 @@ mod tests {
39
39
40
40
#[ test]
41
41
fn adc_with_carry ( ) {
42
- let ( res, carry) = BoxedUint :: max ( Limb :: BITS )
43
- . unwrap ( )
44
- . adc ( & BoxedUint :: one ( ) , Limb :: ZERO ) ;
42
+ let ( res, carry) = BoxedUint :: max ( Limb :: BITS ) . adc ( & BoxedUint :: one ( ) , Limb :: ZERO ) ;
45
43
assert_eq ! ( res, BoxedUint :: zero( ) ) ;
46
44
assert_eq ! ( carry, Limb :: ONE ) ;
47
45
}
@@ -54,9 +52,7 @@ mod tests {
54
52
55
53
#[ test]
56
54
fn checked_add_overflow ( ) {
57
- let result = BoxedUint :: max ( Limb :: BITS )
58
- . unwrap ( )
59
- . checked_add ( & BoxedUint :: one ( ) ) ;
55
+ let result = BoxedUint :: max ( Limb :: BITS ) . checked_add ( & BoxedUint :: one ( ) ) ;
60
56
assert ! ( !bool :: from( result. is_some( ) ) ) ;
61
57
}
62
58
}
Original file line number Diff line number Diff line change @@ -140,7 +140,7 @@ mod tests {
140
140
141
141
#[ test]
142
142
fn overlapping_and_ok ( ) {
143
- let result = BoxedUint :: max ( 128 ) . unwrap ( ) . wrapping_and ( & BoxedUint :: one ( ) ) ;
143
+ let result = BoxedUint :: max ( 128 ) . wrapping_and ( & BoxedUint :: one ( ) ) ;
144
144
assert_eq ! ( result, BoxedUint :: one( ) ) ;
145
145
}
146
146
}
Original file line number Diff line number Diff line change @@ -91,7 +91,7 @@ mod tests {
91
91
fn ct_gt ( ) {
92
92
let a = BoxedUint :: zero ( ) ;
93
93
let b = BoxedUint :: one ( ) ;
94
- let c = BoxedUint :: max ( 64 ) . unwrap ( ) ;
94
+ let c = BoxedUint :: max ( 64 ) ;
95
95
96
96
assert ! ( bool :: from( b. ct_gt( & a) ) ) ;
97
97
assert ! ( bool :: from( c. ct_gt( & a) ) ) ;
@@ -110,7 +110,7 @@ mod tests {
110
110
fn ct_lt ( ) {
111
111
let a = BoxedUint :: zero ( ) ;
112
112
let b = BoxedUint :: one ( ) ;
113
- let c = BoxedUint :: max ( 64 ) . unwrap ( ) ;
113
+ let c = BoxedUint :: max ( 64 ) ;
114
114
115
115
assert ! ( bool :: from( a. ct_lt( & b) ) ) ;
116
116
assert ! ( bool :: from( a. ct_lt( & c) ) ) ;
@@ -129,7 +129,7 @@ mod tests {
129
129
fn cmp ( ) {
130
130
let a = BoxedUint :: zero ( ) ;
131
131
let b = BoxedUint :: one ( ) ;
132
- let c = BoxedUint :: max ( 64 ) . unwrap ( ) ;
132
+ let c = BoxedUint :: max ( 64 ) ;
133
133
134
134
assert_eq ! ( a. cmp( & b) , Ordering :: Less ) ;
135
135
assert_eq ! ( a. cmp( & c) , Ordering :: Less ) ;
Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ mod tests {
44
44
#[ test]
45
45
fn sbb_with_borrow ( ) {
46
46
let ( res, borrow) = BoxedUint :: zero ( ) . sbb ( & BoxedUint :: one ( ) , Limb :: ZERO ) ;
47
- assert_eq ! ( res, BoxedUint :: max( Limb :: BITS ) . unwrap ( ) ) ;
47
+ assert_eq ! ( res, BoxedUint :: max( Limb :: BITS ) ) ;
48
48
assert_eq ! ( borrow, Limb :: MAX ) ;
49
49
}
50
50
Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ proptest! {
42
42
match Option :: <BoxedUint >:: from( a. checked_add( & b) ) {
43
43
Some ( actual) => prop_assert_eq!( expected, to_biguint( & actual) ) ,
44
44
None => {
45
- let max = BoxedUint :: max( a. bits( ) ) . unwrap ( ) ;
45
+ let max = BoxedUint :: max( a. bits( ) ) ;
46
46
prop_assert!( expected > to_biguint( & max) ) ;
47
47
}
48
48
}
You can’t perform that action at this time.
0 commit comments