@@ -44,14 +44,31 @@ pub const fn is_prime(n: u64) -> bool {
44
44
// we can use the fact that there are known perfect bases
45
45
// in order to make the test both fast and deterministic.
46
46
// This list of witnesses was taken from
47
- // <https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Testing_against_small_sets_of_bases>
48
- // and is sufficient for all numbers smaller than 2^64.
49
- const NUM_WITNESSES : usize = 12 ;
50
- const WITNESSES : [ u64 ; NUM_WITNESSES ] = [ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 ] ;
47
+ // <https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Testing_against_small_sets_of_bases>.
48
+ const WITNESSES : & [ ( u64 , & [ u64 ] ) ] = & [
49
+ ( 2_046 , & [ 2 ] ) ,
50
+ ( 1_373_652 , & [ 2 , 3 ] ) ,
51
+ ( 9_080_190 , & [ 31 , 73 ] ) ,
52
+ ( 25_326_000 , & [ 2 , 3 , 5 ] ) ,
53
+ ( 4_759_123_140 , & [ 2 , 7 , 61 ] ) ,
54
+ ( 1_112_004_669_632 , & [ 2 , 13 , 23 , 1662803 ] ) ,
55
+ ( 2_152_302_898_746 , & [ 2 , 3 , 5 , 7 , 11 ] ) ,
56
+ ( 3_474_749_660_382 , & [ 2 , 3 , 5 , 7 , 11 , 13 ] ) ,
57
+ ( 341_550_071_728_320 , & [ 2 , 3 , 5 , 7 , 11 , 13 , 17 ] ) ,
58
+ ( 3_825_123_056_546_413_050 , & [ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 ] ) ,
59
+ ( u64:: MAX , & [ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 ] ) ,
60
+ ] ;
51
61
52
62
let mut i = 0 ;
53
- while i < NUM_WITNESSES && WITNESSES [ i] < n {
54
- if !miller_test ( d, n, WITNESSES [ i] ) {
63
+ while WITNESSES [ i] . 0 < n {
64
+ i += 1 ;
65
+ }
66
+ let witnesses = WITNESSES [ i] . 1 ;
67
+ let num_witnesses = witnesses. len ( ) ;
68
+
69
+ let mut i = 0 ;
70
+ while i < num_witnesses && witnesses[ i] < n {
71
+ if !miller_test ( d, n, witnesses[ i] ) {
55
72
return false ;
56
73
}
57
74
i += 1 ;
0 commit comments