Blame mpz/millerrabin.c

Packit 5c3484
/* mpz_millerrabin(n,reps) -- An implementation of the probabilistic primality
Packit 5c3484
   test found in Knuth's Seminumerical Algorithms book.  If the function
Packit 5c3484
   mpz_millerrabin() returns 0 then n is not prime.  If it returns 1, then n is
Packit 5c3484
   'probably' prime.  The probability of a false positive is (1/4)**reps, where
Packit 5c3484
   reps is the number of internal passes of the probabilistic algorithm.  Knuth
Packit 5c3484
   indicates that 25 passes are reasonable.
Packit 5c3484
Packit 5c3484
   THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
Packit 5c3484
   CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
Packit 5c3484
   FUTURE GNU MP RELEASES.
Packit 5c3484
Packit 5c3484
Copyright 1991, 1993, 1994, 1996-2002, 2005, 2014 Free Software
Packit 5c3484
Foundation, Inc.
Packit 5c3484
Packit 5c3484
Contributed by John Amanatides.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
Packit 5c3484
static int millerrabin (mpz_srcptr, mpz_srcptr,
Packit 5c3484
			mpz_ptr, mpz_ptr,
Packit 5c3484
			mpz_srcptr, unsigned long int);
Packit 5c3484
Packit 5c3484
int
Packit 5c3484
mpz_millerrabin (mpz_srcptr n, int reps)
Packit 5c3484
{
Packit 5c3484
  int r;
Packit 5c3484
  mpz_t nm1, nm3, x, y, q;
Packit 5c3484
  unsigned long int k;
Packit 5c3484
  gmp_randstate_t rstate;
Packit 5c3484
  int is_prime;
Packit 5c3484
  TMP_DECL;
Packit 5c3484
  TMP_MARK;
Packit 5c3484
Packit 5c3484
  MPZ_TMP_INIT (nm1, SIZ (n) + 1);
Packit 5c3484
  mpz_sub_ui (nm1, n, 1L);
Packit 5c3484
Packit 5c3484
  MPZ_TMP_INIT (x, SIZ (n) + 1);
Packit 5c3484
  MPZ_TMP_INIT (y, 2 * SIZ (n)); /* mpz_powm_ui needs excessive memory!!! */
Packit 5c3484
Packit 5c3484
  /* Perform a Fermat test.  */
Packit 5c3484
  mpz_set_ui (x, 210L);
Packit 5c3484
  mpz_powm (y, x, nm1, n);
Packit 5c3484
  if (mpz_cmp_ui (y, 1L) != 0)
Packit 5c3484
    {
Packit 5c3484
      TMP_FREE;
Packit 5c3484
      return 0;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  MPZ_TMP_INIT (q, SIZ (n));
Packit 5c3484
Packit 5c3484
  /* Find q and k, where q is odd and n = 1 + 2**k * q.  */
Packit 5c3484
  k = mpz_scan1 (nm1, 0L);
Packit 5c3484
  mpz_tdiv_q_2exp (q, nm1, k);
Packit 5c3484
Packit 5c3484
  /* n-3 */
Packit 5c3484
  MPZ_TMP_INIT (nm3, SIZ (n) + 1);
Packit 5c3484
  mpz_sub_ui (nm3, n, 3L);
Packit 5c3484
  ASSERT (mpz_cmp_ui (nm3, 1L) >= 0);
Packit 5c3484
Packit 5c3484
  gmp_randinit_default (rstate);
Packit 5c3484
Packit 5c3484
  is_prime = 1;
Packit 5c3484
  for (r = 0; r < reps && is_prime; r++)
Packit 5c3484
    {
Packit 5c3484
      /* 2 to n-2 inclusive, don't want 1, 0 or -1 */
Packit 5c3484
      mpz_urandomm (x, rstate, nm3);
Packit 5c3484
      mpz_add_ui (x, x, 2L);
Packit 5c3484
Packit 5c3484
      is_prime = millerrabin (n, nm1, x, y, q, k);
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  gmp_randclear (rstate);
Packit 5c3484
Packit 5c3484
  TMP_FREE;
Packit 5c3484
  return is_prime;
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
static int
Packit 5c3484
millerrabin (mpz_srcptr n, mpz_srcptr nm1, mpz_ptr x, mpz_ptr y,
Packit 5c3484
	     mpz_srcptr q, unsigned long int k)
Packit 5c3484
{
Packit 5c3484
  unsigned long int i;
Packit 5c3484
Packit 5c3484
  mpz_powm (y, x, q, n);
Packit 5c3484
Packit 5c3484
  if (mpz_cmp_ui (y, 1L) == 0 || mpz_cmp (y, nm1) == 0)
Packit 5c3484
    return 1;
Packit 5c3484
Packit 5c3484
  for (i = 1; i < k; i++)
Packit 5c3484
    {
Packit 5c3484
      mpz_powm_ui (y, y, 2L, n);
Packit 5c3484
      if (mpz_cmp (y, nm1) == 0)
Packit 5c3484
	return 1;
Packit 5c3484
      /* y == 1 means that the previous y was a non-trivial square root
Packit 5c3484
	 of 1 (mod n). y == 0 means that n is a power of the base.
Packit 5c3484
	 In either case, n is not prime. */
Packit 5c3484
      if (mpz_cmp_ui (y, 1L) <= 0)
Packit 5c3484
	return 0;
Packit 5c3484
    }
Packit 5c3484
  return 0;
Packit 5c3484
}