Skip to content

Commit 4acb30a

Browse files
tech6hutchcuviper
authored andcommitted
Add divide-by-zero panic messages
This brings it more in line with std's numbers, and lets the user actually know why it panicked.
1 parent ee2b80b commit 4acb30a

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

src/algorithms.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ pub(crate) fn scalar_mul(a: &mut [BigDigit], b: BigDigit) -> BigDigit {
550550

551551
pub(crate) fn div_rem(mut u: BigUint, mut d: BigUint) -> (BigUint, BigUint) {
552552
if d.is_zero() {
553-
panic!()
553+
panic!("attempt to divide by zero")
554554
}
555555
if u.is_zero() {
556556
return (Zero::zero(), Zero::zero());
@@ -597,7 +597,7 @@ pub(crate) fn div_rem(mut u: BigUint, mut d: BigUint) -> (BigUint, BigUint) {
597597

598598
pub(crate) fn div_rem_ref(u: &BigUint, d: &BigUint) -> (BigUint, BigUint) {
599599
if d.is_zero() {
600-
panic!()
600+
panic!("attempt to divide by zero")
601601
}
602602
if u.is_zero() {
603603
return (Zero::zero(), Zero::zero());

src/bigint.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -3250,7 +3250,10 @@ impl BigInt {
32503250
!exponent.is_negative(),
32513251
"negative exponentiation is not supported!"
32523252
);
3253-
assert!(!modulus.is_zero(), "divide by zero!");
3253+
assert!(
3254+
!modulus.is_zero(),
3255+
"attempt to calculate with zero modulus!"
3256+
);
32543257

32553258
let result = self.data.modpow(&exponent.data, &modulus.data);
32563259
if result.is_zero() {

src/biguint.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ impl Div<BigUint> for u32 {
12481248
#[inline]
12491249
fn div(self, other: BigUint) -> BigUint {
12501250
match other.data.len() {
1251-
0 => panic!(),
1251+
0 => panic!("attempt to divide by zero"),
12521252
1 => From::from(self as BigDigit / other.data[0]),
12531253
_ => Zero::zero(),
12541254
}
@@ -1280,7 +1280,7 @@ impl Div<BigUint> for u64 {
12801280
#[inline]
12811281
fn div(self, other: BigUint) -> BigUint {
12821282
match other.data.len() {
1283-
0 => panic!(),
1283+
0 => panic!("attempt to divide by zero"),
12841284
1 => From::from(self / u64::from(other.data[0])),
12851285
2 => From::from(self / big_digit::to_doublebigdigit(other.data[1], other.data[0])),
12861286
_ => Zero::zero(),
@@ -1291,7 +1291,7 @@ impl Div<BigUint> for u64 {
12911291
#[inline]
12921292
fn div(self, other: BigUint) -> BigUint {
12931293
match other.data.len() {
1294-
0 => panic!(),
1294+
0 => panic!("attempt to divide by zero"),
12951295
1 => From::from(self / other.data[0]),
12961296
_ => Zero::zero(),
12971297
}
@@ -1322,7 +1322,7 @@ impl Div<BigUint> for u128 {
13221322
#[inline]
13231323
fn div(self, other: BigUint) -> BigUint {
13241324
match other.data.len() {
1325-
0 => panic!(),
1325+
0 => panic!("attempt to divide by zero"),
13261326
1 => From::from(self / u128::from(other.data[0])),
13271327
2 => From::from(
13281328
self / u128::from(big_digit::to_doublebigdigit(other.data[1], other.data[0])),
@@ -1339,7 +1339,7 @@ impl Div<BigUint> for u128 {
13391339
#[inline]
13401340
fn div(self, other: BigUint) -> BigUint {
13411341
match other.data.len() {
1342-
0 => panic!(),
1342+
0 => panic!("attempt to divide by zero"),
13431343
1 => From::from(self / other.data[0] as u128),
13441344
2 => From::from(self / big_digit::to_doublebigdigit(other.data[1], other.data[0])),
13451345
_ => Zero::zero(),
@@ -1424,7 +1424,7 @@ macro_rules! impl_rem_assign_scalar {
14241424
fn rem_assign(&mut self, other: &BigUint) {
14251425
*self = match other.$to_scalar() {
14261426
None => *self,
1427-
Some(0) => panic!(),
1427+
Some(0) => panic!("attempt to divide by zero"),
14281428
Some(v) => *self % v
14291429
};
14301430
}
@@ -2686,7 +2686,10 @@ impl BigUint {
26862686
///
26872687
/// Panics if the modulus is zero.
26882688
pub fn modpow(&self, exponent: &Self, modulus: &Self) -> Self {
2689-
assert!(!modulus.is_zero(), "divide by zero!");
2689+
assert!(
2690+
!modulus.is_zero(),
2691+
"attempt to calculate with zero modulus!"
2692+
);
26902693

26912694
if modulus.is_odd() {
26922695
// For an odd modulus, we can use Montgomery multiplication in base 2^32.
@@ -2725,7 +2728,10 @@ impl BigUint {
27252728
}
27262729

27272730
fn plain_modpow(base: &BigUint, exp_data: &[BigDigit], modulus: &BigUint) -> BigUint {
2728-
assert!(!modulus.is_zero(), "divide by zero!");
2731+
assert!(
2732+
!modulus.is_zero(),
2733+
"attempt to calculate with zero modulus!"
2734+
);
27292735

27302736
let i = match exp_data.iter().position(|&r| r != 0) {
27312737
None => return BigUint::one(),

0 commit comments

Comments
 (0)