Skip to content

Commit de8d0fa

Browse files
committed
Rephrase expect messages according to the stdlib guidelines
1 parent caff273 commit de8d0fa

File tree

7 files changed

+24
-21
lines changed

7 files changed

+24
-21
lines changed

src/hazmat/gcd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(crate) fn gcd_vartime<const L: usize>(n: &Uint<L>, m: Word) -> Word {
1818
// Normalize input: the resulting (a, b) are both small, a >= b, and b != 0.
1919
let (mut a, mut b): (Word, Word) = if n.bits() > Word::BITS {
2020
// `m` is non-zero, so we can unwrap.
21-
let r = n.rem_limb(NonZero::new(Limb::from(m)).expect("divisor ensured to be non-zero"));
21+
let r = n.rem_limb(NonZero::new(Limb::from(m)).expect("divisor should be non-zero here"));
2222
(m, r.0)
2323
} else {
2424
// In this branch `n` is `Word::BITS` bits or shorter,

src/hazmat/jacobi.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ pub(crate) fn jacobi_symbol_vartime<const L: usize>(
110110
let (result, a_long, p) = swap(result, a, p_long.get());
111111
// Can unwrap here, since `p` is swapped with `a`,
112112
// and `a` would be odd after `reduce_numerator()`.
113-
let a = a_long.rem_limb(NonZero::new(Limb::from(p)).expect("ensured to be non-zero"));
113+
let a =
114+
a_long.rem_limb(NonZero::new(Limb::from(p)).expect("divisor should be non-zero here"));
114115
(result, a.0, p)
115116
};
116117

src/hazmat/lucas.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,14 @@ fn decompose<const L: usize>(n: &Odd<Uint<L>>) -> (u32, Odd<Uint<L>>) {
192192
// so we right-shifted at least once.
193193
n.as_ref()
194194
.overflowing_shr(s)
195-
.expect("shift within range")
195+
.expect("shift should be within range by construction")
196196
.checked_add(&Uint::ONE)
197-
.expect("Integer overflow")
197+
.expect("addition should not overflow by construction")
198198
} else {
199199
Uint::ONE
200200
};
201201

202-
(s, Odd::new(d).expect("ensured to be odd"))
202+
(s, Odd::new(d).expect("`d` should be odd by construction"))
203203
}
204204

205205
/// The checks to perform in the Lucas test.

src/hazmat/miller_rabin.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<const L: usize> MillerRabin<L> {
4646
// Will not overflow because `candidate` is odd and greater than 1.
4747
let d = candidate_minus_one
4848
.overflowing_shr_vartime(s)
49-
.expect("shift within range");
49+
.expect("shift should be within range by construction");
5050
(s, d)
5151
};
5252

@@ -108,12 +108,13 @@ impl<const L: usize> MillerRabin<L> {
108108

109109
let range = self.candidate.wrapping_sub(&Uint::<L>::from(4u32));
110110
// Can unwrap here since `candidate` is odd, and `candidate >= 4` (as checked above)
111-
let range_nonzero = NonZero::new(range).expect("ensured to be non-zero");
111+
let range_nonzero =
112+
NonZero::new(range).expect("the range should be non-zero by construction");
112113
// This should not overflow as long as `random_mod()` behaves according to the contract
113114
// (that is, returns a number within the given range).
114115
let random = Uint::<L>::random_mod(rng, &range_nonzero)
115116
.checked_add(&Uint::<L>::from(3u32))
116-
.expect("Integer overflow");
117+
.expect("addition should not overflow by construction");
117118
self.test(&random)
118119
}
119120
}

src/hazmat/precomputed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ const fn create_reciprocals() -> [Reciprocal; SMALL_PRIMES.len()] {
152152
arr[i] = Reciprocal::new(
153153
Limb(SMALL_PRIMES[i] as Word)
154154
.to_nz()
155-
.expect("ensured to be non-zero"),
155+
.expect("divisor should be non-zero"),
156156
);
157157
i += 1;
158158
}

src/hazmat/sieve.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ pub fn random_odd_uint<const L: usize>(
4040
// Will not overflow since `bit_length` is ensured to be within the size of the integer.
4141
random |= Uint::<L>::ONE
4242
.overflowing_shl_vartime(bit_length - 1)
43-
.expect("shift within range");
43+
.expect("shift should be within range by construction");
4444

45-
Odd::new(random).expect("ensured to be odd")
45+
Odd::new(random).expect("the number should be odd by construction")
4646
}
4747

4848
// The type we use to calculate incremental residues.
@@ -164,7 +164,7 @@ impl<const L: usize> Sieve<L> {
164164
self.base = self
165165
.base
166166
.checked_add(&self.incr.into())
167-
.expect("Integer overflow");
167+
.expect("addition should not overflow by construction");
168168

169169
self.incr = 0;
170170

@@ -190,7 +190,7 @@ impl<const L: usize> Sieve<L> {
190190
// and `INCR_LIMIT` fits into `Residue`.
191191
let incr_limit_small: Residue = incr_limit.as_words()[0]
192192
.try_into()
193-
.expect("ensured to fit within `Residue`");
193+
.expect("the increment limit should fit within `Residue`");
194194
incr_limit_small
195195
};
196196

@@ -233,7 +233,7 @@ impl<const L: usize> Sieve<L> {
233233
let mut num: Uint<L> = self
234234
.base
235235
.checked_add(&self.incr.into())
236-
.expect("Integer overflow");
236+
.expect("addition should not overflow by construction");
237237
if self.safe_primes {
238238
num = num.wrapping_shl_vartime(1) | Uint::<L>::ONE;
239239
}

src/presets.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crypto_bigint::{Integer, Odd, Uint};
1+
use crypto_bigint::{Odd, Uint};
22
use rand_core::CryptoRngCore;
33

44
#[cfg(feature = "default-rng")]
@@ -125,11 +125,12 @@ pub fn is_prime_with_rng<const L: usize>(rng: &mut impl CryptoRngCore, num: &Uin
125125
if num == &Uint::<L>::from(2u32) {
126126
return true;
127127
}
128-
if num.is_even().into() {
129-
return false;
130-
}
131128

132-
let odd_num = Odd::new(*num).expect("ensured to be odd");
129+
let odd_num = match Odd::new(*num).into() {
130+
Some(x) => x,
131+
None => return false,
132+
};
133+
133134
_is_prime_with_rng(rng, &odd_num)
134135
}
135136

@@ -148,8 +149,8 @@ pub fn is_safe_prime_with_rng<const L: usize>(rng: &mut impl CryptoRngCore, num:
148149
}
149150

150151
// These are ensured to be odd by the check above.
151-
let odd_num = Odd::new(*num).expect("ensured to be odd");
152-
let odd_half_num = Odd::new(num.wrapping_shr_vartime(1)).expect("ensured to be odd");
152+
let odd_num = Odd::new(*num).expect("`num` should be odd here");
153+
let odd_half_num = Odd::new(num.wrapping_shr_vartime(1)).expect("`num/2` should be odd here");
153154

154155
_is_prime_with_rng(rng, &odd_num) && _is_prime_with_rng(rng, &odd_half_num)
155156
}

0 commit comments

Comments
 (0)