security/nss/lib/freebl/mpi/utils/makeprime.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * makeprime.c
michael@0 3 *
michael@0 4 * A simple prime generator function (and test driver). Prints out the
michael@0 5 * first prime it finds greater than or equal to the starting value.
michael@0 6 *
michael@0 7 * Usage: makeprime <start>
michael@0 8 *
michael@0 9 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 10 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 11 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 12
michael@0 13 #include <stdio.h>
michael@0 14 #include <stdlib.h>
michael@0 15 #include <ctype.h>
michael@0 16
michael@0 17 /* These two must be included for make_prime() to work */
michael@0 18
michael@0 19 #include "mpi.h"
michael@0 20 #include "mpprime.h"
michael@0 21
michael@0 22 /*
michael@0 23 make_prime(p, nr)
michael@0 24
michael@0 25 Find the smallest prime integer greater than or equal to p, where
michael@0 26 primality is verified by 'nr' iterations of the Rabin-Miller
michael@0 27 probabilistic primality test. The caller is responsible for
michael@0 28 generating the initial value of p.
michael@0 29
michael@0 30 Returns MP_OKAY if a prime has been generated, otherwise the error
michael@0 31 code indicates some other problem. The value of p is clobbered; the
michael@0 32 caller should keep a copy if the value is needed.
michael@0 33 */
michael@0 34 mp_err make_prime(mp_int *p, int nr);
michael@0 35
michael@0 36 /* The main() is not required -- it's just a test driver */
michael@0 37 int main(int argc, char *argv[])
michael@0 38 {
michael@0 39 mp_int start;
michael@0 40 mp_err res;
michael@0 41
michael@0 42 if(argc < 2) {
michael@0 43 fprintf(stderr, "Usage: %s <start-value>\n", argv[0]);
michael@0 44 return 1;
michael@0 45 }
michael@0 46
michael@0 47 mp_init(&start);
michael@0 48 if(argv[1][0] == '0' && tolower(argv[1][1]) == 'x') {
michael@0 49 mp_read_radix(&start, argv[1] + 2, 16);
michael@0 50 } else {
michael@0 51 mp_read_radix(&start, argv[1], 10);
michael@0 52 }
michael@0 53 mp_abs(&start, &start);
michael@0 54
michael@0 55 if((res = make_prime(&start, 5)) != MP_OKAY) {
michael@0 56 fprintf(stderr, "%s: error: %s\n", argv[0], mp_strerror(res));
michael@0 57 mp_clear(&start);
michael@0 58
michael@0 59 return 1;
michael@0 60
michael@0 61 } else {
michael@0 62 char *buf = malloc(mp_radix_size(&start, 10));
michael@0 63
michael@0 64 mp_todecimal(&start, buf);
michael@0 65 printf("%s\n", buf);
michael@0 66 free(buf);
michael@0 67
michael@0 68 mp_clear(&start);
michael@0 69
michael@0 70 return 0;
michael@0 71 }
michael@0 72
michael@0 73 } /* end main() */
michael@0 74
michael@0 75 /*------------------------------------------------------------------------*/
michael@0 76
michael@0 77 mp_err make_prime(mp_int *p, int nr)
michael@0 78 {
michael@0 79 mp_err res;
michael@0 80
michael@0 81 if(mp_iseven(p)) {
michael@0 82 mp_add_d(p, 1, p);
michael@0 83 }
michael@0 84
michael@0 85 do {
michael@0 86 mp_digit which = prime_tab_size;
michael@0 87
michael@0 88 /* First test for divisibility by a few small primes */
michael@0 89 if((res = mpp_divis_primes(p, &which)) == MP_YES)
michael@0 90 continue;
michael@0 91 else if(res != MP_NO)
michael@0 92 goto CLEANUP;
michael@0 93
michael@0 94 /* If that passes, try one iteration of Fermat's test */
michael@0 95 if((res = mpp_fermat(p, 2)) == MP_NO)
michael@0 96 continue;
michael@0 97 else if(res != MP_YES)
michael@0 98 goto CLEANUP;
michael@0 99
michael@0 100 /* If that passes, run Rabin-Miller as often as requested */
michael@0 101 if((res = mpp_pprime(p, nr)) == MP_YES)
michael@0 102 break;
michael@0 103 else if(res != MP_NO)
michael@0 104 goto CLEANUP;
michael@0 105
michael@0 106 } while((res = mp_add_d(p, 2, p)) == MP_OKAY);
michael@0 107
michael@0 108 CLEANUP:
michael@0 109 return res;
michael@0 110
michael@0 111 } /* end make_prime() */
michael@0 112
michael@0 113 /*------------------------------------------------------------------------*/
michael@0 114 /* HERE THERE BE DRAGONS */

mercurial