/*
* Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <config.h>
#include <atf-c.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
/* igamc(), igam(), etc. were adapted (and cleaned up) from the Cephes
* math library:
*
* Cephes Math Library Release 2.8: June, 2000
* Copyright 1985, 1987, 2000 by Stephen L. Moshier
*
* The Cephes math library was released into the public domain as part
* of netlib.
*/
static double igamc(double a, double x);
static double igam(double a, double x);
static double
igamc(double a, double x) {
if ((x <= 0) || (a <= 0))
return (1.0);
if ((x < 1.0) || (x < a))
return (1.0 - igam(a, x));
return (0.0);
}
/* continued fraction */
y = 1.0 - a;
z = x + y + 1.0;
c = 0.0;
pkm2 = 1.0;
qkm2 = x;
pkm1 = x + 1.0;
qkm1 = z * x;
do {
c += 1.0;
y += 1.0;
z += 2.0;
yc = y * c;
if (qk != 0) {
ans = r;
} else
t = 1.0;
}
} while (t > MACHEP);
}
static double
igam(double a, double x) {
if ((x <= 0) || (a <= 0))
return (0.0);
if ((x > 1.0) && (x > a))
return (1.0 - igamc(a, x));
/* Compute x**a * exp(-x) / md_gamma(a) */
return (0.0);
}
/* power series */
r = a;
c = 1.0;
ans = 1.0;
do {
r += 1.0;
c *= x / r;
ans += c;
}
static isc_int8_t
int i;
sc = 0;
for (i = 0; i < 16; i++) {
lsb = n & 1;
if (lsb != 0)
sc += 1;
else
sc -= 1;
n >>= 1;
}
return (sc);
}
static isc_uint8_t
int i;
bc = 0;
for (i = 0; i < 16; i++) {
lsb = n & 1;
if (lsb != 0)
bc += 1;
n >>= 1;
}
return (bc);
}
static void
tables_init(void) {
isc_uint32_t i;
for (i = 0; i < 65536; i++) {
scounts_table[i] = scount_calculate(i);
bitcounts_table[i] = bitcount_calculate(i);
}
}
/*
* The following code for computing Marsaglia's rank is based on the
* implementation in cdbinrnk.c from the diehard tests by George
* Marsaglia.
*
* This function destroys (modifies) the data passed in bits.
*/
static isc_uint32_t
size_t i, j, k;
unsigned int rt = 0;
for (k = 0; k < rows; k++) {
i = k;
i++;
if (i < rows)
continue;
else {
rt++;
i = k;
continue;
}
}
return (rank);
}
rank++;
if (i != k) {
}
for (j = i + 1; j < rows; j++) {
continue;
else
}
rt++;
}
return (rank);
}
static void
isc_uint32_t m;
isc_uint32_t j;
double proportion;
double p_hat;
double lower_confidence;
double chi_square;
double p_value_t;
tables_init();
m = 1000;
passed = 0;
for (j = 0; j < 11; j++)
histogram[j] = 0;
for (j = 0; j < m; j++) {
isc_uint32_t i;
double p_value;
for (i = 0; i < 128000; i++)
if (p_value >= 0.01)
passed++;
histogram[i]++;
}
/* Fold histogram[10] (p_value = 1.0) into histogram[9] for
* interval [0.9, 1.0]
*/
histogram[10] = 0;
/*
* Check proportion of sequences passing a test (see section
* 4.2.1 in NIST SP 800-22).
*/
proportion = (double) passed / (double) m;
/* Debug message, not displayed when running via atf-run */
printf("lower_confidence=%f, proportion=%f\n",
/*
* Check uniform distribution of p-values (see section 4.2.2 in
* NIST SP 800-22).
*/
/* Pre-requisite that at least 55 sequences are processed. */
ATF_REQUIRE(m >= 55);
chi_square = 0.0;
for (j = 0; j < 10; j++) {
double numer;
double denom;
/* Debug message, not displayed when running via atf-run */
(histogram[j] - (m / 10.0));
denom = m / 10.0;
}
printf("\n");
}
/*
* This is a frequency (monobits) test taken from the NIST SP 800-22
* RNG test suite.
*/
static double
size_t i;
double s_obs;
double p_value;
scount = 0;
for (i = 0; i < length; i++)
/* Preconditions (section 2.1.7 in NIST SP 800-22) */
/* Debug message, not displayed when running via atf-run */
return (p_value);
}
/*
* This is the runs test taken from the NIST SP 800-22 RNG test suite.
*/
static double
size_t i;
double pi;
double tau;
isc_uint32_t j;
isc_uint32_t b;
double numer;
double denom;
double p_value;
bcount = 0;
for (i = 0; i < 128000; i++)
/* Debug message, not displayed when running via atf-run */
/* Preconditions (section 2.3.7 in NIST SP 800-22) */
/*
* Pre-condition implied from the monobit test. This can fail
* for some sequences, and the p-value is taken as 0 in these
* cases.
*/
return (0.0);
/* Compute v_obs */
j = 0;
b = 14;
v_obs = 0;
for (i = 1; i < numbits; i++) {
if (b == 0) {
b = 15;
j++;
} else {
b--;
}
}
v_obs += 1;
return (p_value);
}
/*
* This is the block frequency test taken from the NIST SP 800-22 RNG
* test suite.
*/
static double
isc_uint32_t i;
double *pi;
double chi_square;
double p_value;
mbits = 32000;
/* Debug message, not displayed when running via atf-run */
/* Preconditions (section 2.2.7 in NIST SP 800-22) */
cur_word = 0;
for (i = 0; i < numblocks; i++) {
isc_uint32_t j;
pi[i] = 0.0;
for (j = 0; j < mwords; j++) {
cur_word++;
}
}
/* Compute chi_square */
chi_square = 0.0;
for (i = 0; i < numblocks; i++)
/* Debug message, not displayed when running via atf-run */
return (p_value);
}
/*
* This is the binary matrix rank test taken from the NIST SP 800-22 RNG
* test suite.
*/
static double
isc_uint32_t i;
double term1;
double term2;
double term3;
double chi_square;
double p_value;
matrix_m = 32;
matrix_q = 32;
/* Preconditions (section 2.5.7 in NIST SP 800-22) */
fm_0 = 0;
fm_1 = 0;
fm_rest = 0;
for (i = 0; i < num_matrices; i++) {
/*
* Each isc_uint32_t supplies 32 bits, so a 32x32 bit matrix
* takes up isc_uint32_t array of size 32.
*/
int j;
for (j = 0; j < 32; j++) {
idx += j * 2;
}
fm_0++;
fm_1++;
else
fm_rest++;
}
/* Compute chi_square */
/* Debug message, not displayed when running via atf-run */
printf("fm_0=%u, fm_1=%u, fm_rest=%u, chi_square=%f\n",
return (p_value);
}
}
}
}
}
}
}
}
/*
* This is the binary matrix rank test taken from the NIST SP 800-22 RNG
* test suite.
*/
}
/*
* Main
*/
return (atf_no_error());
}