From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/rand-0.7.3/src/rngs/thread.rs | 124 +++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 vendor/rand-0.7.3/src/rngs/thread.rs (limited to 'vendor/rand-0.7.3/src/rngs/thread.rs') diff --git a/vendor/rand-0.7.3/src/rngs/thread.rs b/vendor/rand-0.7.3/src/rngs/thread.rs new file mode 100644 index 000000000..91ed4c30a --- /dev/null +++ b/vendor/rand-0.7.3/src/rngs/thread.rs @@ -0,0 +1,124 @@ +// Copyright 2018 Developers of the Rand project. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Thread-local random number generator + +use std::cell::UnsafeCell; +use std::ptr::NonNull; + +use super::std::Core; +use crate::rngs::adapter::ReseedingRng; +use crate::rngs::OsRng; +use crate::{CryptoRng, Error, RngCore, SeedableRng}; + +// Rationale for using `UnsafeCell` in `ThreadRng`: +// +// Previously we used a `RefCell`, with an overhead of ~15%. There will only +// ever be one mutable reference to the interior of the `UnsafeCell`, because +// we only have such a reference inside `next_u32`, `next_u64`, etc. Within a +// single thread (which is the definition of `ThreadRng`), there will only ever +// be one of these methods active at a time. +// +// A possible scenario where there could be multiple mutable references is if +// `ThreadRng` is used inside `next_u32` and co. But the implementation is +// completely under our control. We just have to ensure none of them use +// `ThreadRng` internally, which is nonsensical anyway. We should also never run +// `ThreadRng` in destructors of its implementation, which is also nonsensical. + + +// Number of generated bytes after which to reseed `ThreadRng`. +// According to benchmarks, reseeding has a noticable impact with thresholds +// of 32 kB and less. We choose 64 kB to avoid significant overhead. +const THREAD_RNG_RESEED_THRESHOLD: u64 = 1024 * 64; + +/// The type returned by [`thread_rng`], essentially just a reference to the +/// PRNG in thread-local memory. +/// +/// `ThreadRng` uses the same PRNG as [`StdRng`] for security and performance. +/// As hinted by the name, the generator is thread-local. `ThreadRng` is a +/// handle to this generator and thus supports `Copy`, but not `Send` or `Sync`. +/// +/// Unlike `StdRng`, `ThreadRng` uses the [`ReseedingRng`] wrapper to reseed +/// the PRNG from fresh entropy every 64 kiB of random data. +/// [`OsRng`] is used to provide seed data. +/// +/// Note that the reseeding is done as an extra precaution against side-channel +/// attacks and mis-use (e.g. if somehow weak entropy were supplied initially). +/// The PRNG algorithms used are assumed to be secure. +/// +/// [`ReseedingRng`]: crate::rngs::adapter::ReseedingRng +/// [`StdRng`]: crate::rngs::StdRng +#[derive(Copy, Clone, Debug)] +pub struct ThreadRng { + // inner raw pointer implies type is neither Send nor Sync + rng: NonNull>, +} + +thread_local!( + static THREAD_RNG_KEY: UnsafeCell> = { + let r = Core::from_rng(OsRng).unwrap_or_else(|err| + panic!("could not initialize thread_rng: {}", err)); + let rng = ReseedingRng::new(r, + THREAD_RNG_RESEED_THRESHOLD, + OsRng); + UnsafeCell::new(rng) + } +); + +/// Retrieve the lazily-initialized thread-local random number generator, +/// seeded by the system. Intended to be used in method chaining style, +/// e.g. `thread_rng().gen::()`, or cached locally, e.g. +/// `let mut rng = thread_rng();`. Invoked by the `Default` trait, making +/// `ThreadRng::default()` equivalent. +/// +/// For more information see [`ThreadRng`]. +pub fn thread_rng() -> ThreadRng { + let raw = THREAD_RNG_KEY.with(|t| t.get()); + let nn = NonNull::new(raw).unwrap(); + ThreadRng { rng: nn } +} + +impl Default for ThreadRng { + fn default() -> ThreadRng { + crate::prelude::thread_rng() + } +} + +impl RngCore for ThreadRng { + #[inline(always)] + fn next_u32(&mut self) -> u32 { + unsafe { self.rng.as_mut().next_u32() } + } + + #[inline(always)] + fn next_u64(&mut self) -> u64 { + unsafe { self.rng.as_mut().next_u64() } + } + + fn fill_bytes(&mut self, dest: &mut [u8]) { + unsafe { self.rng.as_mut().fill_bytes(dest) } + } + + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { + unsafe { self.rng.as_mut().try_fill_bytes(dest) } + } +} + +impl CryptoRng for ThreadRng {} + + +#[cfg(test)] +mod test { + #[test] + fn test_thread_rng() { + use crate::Rng; + let mut r = crate::thread_rng(); + r.gen::(); + assert_eq!(r.gen_range(0, 1), 0); + } +} -- cgit v1.2.3