bn_prime.pl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/local/bin/perl
  2. # bn_prime.pl
  3. $num=2048;
  4. $num=$ARGV[0] if ($#ARGV >= 0);
  5. push(@primes,2);
  6. $p=1;
  7. loop: while ($#primes < $num-1)
  8. {
  9. $p+=2;
  10. $s=int(sqrt($p));
  11. for ($i=0; defined($primes[$i]) && $primes[$i]<=$s; $i++)
  12. {
  13. next loop if (($p%$primes[$i]) == 0);
  14. }
  15. push(@primes,$p);
  16. }
  17. print <<\EOF;
  18. /* Auto generated by bn_prime.pl */
  19. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  20. * All rights reserved.
  21. *
  22. * This package is an SSL implementation written
  23. * by Eric Young (eay@cryptsoft.com).
  24. * The implementation was written so as to conform with Netscapes SSL.
  25. *
  26. * This library is free for commercial and non-commercial use as long as
  27. * the following conditions are aheared to. The following conditions
  28. * apply to all code found in this distribution, be it the RC4, RSA,
  29. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  30. * included with this distribution is covered by the same copyright terms
  31. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  32. *
  33. * Copyright remains Eric Young's, and as such any Copyright notices in
  34. * the code are not to be removed.
  35. * If this package is used in a product, Eric Young should be given attribution
  36. * as the author of the parts of the library used.
  37. * This can be in the form of a textual message at program startup or
  38. * in documentation (online or textual) provided with the package.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. * 1. Redistributions of source code must retain the copyright
  44. * notice, this list of conditions and the following disclaimer.
  45. * 2. Redistributions in binary form must reproduce the above copyright
  46. * notice, this list of conditions and the following disclaimer in the
  47. * documentation and/or other materials provided with the distribution.
  48. * 3. All advertising materials mentioning features or use of this software
  49. * must display the following acknowledgement:
  50. * "This product includes cryptographic software written by
  51. * Eric Young (eay@cryptsoft.com)"
  52. * The word 'cryptographic' can be left out if the rouines from the library
  53. * being used are not cryptographic related :-).
  54. * 4. If you include any Windows specific code (or a derivative thereof) from
  55. * the apps directory (application code) you must include an acknowledgement:
  56. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  57. *
  58. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  59. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  60. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  61. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  62. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  63. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  64. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  65. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  66. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  67. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  68. * SUCH DAMAGE.
  69. *
  70. * The licence and distribution terms for any publically available version or
  71. * derivative of this code cannot be changed. i.e. this code cannot simply be
  72. * copied and put under another distribution licence
  73. * [including the GNU Public Licence.]
  74. */
  75. EOF
  76. for ($i=0; $i <= $#primes; $i++)
  77. {
  78. if ($primes[$i] > 256)
  79. {
  80. $eight=$i;
  81. last;
  82. }
  83. }
  84. printf "#ifndef EIGHT_BIT\n";
  85. printf "# define NUMPRIMES %d\n",$num;
  86. printf "typedef unsigned short prime_t;\n";
  87. printf "#else\n";
  88. printf "# define NUMPRIMES %d\n",$eight;
  89. printf "typedef unsigned char prime_t;\n";
  90. printf "#endif\n";
  91. print "static const prime_t primes[NUMPRIMES]= {\n ";
  92. $init=0;
  93. for ($i=0; $i <= $#primes; $i++)
  94. {
  95. printf "\n#ifndef EIGHT_BIT\n " if ($primes[$i] > 256) && !($init++);
  96. printf "\n " if (($i%8) == 0) && ($i != 0);
  97. printf "%4d, ", $primes[$i];
  98. }
  99. print "\n#endif\n};\n";