- Project Description: Generation of random numbers using the RP2040 and various entropy sources.
- Features: Lists the main features of the project, such as the use of ADC for entropy, SHA-256 whitening, and the use of RTC for additional randomness.
- Requirements: Specifies what hardware and software are needed.
- Installation: Provides steps for cloning the repository and uploading the files to the RP2040.
- Usage: Details how to use the library and run the example script (generate_random.py).
- How it Works: Explains the steps involved in generating random numbers and how entropy is collected, mixed, and whitened.
- Performance and output: calculation of the TRNG yield.
- Testing Randomness: Mentions statistical tests that can be used to evaluate the quality of randomness.
- Diehard Test for Validation: Test reliably and validation.
- Precautions of use
- Acknowledgements: Credits any technologies or libraries used in the project.
- Disclaimer
This project implements a True Random Number Generator (TRNG) using the RP2040 microcontroller. The generator collects entropy from an ADC (Analog-to-Digital Converter) and mixes it with a SHA-256 hashing function to produce high-quality random numbers. The randomness is further enhanced by using the RP2040's RTC (Real-Time Clock) for time-based entropy.
- ADC-based entropy: Collects noise-based entropy using the RP2040's ADC (GPIO 28).
- Entropy mixing: Uses XOR and Linear Congruential Generator (LCG) techniques for mixing the entropy.
- SHA-256 whitening: Applies the SHA-256 hash function to whiten the entropy and reduce correlations.
- Real-Time Clock (RTC): Uses the RP2040's RTC to add time-based entropy for further randomness.
- Counter-based variability: Introduces additional variability using an internal counter.
- RP2040-based microcontroller.
- MicroPython firmware installed on the board.
machine
,utime
, andhashlib
libraries (built-in with MicroPython).
- git clone https://github.com/MicroControleurMonde/RP2040-RNG/rp2040-trng.git
- cd rp2040-trng
Upload the following Python files to your RP2040 board:
- rp2040_rng_lib.py
- generate_random.py
Once the files are uploaded, you can run the 'generate_random.py' script to start generating random numbers.
This Python library provides a class RandomGenerator that generates random numbers based on the RP2040's ADC and a SHA-256 whitening process.
This script imports the RandomGenerator class from rp2040_rng_lib.py and generates one random number at a time.
Generated random number: 4779610172586853948
The generator uses the analog signal captured by the ADC (analog-to-digital converter) to obtain entropy from sources such as electronic noise, temperature fluctuations, or other random physical phenomena.
The collected entropy is mixed using XOR operations and a Linear Congruential Generator (LCG) to enhance randomness.
The whitening process with the SHA-256 makes possible to transform the collected entropy, often biased or correlated, into a truly random output by eliminating patterns and predictability.
The current Unix timestamp from the RP2040's RTC is used to further whiten the entropy.
The final output is a 64-bit random number that is periodically generated and can be used in applications requiring randomness.
Generating 100,000 random numbers (64 bits) and saving them.
Elapsed time to generate 100000 random numbers: 424.00 seconds.
Random number generation rate: 1886.79 bytes per second.
Number of random numbers generated per second: (100'000/424) ≈ 235
To ensure the quality of the random numbers, you can use statistical randomness tests such as:
- Diehard Tests [Dieharder: A Random Number Test Suite Version 3.31.1] (https://webhome.phy.duke.edu/~rgb/General/dieharder.php)
This test will help evaluate the strength of the randomness generated by this code.
The Dieharder tests Test Suite is used to evaluate whether the output of the random number generator is sufficiently unpredictable and complies with the statistical criteria of pure random numbers.
To perform a Diehard test reliably and validate the random number generator for RP2040, it is recommended to generate a sample of at least several million bits.
For example, for a sample of 10 million bits, you will need to generate approximately 156,250 64-bit numbers.
- Diehard test results (over 200,000 values) can be found in the file 'diehard_sample_report.txt '.
- General results comments in the file 'diehard_sample_report.md '.
To generate 1 million bits, you will need to generate about:
Please consult the CAcert tests and analyses report before considering the use of this generator.
Ent sources (www.fourmilab.ch/random/)
The project is based on the RP2040 microcontroller, and its MicroPython firmware.
The entropy whitening is done using the SHA-256 algorithm from the hashlib library.
Tested on an original Raspberry Pi Pico, a XIAO seed studio RP2040 and a Waveshare RP2040-Plus. The code works perfectly on all three platforms, regardless of the ADC channel used.
- Raspberry Pi Pico: MicroPython v1.23.0 on 2024-06-02;
- XIAO seed studio: MicroPython v1.22.1 on 2024-01-05;
- Waveshare RP2040-Plus: MicroPython ef518cbf2-dirty on 2023-03-24;
The code contained in this repository is provided “as is”, without any warranty of performance, accuracy or result. The author shall not be liable for any direct or indirect damages that may result from the use of this code, including, but not limited to, loss of data or interruption of service.
Use of this code is entirely at your own risk. Please ensure that you fully understand the code before using it in a production environment or integrating it into your projects.