2
0

bn_prime.pl 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #! /usr/bin/env perl
  2. # Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. print <<"EOF";
  9. /*
  10. * WARNING: do not edit!
  11. * Generated by crypto/bn/bn_prime.pl
  12. *
  13. * Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
  14. *
  15. * Licensed under the OpenSSL license (the "License"). You may not use
  16. * this file except in compliance with the License. You can obtain a copy
  17. * in the file LICENSE in the source distribution or at
  18. * https://www.openssl.org/source/license.html
  19. */
  20. EOF
  21. my $num = shift || 2048;
  22. my @primes = ( 2 );
  23. my $p = 1;
  24. loop: while ($#primes < $num-1) {
  25. $p += 2;
  26. my $s = int(sqrt($p));
  27. for (my $i = 0; defined($primes[$i]) && $primes[$i] <= $s; $i++) {
  28. next loop if ($p % $primes[$i]) == 0;
  29. }
  30. push(@primes, $p);
  31. }
  32. print "typedef unsigned short prime_t;\n";
  33. printf "# define NUMPRIMES %d\n\n", $num;
  34. printf "static const prime_t primes[%d] = {", $num;
  35. for (my $i = 0; $i <= $#primes; $i++) {
  36. printf "\n " if ($i % 8) == 0;
  37. printf " %5d,", $primes[$i];
  38. }
  39. print "\n};\n";