-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiller_rabin.c
126 lines (107 loc) · 3.2 KB
/
miller_rabin.c
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
/* miller_rabin.c -- GMP implementation of the Miller-Rabin test
*
* Copyright 2014 by Colin Benner <[email protected]>
*
* This file is part of frobenius-test.
*
* frobenius-test is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* frobenius-test is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with frobenius-test. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmp.h>
#include "helpers.h"
#include "miller_rabin.h"
/*
* Perform one iteration of the strong probable prime test with a given base a.
*/
Primality miller_rabin_base(const mpz_t n, const mpz_t a)
{
Primality result = probably_prime;
uint64_t s;
mpz_t d, x, nm1;
mpz_inits(d, x, nm1, NULL);
mpz_sub_ui(nm1, n, 1);
split(&s, d, n);
/* compute a^d mod n */
mpz_powm(x, a, d, n);
if (mpz_cmp_ui(x, 1) == 0 || mpz_cmp(x, nm1) == 0)
goto exit;
for (uint64_t r = 1; r <= s; r++) {
mpz_powm_ui(x, x, 2, n);
if (mpz_cmp_ui(x, 1) == 0 || mpz_cmp(x, nm1) == 0) {
result = composite;
break;
}
}
exit:
mpz_clears(d, x, nm1, NULL);
return result;
}
/*
* This function checks whether a given number n is a prime or not, using the
* Miller-Rabin primality test. This is a probabilistic test which randomly
* chooses an integer a as a base and checks whether n satisfies a certain
* property (which depends on b). If it does, n is a prime for at least three
* out of four of the possible values of a, if it does not, it is certainly not
* prime.
* The implementation is taken from the pseudo code found on
* http://en.wikipedia.org/wiki/Miller-Rabin_primality_test.
* The function returns `probably_prime` if it found no evidence, that n might
* be composite and `composite` if it did find a counter example.
*/
Primality miller_rabin(const mpz_t n, const unsigned k)
{
Primality result = probably_prime;
uint64_t s;
int foo;
mpz_t a, d, x, nm1;
/* We need an odd integer */
if (mpz_even_p(n))
return mpz_cmp_ui(n, 2) == 0 ? prime : composite;
/* greater than 3 */
foo = mpz_cmp_ui(n, 3);
if (foo == 0)
return prime;
else if (foo < 0)
return composite;
mpz_inits(a, d, x, nm1, NULL);
mpz_sub_ui(nm1, n, 1);
/* compute s and d s.t. n-1=2^s*d */
split(&s, d, n);
/* Repeat the test itself k times to increase the accuracy */
for (unsigned i = 0; i < k; i++) {
get_random(a, n);
/* compute a^d mod n */
mpz_powm(x, a, d, n);
if (mpz_cmp_ui(x, 1) == 0 || mpz_cmp(x, nm1) == 0)
continue;
for (uint64_t r = 1; r <= s; r++) {
mpz_powm_ui(x, x, 2, n);
if (mpz_cmp_ui(x, 1) == 0) {
result = composite;
goto exit;
}
if (mpz_cmp(x, nm1) == 0)
break;
}
if (mpz_cmp(x, nm1) != 0) {
result = composite;
goto exit;
}
}
exit:
mpz_clears(a, d, x, nm1, NULL);
return result;
}