bn_prime.pl 1.4 KB

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