Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
210 Views

Using random library within enclave

Jump to solution

Hello,

I need to generate random numbers (integers or double) and use binomial distribution within an enclave. The user guide mentions that <random.h> is not included for SGX, while a random number can be generated using sgx_read_rand function in <sgx_trts.h>.

First, how do I use sgx_read_rand to generate random integers? It seems only unsigned char is added to the input buffer.

Second, I want to use the binomial_distribution and beta_distribution class available in the standard <random.h>. How do I invoke this within the enclave?

Any help is appreciated.

0 Kudos

Accepted Solutions
Highlighted
210 Views

Hi Swarup,

1. The sgx_read_rand() function populates a buffer with random bytes. To turn these into an integer, you simply populate a buffer of the corresponding size (1 byte for an 8-bit integer, 2 bytes for a 16-bit integer, 4 bytes for a 32-bit integer, and 8 bytes for a 64-bit integer) and recast it to the appropriate integer type.

A quick and easy way of doing this is:
                  uint32_t val; 
                  sgx_read_rand((unsigned char *) &val, 4);  And you have an unsigned, 32-bit random number in val. (Converting that to a number from 0 to N is an exercise for the reader. There are plenty of references online for unbiased algorithms that do this.)
 
2. std::random is part of the C++11 standard. At this time, enclaves only support the C++03 standard.

Thanks and Reagrds,
Surenthar Selvaraj

View solution in original post

0 Kudos
1 Reply
Highlighted
211 Views

Hi Swarup,

1. The sgx_read_rand() function populates a buffer with random bytes. To turn these into an integer, you simply populate a buffer of the corresponding size (1 byte for an 8-bit integer, 2 bytes for a 16-bit integer, 4 bytes for a 32-bit integer, and 8 bytes for a 64-bit integer) and recast it to the appropriate integer type.

A quick and easy way of doing this is:
                  uint32_t val; 
                  sgx_read_rand((unsigned char *) &val, 4);  And you have an unsigned, 32-bit random number in val. (Converting that to a number from 0 to N is an exercise for the reader. There are plenty of references online for unbiased algorithms that do this.)
 
2. std::random is part of the C++11 standard. At this time, enclaves only support the C++03 standard.

Thanks and Reagrds,
Surenthar Selvaraj

View solution in original post

0 Kudos