4
* A simple prime generator function (and test driver). Prints out the
5
* first prime it finds greater than or equal to the starting value.
7
* Usage: makeprime <start>
9
* The contents of this file are subject to the Mozilla Public
10
* License Version 1.1 (the "License"); you may not use this file
11
* except in compliance with the License. You may obtain a copy of
12
* the License at http://www.mozilla.org/MPL/
14
* Software distributed under the License is distributed on an "AS
15
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
16
* implied. See the License for the specific language governing
17
* rights and limitations under the License.
19
* The Original Code is the MPI Arbitrary Precision Integer Arithmetic
22
* The Initial Developer of the Original Code is Michael J. Fromberger.
23
* Portions created by Michael J. Fromberger are
24
* Copyright (C) 1998, 1999, 2000 Michael J. Fromberger.
25
* All Rights Reserved.
29
* Alternatively, the contents of this file may be used under the
30
* terms of the GNU General Public License Version 2 or later (the
31
* "GPL"), in which case the provisions of the GPL are applicable
32
* instead of those above. If you wish to allow use of your
33
* version of this file only under the terms of the GPL and not to
34
* allow others to use your version of this file under the MPL,
35
* indicate your decision by deleting the provisions above and
36
* replace them with the notice and other provisions required by
37
* the GPL. If you do not delete the provisions above, a recipient
38
* may use your version of this file under either the MPL or the GPL.
40
* $Id: makeprime.c,v 1.1 2000/07/14 00:44:59 nelsonb%netscape.com Exp $
47
/* These two must be included for make_prime() to work */
55
Find the smallest prime integer greater than or equal to p, where
56
primality is verified by 'nr' iterations of the Rabin-Miller
57
probabilistic primality test. The caller is responsible for
58
generating the initial value of p.
60
Returns MP_OKAY if a prime has been generated, otherwise the error
61
code indicates some other problem. The value of p is clobbered; the
62
caller should keep a copy if the value is needed.
64
mp_err make_prime(mp_int *p, int nr);
66
/* The main() is not required -- it's just a test driver */
67
int main(int argc, char *argv[])
73
fprintf(stderr, "Usage: %s <start-value>\n", argv[0]);
78
if(argv[1][0] == '0' && tolower(argv[1][1]) == 'x') {
79
mp_read_radix(&start, argv[1] + 2, 16);
81
mp_read_radix(&start, argv[1], 10);
83
mp_abs(&start, &start);
85
if((res = make_prime(&start, 5)) != MP_OKAY) {
86
fprintf(stderr, "%s: error: %s\n", argv[0], mp_strerror(res));
92
char *buf = malloc(mp_radix_size(&start, 10));
94
mp_todecimal(&start, buf);
105
/*------------------------------------------------------------------------*/
107
mp_err make_prime(mp_int *p, int nr)
116
mp_digit which = prime_tab_size;
118
/* First test for divisibility by a few small primes */
119
if((res = mpp_divis_primes(p, &which)) == MP_YES)
121
else if(res != MP_NO)
124
/* If that passes, try one iteration of Fermat's test */
125
if((res = mpp_fermat(p, 2)) == MP_NO)
127
else if(res != MP_YES)
130
/* If that passes, run Rabin-Miller as often as requested */
131
if((res = mpp_pprime(p, nr)) == MP_YES)
133
else if(res != MP_NO)
136
} while((res = mp_add_d(p, 2, p)) == MP_OKAY);
141
} /* end make_prime() */
143
/*------------------------------------------------------------------------*/
144
/* HERE THERE BE DRAGONS */