return st;
}
+/*
+ * Run some statistical tests to provide a degree confidence that the
+ * BN_rand_range() function works as expected. The test cases and
+ * critical values are generated by the bn_rand_range script.
+ *
+ * Each individual test is a Chi^2 goodness of fit for a specified number
+ * of samples and range. The samples are assumed to be independent and
+ * that they are from a discrete uniform distribution.
+ *
+ * Some of these individual tests are expected to fail, the success/failure
+ * of each is an independent Bernoulli trial. The number of such successes
+ * will form a binomial distribution. The count of the successes is compared
+ * against a precomputed critical value to determine the overall outcome.
+ */
+struct rand_range_case {
+ unsigned int range;
+ unsigned int iterations;
+ double critical;
+};
+
+#include "bn_rand_range.h"
+
+static int test_rand_range_single(size_t n)
+{
+ const unsigned int range = rand_range_cases[n].range;
+ const unsigned int iterations = rand_range_cases[n].iterations;
+ const double critical = rand_range_cases[n].critical;
+ const double expected = iterations / (double)range;
+ double sum = 0;
+ BIGNUM *rng = NULL, *val = NULL;
+ size_t *counts;
+ unsigned int i, v;
+ int res = 0;
+
+ if (!TEST_ptr(counts = OPENSSL_zalloc(sizeof(*counts) * range))
+ || !TEST_ptr(rng = BN_new())
+ || !TEST_ptr(val = BN_new())
+ || !TEST_true(BN_set_word(rng, range)))
+ goto err;
+ for (i = 0; i < iterations; i++) {
+ if (!TEST_true(BN_rand_range(val, rng))
+ || !TEST_uint_lt(v = (unsigned int)BN_get_word(val), range))
+ goto err;
+ counts[v]++;
+ }
+
+ for (i = 0; i < range; i++) {
+ const double delta = counts[i] - expected;
+ sum += delta * delta;
+ }
+ sum /= expected;
+
+ if (sum > critical) {
+ TEST_info("Chi^2 test negative %.4f > %4.f", sum, critical);
+ TEST_note("test case %zu range %u iterations %u", n + 1, range,
+ iterations);
+ goto err;
+ }
+
+ res = 1;
+err:
+ BN_free(rng);
+ BN_free(val);
+ OPENSSL_free(counts);
+ return res;
+}
+
+static int test_rand_range(void)
+{
+ int n_success = 0;
+ size_t i;
+
+ for (i = 0; i < OSSL_NELEM(rand_range_cases); i++)
+ n_success += test_rand_range_single(i);
+ if (TEST_int_ge(n_success, binomial_critical))
+ return 1;
+ TEST_note("This test is expeced to fail by chance 0.01%% of the time.");
+ return 0;
+}
+
static int test_negzero(void)
{
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
return ret;
}
-static int test_smallprime(void)
+static int test_smallprime(int kBits)
+{
+ BIGNUM *r;
+ int st = 0;
+
+ if (!TEST_ptr(r = BN_new()))
+ goto err;
+
+ if (kBits <= 1) {
+ if (!TEST_false(BN_generate_prime_ex(r, kBits, 0,
+ NULL, NULL, NULL)))
+ goto err;
+ } else {
+ if (!TEST_true(BN_generate_prime_ex(r, kBits, 0,
+ NULL, NULL, NULL))
+ || !TEST_int_eq(BN_num_bits(r), kBits))
+ goto err;
+ }
+
+ st = 1;
+ err:
+ BN_free(r);
+ return st;
+}
+
+static int test_smallsafeprime(int kBits)
{
- static const int kBits = 10;
BIGNUM *r;
int st = 0;
- if (!TEST_ptr(r = BN_new())
- || !TEST_true(BN_generate_prime_ex(r, (int)kBits, 0,
- NULL, NULL, NULL))
- || !TEST_int_eq(BN_num_bits(r), kBits))
+ if (!TEST_ptr(r = BN_new()))
goto err;
+ if (kBits <= 5 && kBits != 3) {
+ if (!TEST_false(BN_generate_prime_ex(r, kBits, 1,
+ NULL, NULL, NULL)))
+ goto err;
+ } else {
+ if (!TEST_true(BN_generate_prime_ex(r, kBits, 1,
+ NULL, NULL, NULL))
+ || !TEST_int_eq(BN_num_bits(r), kBits))
+ goto err;
+ }
+
st = 1;
err:
BN_free(r);
for (trial = 0; trial <= 1; ++trial) {
if (!TEST_true(BN_set_word(r, primes[i]))
- || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 1, ctx, trial, NULL),
+ || !TEST_int_eq(BN_check_prime(r, ctx, NULL),
1))
goto err;
}
for (trial = 0; trial <= 1; ++trial) {
if (!TEST_true(BN_set_word(r, not_primes[i]))
- || !TEST_false(BN_is_prime_fasttest_ex(r, 1, ctx, trial, NULL)))
+ || !TEST_false(BN_check_prime(r, ctx, NULL)))
goto err;
}
return c == 0;
}
+typedef enum OPTION_choice {
+ OPT_ERR = -1,
+ OPT_EOF = 0,
+ OPT_STOCHASTIC_TESTS,
+ OPT_TEST_ENUM
+} OPTION_CHOICE;
+
const OPTIONS *test_get_options(void)
{
- enum { OPT_TEST_ENUM };
static const OPTIONS test_options[] = {
OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
+ { "stochastic", OPT_STOCHASTIC_TESTS, '-', "Run stochastic tests" },
{ OPT_HELP_STR, 1, '-',
"file\tFile to run tests on. Normal tests are not run\n" },
{ NULL }
int setup_tests(void)
{
- int n = test_get_argument_count();
+ OPTION_CHOICE o;
+ int n, stochastic = 0;
+
+ while ((o = opt_next()) != OPT_EOF) {
+ switch (o) {
+ case OPT_STOCHASTIC_TESTS:
+ stochastic = 1;
+ break;
+ case OPT_TEST_CASES:
+ break;
+ default:
+ case OPT_ERR:
+ return 0;
+ }
+ }
+ n = test_get_argument_count();
if (!TEST_ptr(ctx = BN_CTX_new()))
return 0;
ADD_TEST(test_badmod);
ADD_TEST(test_expmodzero);
ADD_TEST(test_expmodone);
- ADD_TEST(test_smallprime);
+ ADD_ALL_TESTS(test_smallprime, 16);
+ ADD_ALL_TESTS(test_smallsafeprime, 16);
ADD_TEST(test_swap);
ADD_TEST(test_ctx_consttime_flag);
#ifndef OPENSSL_NO_EC2M
#endif
ADD_ALL_TESTS(test_is_prime, (int)OSSL_NELEM(primes));
ADD_ALL_TESTS(test_not_prime, (int)OSSL_NELEM(not_primes));
+ if (stochastic)
+ ADD_TEST(test_rand_range);
} else {
ADD_ALL_TESTS(run_file_tests, n);
}