Skip to content

Commit 9a7d869

Browse files
committed
Some coverage fixes
1 parent 4ac32fb commit 9a7d869

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

src/hazmat.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,11 @@ mod tests {
5252
assert!(Primality::Prime != Primality::ProbablyPrime);
5353
assert_eq!(Primality::Prime.clone(), Primality::Prime);
5454
}
55+
56+
#[test]
57+
fn primality_to_bool() {
58+
assert!(Primality::Prime.is_probably_prime());
59+
assert!(Primality::ProbablyPrime.is_probably_prime());
60+
assert!(!Primality::Composite.is_probably_prime());
61+
}
5562
}

src/hazmat/miller_rabin.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ mod tests {
109109

110110
use crypto_bigint::{Uint, U1024, U128, U1536, U64};
111111
use rand_chacha::ChaCha8Rng;
112-
use rand_core::{CryptoRngCore, SeedableRng};
112+
use rand_core::{CryptoRngCore, OsRng, SeedableRng};
113113

114114
#[cfg(feature = "tests-exhaustive")]
115115
use num_prime::nt_funcs::is_prime64;
@@ -124,6 +124,21 @@ mod tests {
124124
assert_eq!(mr.clone(), mr);
125125
}
126126

127+
#[test]
128+
#[should_panic(expected = "`candidate` must be odd.")]
129+
fn parity_check() {
130+
let _mr = MillerRabin::new(&U64::from(10u32));
131+
}
132+
133+
#[test]
134+
#[should_panic(
135+
expected = "No suitable random base possible when `candidate == 3`; use the base 2 test."
136+
)]
137+
fn random_base_range_check() {
138+
let mr = MillerRabin::new(&U64::from(3u32));
139+
mr.test_random_base(&mut OsRng);
140+
}
141+
127142
fn is_spsp(num: u32) -> bool {
128143
pseudoprimes::STRONG_BASE_2.iter().any(|x| *x == num)
129144
}

src/presets.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,8 @@ fn _is_prime_with_rng<const L: usize>(rng: &mut impl CryptoRngCore, num: &Uint<L
145145
debug_assert!(bool::from(num.is_odd()));
146146
let mr = MillerRabin::new(num);
147147

148-
match mr.test_base_two() {
149-
Primality::Composite => return false,
150-
Primality::Prime => return true,
151-
_ => {}
148+
if !mr.test_base_two().is_probably_prime() {
149+
return false;
152150
}
153151

154152
match lucas_test(num, SelfridgeBase, LucasCheck::Strong) {
@@ -158,12 +156,8 @@ fn _is_prime_with_rng<const L: usize>(rng: &mut impl CryptoRngCore, num: &Uint<L
158156
}
159157

160158
// The random base test only makes sense when `num > 3`.
161-
if num.bits() > 2 {
162-
match mr.test_random_base(rng) {
163-
Primality::Composite => return false,
164-
Primality::Prime => return true,
165-
_ => {}
166-
}
159+
if num.bits() > 2 && !mr.test_random_base(rng).is_probably_prime() {
160+
return false;
167161
}
168162

169163
true

0 commit comments

Comments
 (0)