| 1 | /* |
| 2 | Name: iprime.c |
| 3 | Purpose: Pseudoprimality testing routines |
| 4 | Author: M. J. Fromberger |
| 5 | |
| 6 | Copyright (C) 2002-2008 Michael J. Fromberger, All Rights Reserved. |
| 7 | |
| 8 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | of this software and associated documentation files (the "Software"), to deal |
| 10 | in the Software without restriction, including without limitation the rights |
| 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | copies of the Software, and to permit persons to whom the Software is |
| 13 | furnished to do so, subject to the following conditions: |
| 14 | |
| 15 | The above copyright notice and this permission notice shall be included in |
| 16 | all copies or substantial portions of the Software. |
| 17 | |
| 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include "iprime.h" |
| 28 | #include <stdlib.h> |
| 29 | |
| 30 | static int s_ptab[] = { |
| 31 | 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, |
| 32 | 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, |
| 33 | 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, |
| 34 | 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, |
| 35 | 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, |
| 36 | 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, |
| 37 | 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, |
| 38 | 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, |
| 39 | 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, |
| 40 | 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, |
| 41 | 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, |
| 42 | 983, 991, 997, 0, /* sentinel */ |
| 43 | }; |
| 44 | |
| 45 | mp_result mp_int_is_prime(mp_int z) { |
| 46 | /* Reject values less than 2 immediately. */ |
| 47 | if (mp_int_compare_value(z, v: 2) < 0) { |
| 48 | return MP_FALSE; |
| 49 | } |
| 50 | /* First check for divisibility by small primes; this eliminates a large |
| 51 | number of composite candidates quickly |
| 52 | */ |
| 53 | for (int i = 0; s_ptab[i] != 0; i++) { |
| 54 | mp_small rem; |
| 55 | mp_result res; |
| 56 | if (mp_int_compare_value(z, v: s_ptab[i]) == 0) return MP_TRUE; |
| 57 | if ((res = mp_int_div_value(a: z, value: s_ptab[i], NULL, r: &rem)) != MP_OK) return res; |
| 58 | if (rem == 0) return MP_FALSE; |
| 59 | } |
| 60 | |
| 61 | /* Now try Fermat's test for several prime witnesses (since we now know from |
| 62 | the above that z is not a multiple of any of them) |
| 63 | */ |
| 64 | mp_result res; |
| 65 | mpz_t tmp; |
| 66 | |
| 67 | if ((res = mp_int_init(z: &tmp)) != MP_OK) return res; |
| 68 | |
| 69 | for (int i = 0; i < 10 && s_ptab[i] != 0; i++) { |
| 70 | if ((res = mp_int_exptmod_bvalue(value: s_ptab[i], b: z, m: z, c: &tmp)) != MP_OK) { |
| 71 | return res; |
| 72 | } |
| 73 | if (mp_int_compare_value(z: &tmp, v: s_ptab[i]) != 0) { |
| 74 | mp_int_clear(z: &tmp); |
| 75 | return MP_FALSE; |
| 76 | } |
| 77 | } |
| 78 | mp_int_clear(z: &tmp); |
| 79 | return MP_TRUE; |
| 80 | } |
| 81 | |
| 82 | /* Find the first apparent prime in ascending order from z */ |
| 83 | mp_result mp_int_find_prime(mp_int z) { |
| 84 | mp_result res; |
| 85 | |
| 86 | if (mp_int_is_even(z) && ((res = mp_int_add_value(a: z, value: 1, c: z)) != MP_OK)) |
| 87 | return res; |
| 88 | |
| 89 | while ((res = mp_int_is_prime(z)) == MP_FALSE) { |
| 90 | if ((res = mp_int_add_value(a: z, value: 2, c: z)) != MP_OK) break; |
| 91 | } |
| 92 | |
| 93 | return res; |
| 94 | } |
| 95 | |
| 96 | /* Here there be dragons */ |
| 97 | |