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
* ***** BEGIN LICENSE BLOCK *****
10
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
12
* The contents of this file are subject to the Mozilla Public License Version
13
* 1.1 (the "License"); you may not use this file except in compliance with
14
* the License. You may obtain a copy of the License at
15
* http://www.mozilla.org/MPL/
17
* Software distributed under the License is distributed on an "AS IS" basis,
18
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
19
* for the specific language governing rights and limitations under the
22
* The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
24
* The Initial Developer of the Original Code is
25
* Michael J. Fromberger.
26
* Portions created by the Initial Developer are Copyright (C) 1998
27
* the Initial Developer. All Rights Reserved.
31
* Alternatively, the contents of this file may be used under the terms of
32
* either the GNU General Public License Version 2 or later (the "GPL"), or
33
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
34
* in which case the provisions of the GPL or the LGPL are applicable instead
35
* of those above. If you wish to allow use of your version of this file only
36
* under the terms of either the GPL or the LGPL, and not to allow others to
37
* use your version of this file under the terms of the MPL, indicate your
38
* decision by deleting the provisions above and replace them with the notice
39
* and other provisions required by the GPL or the LGPL. If you do not delete
40
* the provisions above, a recipient may use your version of this file under
41
* the terms of any one of the MPL, the GPL or the LGPL.
43
* ***** END LICENSE BLOCK ***** */
44
/* $Id: makeprime.c,v 1.3 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
50
/* These two must be included for make_prime() to work */
58
Find the smallest prime integer greater than or equal to p, where
59
primality is verified by 'nr' iterations of the Rabin-Miller
60
probabilistic primality test. The caller is responsible for
61
generating the initial value of p.
63
Returns MP_OKAY if a prime has been generated, otherwise the error
64
code indicates some other problem. The value of p is clobbered; the
65
caller should keep a copy if the value is needed.
67
mp_err make_prime(mp_int *p, int nr);
69
/* The main() is not required -- it's just a test driver */
70
int main(int argc, char *argv[])
76
fprintf(stderr, "Usage: %s <start-value>\n", argv[0]);
81
if(argv[1][0] == '0' && tolower(argv[1][1]) == 'x') {
82
mp_read_radix(&start, argv[1] + 2, 16);
84
mp_read_radix(&start, argv[1], 10);
86
mp_abs(&start, &start);
88
if((res = make_prime(&start, 5)) != MP_OKAY) {
89
fprintf(stderr, "%s: error: %s\n", argv[0], mp_strerror(res));
95
char *buf = malloc(mp_radix_size(&start, 10));
97
mp_todecimal(&start, buf);
108
/*------------------------------------------------------------------------*/
110
mp_err make_prime(mp_int *p, int nr)
119
mp_digit which = prime_tab_size;
121
/* First test for divisibility by a few small primes */
122
if((res = mpp_divis_primes(p, &which)) == MP_YES)
124
else if(res != MP_NO)
127
/* If that passes, try one iteration of Fermat's test */
128
if((res = mpp_fermat(p, 2)) == MP_NO)
130
else if(res != MP_YES)
133
/* If that passes, run Rabin-Miller as often as requested */
134
if((res = mpp_pprime(p, nr)) == MP_YES)
136
else if(res != MP_NO)
139
} while((res = mp_add_d(p, 2, p)) == MP_OKAY);
144
} /* end make_prime() */
146
/*------------------------------------------------------------------------*/
147
/* HERE THERE BE DRAGONS */