Skip to content

Commit

Permalink
Try to avoid some allocations here
Browse files Browse the repository at this point in the history
  • Loading branch information
burdges committed Mar 14, 2019
1 parent e3436d1 commit 961caee
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions vdf/src/create_discriminant.rs
Expand Up @@ -92,24 +92,25 @@ pub fn create_discriminant<T: BigNumExt>(seed: &[u8], length: u16) -> T {
debug_assert!(n >= Zero::zero());

// This generates the smallest prime ≥ n that is of the form n + m*x.
let mut sieve = ::bit_vec::BitVec::from_elem(1 << 16, true);
loop {
// Speed up prime-finding by quickly ruling out numbers
// that are known to be composite.
let mut sieve = ::bit_vec::BitVec::from_elem(1 << 16, false);
sieve.set_all();
for &(p, q) in SIEVE_INFO.iter() {
// The reference implementation changes the sign of `n` before taking its
// remainder. Instead, we leave `n` as positive, but use ceiling
// division instead of floor division. This is mathematically
// equivalent and potentially faster.
let mut i: usize = (n.crem_u16(p) as usize * q as usize) % p as usize;
while i < sieve.len() {
sieve.set(i, true);
sieve.set(i, false);
i += p as usize;
}
}

for (i, x) in sieve.iter().enumerate() {
if !x {
if x {
let q = u64::from(M) * u64::from(i as u32);
n = n + q;
if n.probab_prime(2) {
Expand Down

0 comments on commit 961caee

Please sign in to comment.