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