Skip to content

Instantly share code, notes, and snippets.

@unprovable
Last active October 11, 2017 14:31
Show Gist options
  • Save unprovable/a1e40921d7ca09aa0867d6e9537ff3b2 to your computer and use it in GitHub Desktop.
Save unprovable/a1e40921d7ca09aa0867d6e9537ff3b2 to your computer and use it in GitHub Desktop.
RC Entropy Pool - test circuit and code
// RC Entropy source...
// experimental code - NOT FOR USE IN PRODUCTION!!
// --- not proven to be anywhere close to CSPRNG nor TruRNG ---
// by Mark C (@LargeCardinal)
//
// So, a 1.2 to 1nF ceramic cap and 100Ohm resistor are placed in
// parallel across the A6 and GND pins. A fly wire from Digital
// pin 2 then goes to the A6 rail, powering the cap of 1nF and 100Ohm res
//
// In practice, it seems we really don't need the capacitor... maybe
// we can rely on the capacitance/resistance in the ADC converter itself?!
int coin_flip(void) {
int little_bit;
digitalWrite(2,1);
delay(2);
return little_bit = (analogRead(A6) & 1);
}
int getFairBit(void) {
while(1)
{
int a = coin_flip();
if (a != coin_flip()){
return a;
}
}
}
int get_rand_byte(void){
int n=0;
int bits = 7;
while (bits--) {
n<<=1;
n |= getFairBit();
}
return n;
}
void setup()
{
// set driver pin to output:
pinMode(2, OUTPUT);
// start serial
Serial.begin(9600);
// warm up the capacitor! :P
digitalWrite(2, 1);
delay(200);
digitalWrite(2, 0);
Serial.println("init done!");
}
void loop()
{
unsigned int rand_word, rand_byte1, rand_byte2 = 0;
for ( int i = 0; i < 10000; i++) {
rand_byte1 = get_rand_byte();
Serial.print("random byte: "); Serial.println(rand_byte1, HEX);
}
// this is just so I can see when it's done :P
delay(100000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment