From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: Florian Weimer <fweimer@redhat.com>
Cc: libc-alpha@sourceware.org,
"Jason A . Donenfeld" <Jason@zx2c4.com>,
Carlos O'Donell <carlos@redhat.com>
Subject: Re: [PATCH] stdlib: Tuned down tst-arc4random-thread internal parameters
Date: Wed, 27 Jul 2022 12:57:06 -0300 [thread overview]
Message-ID: <08da08a2-b589-7e5f-0fe3-a211929dc189@linaro.org> (raw)
In-Reply-To: <87bktad0tp.fsf@oldenburg.str.redhat.com>
On 27/07/22 12:36, Florian Weimer wrote:
> * Adhemerval Zanella Netto:
>
>>>>> By the way, I think we should switch to the standard arc4random_uniform
>>>>> implementation that doesn't try to conserve bits. It's cheaper to get a
>>>>> larger number of bits once instead of obtaining 24 bits first, then 8
>>>>> bits.
>>>>
>>>> Yeah, it should simpler indeed. But I do not consider this urgent
>>>> for 2.36.
>>>
>>> Well, the standard way probably has more obvious statistical properties
>>> and is harder to screw up. 8-/
>>
>> Right, do you consider this a blocker? I think I can send a patch to
>> use a simpler algorithm.
>
> I like it because it would minimize risk. It's not a strict blocker.
> I'm not sure if I'll be able to work on it this week. Maybe I can
> review a patch.
Without trying to be clever, what about something like the below, adapted
from Bitmask with Rejection [1]. It should be fast on most case since it
avoid the modulo operation, and the last part that tries to reuse the
extra entropy bits are not strictly required.
uint32_t
__arc4random_uniform (uint32_t n)
{
if (n <= 1)
/* There is no valid return value for a zero limit, and 0 is the
only possible result for limit 1. */
return 0;
/* mask is the smallest power of 2 minus 1 which is larger than n. */
int z = __builtin_clz (n);
int bits = CHAR_BIT * sizeof (uint32_t) - z;
uint32_t mask = ~UINT32_C(0) >> z;
while (1)
{
uint32_t value = __arc4random ();
/* Return is the lower power of 2 minus 1 satisfy the condition. */
uint32_t r = value & mask;
if (r < n)
return r;
/* Otherwise check if remaining bits of entropy provides fits in the
bound. */
int bits_left = z;
while (bits_left >= bits)
{
value >>= bits;
r = value & mask;
if (r < n)
return r;
bits_left -= bits;
}
}
}
[1] https://www.pcg-random.org/posts/bounded-rands.html
next prev parent reply other threads:[~2022-07-27 15:57 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-27 12:06 Adhemerval Zanella
2022-07-27 12:25 ` Florian Weimer
2022-07-27 12:29 ` Adhemerval Zanella Netto
2022-07-27 12:42 ` Florian Weimer
2022-07-27 13:00 ` Adhemerval Zanella Netto
2022-07-27 15:36 ` Florian Weimer
2022-07-27 15:57 ` Adhemerval Zanella Netto [this message]
2022-07-27 17:10 ` Yann Droneaud
2022-07-27 19:18 ` Adhemerval Zanella Netto
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=08da08a2-b589-7e5f-0fe3-a211929dc189@linaro.org \
--to=adhemerval.zanella@linaro.org \
--cc=Jason@zx2c4.com \
--cc=carlos@redhat.com \
--cc=fweimer@redhat.com \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).