Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Intel Community
- Software Development Technologies
- Intel® Software Guard Extensions (Intel® SGX)
- Using random library within enclave

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Highlighted

swarup

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-27-2016
10:27 AM

210 Views

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.

Accepted Solutions

Highlighted

Surenthar_S_Intel

Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-28-2016
08:59 PM

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

1 Reply

Highlighted

Surenthar_S_Intel

Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-28-2016
08:59 PM

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

For more complete information about compiler optimizations, see our Optimization Notice.