xref: /dflybsd-src/contrib/gmp/mpz/millerrabin.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* mpz_millerrabin(n,reps) -- An implementation of the probabilistic primality
286d7f5d3SJohn Marino    test found in Knuth's Seminumerical Algorithms book.  If the function
386d7f5d3SJohn Marino    mpz_millerrabin() returns 0 then n is not prime.  If it returns 1, then n is
486d7f5d3SJohn Marino    'probably' prime.  The probability of a false positive is (1/4)**reps, where
586d7f5d3SJohn Marino    reps is the number of internal passes of the probabilistic algorithm.  Knuth
686d7f5d3SJohn Marino    indicates that 25 passes are reasonable.
786d7f5d3SJohn Marino 
886d7f5d3SJohn Marino    THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
986d7f5d3SJohn Marino    CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
1086d7f5d3SJohn Marino    FUTURE GNU MP RELEASES.
1186d7f5d3SJohn Marino 
1286d7f5d3SJohn Marino Copyright 1991, 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2005 Free
1386d7f5d3SJohn Marino Software Foundation, Inc.  Contributed by John Amanatides.
1486d7f5d3SJohn Marino 
1586d7f5d3SJohn Marino This file is part of the GNU MP Library.
1686d7f5d3SJohn Marino 
1786d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
1886d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1986d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
2086d7f5d3SJohn Marino option) any later version.
2186d7f5d3SJohn Marino 
2286d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
2386d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
2486d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
2586d7f5d3SJohn Marino License for more details.
2686d7f5d3SJohn Marino 
2786d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
2886d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
2986d7f5d3SJohn Marino 
3086d7f5d3SJohn Marino #include "gmp.h"
3186d7f5d3SJohn Marino #include "gmp-impl.h"
3286d7f5d3SJohn Marino 
3386d7f5d3SJohn Marino static int millerrabin __GMP_PROTO ((mpz_srcptr, mpz_srcptr,
3486d7f5d3SJohn Marino 				     mpz_ptr, mpz_ptr,
3586d7f5d3SJohn Marino 				     mpz_srcptr, unsigned long int));
3686d7f5d3SJohn Marino 
3786d7f5d3SJohn Marino int
mpz_millerrabin(mpz_srcptr n,int reps)3886d7f5d3SJohn Marino mpz_millerrabin (mpz_srcptr n, int reps)
3986d7f5d3SJohn Marino {
4086d7f5d3SJohn Marino   int r;
4186d7f5d3SJohn Marino   mpz_t nm1, nm3, x, y, q;
4286d7f5d3SJohn Marino   unsigned long int k;
4386d7f5d3SJohn Marino   gmp_randstate_t rstate;
4486d7f5d3SJohn Marino   int is_prime;
4586d7f5d3SJohn Marino   TMP_DECL;
4686d7f5d3SJohn Marino   TMP_MARK;
4786d7f5d3SJohn Marino 
4886d7f5d3SJohn Marino   MPZ_TMP_INIT (nm1, SIZ (n) + 1);
4986d7f5d3SJohn Marino   mpz_sub_ui (nm1, n, 1L);
5086d7f5d3SJohn Marino 
5186d7f5d3SJohn Marino   MPZ_TMP_INIT (x, SIZ (n) + 1);
5286d7f5d3SJohn Marino   MPZ_TMP_INIT (y, 2 * SIZ (n)); /* mpz_powm_ui needs excessive memory!!! */
5386d7f5d3SJohn Marino 
5486d7f5d3SJohn Marino   /* Perform a Fermat test.  */
5586d7f5d3SJohn Marino   mpz_set_ui (x, 210L);
5686d7f5d3SJohn Marino   mpz_powm (y, x, nm1, n);
5786d7f5d3SJohn Marino   if (mpz_cmp_ui (y, 1L) != 0)
5886d7f5d3SJohn Marino     {
5986d7f5d3SJohn Marino       TMP_FREE;
6086d7f5d3SJohn Marino       return 0;
6186d7f5d3SJohn Marino     }
6286d7f5d3SJohn Marino 
6386d7f5d3SJohn Marino   MPZ_TMP_INIT (q, SIZ (n));
6486d7f5d3SJohn Marino 
6586d7f5d3SJohn Marino   /* Find q and k, where q is odd and n = 1 + 2**k * q.  */
6686d7f5d3SJohn Marino   k = mpz_scan1 (nm1, 0L);
6786d7f5d3SJohn Marino   mpz_tdiv_q_2exp (q, nm1, k);
6886d7f5d3SJohn Marino 
6986d7f5d3SJohn Marino   /* n-3 */
7086d7f5d3SJohn Marino   MPZ_TMP_INIT (nm3, SIZ (n) + 1);
7186d7f5d3SJohn Marino   mpz_sub_ui (nm3, n, 3L);
7286d7f5d3SJohn Marino   ASSERT (mpz_cmp_ui (nm3, 1L) >= 0);
7386d7f5d3SJohn Marino 
7486d7f5d3SJohn Marino   gmp_randinit_default (rstate);
7586d7f5d3SJohn Marino 
7686d7f5d3SJohn Marino   is_prime = 1;
7786d7f5d3SJohn Marino   for (r = 0; r < reps && is_prime; r++)
7886d7f5d3SJohn Marino     {
7986d7f5d3SJohn Marino       /* 2 to n-2 inclusive, don't want 1, 0 or -1 */
8086d7f5d3SJohn Marino       mpz_urandomm (x, rstate, nm3);
8186d7f5d3SJohn Marino       mpz_add_ui (x, x, 2L);
8286d7f5d3SJohn Marino 
8386d7f5d3SJohn Marino       is_prime = millerrabin (n, nm1, x, y, q, k);
8486d7f5d3SJohn Marino     }
8586d7f5d3SJohn Marino 
8686d7f5d3SJohn Marino   gmp_randclear (rstate);
8786d7f5d3SJohn Marino 
8886d7f5d3SJohn Marino   TMP_FREE;
8986d7f5d3SJohn Marino   return is_prime;
9086d7f5d3SJohn Marino }
9186d7f5d3SJohn Marino 
9286d7f5d3SJohn Marino static int
millerrabin(mpz_srcptr n,mpz_srcptr nm1,mpz_ptr x,mpz_ptr y,mpz_srcptr q,unsigned long int k)9386d7f5d3SJohn Marino millerrabin (mpz_srcptr n, mpz_srcptr nm1, mpz_ptr x, mpz_ptr y,
9486d7f5d3SJohn Marino              mpz_srcptr q, unsigned long int k)
9586d7f5d3SJohn Marino {
9686d7f5d3SJohn Marino   unsigned long int i;
9786d7f5d3SJohn Marino 
9886d7f5d3SJohn Marino   mpz_powm (y, x, q, n);
9986d7f5d3SJohn Marino 
10086d7f5d3SJohn Marino   if (mpz_cmp_ui (y, 1L) == 0 || mpz_cmp (y, nm1) == 0)
10186d7f5d3SJohn Marino     return 1;
10286d7f5d3SJohn Marino 
10386d7f5d3SJohn Marino   for (i = 1; i < k; i++)
10486d7f5d3SJohn Marino     {
10586d7f5d3SJohn Marino       mpz_powm_ui (y, y, 2L, n);
10686d7f5d3SJohn Marino       if (mpz_cmp (y, nm1) == 0)
10786d7f5d3SJohn Marino 	return 1;
10886d7f5d3SJohn Marino       if (mpz_cmp_ui (y, 1L) == 0)
10986d7f5d3SJohn Marino 	return 0;
11086d7f5d3SJohn Marino     }
11186d7f5d3SJohn Marino   return 0;
11286d7f5d3SJohn Marino }
113