Back to index

lightning-sunbird  0.9+nobinonly
makeprime.c
Go to the documentation of this file.
00001 /*
00002  * makeprime.c
00003  *
00004  * A simple prime generator function (and test driver).  Prints out the
00005  * first prime it finds greater than or equal to the starting value.
00006  *
00007  * Usage: makeprime <start>
00008  *
00009  * ***** BEGIN LICENSE BLOCK *****
00010  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00011  *
00012  * The contents of this file are subject to the Mozilla Public License Version
00013  * 1.1 (the "License"); you may not use this file except in compliance with
00014  * the License. You may obtain a copy of the License at
00015  * http://www.mozilla.org/MPL/
00016  *
00017  * Software distributed under the License is distributed on an "AS IS" basis,
00018  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00019  * for the specific language governing rights and limitations under the
00020  * License.
00021  *
00022  * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
00023  *
00024  * The Initial Developer of the Original Code is
00025  * Michael J. Fromberger.
00026  * Portions created by the Initial Developer are Copyright (C) 1998
00027  * the Initial Developer. All Rights Reserved.
00028  *
00029  * Contributor(s):
00030  *
00031  * Alternatively, the contents of this file may be used under the terms of
00032  * either the GNU General Public License Version 2 or later (the "GPL"), or
00033  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00034  * in which case the provisions of the GPL or the LGPL are applicable instead
00035  * of those above. If you wish to allow use of your version of this file only
00036  * under the terms of either the GPL or the LGPL, and not to allow others to
00037  * use your version of this file under the terms of the MPL, indicate your
00038  * decision by deleting the provisions above and replace them with the notice
00039  * and other provisions required by the GPL or the LGPL. If you do not delete
00040  * the provisions above, a recipient may use your version of this file under
00041  * the terms of any one of the MPL, the GPL or the LGPL.
00042  *
00043  * ***** END LICENSE BLOCK ***** */
00044 /* $Id: makeprime.c,v 1.3 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
00045 
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <ctype.h>
00049 
00050 /* These two must be included for make_prime() to work */
00051 
00052 #include "mpi.h"
00053 #include "mpprime.h"
00054 
00055 /*
00056   make_prime(p, nr)
00057 
00058   Find the smallest prime integer greater than or equal to p, where
00059   primality is verified by 'nr' iterations of the Rabin-Miller
00060   probabilistic primality test.  The caller is responsible for
00061   generating the initial value of p.
00062 
00063   Returns MP_OKAY if a prime has been generated, otherwise the error
00064   code indicates some other problem.  The value of p is clobbered; the
00065   caller should keep a copy if the value is needed.  
00066  */
00067 mp_err   make_prime(mp_int *p, int nr);
00068 
00069 /* The main() is not required -- it's just a test driver */
00070 int main(int argc, char *argv[])
00071 {
00072   mp_int    start;
00073   mp_err    res;
00074 
00075   if(argc < 2) {
00076     fprintf(stderr, "Usage: %s <start-value>\n", argv[0]);
00077     return 1;
00078   }
00079            
00080   mp_init(&start);
00081   if(argv[1][0] == '0' && tolower(argv[1][1]) == 'x') {
00082     mp_read_radix(&start, argv[1] + 2, 16);
00083   } else {
00084     mp_read_radix(&start, argv[1], 10);
00085   }
00086   mp_abs(&start, &start);
00087 
00088   if((res = make_prime(&start, 5)) != MP_OKAY) {
00089     fprintf(stderr, "%s: error: %s\n", argv[0], mp_strerror(res));
00090     mp_clear(&start);
00091 
00092     return 1;
00093 
00094   } else {
00095     char  *buf = malloc(mp_radix_size(&start, 10));
00096 
00097     mp_todecimal(&start, buf);
00098     printf("%s\n", buf);
00099     free(buf);
00100     
00101     mp_clear(&start);
00102 
00103     return 0;
00104   }
00105   
00106 } /* end main() */
00107 
00108 /*------------------------------------------------------------------------*/
00109 
00110 mp_err   make_prime(mp_int *p, int nr)
00111 {
00112   mp_err  res;
00113 
00114   if(mp_iseven(p)) {
00115     mp_add_d(p, 1, p);
00116   }
00117 
00118   do {
00119     mp_digit   which = prime_tab_size;
00120 
00121     /*  First test for divisibility by a few small primes */
00122     if((res = mpp_divis_primes(p, &which)) == MP_YES)
00123       continue;
00124     else if(res != MP_NO)
00125       goto CLEANUP;
00126 
00127     /* If that passes, try one iteration of Fermat's test */
00128     if((res = mpp_fermat(p, 2)) == MP_NO)
00129       continue;
00130     else if(res != MP_YES)
00131       goto CLEANUP;
00132 
00133     /* If that passes, run Rabin-Miller as often as requested */
00134     if((res = mpp_pprime(p, nr)) == MP_YES)
00135       break;
00136     else if(res != MP_NO)
00137       goto CLEANUP;
00138       
00139   } while((res = mp_add_d(p, 2, p)) == MP_OKAY);
00140 
00141  CLEANUP:
00142   return res;
00143 
00144 } /* end make_prime() */
00145 
00146 /*------------------------------------------------------------------------*/
00147 /* HERE THERE BE DRAGONS                                                  */