Skip to content

Commit e334064

Browse files
authored
Replace BoxedUint::new with ::zero_with_precision (#327)
The two APIs are nearly identical. The only difference is the latter is infallible and panics if an invalid precision is given. This also changes `BoxedUint::max` to have the same behavior. Whether they should panic is a debatable point. Indeed having both options would be good, but it should probably return `CtOption` where `BoxedUint::max` was returning `Option`. Regardless, this removes duplication and makes the API at least more consistent with itself.
1 parent 464673b commit e334064

File tree

6 files changed

+16
-37
lines changed

6 files changed

+16
-37
lines changed

src/uint/boxed.rs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,34 +78,17 @@ impl BoxedUint {
7878
.fold(Choice::from(1), |acc, limb| acc & limb.is_zero())
7979
}
8080

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-
9781
/// Get the maximum value for a given number of bits of precision.
9882
///
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+
);
10790

108-
Some(ret)
91+
vec![Limb::MAX; bits_precision / Limb::BITS].into()
10992
}
11093

11194
/// Create a [`BoxedUint`] from an array of [`Word`]s (i.e. word-sized unsigned

src/uint/boxed/add.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ mod tests {
3939

4040
#[test]
4141
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);
4543
assert_eq!(res, BoxedUint::zero());
4644
assert_eq!(carry, Limb::ONE);
4745
}
@@ -54,9 +52,7 @@ mod tests {
5452

5553
#[test]
5654
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());
6056
assert!(!bool::from(result.is_some()));
6157
}
6258
}

src/uint/boxed/bit_and.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ mod tests {
140140

141141
#[test]
142142
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());
144144
assert_eq!(result, BoxedUint::one());
145145
}
146146
}

src/uint/boxed/cmp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ mod tests {
9191
fn ct_gt() {
9292
let a = BoxedUint::zero();
9393
let b = BoxedUint::one();
94-
let c = BoxedUint::max(64).unwrap();
94+
let c = BoxedUint::max(64);
9595

9696
assert!(bool::from(b.ct_gt(&a)));
9797
assert!(bool::from(c.ct_gt(&a)));
@@ -110,7 +110,7 @@ mod tests {
110110
fn ct_lt() {
111111
let a = BoxedUint::zero();
112112
let b = BoxedUint::one();
113-
let c = BoxedUint::max(64).unwrap();
113+
let c = BoxedUint::max(64);
114114

115115
assert!(bool::from(a.ct_lt(&b)));
116116
assert!(bool::from(a.ct_lt(&c)));
@@ -129,7 +129,7 @@ mod tests {
129129
fn cmp() {
130130
let a = BoxedUint::zero();
131131
let b = BoxedUint::one();
132-
let c = BoxedUint::max(64).unwrap();
132+
let c = BoxedUint::max(64);
133133

134134
assert_eq!(a.cmp(&b), Ordering::Less);
135135
assert_eq!(a.cmp(&c), Ordering::Less);

src/uint/boxed/sub.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ mod tests {
4444
#[test]
4545
fn sbb_with_borrow() {
4646
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));
4848
assert_eq!(borrow, Limb::MAX);
4949
}
5050

tests/boxed_uint_proptests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ proptest! {
4242
match Option::<BoxedUint>::from(a.checked_add(&b)) {
4343
Some(actual) => prop_assert_eq!(expected, to_biguint(&actual)),
4444
None => {
45-
let max = BoxedUint::max(a.bits()).unwrap();
45+
let max = BoxedUint::max(a.bits());
4646
prop_assert!(expected > to_biguint(&max));
4747
}
4848
}

0 commit comments

Comments
 (0)