https://github.com/microcontroleurmonde/pyboard_rng
Implementation in micro-python of the `pyb.rng()` function to generate random numbers.
https://github.com/microcontroleurmonde/pyboard_rng
Last synced: 5 months ago
JSON representation
Implementation in micro-python of the `pyb.rng()` function to generate random numbers.
- Host: GitHub
- URL: https://github.com/microcontroleurmonde/pyboard_rng
- Owner: MicroControleurMonde
- License: cc0-1.0
- Created: 2024-11-29T19:33:49.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-13T15:37:37.000Z (over 1 year ago)
- Last Synced: 2024-12-30T03:28:24.208Z (over 1 year ago)
- Language: Python
- Homepage:
- Size: 10.5 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PyBOARD_RNG

**Abstract:** Micro-python implementation of the **`pyb.rng()`** function to generate random numbers **FOR TESTING PURPOSES**.
The function specifically calls the hardcoded RNG in the chip directly (*as for the ESP32*)
## Generator coding:
Here is a typical example of use :
import pyb
random_number = pyb.rng() # Returns a 32-bit random number
print(random_number)
Output: 112922794
I made a very basic code that makes 200'000 calls to the 'pyb.rng()' function. Here: [pyboard_rng.py](https://github.com/MicroControleurMonde/PyBOARD_RNG/blob/main/pyboard_rng.py)
### Note:
Unlike the ESP32 board, **I did not rewrite the RNG module** which is perfectly functional natively for the Pyboard board in micropython.
The goal here is more to test the performance and RNG capabilities of the MCU than to code anything.
## Performance:
- Time spent to generate 200'000 values: **108 seconds** (avg)
- Throughput: **7,407 bytes/sec**
- **1847** random values / sec.
## Ent/DJent Test Report:
(www.fourmilab.ch) John Walker and [David Johnston](https://github.com/dj-on-github/djent)
##### Max possible enropy = 1
- Min Entropy (by max occurrence of symbol 0) = 0.912487
- Shannon IID Entropy = **0.997177** bits per symbol
- Sample size: **53.4 MB**
- Total generated: **13'336'713 values**
- [Ent Report -Raw](https://github.com/MicroControleurMonde/PyBOARD_RNG/blob/main/Reports/Pyb_RNG_Test_13Mi_djent.txt)
- [Ent Report Analyse]***To be updated***
## CAcert Report:
[Results page](https://www.cacert.at/cgi-bin/rngresults)

## Dieharder Test Report
(https://webhome.phy.duke.edu/~rgb/General/dieharder.php) Robert G. Brown
- Sample size: **53.4 MB**
- Total generated: **13'336'713 values**
- [Dieharder Report - Raw](https://github.com/MicroControleurMonde/PyBOARD_RNG/blob/main/Reports/Pyb_RNG_Test_13Mi_dieharder.txt)
- [Dieharder Report Analyses] ***To be updated***
## Reference:
[Reference manual STM32F405/415](https://www.st.com/resource/en/reference_manual/rm0090-stm32f405415-stm32f407417-stm32f427437-and-stm32f429439-advanced-armbased-32bit-mcus-stmicroelectronics.pdf) (page 770 - 24 Random number generator)
Doc. excerpt:
The RNG processor is a random number generator, based on continuous analogue noise,
which provides a 32-bit random value to the host when read.
The RNG passed the FIPS PUB 140-2 tests (10 October 2001) with a pass rate of 99%.
The analogue circuit consists of several ring oscillators whose outputs are combined
by XOR to generate the seeds.
The RNG_LFSR is clocked by a dedicated clock (RNG_CLK) at a constant frequency,
so that the quality of the random number is independent of the HCLK frequency.
The contents of the RNG_LFSR are transferred to the data register (RNG_DR) when
a significant number of seeds have been entered into the RNG_LFSR.
In parallel, the analogue seed and the dedicated RNG_CLK clock are monitored.
Status bits (in the RNG_SR register) indicate when an abnormal sequence occurs
on the seed or when the RNG_CLK clock frequency is too low.
An interrupt can be generated when an error is detected.
### Comment:
For STM32F4xx microcontrollers, the manual indicates that these devices are intended for use in functional safety applications. However, it doesn't provide specific details about the results of the FIPS 140-2 tests. I couldn't find any specific results related to on the NIST website. Since this standard was released back in 2001, the associated documentation could be found in a large collection on the NIST website / archives.
Here is the output of the Debian **`rngtest`** which implements the FIPS 140-2 tests: [Output](https://github.com/MicroControleurMonde/PyBOARD_RNG/blob/main/Reports/Pyb_RNG_Test_13Mi_rngtest.txt)
That being said, it's worth noting that STMicroelectronics has currently many other MCU models that are FIPS certified.