public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org, "H.J. Lu" <hjl.tools@gmail.com>
Cc: Florian Weimer <fweimer@redhat.com>
Subject: Re: [PATCH v4 1/9] stdlib: Add arc4random, arc4random_buf, and arc4random_uniform (BZ #4417)
Date: Tue, 26 Apr 2022 09:36:15 -0300	[thread overview]
Message-ID: <a2678d3d-1a32-630e-bba3-190729a27f80@linaro.org> (raw)
In-Reply-To: <20220425130156.1062525-2-adhemerval.zanella@linaro.org>



On 25/04/2022 10:01, Adhemerval Zanella wrote:
> The implementation is based on scalar Chacha20, with global cache and
> locking.  It uses getrandom or /dev/urandom as fallback to get the
> initial entropy, and reseeds the internal state on every 16MB of
> consumed buffer.
> 
> It maintains an internal buffer which consumes at maximum one page on
> most systems (assuming minimum of 4k pages).  The internal buf optimizes
> the cipher encrypt calls, by amortizing arc4random calls (where both
> function call and lock cost are the dominating factor).
> 
> The ChaCha20 implementation is based on RFC8439 [1], omitting the final
> XOR of the keystream with the plaintext because the plaintext is a
> stream of zeros.  This strategy is similar to what OpenBSD arc4random
> does.
> 
> The arc4random_uniform is based on previous work by Florian Weimer,
> where the algorithm is based on Jérémie Lumbroso paper Optimal Discrete
> Uniform Generation from Coin Flips, and Applications (2013) [2], who
> credits Donald E. Knuth and Andrew C. Yao, The complexity of nonuniform
> random number generation (1976), for solving the general case.
> 
> The main advantage of this method is the that the unit of randomness is not
> the uniform random variable (uint32_t), but a random bit.  It optimizes the
> internal buffer sampling by initially consuming a 32-bit random variable
> and then sampling byte per byte.  Depending of the upper bound requested,
> it might lead to better CPU utilization.
> 
> Checked on x86_64-linux-gnu, aarch64-linux, and powerpc64le-linux-gnu.
> 
> Co-authored-by: Florian Weimer <fweimer@redhat.com>
> 
> [1] https://datatracker.ietf.org/doc/html/rfc8439
> [2] https://arxiv.org/pdf/1304.1916.pdf

And I forgot to address H.J review [1] for this one. I fixed them locally.

[1] https://sourceware.org/pipermail/libc-alpha/2022-April/137973.html

  reply	other threads:[~2022-04-26 12:36 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-25 13:01 [PATCH v4 0/9] Add arc4random support Adhemerval Zanella
2022-04-25 13:01 ` [PATCH v4 1/9] stdlib: Add arc4random, arc4random_buf, and arc4random_uniform (BZ #4417) Adhemerval Zanella
2022-04-26 12:36   ` Adhemerval Zanella [this message]
2022-04-25 13:01 ` [PATCH v4 2/9] stdlib: Add arc4random tests Adhemerval Zanella
2022-04-25 13:01 ` [PATCH v4 3/9] benchtests: Add arc4random benchtest Adhemerval Zanella
2022-04-25 13:01 ` [PATCH v4 4/9] aarch64: Add optimized chacha20 Adhemerval Zanella
2022-04-25 13:01 ` [PATCH v4 5/9] x86: Add SSE2 " Adhemerval Zanella
2022-04-25 13:01 ` [PATCH v4 6/9] x86: Add AVX2 " Adhemerval Zanella
2022-04-25 18:52   ` Noah Goldstein
2022-04-25 13:01 ` [PATCH v4 7/9] powerpc64: Add " Adhemerval Zanella
2022-04-25 13:01 ` [PATCH v4 8/9] s390x: " Adhemerval Zanella
2022-04-25 13:01 ` [PATCH v4 9/9] stdlib: Add TLS optimization to arc4random Adhemerval Zanella

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=a2678d3d-1a32-630e-bba3-190729a27f80@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=fweimer@redhat.com \
    --cc=hjl.tools@gmail.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).