Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example and NaN fix #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = "MIT"

[dependencies]
libm = "0.2.7"
special = "0.10.3"
rand_core_0_6 = { package = "rand_core", version = "0.6" }

[dev-dependencies]
Expand Down
19 changes: 19 additions & 0 deletions examples/osrng.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use rand::{RngCore, rngs::OsRng};
use rngcheck::{nist::*, helpers::*};

fn main() {
let mut rng = OsRng;

// Fetch random bytes for tests
let mut a = [0xFF; 100];
rng.fill_bytes(&mut a);

// Check we filled -something- before attempting more in-depth tests
if &a[..2] == &[0xFF; 2] && &a[a.len() - 2..] == &[0xFF; 2] {
panic!("RNG seems to have no-op'ed");
}

// Run NIST frequency checks
println!("Monobit result: {:?}", nist_freq_monobit(BitIter::new(&a)));
println!("Freq block result: {:?}", nist_freq_block(BitIter::new(&a), 10));
}
31 changes: 7 additions & 24 deletions src/nist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub fn nist_freq_monobit(data: impl Iterator<Item = bool>) -> Result<f32, Error>
// Compute P-value
let p = libm::erfcf(s / libm::sqrtf(2.0));

// Check P value limit
if p < 0.01 {
// Check P value limit. The inverted logic ensures NaNs cause an error.
if !(p >= 0.01) {
return Err(Error::BadPValue(p));
}

Expand Down Expand Up @@ -86,35 +86,18 @@ pub fn nist_freq_block(
// Compute p
let p = 1.0 - nist_igamma(num_blocks as f32 / 2.0, x2 / 2.0);

// Check p value
if p < 0.01 {
// Check p value. The inverted logic ensures NaNs cause an error.
if !(p >= 0.01) {
return Err(Error::BadPValue(p));
}

Ok(p)
}

// {\displaystyle \gamma (s,x)} = EXP(GAMMALN(s))*GAMMA.DIST(x,s,1,TRUE).

/// Incomplete gamma function (attempt, probably incomplete / incorrect)
// TODO: find a better version / test suite to confirm the correct operation of this
/// Incomplete gamma function
fn nist_igamma(a: f32, x: f32) -> f32 {
let epsilon = 1e-8;

let mut sum = 1.0f32;
let mut term = 1.0f32;
let mut n = 0;
let max_iterations = 100;

while libm::fabsf(term) >= epsilon * libm::fabsf(sum) && n < max_iterations {
term *= x / (a + n as f32 + 1.0);
sum += term;
n += 1;
}

let gamma = sum * libm::powf(x, a) * libm::expf(-x) / libm::tgammaf(a + 1.0);

gamma
use special::Gamma;
x.inc_gamma(a)
}

#[cfg(test)]
Expand Down