1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
extern crate num_bigint as bigint;
extern crate num_integer as integer;
extern crate num_traits as traits;
extern crate rand;
extern crate rayon;

use {
    bigint::{BigUint, RandBigInt, ToBigUint},
    rayon::prelude::*,
    std::iter::repeat_with,
    traits::{One, Zero},
};

macro_rules! biguint {
    ($e:expr) => {
        ($e).to_biguint().unwrap()
    };
}

fn decompose(n: &BigUint) -> (BigUint, BigUint) {
    let one = One::one();
    let ref two = biguint!(2);
    let ref n_minus_one: BigUint = n - 1u8;
    let mut d: BigUint = n_minus_one.clone();
    let mut r: BigUint = Zero::zero();

    while &d % two == one {
        d /= two;
        r += 1u8;
    }

    (d, r)
}

fn __miller_rabin(a: &BigUint, n: &BigUint, d: &BigUint, r: &BigUint) -> bool {
    let n_minus_one = n - 1u8;
    let mut x = a.modpow(d, n);
    let mut count: BigUint = One::one();
    let ref two = biguint!(2);

    if x == One::one() || x == n_minus_one {
        return false;
    }
    while &count < r {
        x = x.modpow(two, n);
        if x == n_minus_one {
            return false;
        }
        count += 1u8;
    }

    true
}

/// Test whether an integer `a` is a witness for the compositeness of `n`.
///
/// NOTE: This function fails if `a < 2` or `n < 3`.
///
/// # Examples
///
/// ```
/// extern crate rand;
/// use rand::distributions::{Distribution, Uniform};
/// use miller_rabin::is_witness;
///
/// let n: u64 = 27;
/// let dist = Uniform::new(2, n);
/// let mut rng = rand::thread_rng();
/// // A random integer in [2..n]
/// let a: u64 = dist.sample(&mut rng);
/// assert!(is_witness(&a, &n).unwrap());
/// ```
pub fn is_witness<T: ToBigUint>(a: &T, n: &T) -> Option<bool> {
    let (ref a, ref n) = (biguint!(a), biguint!(n));

    if a < &biguint!(2) || n < &biguint!(3) {
        return None;
    }

    let (ref d, ref r) = decompose(n);
    Some(__miller_rabin(a, n, d, r))
}

/// Test whether an integer `n` is likely prime using the Miller-Rabin primality test.
///
/// # Examples
///
/// ```
/// use miller_rabin::is_prime;
///
/// // Mersenne Prime (2^31 - 1)
/// let n: u64 = 0x7FFF_FFFF;
/// // Try the miller-rabin test 100 times in parallel
/// // (or, if `n` is less than `u64::max()`, test only the numbers known to be sufficient).
/// // In general, the algorithm should fail at a rate of at most `4^{-k}`.
/// assert!(is_prime(&n, 100));
/// ```
pub fn is_prime<T: ToBigUint>(n: &T, k: usize) -> bool {
    let ref n = biguint!(n);
    let (ref d, ref r) = decompose(n);

    if n == &Zero::zero() {
        return false;
    } else if n < &biguint!(3) {
        return true;
    } else if n < &biguint!(0xFFFF_FFFF_FFFF_FFFFu64) {
        let samples: Vec<u8> = vec![2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37];
        return samples
            .par_iter()
            .find_any(|&&a| __miller_rabin(&biguint!(a), n, d, r))
            .is_none();
    }

    let mut rng = rand::thread_rng();
    let samples: Vec<BigUint> = repeat_with(|| rng.gen_biguint(n.bits()))
        .filter(|m| m < &n)
        .take(k)
        .collect();

    samples
        .par_iter()
        .find_any(|&a| __miller_rabin(a, n, d, r))
        .is_none()
}

#[cfg(test)]
mod tests {
    const K: usize = 16;

    use super::*;
    use std::io;

    #[test]
    fn test_prime() -> io::Result<()> {
        let prime: u64 = 0x7FFF_FFFF;
        assert!(is_prime(&prime, K));
        Ok(())
    }

    #[test]
    fn test_composite() -> io::Result<()> {
        let composite: u64 = 0x7FFF_FFFE;
        assert!(!is_prime(&composite, K));
        Ok(())
    }

    #[test]
    fn test_big_mersenne_prime() -> io::Result<()> {
        let prime: BigUint =
            BigUint::parse_bytes(b"170141183460469231731687303715884105727", 10).unwrap();
        assert!(is_prime(&prime, K));
        Ok(())
    }

    #[test]
    fn test_big_wagstaff_prime() -> io::Result<()> {
        let prime: BigUint =
            BigUint::parse_bytes(b"56713727820156410577229101238628035243", 10).unwrap();
        assert!(is_prime(&prime, K));
        Ok(())
    }

    #[test]
    fn test_big_composite() -> io::Result<()> {
        let prime: BigUint =
            BigUint::parse_bytes(b"170141183460469231731687303715884105725", 10).unwrap();
        assert!(!is_prime(&prime, K));
        Ok(())
    }
}