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