1 /* mpz_probab_prime_p --
2 An implementation of the probabilistic primality test found in Knuth's
3 Seminumerical Algorithms book. If the function mpz_probab_prime_p()
4 returns 0 then n is not prime. If it returns 1, then n is 'probably'
5 prime. The probability of a false positive is (1/4)**reps, where
6 reps is the number of internal passes of the probabilistic algorithm.
7 Knuth indicates that 25 passes are reasonable.
9 Copyright (C) 1991, 1993, 1994 Free Software Foundation, Inc.
10 Contributed by John Amanatides.
12 This file is part of the GNU MP Library.
14 The GNU MP Library is free software; you can redistribute it and/or modify
15 it under the terms of the GNU Library General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or (at your
17 option) any later version.
19 The GNU MP Library is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
22 License for more details.
24 You should have received a copy of the GNU Library General Public License
25 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
26 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 MA 02111-1307, USA. */
32 possibly_prime (n, n_minus_1, x, y, q, k)
42 /* find random x s.t. 1 < x < n */
45 mpz_random (x, mpz_size (n));
48 while (mpz_cmp_ui (x, 1L) <= 0);
50 mpz_powm (y, x, q, n);
52 if (mpz_cmp_ui (y, 1L) == 0 || mpz_cmp (y, n_minus_1) == 0)
55 for (i = 1; i < k; i++)
57 mpz_powm_ui (y, y, 2L, n);
58 if (mpz_cmp (y, n_minus_1) == 0)
60 if (mpz_cmp_ui (y, 1L) == 0)
68 mpz_probab_prime_p (mpz_srcptr m, int reps)
70 mpz_probab_prime_p (m, reps)
75 mpz_t n, n_minus_1, x, y, q;
80 /* Take the absolute value of M, to handle positive and negative primes. */
83 if (mpz_cmp_ui (n, 3L) <= 0)
86 return mpz_cmp_ui (n, 1L) > 0;
89 if ((mpz_get_ui (n) & 1) == 0)
96 mpz_sub_ui (n_minus_1, n, 1L);
100 /* find q and k, s.t. n = 1 + 2**k * q */
101 mpz_init_set (q, n_minus_1);
102 k = mpz_scan1 (q, 0);
103 mpz_tdiv_q_2exp (q, q, k);
106 for (i = 0; i < reps && is_prime; i++)
107 is_prime &= possibly_prime (n, n_minus_1, x, y, q, k);
109 mpz_clear (n_minus_1);