-
Notifications
You must be signed in to change notification settings - Fork 33
/
BPSW_primality_test.pl
executable file
·116 lines (89 loc) · 2.67 KB
/
BPSW_primality_test.pl
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
#!/usr/bin/perl
# The Baillie-PSW primality test, named after Robert Baillie, Carl Pomerance, John Selfridge, and Samuel Wagstaff.
# No counter-examples are known to this test.
# Algorithm: given an odd integer n, that is not a perfect power:
# 1. Perform a (strong) base-2 Fermat test.
# 2. Find the first D in the sequence 5, −7, 9, −11, 13, −15, ... for which the Jacobi symbol (D/n) is −1.
# Set P = 1 and Q = (1 − D) / 4.
# 3. Perform a strong Lucas probable prime test on n using parameters D, P, and Q.
# See also:
# https://en.wikipedia.org/wiki/Lucas_pseudoprime
# https://en.wikipedia.org/wiki/Baillie%E2%80%93PSW_primality_test
use 5.020;
use warnings;
use experimental qw(signatures);
use Math::AnyNum qw(
is_prime is_power is_congruent
kronecker powmod as_bin bit_scan1
);
sub findQ($n) {
# Find first D for which kronecker(D, n) == -1
for (my $k = 2 ; ; ++$k) {
my $D = (-1)**$k * (2 * $k + 1);
if (kronecker($D, $n) == -1) {
return ((1 - $D) / 4);
}
}
}
sub BPSW_primality_test($n) {
return 0 if $n <= 1;
return 1 if $n == 2;
return 0 if !($n & 1);
return 0 if is_power($n);
# Fermat base-2 test
powmod(2, $n - 1, $n) == 1 or return 0;
# Perform a strong Lucas probable test
my $Q = findQ($n);
my $d = $n + 1;
my $s = bit_scan1($d, 0);
my $t = $d >> ($s+1);
my ($U1 ) = (1 );
my ($V1, $V2) = (2, 1);
my ($Q1, $Q2) = (1, 1);
foreach my $bit (split(//, as_bin($t))) {
$Q1 = ($Q1 * $Q2) % $n;
if ($bit) {
$Q2 = ($Q1 * $Q) % $n;
$U1 = ($U1 * $V2) % $n;
$V1 = ($V2 * $V1 - $Q1) % $n;
$V2 = ($V2 * $V2 - ($Q2 + $Q2)) % $n;
}
else {
$Q2 = $Q1;
$U1 = ($U1 * $V1 - $Q1) % $n;
$V2 = ($V2 * $V1 - $Q1) % $n;
$V1 = ($V1 * $V1 - ($Q2 + $Q2)) % $n;
}
}
$Q1 = ($Q1 * $Q2) % $n;
$Q2 = ($Q1 * $Q) % $n;
$U1 = ($U1 * $V1 - $Q1) % $n;
$V1 = ($V2 * $V1 - $Q1) % $n;
$Q1 = ($Q1 * $Q2) % $n;
return 1 if is_congruent($U1, 0, $n);
return 1 if is_congruent($V1, 0, $n);
for (1 .. $s-1) {
$V1 = ($V1 * $V1 - 2 * $Q1) % $n;
$Q1 = ($Q1 * $Q1) % $n;
return 1 if is_congruent($V1, 0, $n);
}
return 0;
}
#
## Run some tests
#
my $from = 1;
my $to = 1e5;
my $count = 0;
foreach my $n ($from .. $to) {
if (BPSW_primality_test($n)) {
if (not is_prime($n)) {
say "Counter-example: $n";
}
++$count;
}
elsif (is_prime($n)) {
say "Missed a prime: $n";
}
}
say "There are $count primes between $from and $to.";