forked from cslarsen/miller-rabin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiller-rabin.cpp
142 lines (124 loc) · 3.24 KB
/
miller-rabin.cpp
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
/*
* The Miller-Rabin primality test
*
* Written by Christian Stigen Larsen, 2012-01-10
* http://csl.sublevel3.org
*
* Distributed under the modified BSD license
*
* NOTE: I implemented this probabilistic algorithm purely as a recreational
* challenge. The code has big room for improvements, but it does work
* as advertised.
*/
#include <stdlib.h> // rand
#include <stdint.h> // uint64_t
#include "miller-rabin.h"
/*
* Which PRNG function to use; libc rand() by default
*/
static int (*rand_func)(void) = rand;
/*
* Maximum number that rand_func can return.
*/
static int rand_max = RAND_MAX;
/*
* Fast calculation of `a^x mod n´ by using right-to-left
* binary modular exponentiation.
*
* This algorithm is taken from Bruce Schneier's book
* APPLIED CRYPTOGRAPHY.
*
* See http://en.wikipedia.org/wiki/Modular_exponentiation
*/
static uint64_t pow_mod(uint64_t a, uint64_t x, uint64_t n)
{
/*
* Note that this code is sensitive to overflowing for testing
* of large prime numbers. The `a*r´ and `a*a´ operations can
* overflow. One easy way of solving this is to use 128-bit
* precision for calculating a*b % n, since the mod operator
* should always get us back to 64bits again.
*
* You can either use GCC's built-in __int128_t or use
*
* typedef unsigned int uint128_t __attribute__((mode(TI)));
*
* to create a 128-bit datatype.
*/
uint64_t r=1;
while ( x ) {
if ( (x & 1) == 1 )
//r = (__int128_t)a*r % n; // Slow
r = a*r % n;
x >>= 1;
//a = (__int128_t)a*a % n; // SLow
a = a*a % n;
}
return r;
}
/*
* Return an integer between a and b.
*
* Note that we use rand() here, meaning that all its pathological cases
* will apply here as well --- i.e., it's slow and not very random --- but
* should suffice.
*
*/
static uint64_t rand_between(uint64_t a, uint64_t b)
{
// Assume rand_func() is 32 bits
uint64_t r = (static_cast<uint64_t>(rand_func())<<32) | rand_func();
return a + (uint64_t)((double)(b-a+1)*r/(UINT64_MAX+1.0));
}
/*
* The Miller-Rabin probabilistic primality test.
*
* Returns true if ``n´´ is PROBABLY prime, false if it's composite.
* The parameter ``k´´ is the accuracy.
*
* The running time should be somewhere around O(k log_3 n).
*
*/
bool isprime(uint64_t n, int k)
{
// Must have ODD n greater than THREE
if ( n==2 || n==3 ) return true;
if ( n<=1 || !(n & 1) ) return false;
// Write n-1 as d*2^s by factoring powers of 2 from n-1
int s = 0;
for ( uint64_t m = n-1; !(m & 1); ++s, m >>= 1 )
; // loop
uint64_t d = (n-1) / (1<<s);
for ( int i = 0; i < k; ++i ) {
uint64_t a = rand_between(2, n-2);
uint64_t x = pow_mod(a,d,n);
if ( x == 1 || x == n-1 )
continue;
for ( int r = 1; r <= s-1; ++r ) {
x = pow_mod(x, 2, n);
if ( x == 1 ) return false;
if ( x == n - 1 ) goto LOOP;
}
return false;
LOOP:
continue;
}
// n is *probably* prime
return true;
}
/*
* Set which rand function to use.
*
* If passed a NULL parameter, it will revert back to the default libc
* rand().
*/
void setrand(int (*pf)(void), const int rmax)
{
if ( pf != NULL ) {
rand_func = pf;
rand_max = rmax;
} else {
rand_func = rand;
rand_max = RAND_MAX;
}
}