Skip to content

Instantly share code, notes, and snippets.

@wandrson
Created April 5, 2014 18:24
Show Gist options
  • Save wandrson/9995925 to your computer and use it in GitHub Desktop.
Save wandrson/9995925 to your computer and use it in GitHub Desktop.
A short Arduino Due sketch that measures the time it takes the Due's ARM processor's true random number generator to produce 1Mb of entropy.
// due_hwrng_speed_test.ino
// by Walter Anderson
// April 5, 2014
//
// This is a speed test run on an Arduino Due with an ATSAM3X8E-AU chip dated 1317, it produced the following
// output:
// Starting time test for speed of generation of hardware randrom number gebnrator.
// It took 262 milliseconds to generato 1,048,576 bytes of entropy!
// This equates to 4,002,198 bytes per second!
//
#include <sam.h>
#include <sam3xa/include/component/component_trng.h>
uint32_t test_hwrng()
{
static bool enabled = false;
if (!enabled) {
pmc_enable_periph_clk(ID_TRNG);
TRNG->TRNG_IDR = 0xFFFFFFFF;
TRNG->TRNG_CR = TRNG_CR_KEY(0x524e47) | TRNG_CR_ENABLE;
enabled = true;
}
while (! (TRNG->TRNG_ISR & TRNG_ISR_DATRDY))
;
return TRNG->TRNG_ODATA;
}
void setup() {
unsigned long start_time;
unsigned long end_time;
unsigned long elapsed_time;
Serial.begin(9600); // I use 9600 baud to maintain consistency for my Entropy library testing procedures
Serial.println("Starting time test for speed of generation of hardware randrom number gebnrator.");
start_time = millis();
for (long i = 0; i < 262144; i++)
{
elapsed_time = test_hwrng(); // just reusing a 32-but variable
}
end_time = millis();
elapsed_time = end_time - start_time;
Serial.print("It took ");
Serial.print(elapsed_time);
Serial.println(" milliseconds to generato 1,048,576 bytes of entropy!");
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment