From: "Sam James" <sam@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/gentoo-functions:master commit in: /
Date: Sat, 5 Oct 2024 04:15:22 +0000 (UTC) [thread overview]
Message-ID: <1724243779.b51db5ffb23808dcddf93bdbdfaf3a5ee0e391cc.sam@gentoo> (raw)
commit: b51db5ffb23808dcddf93bdbdfaf3a5ee0e391cc
Author: Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Tue Aug 13 02:08:59 2024 +0000
Commit: Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Wed Aug 21 12:36:19 2024 +0000
URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=b51db5ff
Reduce the two non-bash srandom() implementations to just one
The implementation of srandom() that was written with mksh first and
foremost in mind is no longer as slow as it was. I decided to benchmark
30,000 iterations of both of the non-bash implementations with varying
maximal pool sizes. The results are beneath. Note that both "dash/1" and
"mksh/1" refer to the mksh-targeting implementation.
Pool Size dash/1 dash/2 mksh/1
48 B 6.67s 5.57s 58.84s
64 B 5.39s 4.78s 58.20s
96 B 5.49s 4.36s 58.13s
128 B 5.87s 4.63s 59.94s
160 B 5.93s 5.46s 64.64s
These figures demonstrate that the optimal pool size is roughly between
64 and 96 bytes, and that the performance of both implementations is now
comparable. In addition to testing Linux (6.6) on x86_64 hardware, I
experimented with the pool size on macOS Sonoma (using an Apple M1 CPU)
and found a value of 64 to be close to optimal.
In view of these findings, have _collect_entropy() collect 64 bytes at a
time and remove the marginally faster implementation. That is, the one
that depended on being able to perform arithmetic on a number as high as
2^32-1 without overflowing.
Additionally, increase the maximum number of times that the remaining
implementation tries to find a suitable sequence of hex digits from 2 to
3. Finally, remove the overflow check, for it is no longer required.
Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
functions.sh | 53 ++++++++++++-----------------------------------------
1 file changed, 12 insertions(+), 41 deletions(-)
diff --git a/functions.sh b/functions.sh
index 4598c5b..37ac8c2 100644
--- a/functions.sh
+++ b/functions.sh
@@ -580,12 +580,9 @@ srandom()
{
printf '%d\n' "$(( SRANDOM >> 1 ))"
}
- elif [ -c /dev/urandom ] && [ "$(( 1 << 31 == -2147483648 ))" -eq 1 ]; then
- # The shell implements integers as signed int rather than
- # signed long, contrary to the specification. Therefore, bit
- # shifting cannot be a viable strategy. Instead, try to discern
- # a suitably constrained sequence of 8 hex digits.
-
+ elif [ -c /dev/urandom ]; then
+ # Employ a method which entails searching for 8 consecutive hex
+ # digits, where the first is between 0 and 7.
genfun_int32_pat='[0-7][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]'
unset -v genfun_entropy
@@ -594,7 +591,7 @@ srandom()
local hex i slice
# If the shell has forked, or if it cannot be determined
- # whether it has done so, repopulate the pool with 256
+ # whether it has done so, populate the pool with 64
# bytes worth of fresh entropy.
if ! _update_pid; then
_collect_entropy
@@ -603,7 +600,7 @@ srandom()
eval "genfun_pool_${genfun_pid}=1"
fi || return
- for i in 1 2; do
+ for i in 1 2 3; do
# shellcheck disable=2295
slice=${genfun_entropy%${genfun_int32_pat}*}
if [ "${#slice}" -ne "${#genfun_entropy}" ]; then
@@ -613,10 +610,9 @@ srandom()
hex=${hex%?}
done
break
- elif [ "$i" -eq 1 ]; then
- # The pool is too small to contain a
- # suitable sequence. Refill then try
- # again.
+ elif [ "$i" -lt 3 ]; then
+ # The pool does not contain a suitable
+ # sequence. Refill it then try again.
_collect_entropy
else
false
@@ -624,31 +620,6 @@ srandom()
done &&
printf '%d\n' "0x${hex}"
}
- elif [ -c /dev/urandom ]; then
- unset -v genfun_entropy
-
- srandom()
- {
- local hex
-
- if ! _update_pid; then
- # It cannot be determined whether the shell has
- # forked. Generate a number from 4 bytes worth
- # of fresh entropy.
- hex=$(LC_ALL=C od -vAn -N4 -tx1 /dev/urandom | tr -d '[:space:]')
- test "${#hex}" -eq 8 && printf '%d\n' "$(( 0x${hex} >> 1 ))"
- return
- elif [ "${#genfun_entropy}" -lt 8 ] || ! eval "test \"\${genfun_pool_${genfun_pid}+set}\""; then
- # Either the pool is too small or the shell has
- # forked. Repopulate the pool with 256 bytes
- # worth of fresh entropy.
- _collect_entropy || return
- eval "genfun_pool_${genfun_pid}=1"
- fi
- hex=${genfun_entropy}
- genfun_entropy=${genfun_entropy%????????}
- printf '%d\n' "$(( 0x${hex#"$genfun_entropy"} >> 1 ))"
- }
else
warn "srandom: /dev/urandom doesn't exist as a character device"
return 1
@@ -799,12 +770,12 @@ whenceforth()
#------------------------------------------------------------------------------#
#
-# Collects 256 bytes worth of entropy from /dev/urandom and assigns it to the
-# genfun_entropy variable in the form of 512 hex digits.
+# Collects 64 bytes worth of entropy from /dev/urandom and assigns it to the
+# genfun_entropy variable in the form of 128 hex digits.
#
_collect_entropy() {
- genfun_entropy=$(LC_ALL=C od -vAn -N256 -tx1 /dev/urandom | tr -d '[:space:]')
- test "${#genfun_entropy}" -eq 512
+ genfun_entropy=$(LC_ALL=C od -vAn -N64 -tx1 /dev/urandom | tr -d '[:space:]')
+ test "${#genfun_entropy}" -eq 128
}
#
next reply other threads:[~2024-10-05 4:15 UTC|newest]
Thread overview: 281+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-05 4:15 Sam James [this message]
-- strict thread matches above, loose matches on Subject: below --
2024-10-05 7:25 [gentoo-commits] proj/gentoo-functions:master commit in: / Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-08-11 10:23 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-05 20:39 Sam James
2024-08-05 20:39 Sam James
2024-08-05 2:03 Sam James
2024-08-05 2:02 Sam James
2024-08-05 2:02 Sam James
2024-08-05 2:02 Sam James
2024-08-05 2:02 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-07-08 3:00 Sam James
2024-07-08 2:31 Sam James
2024-07-08 2:31 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-06-25 4:06 Sam James
2024-06-25 4:06 Sam James
2024-06-25 4:06 Sam James
2024-06-25 4:06 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-05-24 6:05 Sam James
2024-05-24 1:18 Sam James
2024-05-24 1:18 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-19 15:27 Sam James
2024-05-19 15:27 Sam James
2024-05-19 15:27 Sam James
2024-05-19 15:27 Sam James
2024-05-18 16:07 Sam James
2024-05-18 16:06 Sam James
2024-05-18 16:06 Sam James
2024-05-18 15:34 Sam James
2024-05-18 15:32 Sam James
2024-05-18 15:32 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-15 10:28 Sam James
2024-05-15 10:28 Sam James
2024-05-14 0:18 Sam James
2024-05-14 0:15 Sam James
2024-05-14 0:12 Sam James
2024-05-14 0:12 Sam James
2024-05-14 0:08 Sam James
2024-05-14 0:08 Sam James
2024-05-14 0:05 Sam James
2024-05-14 0:05 Sam James
2024-05-14 0:05 Sam James
2024-05-14 0:05 Sam James
2024-05-14 0:05 Sam James
2024-02-16 21:35 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-10 7:23 Sam James
2023-06-10 7:23 Sam James
2023-06-10 6:04 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-09 11:17 Sam James
2023-06-09 11:11 Sam James
2023-06-09 11:02 Sam James
2023-06-09 11:02 Sam James
2023-06-09 11:02 Sam James
2023-06-09 11:02 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-17 7:44 Sam James
2023-02-17 7:44 Sam James
2023-02-17 7:44 Sam James
2023-02-17 1:33 Sam James
2023-02-17 1:33 Sam James
2023-02-17 1:33 Sam James
2023-02-15 8:18 Sam James
2023-02-15 7:48 Sam James
2023-02-15 7:46 Sam James
2023-02-15 7:46 Sam James
2023-02-15 7:46 Sam James
2023-02-15 7:46 Sam James
2023-02-15 7:46 Sam James
2023-02-15 7:46 Sam James
2023-02-15 2:24 Sam James
2023-02-15 2:24 Sam James
2023-02-15 2:24 Sam James
2023-02-14 3:40 Sam James
2023-02-14 3:40 Sam James
2023-02-14 3:40 Sam James
2023-02-14 3:40 Sam James
2023-02-14 0:09 Sam James
2023-02-14 0:09 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-12 18:53 Sam James
2023-02-12 18:53 Sam James
2023-02-12 6:53 Sam James
2023-02-12 6:53 Sam James
2023-02-12 6:53 Sam James
2023-02-11 1:43 Sam James
2023-02-11 1:43 Sam James
2023-02-10 6:09 Sam James
2023-02-10 6:09 Sam James
2023-02-10 6:09 Sam James
2023-02-09 3:54 Sam James
2023-02-09 3:54 Sam James
2023-02-08 3:37 Sam James
2023-02-08 1:06 Sam James
2023-02-08 0:03 Sam James
2023-02-08 0:03 Sam James
2023-02-07 23:47 Sam James
2023-02-07 23:42 Sam James
2023-02-07 23:42 Sam James
2023-02-07 23:42 Sam James
2023-02-07 23:42 Sam James
2023-02-07 1:08 Sam James
2023-02-07 1:08 Sam James
2023-02-06 13:47 Sam James
2023-02-06 4:32 Sam James
2023-02-06 4:23 Sam James
2023-02-06 4:19 Sam James
2023-02-06 4:10 Sam James
2023-02-06 4:10 Sam James
2023-02-06 3:59 Sam James
2023-02-06 3:59 Sam James
2023-02-06 3:59 Sam James
2022-07-30 5:48 Sam James
2022-07-29 2:03 Sam James
2022-07-29 2:03 Sam James
2022-07-29 2:03 Sam James
2021-08-30 21:14 Mike Gilbert
2021-08-30 21:14 Mike Gilbert
2020-11-19 18:20 Mike Gilbert
2020-11-19 18:20 Mike Gilbert
2020-11-19 18:20 Mike Gilbert
2020-01-26 23:19 Mike Gilbert
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=1724243779.b51db5ffb23808dcddf93bdbdfaf3a5ee0e391cc.sam@gentoo \
--to=sam@gentoo.org \
--cc=gentoo-commits@lists.gentoo.org \
--cc=gentoo-dev@lists.gentoo.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