Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making use of sandy bridge's hardware true random number generator?

I was wondering if there is a way to make use of the new hardware based true number generator found in intel's sandy bridge CPU? I read that intel's MKL (Math Kernel Library) exposes this functionality, but this requires the MKL suite and an intel complier, ending up pretty expensive.

Is there another way to employ the hardware random number generator in my C++ code? For example a nice, header only library?

like image 598
dtech Avatar asked Oct 26 '11 11:10

dtech


People also ask

What is the use of true random number generator?

A TRNG is a function or device based on an unpredictable physical phenomenon, called an entropy source, that is designed to generate non-deterministic data (e.g., a succession of numbers) to seed security algorithms.

How does a hardware random number generator work?

A hardware random number generator typically consists of a transducer to convert some aspect of the physical phenomena to an electrical signal, an amplifier and other electronic circuitry to increase the amplitude of the random fluctuations to a measurable level, and some type of analog-to-digital converter to convert ...

What is the best method to generate true random numbers?

There are two main methods that a computer generates a random number: true random number generators (TRNGs) and pseudo-random number generators (PRNGs). The former uses some phenomenon outside the computer for its number generation, whereas the latter relies on pre-set algorithms to emulate randomness².

What are different types of random number generator techniques?

There are generally two kinds of random number generators: non-deterministic random number generators, sometimes called "true random number generators" (TRNG), and deterministic random number generators, also called pseudorandom number generators (PRNG).


2 Answers

Intel has posted a manual, library, and code examples for the rdrand instruction at http://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide.

From the Readme:

"Because the many of compiler toolchains do not support this new instruction, this library was created to facilitate easy access to it. The idea is simple: link to a built static library and enjoy the new feature!"

There are examples of all the library calls in main.c.

I was able to compile the static library and test program in gcc on Mac OS X. The documentation states that it is also compatible with Linux and Windows.

Be aware that rdrand is actually a 128-bit pseudo-random number generator with hardware-generated entropy. (The upcoming Broadwell architecture will provide an rdseed instruction to access the true random number generator.) The details of the difference and its implications can be found under the "Long Answer" heading at http://software.intel.com/en-us/blogs/2012/11/17/the-difference-between-rdrand-and-rdseed.

like image 162
Doug Anger Avatar answered Oct 29 '22 12:10

Doug Anger


Here is the example code:

#include <immintrin.h>
#include <cstdint>
...
uint64_t val;
if(!_rdseed64_step(&val)) {
  printf("Error generating hardware random value\n");
}
// Now val contains 64-bit pseudo-random number

uint64_t val;
if(!_rdrand64_step(&val)) {
  printf("Error generating hardware random value\n");
}
// Now val contains 64-bit true random number

Reference: Intel Intrinsics Guide

like image 33
Serge Rogatch Avatar answered Oct 29 '22 11:10

Serge Rogatch