Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,16 @@ pub fn sieve_generate_safe_primes(

for &small_prime in &small_primes::SMALL_PRIMES[0..amount] {
let mod_result = x.mod_u(small_prime);
if mod_result == (small_prime - 1) / 2 {
if mod_result == 0 || mod_result == (small_prime - 1) / 2 {
continue 'trial;
}
}

// 25 taken same as one used in mpz_nextprime
if let IsPrime::Yes | IsPrime::Probably = x.is_probably_prime(25, rng) {
if let IsPrime::Yes | IsPrime::Probably = x.is_probably_prime_without_sieve() {
x <<= 1;
x += 1;
if let IsPrime::Yes | IsPrime::Probably = x.is_probably_prime(25, rng) {
if let IsPrime::Yes | IsPrime::Probably = x.is_probably_prime_without_sieve() {
return x;
}
}
Expand Down
20 changes: 18 additions & 2 deletions src/backend/num_bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ impl Integer {
self
}

// TODO neither reps nor rng are used here, maybe we should get rid of them
pub fn is_probably_prime(&self, _reps: u32, _rng: &mut impl rand_core::RngCore) -> IsPrime {
/// Performs primality test without sieve optimization
pub(super) fn is_probably_prime_without_sieve(&self) -> IsPrime {
fn boxed_uint(n: &num_bigint::BigUint) -> crypto_bigint::BoxedUint {
crypto_bigint::cpubits! {
16 | 32 => { crypto_bigint::BoxedUint::from_words(n.iter_u32_digits()) }
Expand All @@ -319,6 +319,22 @@ impl Integer {
}
}

/// Performs primality tests agains provided integer
// TODO neither reps nor rng are used here, maybe we should get rid of them
pub fn is_probably_prime(&self, _reps: u32, _rng: &mut impl rand_core::RngCore) -> IsPrime {
for &small_prime in &crate::utils::small_primes::SMALL_PRIMES {
if self.mod_u(small_prime) == 0 {
return if *self == Integer::from(small_prime) {
IsPrime::Yes
} else {
IsPrime::No
};
}
}
self.is_probably_prime_without_sieve()
}

/// Generates a random prime
pub fn generate_prime(rng: &mut impl rand_core::RngCore, bit_size: u32) -> Self {
let mut x = Integer::zero();
loop {
Expand Down
30 changes: 27 additions & 3 deletions src/backend/rug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,13 @@ impl Integer {
self
}

pub fn is_probably_prime(&self, reps: u32, _rng: &mut impl rand_core::RngCore) -> IsPrime {
/// Performs primality test without sieve optimization
#[cfg_attr(feature = "backend-num-bigint", allow(dead_code))] // if num-bigint backend is enabled, this function is unused
pub(super) fn is_probably_prime_without_sieve(&self) -> IsPrime {
self.is_probably_prime_without_sieve_with_reps(25)
}

fn is_probably_prime_without_sieve_with_reps(&self, reps: u32) -> IsPrime {
if self.cmp0().is_le() {
return IsPrime::No;
}
Expand All @@ -263,14 +269,32 @@ impl Integer {
}
}

/// Performs primality tests agains provided integer
pub fn is_probably_prime(&self, reps: u32, _rng: &mut impl rand_core::RngCore) -> IsPrime {
for &small_prime in &crate::utils::small_primes::SMALL_PRIMES {
if self.mod_u(small_prime) == 0 {
return if *self == Integer::from(small_prime) {
IsPrime::Yes
} else {
IsPrime::No
};
}
}
self.is_probably_prime_without_sieve_with_reps(reps)
}

/// Generates a random prime
pub fn generate_prime(rng: &mut impl rand_core::RngCore, bit_size: u32) -> Self {
let mut x = Integer::zero();
loop {
x.assign_random_bits(bit_size, rng);
x.set_bit(bit_size - 1, true);
x |= 1u32;
if let IsPrime::Yes | IsPrime::Probably = x.is_probably_prime(25, rng) {
return x;
if matches!(
x.is_probably_prime(25, rng),
IsPrime::Probably | IsPrime::Yes
) {
break x;
}
}
}
Expand Down