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