Skip to content

Commit

Permalink
tests: Add random test
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Nov 24, 2023
1 parent 438a346 commit 80f2b3e
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/random/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "riot-wrappers-test-random"
version = "0.1.0"
authors = ["Christian Amsüss <[email protected]>"]
edition = "2021"
publish = false

[lib]
crate-type = ["staticlib"]

[profile.release]
panic = "abort"

[dependencies]
riot-wrappers = { version = "*", features = [ "set_panic_handler" ] }
rand_core = "0.6"
rngcheck = "0.1.1"
14 changes: 14 additions & 0 deletions tests/random/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# name of your application
APPLICATION = riot-wrappers-test-random
BOARD ?= native
APPLICATION_RUST_MODULE = riot_wrappers_test_random
BASELIBS += $(APPLICATION_RUST_MODULE).module
FEATURES_REQUIRED += rust_target

USEMODULE += random
# So that the RNG can be Default-constructed
FEATURES_REQUIRED += periph_hwrng
# So that the RNG also satisfies the CryptoRng requirements
USEMODULE += prng_sha256prng

include $(RIOTBASE)/Makefile.include
77 changes: 77 additions & 0 deletions tests/random/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#![no_std]

use riot_wrappers::println;
use riot_wrappers::random::Random;
use riot_wrappers::riot_main;

riot_main!(main);

fn check_csrng(mut rng: impl rand_core::CryptoRng + rand_core::RngCore) {
use rngcheck::{helpers::*, nist::*};

struct BitIter<'a, R: rand_core::RngCore> {
rng: &'a mut R,
remaining: usize,
buffer: u32,
buffered: u8,
}

impl<'a, R: rand_core::RngCore> BitIter<'a, R> {
fn new(rng: &'a mut R, items: usize) -> Self {
Self {
rng,
remaining: items,
buffer: 0,
buffered: 0,
}
}
}
impl<'a, R: rand_core::RngCore> Iterator for BitIter<'a, R> {
type Item = bool;
fn next(&mut self) -> Option<bool> {
if self.remaining == 0 {
return None;
}

self.remaining -= 1;

if self.buffered == 0 {
self.buffer = self.rng.next_u32();
self.buffered = 32;
}
let result = self.buffer & 1 != 0;
self.buffer >>= 1;
self.buffered -= 1;
Some(result)
}
}

// 1 megabit; enough to complete on an nRF52840 within the test timeout
let count = 1000000;

println!(
"Monobit stat: {}",
nist_freq_monobit(BitIter::new(&mut rng, count)).unwrap()
);
// To be fair, I have no clue whether 16 is a good number here.
//
// These are all reporting NaN due to https://github.com/ryankurte/rngcheck/issues/1 -- but
// then again, we're mainly doing these so that the RNG runs for a bit.
println!(
"Frequency block stat: {}",
nist_freq_block(BitIter::new(&mut rng, count), 16).unwrap()
);

println!("Generating 10 more random numbers");
for _ in 0..10 {
println!("{}", rng.next_u32());
}

println!("Done");
}

fn main() {
// works because we have periph_hwrng
let rng: Random = Default::default();
check_csrng(rng);
}
11 changes: 11 additions & 0 deletions tests/random/tests/01-run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python3

import os
import sys
from testrunner import run

def test(child):
child.expect("Done")

if __name__ == "__main__":
sys.exit(run(test))

0 comments on commit 80f2b3e

Please sign in to comment.