Allocation-free (exception-free) seeding of a random engine

52 views
Skip to first unread message

Thiago Macieira

unread,
Jun 22, 2017, 12:32:38 AM6/22/17
to std-dis...@isocpp.org
I have this code:

using RandomBytes = uint32_t[4];
RandomBytes scratch = {};
// fill area with random data collected elsewhere
std::seed_seq sseq(std::begin(scratch), std::end(scratch));
std::mt19937 generator(sseq);

The code collects 16 bytes of random data to seed the Mersenne Twister, then
allocates 16 bytes on the heap, copies from one buffer to the other, generates
into the buffer provided by the MT.

As you can see, the heap buffer is totally unnecessary in my case. This
violates the C++ "don't pay for what you don't need" principle. I'd like to
either skip the heap buffer entirely, either by having the generator object
use my buffer, or by forcing the generator to use a static buffer (and hope
that the compiler eliminates one once it realises that it's just a copy).

Failing that, I'd like the generator to give me access to the buffer it did
allocate, so I can copy my data there myself.

Has this ever been brought up before?

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center

Nicol Bolas

unread,
Jun 22, 2017, 10:24:46 AM6/22/17
to ISO C++ Standard - Discussion
On Thursday, June 22, 2017 at 12:32:38 AM UTC-4, Thiago Macieira wrote:
Has this ever been brought up before?

Thiago Macieira

unread,
Jun 22, 2017, 11:44:31 PM6/22/17
to std-dis...@isocpp.org
On Thursday, 22 June 2017 07:24:46 PDT Nicol Bolas wrote:
> On Thursday, June 22, 2017 at 12:32:38 AM UTC-4, Thiago Macieira wrote:
> > Has this ever been brought up before?
>
> Yes. <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0205r0.html>

Thank you for the link, Nicol.

This goes in the right direction, but is not exactly what I want. This is
making much easier to seed a deterministic engine, but it won't work for my
case. Specifically, this is the fallback code where std::random_engine failed
(or would have failed if I had used it). I don't have 19968 random bits (632
uint32_t) to fill the range that the generate() function will be called with.

My current code is trying to augment 128 bits of random data with 320 bits of
"hard to guess" data based on time and memory layout.

That's why I was using std::seed_seq: it spreads the 512 bits over the 19968
bits that the engine asks for. I don't know why the algorithm that is in the
standard was chosen to be used, I'm just trusting there's a reason for it.

Thiago Macieira

unread,
Jun 22, 2017, 11:50:40 PM6/22/17
to std-dis...@isocpp.org
On Thursday, 22 June 2017 07:24:46 PDT Nicol Bolas wrote:
> On Thursday, June 22, 2017 at 12:32:38 AM UTC-4, Thiago Macieira wrote:
> > Has this ever been brought up before?
>
> Yes. <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0205r0.html>

BTW, without knowing the paper, I made QRandomGenerator follow it:

http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/global/qrandom.h?
h=dev#n111

http://doc-snapshots.qt.io/qt5-dev/qrandomgenerator.html
(unfortunately, the qdoc tool isn't extracting the docs for member template
functions)
Reply all
Reply to author
Forward
0 new messages