-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBPSW_primality_test_anynum.pl
121 lines (91 loc) · 2.4 KB
/
BPSW_primality_test_anynum.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
117
118
119
120
121
#!/usr/bin/perl
use 5.020;
use strict;
use warnings;
use ntheory qw(forprimes foroddcomposites);
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 - 2 * $Q2) % $n;
}
else {
$Q2 = $Q1;
$U1 = ($U1 * $V1 - $Q1) % $n;
$V2 = ($V2 * $V1 - $Q1) % $n;
$V1 = ($V1 * $V1 - 2 * $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;
}
say "Sanity check...";
forprimes {
if (!BPSW_primality_test($_)) {
die "Missed prime: $_";
}
} 1e6;
foroddcomposites {
if (BPSW_primality_test($_)) {
die "Counter-example: $_";
}
} 1e6;
say "Done...";
say "Beginning the test...";
my %seen;
while (<>) {
next if /^\h*#/;
/\S/ or next;
my $n = (split(' ', $_))[-1];
$n || next;
$n = Math::GMPz->new("$n");
if (BPSW_primality_test($n)) {
say "Counter-example: $n";
}
#ntheory::is_prime($n) && die "error: $n\n";
#ntheory::is_prime($n) && die "error: $n\n";
#ntheory::is_aks_prime($n) && die "error: $n\n";
#ntheory::miller_rabin_random($n, 7) && die "error: $n\n";
}