-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |