public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Noah Goldstein <goldstein.w.n@gmail.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: "H.J. Lu" <hjl.tools@gmail.com>,
	GNU C Library <libc-alpha@sourceware.org>
Subject: Re: [PATCH v1 1/2] random-bits: Factor out entropy generating function
Date: Tue, 29 Mar 2022 15:52:46 -0500	[thread overview]
Message-ID: <CAFUsyfK00i9eqV=6jZXjR5YOjzvmRa2kq3heWSYZWgy=MDOboA@mail.gmail.com> (raw)
In-Reply-To: <1802122d-9160-8596-b22c-4c909df9a68d@linaro.org>

On Tue, Mar 29, 2022 at 3:44 PM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
>
>
> On 29/03/2022 17:14, H.J. Lu wrote:
> > On Tue, Mar 29, 2022 at 12:56 PM Noah Goldstein via Libc-alpha
> > <libc-alpha@sourceware.org> wrote:
> >>
> >> On Tue, Mar 29, 2022 at 2:51 PM Adhemerval Zanella
> >> <adhemerval.zanella@linaro.org> wrote:
> >>>
> >>>
> >>>
> >>> On 28/03/2022 19:09, Noah Goldstein via Libc-alpha wrote:
> >>>> On some architectures `clock_gettime` is undesirable as
> >>>> it may use a syscall or there may be a faster alternative.
> >>>> Future architecture specific functions can be added in
> >>>> sysdeps/<arch>/random-bits-entropy.h to provide a version of
> >>>> 'random_bits_entropy' that doesn't use 'clock_gettime'.
> >>>> ---
> >>>>  include/random-bits.h                 | 16 ++++++--------
> >>>>  sysdeps/generic/random-bits-entropy.h | 31 +++++++++++++++++++++++++++
> >>>>  2 files changed, 37 insertions(+), 10 deletions(-)
> >>>>  create mode 100644 sysdeps/generic/random-bits-entropy.h
> >>>>
> >>>> diff --git a/include/random-bits.h b/include/random-bits.h
> >>>> index 17665b479a..016b87576c 100644
> >>>> --- a/include/random-bits.h
> >>>> +++ b/include/random-bits.h
> >>>> @@ -19,21 +19,17 @@
> >>>>  #ifndef _RANDOM_BITS_H
> >>>>  # define _RANDOM_BITS_H
> >>>>
> >>>> -#include <time.h>
> >>>> -#include <stdint.h>
> >>>> +# include <random-bits-entropy.h>
> >>>> +# include <stdint.h>
> >>>>
> >>>> -/* Provides fast pseudo-random bits through clock_gettime.  It has unspecified
> >>>> -   starting time, nano-second accuracy, its randomness is significantly better
> >>>> -   than gettimeofday, and for mostly architectures it is implemented through
> >>>> -   vDSO instead of a syscall.  Since the source is a system clock, the upper
> >>>> -   bits will have less entropy. */
> >>>> +/* Provides fast pseudo-random bits through architecture specific
> >>>> +   random_bits_entropy.  Expectation is source is some timing function so
> >>>> +   the upper bits have less entropy.  */
> >>>>  static inline uint32_t
> >>>>  random_bits (void)
> >>>>  {
> >>>> -  struct __timespec64 tv;
> >>>> -  __clock_gettime64 (CLOCK_MONOTONIC, &tv);
> >>>> +  uint32_t ret = random_bits_entropy ();
> >>>>    /* Shuffle the lower bits to minimize the clock bias.  */
> >>>> -  uint32_t ret = tv.tv_nsec ^ tv.tv_sec;
> >>>>    ret ^= (ret << 24) | (ret >> 8);
> >>>>    return ret;
> >>>>  }
> >>>
> >>> We already provide hp-timing.h, which uses rdtsc on x86 and clock_gettime on
> >>> generic interface (and other high precision timing on other architectures).
> >>> So I think a better way would be to:
> >>
> >> For x86/generic that works but other architectures also have hp-timing
> >> implementations that might not be suitable for this (i.e there might be
> >> an entropy regression).
> >
> > The default hp-timing.h has
> >
> > # define HP_TIMING_NOW(var) \
> > ({ \
> >   struct __timespec64 tv; \
> >   __clock_gettime64 (CLOCK_MONOTONIC, &tv); \
> >   (var) = (tv.tv_nsec + UINT64_C(1000000000) * tv.tv_sec); \
> > })
> >
> > It isn't the same as the current include/random-bits.h.
>
> Maybe refactor hp-timing.h to add a routine to get the system clock without
> any adjustments?  I don't have a strong preference here, but I take that
> you are not aiming to use RDRAND or similar instruction, since you are
> optimizing to latency.  So I see that using hp-timing.h seems the best
> approach, since it should work similar on different architectures (and
> each one might disable if the entropy is not large enough).

That would work. But we would still need to hardcode the random-bits needs
in some cases.

For example in generic we would still need to combine seconds and nanoseconds
and ideally wouldn't use multiply for that.

At that point it seems we are doing something logically different
enough it would
make more sense to just include <hp-timing.h> in <random-bits-entropy.h> if
it was appropriate for the architecture.

  reply	other threads:[~2022-03-29 20:52 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-28 22:09 Noah Goldstein
2022-03-28 22:09 ` [PATCH v1 2/2] x86: Use rdtsc for generating entropy for random_bits Noah Goldstein
2022-03-29 19:51 ` [PATCH v1 1/2] random-bits: Factor out entropy generating function Adhemerval Zanella
2022-03-29 19:56   ` Noah Goldstein
2022-03-29 20:04     ` Noah Goldstein
2022-03-29 20:14     ` H.J. Lu
2022-03-29 20:44       ` Adhemerval Zanella
2022-03-29 20:52         ` Noah Goldstein [this message]
2022-03-29 20:37     ` Adhemerval Zanella
2022-03-29 20:44       ` Noah Goldstein
2022-03-30 15:37         ` Adhemerval Zanella
2022-03-30 16:30           ` Noah Goldstein
2022-03-30 19:38             ` Cristian Rodríguez
2022-03-31  4:45               ` Jason A. Donenfeld
2022-03-31 10:08                 ` Cristian Rodríguez
2022-03-31 11:17                   ` Adhemerval Zanella
2022-03-31 11:25                     ` Cristian Rodríguez
2022-03-31 11:48                       ` Adhemerval Zanella
2022-03-31 12:14                         ` Cristian Rodríguez
2022-03-31 13:12                           ` Yann Droneaud
2022-03-31 15:31                     ` Jason A. Donenfeld
2022-03-31 18:16                       ` Noah Goldstein
2022-03-31 21:57                       ` Cristian Rodríguez
2022-03-31 22:33                         ` Noah Goldstein
2022-03-31 22:51                         ` Jason A. Donenfeld
2022-03-31 23:05                           ` Noah Goldstein
2022-03-31 23:25                             ` Jason A. Donenfeld
2022-04-01 18:01                             ` Cristian Rodríguez
2022-04-04 17:42                               ` Adhemerval Zanella
2022-04-04 18:23                                 ` Noah Goldstein
2022-04-04 18:38                                   ` Adhemerval Zanella
2022-04-04 18:52                                     ` Noah Goldstein
2022-04-04 19:20                                       ` Adhemerval Zanella
2022-04-04 19:48                                         ` Noah Goldstein
2022-04-04 19:57                                           ` Adhemerval Zanella
2022-04-04 14:51               ` Florian Weimer
2022-04-04 14:54                 ` Jason A. Donenfeld
2022-04-04 15:00                   ` Florian Weimer
2022-04-04 16:51                     ` Noah Goldstein
2022-04-04 17:22                       ` Adhemerval Zanella
2022-04-04 18:32                       ` Jason A. Donenfeld
2022-04-04 19:16                         ` Noah Goldstein
2022-04-05  0:10                         ` Cristian Rodríguez
2022-04-05  0:18                           ` Jason A. Donenfeld
2022-04-05 13:45                             ` Cristian Rodríguez
2022-04-05  9:22                       ` Florian Weimer
2022-04-04 18:28                     ` Jason A. Donenfeld
2022-04-05  9:20                       ` Florian Weimer

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='CAFUsyfK00i9eqV=6jZXjR5YOjzvmRa2kq3heWSYZWgy=MDOboA@mail.gmail.com' \
    --to=goldstein.w.n@gmail.com \
    --cc=adhemerval.zanella@linaro.org \
    --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).