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

http://github.com/zpao/v8monkey · C · 147 lines · 58 code · 25 blank · 64 comment · 27 complexity · 8c177707a4c8ea6e99dfd9e98060df41 MD5 · raw file

  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. * ***** BEGIN LICENSE BLOCK *****
  10. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  11. *
  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/
  16. *
  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
  20. * License.
  21. *
  22. * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
  23. *
  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.
  28. *
  29. * Contributor(s):
  30. *
  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.
  42. *
  43. * ***** END LICENSE BLOCK ***** */
  44. /* $Id: makeprime.c,v 1.3 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <ctype.h>
  48. /* These two must be included for make_prime() to work */
  49. #include "mpi.h"
  50. #include "mpprime.h"
  51. /*
  52. make_prime(p, nr)
  53. Find the smallest prime integer greater than or equal to p, where
  54. primality is verified by 'nr' iterations of the Rabin-Miller
  55. probabilistic primality test. The caller is responsible for
  56. generating the initial value of p.
  57. Returns MP_OKAY if a prime has been generated, otherwise the error
  58. code indicates some other problem. The value of p is clobbered; the
  59. caller should keep a copy if the value is needed.
  60. */
  61. mp_err make_prime(mp_int *p, int nr);
  62. /* The main() is not required -- it's just a test driver */
  63. int main(int argc, char *argv[])
  64. {
  65. mp_int start;
  66. mp_err res;
  67. if(argc < 2) {
  68. fprintf(stderr, "Usage: %s <start-value>\n", argv[0]);
  69. return 1;
  70. }
  71. mp_init(&start);
  72. if(argv[1][0] == '0' && tolower(argv[1][1]) == 'x') {
  73. mp_read_radix(&start, argv[1] + 2, 16);
  74. } else {
  75. mp_read_radix(&start, argv[1], 10);
  76. }
  77. mp_abs(&start, &start);
  78. if((res = make_prime(&start, 5)) != MP_OKAY) {
  79. fprintf(stderr, "%s: error: %s\n", argv[0], mp_strerror(res));
  80. mp_clear(&start);
  81. return 1;
  82. } else {
  83. char *buf = malloc(mp_radix_size(&start, 10));
  84. mp_todecimal(&start, buf);
  85. printf("%s\n", buf);
  86. free(buf);
  87. mp_clear(&start);
  88. return 0;
  89. }
  90. } /* end main() */
  91. /*------------------------------------------------------------------------*/
  92. mp_err make_prime(mp_int *p, int nr)
  93. {
  94. mp_err res;
  95. if(mp_iseven(p)) {
  96. mp_add_d(p, 1, p);
  97. }
  98. do {
  99. mp_digit which = prime_tab_size;
  100. /* First test for divisibility by a few small primes */
  101. if((res = mpp_divis_primes(p, &which)) == MP_YES)
  102. continue;
  103. else if(res != MP_NO)
  104. goto CLEANUP;
  105. /* If that passes, try one iteration of Fermat's test */
  106. if((res = mpp_fermat(p, 2)) == MP_NO)
  107. continue;
  108. else if(res != MP_YES)
  109. goto CLEANUP;
  110. /* If that passes, run Rabin-Miller as often as requested */
  111. if((res = mpp_pprime(p, nr)) == MP_YES)
  112. break;
  113. else if(res != MP_NO)
  114. goto CLEANUP;
  115. } while((res = mp_add_d(p, 2, p)) == MP_OKAY);
  116. CLEANUP:
  117. return res;
  118. } /* end make_prime() */
  119. /*------------------------------------------------------------------------*/
  120. /* HERE THERE BE DRAGONS */