Home | History | Annotate | Line # | Download | only in primes
spsp.c revision 1.1
      1  1.1  ast /*	$NetBSD: spsp.c,v 1.1 2014/10/02 21:36:37 ast Exp $	*/
      2  1.1  ast 
      3  1.1  ast /*-
      4  1.1  ast  * Copyright (c) 2014 Colin Percival
      5  1.1  ast  * All rights reserved.
      6  1.1  ast  *
      7  1.1  ast  * Redistribution and use in source and binary forms, with or without
      8  1.1  ast  * modification, are permitted provided that the following conditions
      9  1.1  ast  * are met:
     10  1.1  ast  * 1. Redistributions of source code must retain the above copyright
     11  1.1  ast  *    notice, this list of conditions and the following disclaimer.
     12  1.1  ast  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  ast  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  ast  *    documentation and/or other materials provided with the distribution.
     15  1.1  ast  *
     16  1.1  ast  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  ast  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  ast  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  ast  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  ast  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  ast  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  ast  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  ast  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  ast  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  ast  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  ast  * SUCH DAMAGE.
     27  1.1  ast  */
     28  1.1  ast 
     29  1.1  ast #include <sys/cdefs.h>
     30  1.1  ast #ifndef lint
     31  1.1  ast __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
     32  1.1  ast  The Regents of the University of California.  All rights reserved.");
     33  1.1  ast #endif /* not lint */
     34  1.1  ast 
     35  1.1  ast #ifndef lint
     36  1.1  ast #if 0
     37  1.1  ast static char sccsid[] = "@(#)primes.c    8.5 (Berkeley) 5/10/95";
     38  1.1  ast #else
     39  1.1  ast __RCSID("$NetBSD: spsp.c,v 1.1 2014/10/02 21:36:37 ast Exp $");
     40  1.1  ast #endif
     41  1.1  ast #endif /* not lint */
     42  1.1  ast 
     43  1.1  ast #include <assert.h>
     44  1.1  ast #include <stddef.h>
     45  1.1  ast #include <stdint.h>
     46  1.1  ast 
     47  1.1  ast #include "primes.h"
     48  1.1  ast 
     49  1.1  ast /* Return a * b % n, where 0 <= a, b < 2^63, 0 < n < 2^63. */
     50  1.1  ast static uint64_t
     51  1.1  ast mulmod(uint64_t a, uint64_t b, uint64_t n)
     52  1.1  ast {
     53  1.1  ast 	uint64_t x = 0;
     54  1.1  ast 
     55  1.1  ast 	while (b != 0) {
     56  1.1  ast 		if (b & 1)
     57  1.1  ast 			x = (x + a) % n;
     58  1.1  ast 		a = (a + a) % n;
     59  1.1  ast 		b >>= 1;
     60  1.1  ast 	}
     61  1.1  ast 
     62  1.1  ast 	return (x);
     63  1.1  ast }
     64  1.1  ast 
     65  1.1  ast /* Return a^r % n, where 0 <= a < 2^63, 0 < n < 2^63. */
     66  1.1  ast static uint64_t
     67  1.1  ast powmod(uint64_t a, uint64_t r, uint64_t n)
     68  1.1  ast {
     69  1.1  ast 	uint64_t x = 1;
     70  1.1  ast 
     71  1.1  ast 	while (r != 0) {
     72  1.1  ast 		if (r & 1)
     73  1.1  ast 			x = mulmod(a, x, n);
     74  1.1  ast 		a = mulmod(a, a, n);
     75  1.1  ast 		r >>= 1;
     76  1.1  ast 	}
     77  1.1  ast 
     78  1.1  ast 	return (x);
     79  1.1  ast }
     80  1.1  ast 
     81  1.1  ast /* Return non-zero if n is a strong pseudoprime to base p. */
     82  1.1  ast static int
     83  1.1  ast spsp(uint64_t n, uint64_t p)
     84  1.1  ast {
     85  1.1  ast 	uint64_t x;
     86  1.1  ast 	uint64_t r = n - 1;
     87  1.1  ast 	int k = 0;
     88  1.1  ast 
     89  1.1  ast 	/* Compute n - 1 = 2^k * r. */
     90  1.1  ast 	while ((r & 1) == 0) {
     91  1.1  ast 		k++;
     92  1.1  ast 		r >>= 1;
     93  1.1  ast 	}
     94  1.1  ast 
     95  1.1  ast 	/* Compute x = p^r mod n.  If x = 1, n is a p-spsp. */
     96  1.1  ast 	x = powmod(p, r, n);
     97  1.1  ast 	if (x == 1)
     98  1.1  ast 		return (1);
     99  1.1  ast 
    100  1.1  ast 	/* Compute x^(2^i) for 0 <= i < n.  If any are -1, n is a p-spsp. */
    101  1.1  ast 	while (k > 0) {
    102  1.1  ast 		if (x == n - 1)
    103  1.1  ast 			return (1);
    104  1.1  ast 		x = powmod(x, 2, n);
    105  1.1  ast 		k--;
    106  1.1  ast 	}
    107  1.1  ast 
    108  1.1  ast 	/* Not a p-spsp. */
    109  1.1  ast 	return (0);
    110  1.1  ast }
    111  1.1  ast 
    112  1.1  ast /* Test for primality using strong pseudoprime tests. */
    113  1.1  ast int
    114  1.1  ast isprime(uint64_t _n)
    115  1.1  ast {
    116  1.1  ast 	uint64_t n = _n;
    117  1.1  ast 
    118  1.1  ast 	/*
    119  1.1  ast 	 * Values from:
    120  1.1  ast 	 * C. Pomerance, J.L. Selfridge, and S.S. Wagstaff, Jr.,
    121  1.1  ast 	 * The pseudoprimes to 25 * 10^9, Math. Comp. 35(151):1003-1026, 1980.
    122  1.1  ast 	 */
    123  1.1  ast 
    124  1.1  ast 	/* No SPSPs to base 2 less than 2047. */
    125  1.1  ast 	if (!spsp(n, 2))
    126  1.1  ast 		return (0);
    127  1.1  ast 	if (n < 2047ULL)
    128  1.1  ast 		return (1);
    129  1.1  ast 
    130  1.1  ast 	/* No SPSPs to bases 2,3 less than 1373653. */
    131  1.1  ast 	if (!spsp(n, 3))
    132  1.1  ast 		return (0);
    133  1.1  ast 	if (n < 1373653ULL)
    134  1.1  ast 		return (1);
    135  1.1  ast 
    136  1.1  ast 	/* No SPSPs to bases 2,3,5 less than 25326001. */
    137  1.1  ast 	if (!spsp(n, 5))
    138  1.1  ast 		return (0);
    139  1.1  ast 	if (n < 25326001ULL)
    140  1.1  ast 		return (1);
    141  1.1  ast 
    142  1.1  ast 	/* No SPSPs to bases 2,3,5,7 less than 3215031751. */
    143  1.1  ast 	if (!spsp(n, 7))
    144  1.1  ast 		return (0);
    145  1.1  ast 	if (n < 3215031751ULL)
    146  1.1  ast 		return (1);
    147  1.1  ast 
    148  1.1  ast 	/*
    149  1.1  ast 	 * Values from:
    150  1.1  ast 	 * G. Jaeschke, On strong pseudoprimes to several bases,
    151  1.1  ast 	 * Math. Comp. 61(204):915-926, 1993.
    152  1.1  ast 	 */
    153  1.1  ast 
    154  1.1  ast 	/* No SPSPs to bases 2,3,5,7,11 less than 2152302898747. */
    155  1.1  ast 	if (!spsp(n, 11))
    156  1.1  ast 		return (0);
    157  1.1  ast 	if (n < 2152302898747ULL)
    158  1.1  ast 		return (1);
    159  1.1  ast 
    160  1.1  ast 	/* No SPSPs to bases 2,3,5,7,11,13 less than 3474749660383. */
    161  1.1  ast 	if (!spsp(n, 13))
    162  1.1  ast 		return (0);
    163  1.1  ast 	if (n < 3474749660383ULL)
    164  1.1  ast 		return (1);
    165  1.1  ast 
    166  1.1  ast 	/* No SPSPs to bases 2,3,5,7,11,13,17 less than 341550071728321. */
    167  1.1  ast 	if (!spsp(n, 17))
    168  1.1  ast 		return (0);
    169  1.1  ast 	if (n < 341550071728321ULL)
    170  1.1  ast 		return (1);
    171  1.1  ast 
    172  1.1  ast 	/* No SPSPs to bases 2,3,5,7,11,13,17,19 less than 341550071728321. */
    173  1.1  ast 	if (!spsp(n, 19))
    174  1.1  ast 		return (0);
    175  1.1  ast 	if (n < 341550071728321ULL)
    176  1.1  ast 		return (1);
    177  1.1  ast 
    178  1.1  ast 	/*
    179  1.1  ast 	 * Value from:
    180  1.1  ast 	 * Y. Jiang and Y. Deng, Strong pseudoprimes to the first eight prime
    181  1.1  ast 	 * bases, Math. Comp. 83(290):2915-2924, 2014.
    182  1.1  ast 	 */
    183  1.1  ast 
    184  1.1  ast 	/* No SPSPs to bases 2..23 less than 3825123056546413051. */
    185  1.1  ast 	if (!spsp(n, 23))
    186  1.1  ast 		return (0);
    187  1.1  ast 	if (n < 3825123056546413051)
    188  1.1  ast 		return (1);
    189  1.1  ast 
    190  1.1  ast 	/* We can't handle values larger than this. */
    191  1.1  ast 	assert(n <= SPSPMAX);
    192  1.1  ast 
    193  1.1  ast 	/* UNREACHABLE */
    194  1.1  ast 	return (0);
    195  1.1  ast }
    196