/// generate a number p with the bits length of `bits_len`, such that p is prime 
/// with high probability that is related to the number of `test_round_num`;
/// 
/// `test_round_num` means the number of test rounds, for any odd number that great than 2 and positive integer n, the probability of error 
/// in MillerRabinPrimeTest is at most $2^{-n}$.
generate a number p with the bits length of `bits_len`, such that p is prime
with high probability that is related to the number of `test_round_num`.

`test_round_num` means the number of test rounds, for any odd number that great than 2 and positive integer n, the probability of error
in MillerRabinPrimeTest is at most $2^{-n}$.
generate a number p with the bits length of ` bits_len ` such that p is prime with high probability that is related to the number of ` test_round_num ` . ` test_round_num ` means the number of test rounds for any odd number that great than 2 and positive integer n the probability of error in MillerRabinPrimeTest is at most $2^ { - n } $ .
pub fn generate_prime<Rng: IterSource<u32>>(bits_len: usize, test_round_num: usize, rng: &mut Rng) -> Result<Nat, NatError> {
        if bits_len < 2 {
            return Err(NatError::new(ParseErrKind::InvalidParameters, "prime size must at least 2-bits"));
        }
        const SMALL_PRIMES: [u8; 15] = [
            3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
        ];
        const SMALL_PRIMES_PRODUCT: u64 = 16294579238595022365u64;
        // let b = if (bits_len & 7) == 0 {8} else {bits_len & 7};
        // 
        // let len = (bits_len + 7) >> 3;
        // let mut buf = Vec::with_capacity(len);
        let b = if (bits_len & 31) == 0 {32} else {bits_len & 31};
        let len = (bits_len + 31) >> 5;
        let mut p = Nat::with_capacity(len);
        loop {
            p.as_mut_vec().clear();
            for x in rng.iter_mut().take(len) {
                p.as_mut_vec().push(x);
            }
            // 清除大于bits_len的位;
            if b != 32 {
                *p.iter_mut().last().unwrap() &= (1u32 << b) - 1;
            }
            // Don't let the value be too small, i.e, set the most significant two bits.
            // Setting the top two bits, rather than just the top bit,
            // means that when two of these values are multiplied together,
            // the result isn't ever one bit short.
            if b >= 2 {
                *p.iter_mut().last().unwrap() |= 3 << (b - 2);
            } else {
                for (i, x) in p.iter_mut().rev().enumerate() {
                    if i == 0 {
                        *x |= 1;
                    } else if i == 1 {
                        *x |= 0x80000000;
                        break;
                    }
                }
            }
            // 奇数
            *p.iter_mut().next().unwrap() |= 0x1;
            // Calculate the value mod the product of smallPrimes. If it's
            // a multiple of any of these primes we add two until it isn't.
            // The probability of overflowing is minimal and can be ignored
            // because we still perform Miller-Rabin tests on the result.
            let modulus = p.rem_inner_u64(SMALL_PRIMES_PRODUCT);
            'next_delta: for delta in (0u64..(1u64 << 20)).step_by(2) {
                let m = modulus + delta;
                for &prime in SMALL_PRIMES.iter() {
                    let prime = prime as u64;
                    if (m % prime) == 0 && (bits_len > 6 || m != prime) {
                        continue 'next_delta;
                    }
                }
                if delta > 0 {
                    p += (delta & 0xffff_ffff) as u32;
                }
                break;
            }
            if p.bits_len() == bits_len && p.probably_prime_test(test_round_num, rng) {
                return Ok(p);
            }
        }
    }
pub fn generate_prime < Rng : IterSource < u32 > > ( bits_len : usize , test_round_num : usize , rng : & mut Rng ) -> Result < Nat , NatError > { if bits_len < 2 { return Err ( NatError :: new ( ParseErrKind :: InvalidParameters , " " ) ) ; } const SMALL_PRIMES : [ u8 ; 15 ] = [ 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , ] ; const SMALL_PRIMES_PRODUCT : u64 = 16294579238595022365u64 ; // let b = if (bits_len & 7) == 0 {8} else {bits_len & 7}; //  // let len = (bits_len + 7) >> 3; // let mut buf = Vec::with_capacity(len); let b = if ( bits_len & 31 ) == 0 { 32 } else { bits_len & 31 } ; let len = ( bits_len + 31 ) >> 5 ; let mut p = Nat :: with_capacity ( len ) ; loop { p . as_mut_vec ( ) . clear ( ) ; for x in rng . iter_mut ( ) . take ( len ) { p . as_mut_vec ( ) . push ( x ) ; } // 清除大于bits_len的位; if b != 32 { * p . iter_mut ( ) . last ( ) . unwrap ( ) &= ( 1u32 << b ) - 1 ; } // Don't let the value be too small, i.e, set the most significant two bits. // Setting the top two bits, rather than just the top bit, // means that when two of these values are multiplied together, // the result isn't ever one bit short. if b >= 2 { * p . iter_mut ( ) . last ( ) . unwrap ( ) |= 3 << ( b - 2 ) ; } else { for ( i , x ) in p . iter_mut ( ) . rev ( ) . enumerate ( ) { if i == 0 { * x |= 1 ; } else if i == 1 { * x |= 0x80000000 ; break ; } } } // 奇数 * p . iter_mut ( ) . next ( ) . unwrap ( ) |= 0x1 ; // Calculate the value mod the product of smallPrimes. If it's // a multiple of any of these primes we add two until it isn't. // The probability of overflowing is minimal and can be ignored // because we still perform Miller-Rabin tests on the result. let modulus = p . rem_inner_u64 ( SMALL_PRIMES_PRODUCT ) ; ' next_delta : for delta in ( 0u64 .. ( 1u64 << 20 ) ) . step_by ( 2 ) { let m = modulus + delta ; for & prime in SMALL_PRIMES . iter ( ) { let prime = prime as u64 ; if ( m % prime ) == 0 && ( bits_len > 6 || m != prime ) { continue ' next_delta ; } } if delta > 0 { p += ( delta & 0xffff_ffff ) as u32 ; } break ; } if p . bits_len ( ) == bits_len && p . probably_prime_test ( test_round_num , rng ) { return Ok ( p ) ; } } }
generate a number p with the bits length of `bits_len`, such that p is prime
with high probability that is related to the number of `test_round_num`;
generate a number p with the bits length of ` bits_len ` such that p is prime with high probability that is related to the number of ` test_round_num ` ;
bits_len
usize
test_round_num
usize
rng
&mut Rng