Skip to content

Commit f35dfdd

Browse files
committed
BN random range testing.
Add a script to generate critical value tables for the bn_rand_range stochastic test. Reviewed-by: Matt Caswell <[email protected]> (Merged from openssl/openssl#38)
1 parent 191d279 commit f35dfdd

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

statistics/README

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
This collection of scripts contains statistical scripts.
3+
4+
5+
bn_rand_range.py Generate the critical values tables for test/bn_rand_range.c
6+
There is a dependency on the scipi package.

statistics/bn_rand_range.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License 2.0 (the "License"). You may not use
6+
# this file except in compliance with the License. You can obtain a copy
7+
# in the file LICENSE in the source distribution or at
8+
# https://www.openssl.org/source/license.html
9+
10+
# Run this using:
11+
# bn_rand_range.py > $(OPENSSL)/test/bn_rand_range.h
12+
#
13+
# There is a dependency of scipi, include the package python3-scipy to resolve
14+
# this.
15+
16+
from datetime import datetime
17+
from scipy.stats import chi2, binom
18+
19+
alpha_chi2 = 0.95
20+
alpha_binomial = 0.9999
21+
test_cases = list(range(2, 20)) \
22+
+ [x * 10 + 10 for x in range(1, 10)] \
23+
+ [x * 1000 for x in range(1, 11)]
24+
25+
# The rest of this file produces the C include file
26+
27+
def do_case(n):
28+
"Output a single formatted row in the table"
29+
ns = "%d," % n
30+
iterations = "%d," % (n * (100 if n < 1000 else 10))
31+
critical = "%f" % (chi2.ppf(alpha_chi2, n - 1))
32+
print(" { %6s %8s %12s }," % ( ns, iterations, critical ))
33+
34+
# Work out the copyright year range
35+
year = datetime.today().year
36+
if year != 2019:
37+
year = "2019-%d" % year
38+
39+
print("""/*
40+
* WARNING: do not edit!
41+
* Generated by statistics/bn_rand_range.py in the OpenSSL tool repository.
42+
*
43+
* Copyright %s The OpenSSL Project Authors. All Rights Reserved.
44+
*
45+
* Licensed under the Apache License 2.0 (the "License"). You may not use
46+
* this file except in compliance with the License. You can obtain a copy
47+
* in the file LICENSE in the source distribution or at
48+
* https://www.openssl.org/source/license.html
49+
*/
50+
51+
static const struct {
52+
unsigned int range;
53+
unsigned int iterations;
54+
double critical;
55+
} rand_range_cases[] = {""" % year)
56+
num_cases = len(list(map(do_case, test_cases)))
57+
print("};\n")
58+
59+
# Finally, calculate and output the lower tail binomial threshold.
60+
b_thresh = binom.isf(alpha_binomial, num_cases, alpha_chi2)
61+
print("static const int binomial_critical = %d;\n" % b_thresh)

0 commit comments

Comments
 (0)