| 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 |
"""Functions to create and test prime numbers. |
| 32 |
|
| 33 |
:undocumented: __package__ |
| 34 |
""" |
| 35 |
|
| 36 |
from Crypto import Random |
| 37 |
from Crypto.Math.Numbers import Integer |
| 38 |
|
| 39 |
from Crypto.Util.py3compat import iter_range |
| 40 |
|
| 41 |
COMPOSITE = 0 |
| 42 |
PROBABLY_PRIME = 1 |
| 43 |
|
| 44 |
|
| 45 |
def miller_rabin_test(candidate, iterations, randfunc=None): |
| 46 |
"""Perform a Miller-Rabin primality test on an integer. |
| 47 |
|
| 48 |
The test is specified in Section C.3.1 of `FIPS PUB 186-4`__. |
| 49 |
|
| 50 |
:Parameters: |
| 51 |
candidate : integer |
| 52 |
The number to test for primality. |
| 53 |
iterations : integer |
| 54 |
The maximum number of iterations to perform before |
| 55 |
declaring a candidate a probable prime. |
| 56 |
randfunc : callable |
| 57 |
An RNG function where bases are taken from. |
| 58 |
|
| 59 |
:Returns: |
| 60 |
``Primality.COMPOSITE`` or ``Primality.PROBABLY_PRIME``. |
| 61 |
|
| 62 |
.. __: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf |
| 63 |
""" |
| 64 |
|
| 65 |
if not isinstance(candidate, Integer): |
| 66 |
candidate = Integer(candidate) |
| 67 |
|
| 68 |
if candidate in (1, 2, 3, 5): |
| 69 |
return PROBABLY_PRIME |
| 70 |
|
| 71 |
if candidate.is_even(): |
| 72 |
return COMPOSITE |
| 73 |
|
| 74 |
one = Integer(1) |
| 75 |
minus_one = Integer(candidate - 1) |
| 76 |
|
| 77 |
if randfunc is None: |
| 78 |
randfunc = Random.new().read |
| 79 |
|
| 80 |
|
| 81 |
m = Integer(minus_one) |
| 82 |
a = 0 |
| 83 |
while m.is_even(): |
| 84 |
m >>= 1 |
| 85 |
a += 1 |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
for i in iter_range(iterations): |
| 91 |
|
| 92 |
|
| 93 |
base = 1 |
| 94 |
while base in (one, minus_one): |
| 95 |
base = Integer.random_range(min_inclusive=2, |
| 96 |
max_inclusive=candidate - 2, |
| 97 |
randfunc=randfunc) |
| 98 |
assert(2 <= base <= candidate - 2) |
| 99 |
|
| 100 |
|
| 101 |
z = pow(base, m, candidate) |
| 102 |
if z in (one, minus_one): |
| 103 |
continue |
| 104 |
|
| 105 |
|
| 106 |
for j in iter_range(1, a): |
| 107 |
z = pow(z, 2, candidate) |
| 108 |
if z == minus_one: |
| 109 |
break |
| 110 |
if z == one: |
| 111 |
return COMPOSITE |
| 112 |
else: |
| 113 |
return COMPOSITE |
| 114 |
|
| 115 |
|
| 116 |
return PROBABLY_PRIME |
| 117 |
|
| 118 |
|
| 119 |
def lucas_test(candidate): |
| 120 |
"""Perform a Lucas primality test on an integer. |
| 121 |
|
| 122 |
The test is specified in Section C.3.3 of `FIPS PUB 186-4`__. |
| 123 |
|
| 124 |
:Parameters: |
| 125 |
candidate : integer |
| 126 |
The number to test for primality. |
| 127 |
|
| 128 |
:Returns: |
| 129 |
``Primality.COMPOSITE`` or ``Primality.PROBABLY_PRIME``. |
| 130 |
|
| 131 |
.. __: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf |
| 132 |
""" |
| 133 |
|
| 134 |
if not isinstance(candidate, Integer): |
| 135 |
candidate = Integer(candidate) |
| 136 |
|
| 137 |
|
| 138 |
if candidate in (1, 2, 3, 5): |
| 139 |
return PROBABLY_PRIME |
| 140 |
if candidate.is_even() or candidate.is_perfect_square(): |
| 141 |
return COMPOSITE |
| 142 |
|
| 143 |
|
| 144 |
def alternate(): |
| 145 |
value = 5 |
| 146 |
while True: |
| 147 |
yield value |
| 148 |
if value > 0: |
| 149 |
value += 2 |
| 150 |
else: |
| 151 |
value -= 2 |
| 152 |
value = -value |
| 153 |
|
| 154 |
for D in alternate(): |
| 155 |
if candidate in (D, -D): |
| 156 |
continue |
| 157 |
js = Integer.jacobi_symbol(D, candidate) |
| 158 |
if js == 0: |
| 159 |
return COMPOSITE |
| 160 |
if js == -1: |
| 161 |
break |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
K = candidate + 1 |
| 167 |
|
| 168 |
r = K.size_in_bits() - 1 |
| 169 |
|
| 170 |
|
| 171 |
U_i = Integer(1) |
| 172 |
V_i = Integer(1) |
| 173 |
U_temp = Integer(0) |
| 174 |
V_temp = Integer(0) |
| 175 |
|
| 176 |
for i in iter_range(r - 1, -1, -1): |
| 177 |
|
| 178 |
|
| 179 |
U_temp.set(U_i) |
| 180 |
U_temp *= V_i |
| 181 |
U_temp %= candidate |
| 182 |
|
| 183 |
V_temp.set(U_i) |
| 184 |
V_temp *= U_i |
| 185 |
V_temp *= D |
| 186 |
V_temp.multiply_accumulate(V_i, V_i) |
| 187 |
if V_temp.is_odd(): |
| 188 |
V_temp += candidate |
| 189 |
V_temp >>= 1 |
| 190 |
V_temp %= candidate |
| 191 |
|
| 192 |
if K.get_bit(i): |
| 193 |
|
| 194 |
U_i.set(U_temp) |
| 195 |
U_i += V_temp |
| 196 |
if U_i.is_odd(): |
| 197 |
U_i += candidate |
| 198 |
U_i >>= 1 |
| 199 |
U_i %= candidate |
| 200 |
|
| 201 |
V_i.set(V_temp) |
| 202 |
V_i.multiply_accumulate(U_temp, D) |
| 203 |
if V_i.is_odd(): |
| 204 |
V_i += candidate |
| 205 |
V_i >>= 1 |
| 206 |
V_i %= candidate |
| 207 |
else: |
| 208 |
U_i.set(U_temp) |
| 209 |
V_i.set(V_temp) |
| 210 |
|
| 211 |
if U_i == 0: |
| 212 |
return PROBABLY_PRIME |
| 213 |
return COMPOSITE |
| 214 |
|
| 215 |
|
| 216 |
from Crypto.Util.number import sieve_base as _sieve_base_large |
| 217 |
|
| 218 |
|
| 219 |
_sieve_base = set(_sieve_base_large[:100]) |
| 220 |
|
| 221 |
|
| 222 |
def test_probable_prime(candidate, randfunc=None): |
| 223 |
"""Test if a number is prime. |
| 224 |
|
| 225 |
A number is qualified as prime if it passes a certain |
| 226 |
number of Miller-Rabin tests (dependent on the size |
| 227 |
of the number, but such that probability of a false |
| 228 |
positive is less than 10^-30) and a single Lucas test. |
| 229 |
|
| 230 |
For instance, a 1024-bit candidate will need to pass |
| 231 |
4 Miller-Rabin tests. |
| 232 |
|
| 233 |
:Parameters: |
| 234 |
candidate : integer |
| 235 |
The number to test for primality. |
| 236 |
randfunc : callable |
| 237 |
The routine to draw random bytes from to select Miller-Rabin bases. |
| 238 |
:Returns: |
| 239 |
``PROBABLE_PRIME`` if the number if prime with very high probability. |
| 240 |
``COMPOSITE`` if the number is a composite. |
| 241 |
For efficiency reasons, ``COMPOSITE`` is also returned for small primes. |
| 242 |
""" |
| 243 |
|
| 244 |
if randfunc is None: |
| 245 |
randfunc = Random.new().read |
| 246 |
|
| 247 |
if not isinstance(candidate, Integer): |
| 248 |
candidate = Integer(candidate) |
| 249 |
|
| 250 |
|
| 251 |
if int(candidate) in _sieve_base: |
| 252 |
return PROBABLY_PRIME |
| 253 |
try: |
| 254 |
map(candidate.fail_if_divisible_by, _sieve_base) |
| 255 |
except ValueError: |
| 256 |
return COMPOSITE |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
mr_ranges = ((220, 30), (280, 20), (390, 15), (512, 10), |
| 262 |
(620, 7), (740, 6), (890, 5), (1200, 4), |
| 263 |
(1700, 3), (3700, 2)) |
| 264 |
|
| 265 |
bit_size = candidate.size_in_bits() |
| 266 |
try: |
| 267 |
mr_iterations = list(filter(lambda x: bit_size < x[0], |
| 268 |
mr_ranges))[0][1] |
| 269 |
except IndexError: |
| 270 |
mr_iterations = 1 |
| 271 |
|
| 272 |
if miller_rabin_test(candidate, mr_iterations, |
| 273 |
randfunc=randfunc) == COMPOSITE: |
| 274 |
return COMPOSITE |
| 275 |
if lucas_test(candidate) == COMPOSITE: |
| 276 |
return COMPOSITE |
| 277 |
return PROBABLY_PRIME |
| 278 |
|
| 279 |
|
| 280 |
def generate_probable_prime(**kwargs): |
| 281 |
"""Generate a random probable prime. |
| 282 |
|
| 283 |
The prime will not have any specific properties |
| 284 |
(e.g. it will not be a *strong* prime). |
| 285 |
|
| 286 |
Random numbers are evaluated for primality until one |
| 287 |
passes all tests, consisting of a certain number of |
| 288 |
Miller-Rabin tests with random bases followed by |
| 289 |
a single Lucas test. |
| 290 |
|
| 291 |
The number of Miller-Rabin iterations is chosen such that |
| 292 |
the probability that the output number is a non-prime is |
| 293 |
less than 1E-30 (roughly 2^{-100}). |
| 294 |
|
| 295 |
This approach is compliant to `FIPS PUB 186-4`__. |
| 296 |
|
| 297 |
:Keywords: |
| 298 |
exact_bits : integer |
| 299 |
The desired size in bits of the probable prime. |
| 300 |
It must be at least 160. |
| 301 |
randfunc : callable |
| 302 |
An RNG function where candidate primes are taken from. |
| 303 |
prime_filter : callable |
| 304 |
A function that takes an Integer as parameter and returns |
| 305 |
True if the number can be passed to further primality tests, |
| 306 |
False if it should be immediately discarded. |
| 307 |
|
| 308 |
:Return: |
| 309 |
A probable prime in the range 2^exact_bits > p > 2^(exact_bits-1). |
| 310 |
|
| 311 |
.. __: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf |
| 312 |
""" |
| 313 |
|
| 314 |
exact_bits = kwargs.pop("exact_bits", None) |
| 315 |
randfunc = kwargs.pop("randfunc", None) |
| 316 |
prime_filter = kwargs.pop("prime_filter", lambda x: True) |
| 317 |
if kwargs: |
| 318 |
raise ValueError("Unknown parameters: " + kwargs.keys()) |
| 319 |
|
| 320 |
if exact_bits is None: |
| 321 |
raise ValueError("Missing exact_bits parameter") |
| 322 |
if exact_bits < 160: |
| 323 |
raise ValueError("Prime number is not big enough.") |
| 324 |
|
| 325 |
if randfunc is None: |
| 326 |
randfunc = Random.new().read |
| 327 |
|
| 328 |
result = COMPOSITE |
| 329 |
while result == COMPOSITE: |
| 330 |
candidate = Integer.random(exact_bits=exact_bits, |
| 331 |
randfunc=randfunc) | 1 |
| 332 |
if not prime_filter(candidate): |
| 333 |
continue |
| 334 |
result = test_probable_prime(candidate, randfunc) |
| 335 |
return candidate |
| 336 |
|
| 337 |
|
| 338 |
def generate_probable_safe_prime(**kwargs): |
| 339 |
"""Generate a random, probable safe prime. |
| 340 |
|
| 341 |
Note this operation is much slower than generating a simple prime. |
| 342 |
|
| 343 |
:Keywords: |
| 344 |
exact_bits : integer |
| 345 |
The desired size in bits of the probable safe prime. |
| 346 |
randfunc : callable |
| 347 |
An RNG function where candidate primes are taken from. |
| 348 |
|
| 349 |
:Return: |
| 350 |
A probable safe prime in the range |
| 351 |
2^exact_bits > p > 2^(exact_bits-1). |
| 352 |
""" |
| 353 |
|
| 354 |
exact_bits = kwargs.pop("exact_bits", None) |
| 355 |
randfunc = kwargs.pop("randfunc", None) |
| 356 |
if kwargs: |
| 357 |
raise ValueError("Unknown parameters: " + kwargs.keys()) |
| 358 |
|
| 359 |
if randfunc is None: |
| 360 |
randfunc = Random.new().read |
| 361 |
|
| 362 |
result = COMPOSITE |
| 363 |
while result == COMPOSITE: |
| 364 |
q = generate_probable_prime(exact_bits=exact_bits - 1, randfunc=randfunc) |
| 365 |
candidate = q * 2 + 1 |
| 366 |
if candidate.size_in_bits() != exact_bits: |
| 367 |
continue |
| 368 |
result = test_probable_prime(candidate, randfunc=randfunc) |
| 369 |
return candidate |