Skip to content

Commit 4222476

Browse files
committed
Panic if Sieve::new() is called with max_bit_length == 0
1 parent ceb6557 commit 4222476

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
44
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
55

66

7+
## [0.3.1] - Unreleased
8+
9+
### Fixed
10+
11+
- `Sieve::new()` now panics when `max_bit_length == 0` (which would lead to incorrect results anyway, so it is not considered a breaking change). ([#26])
12+
13+
14+
[#26]: https://github.com/nucypher/rust-umbral/pull/26
15+
16+
717
## [0.3.0] - 2023-05-05
818

919
### Changed

src/hazmat/sieve.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,14 @@ impl<const L: usize> Sieve<L> {
7575
/// Note that `start` is adjusted to `2`, or the next `1 mod 2` number (`safe_primes = false`);
7676
/// and `5`, or `3 mod 4` number (`safe_primes = true`).
7777
///
78-
/// Panics if `max_bit_length` is greater than the size of the target `Uint`.
78+
/// Panics if `max_bit_length` is zero or greater than the size of the target `Uint`.
7979
///
8080
/// If `safe_primes` is `true`, both the returned `n` and `n/2` are sieved.
8181
pub fn new(start: &Uint<L>, max_bit_length: usize, safe_primes: bool) -> Self {
82+
if max_bit_length == 0 {
83+
panic!("The requested bit length cannot be zero");
84+
}
85+
8286
if max_bit_length > Uint::<L>::BITS {
8387
panic!(
8488
"The requested bit length ({}) is larger than the chosen Uint size",
@@ -333,6 +337,12 @@ mod tests {
333337
check_sieve(13, 4, true, &[]);
334338
}
335339

340+
#[test]
341+
#[should_panic(expected = "The requested bit length cannot be zero")]
342+
fn sieve_zero_bits() {
343+
let _sieve = Sieve::new(&U64::ONE, 0, false);
344+
}
345+
336346
#[test]
337347
#[should_panic(expected = "The requested bit length (65) is larger than the chosen Uint size")]
338348
fn sieve_too_many_bits() {

0 commit comments

Comments
 (0)