~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/security/nss/lib/freebl/mpi/utils/makeprime.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * makeprime.c
 
3
 *
 
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.
 
6
 *
 
7
 * Usage: makeprime <start>
 
8
 *
 
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/
 
13
 *
 
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.
 
18
 *
 
19
 * The Original Code is the MPI Arbitrary Precision Integer Arithmetic
 
20
 * library.
 
21
 *
 
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.
 
26
 *
 
27
 * Contributor(s):
 
28
 *
 
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.
 
39
 *
 
40
 * $Id: makeprime.c,v 1.1 2000/07/14 00:44:59 nelsonb%netscape.com Exp $
 
41
 */
 
42
 
 
43
#include <stdio.h>
 
44
#include <stdlib.h>
 
45
#include <ctype.h>
 
46
 
 
47
/* These two must be included for make_prime() to work */
 
48
 
 
49
#include "mpi.h"
 
50
#include "mpprime.h"
 
51
 
 
52
/*
 
53
  make_prime(p, nr)
 
54
 
 
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.
 
59
 
 
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.  
 
63
 */
 
64
mp_err   make_prime(mp_int *p, int nr);
 
65
 
 
66
/* The main() is not required -- it's just a test driver */
 
67
int main(int argc, char *argv[])
 
68
{
 
69
  mp_int    start;
 
70
  mp_err    res;
 
71
 
 
72
  if(argc < 2) {
 
73
    fprintf(stderr, "Usage: %s <start-value>\n", argv[0]);
 
74
    return 1;
 
75
  }
 
76
            
 
77
  mp_init(&start);
 
78
  if(argv[1][0] == '0' && tolower(argv[1][1]) == 'x') {
 
79
    mp_read_radix(&start, argv[1] + 2, 16);
 
80
  } else {
 
81
    mp_read_radix(&start, argv[1], 10);
 
82
  }
 
83
  mp_abs(&start, &start);
 
84
 
 
85
  if((res = make_prime(&start, 5)) != MP_OKAY) {
 
86
    fprintf(stderr, "%s: error: %s\n", argv[0], mp_strerror(res));
 
87
    mp_clear(&start);
 
88
 
 
89
    return 1;
 
90
 
 
91
  } else {
 
92
    char  *buf = malloc(mp_radix_size(&start, 10));
 
93
 
 
94
    mp_todecimal(&start, buf);
 
95
    printf("%s\n", buf);
 
96
    free(buf);
 
97
    
 
98
    mp_clear(&start);
 
99
 
 
100
    return 0;
 
101
  }
 
102
  
 
103
} /* end main() */
 
104
 
 
105
/*------------------------------------------------------------------------*/
 
106
 
 
107
mp_err   make_prime(mp_int *p, int nr)
 
108
{
 
109
  mp_err  res;
 
110
 
 
111
  if(mp_iseven(p)) {
 
112
    mp_add_d(p, 1, p);
 
113
  }
 
114
 
 
115
  do {
 
116
    mp_digit   which = prime_tab_size;
 
117
 
 
118
    /*  First test for divisibility by a few small primes */
 
119
    if((res = mpp_divis_primes(p, &which)) == MP_YES)
 
120
      continue;
 
121
    else if(res != MP_NO)
 
122
      goto CLEANUP;
 
123
 
 
124
    /* If that passes, try one iteration of Fermat's test */
 
125
    if((res = mpp_fermat(p, 2)) == MP_NO)
 
126
      continue;
 
127
    else if(res != MP_YES)
 
128
      goto CLEANUP;
 
129
 
 
130
    /* If that passes, run Rabin-Miller as often as requested */
 
131
    if((res = mpp_pprime(p, nr)) == MP_YES)
 
132
      break;
 
133
    else if(res != MP_NO)
 
134
      goto CLEANUP;
 
135
      
 
136
  } while((res = mp_add_d(p, 2, p)) == MP_OKAY);
 
137
 
 
138
 CLEANUP:
 
139
  return res;
 
140
 
 
141
} /* end make_prime() */
 
142
 
 
143
/*------------------------------------------------------------------------*/
 
144
/* HERE THERE BE DRAGONS                                                  */