1 /* $NetBSD: spsp.c,v 1.1 2014/10/02 21:36:37 ast Exp $ */
4 * Copyright (c) 2014 Colin Percival
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
31 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
32 The Regents of the University of California. All rights reserved.");
37 static char sccsid
[] = "@(#)primes.c 8.5 (Berkeley) 5/10/95";
39 __RCSID("$NetBSD: spsp.c,v 1.1 2014/10/02 21:36:37 ast Exp $");
49 /* Return a * b % n, where 0 <= a, b < 2^63, 0 < n < 2^63. */
51 mulmod(uint64_t a
, uint64_t b
, uint64_t n
)
65 /* Return a^r % n, where 0 <= a < 2^63, 0 < n < 2^63. */
67 powmod(uint64_t a
, uint64_t r
, uint64_t n
)
81 /* Return non-zero if n is a strong pseudoprime to base p. */
83 spsp(uint64_t n
, uint64_t p
)
89 /* Compute n - 1 = 2^k * r. */
90 while ((r
& 1) == 0) {
95 /* Compute x = p^r mod n. If x = 1, n is a p-spsp. */
100 /* Compute x^(2^i) for 0 <= i < n. If any are -1, n is a p-spsp. */
112 /* Test for primality using strong pseudoprime tests. */
120 * C. Pomerance, J.L. Selfridge, and S.S. Wagstaff, Jr.,
121 * The pseudoprimes to 25 * 10^9, Math. Comp. 35(151):1003-1026, 1980.
124 /* No SPSPs to base 2 less than 2047. */
130 /* No SPSPs to bases 2,3 less than 1373653. */
136 /* No SPSPs to bases 2,3,5 less than 25326001. */
142 /* No SPSPs to bases 2,3,5,7 less than 3215031751. */
145 if (n
< 3215031751ULL)
150 * G. Jaeschke, On strong pseudoprimes to several bases,
151 * Math. Comp. 61(204):915-926, 1993.
154 /* No SPSPs to bases 2,3,5,7,11 less than 2152302898747. */
157 if (n
< 2152302898747ULL)
160 /* No SPSPs to bases 2,3,5,7,11,13 less than 3474749660383. */
163 if (n
< 3474749660383ULL)
166 /* No SPSPs to bases 2,3,5,7,11,13,17 less than 341550071728321. */
169 if (n
< 341550071728321ULL)
172 /* No SPSPs to bases 2,3,5,7,11,13,17,19 less than 341550071728321. */
175 if (n
< 341550071728321ULL)
180 * Y. Jiang and Y. Deng, Strong pseudoprimes to the first eight prime
181 * bases, Math. Comp. 83(290):2915-2924, 2014.
184 /* No SPSPs to bases 2..23 less than 3825123056546413051. */
187 if (n
< 3825123056546413051)
190 /* We can't handle values larger than this. */
191 assert(n
<= SPSPMAX
);