public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Noah Goldstein <goldstein.w.n@gmail.com>
To: libc-alpha@sourceware.org
Subject: [PATCH v1 2/2] x86: Use rdtsc for generating entropy for random_bits
Date: Mon, 28 Mar 2022 17:09:36 -0500	[thread overview]
Message-ID: <20220328220936.2724834-2-goldstein.w.n@gmail.com> (raw)
In-Reply-To: <20220328220936.2724834-1-goldstein.w.n@gmail.com>

`rdtsc` generates a comparable amount of entropy and is faster than
`clock_gettime`.

Entropy:
random_bits w/ rdtsc:
    Entropy = 7.805972 bits per byte.

random_bits w/ clock_gettime:
    Entropy = 7.806116 bits per byte.

Measured entropy of `random_bits` using `rdtsc` vs `clock_gettime`
using ent: https://www.fourmilab.ch/random/

Performance:
random_bits w/:   rdtsc,  clock_gettime

    throughput:   16.35,  38.36
       latency:   23.68,  54.15

Time as cycles (measured with `rdtsc`) / Trials. benchmarks done on
Tigerlake.
---
 sysdeps/x86_64/random-bits-entropy.h | 36 ++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 sysdeps/x86_64/random-bits-entropy.h

diff --git a/sysdeps/x86_64/random-bits-entropy.h b/sysdeps/x86_64/random-bits-entropy.h
new file mode 100644
index 0000000000..1e96659aa3
--- /dev/null
+++ b/sysdeps/x86_64/random-bits-entropy.h
@@ -0,0 +1,36 @@
+/* Function for generating entropy quick on x86 with rdtsc.
+   Copyright (C) 2022 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <stdint.h>
+
+/* rdtsc is faster than generic (clock_gettime) and has roughly the same
+   entropy. Result of filling a file using random_bits with rdtsc vs
+   clock_gettime as an entropy function:
+
+   $> ent gettime-random.txt
+      Entropy = 7.806116 bits per byte.
+
+   $> ent rdtsc-random.txt
+      Entropy = 7.805972 bits per byte.
+ */
+
+static inline uint32_t
+random_bits_entropy (void)
+{
+  return __builtin_ia32_rdtsc ();
+}
-- 
2.25.1


  reply	other threads:[~2022-03-28 22:09 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-28 22:09 [PATCH v1 1/2] random-bits: Factor out entropy generating function Noah Goldstein
2022-03-28 22:09 ` Noah Goldstein [this message]
2022-03-29 19:51 ` 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
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=20220328220936.2724834-2-goldstein.w.n@gmail.com \
    --to=goldstein.w.n@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).