bn_prime.pl 1.4 KB

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