pub struct LibQRng { /* private fields */ }Expand description
Main libQ random number generator
This is the primary RNG implementation for the libQ ecosystem, providing a unified interface for secure random number generation across different platforms and use cases.
Implementations§
Source§impl LibQRng
impl LibQRng
Sourcepub fn new_secure() -> Result<Self>
pub fn new_secure() -> Result<Self>
Create a new secure RNG using the best available entropy source
This method creates a cryptographically secure RNG using the highest quality entropy source available on the current platform.
§Errors
Returns an error if no secure entropy source is available.
§Examples
use lib_q_random::LibQRng;
use rand_core::Rng;
let mut rng = LibQRng::new_secure().unwrap();
let mut bytes = [0u8; 32];
rng.fill_bytes(&mut bytes);Sourcepub fn new_deterministic(seed: [u8; 32]) -> Self
pub fn new_deterministic(seed: [u8; 32]) -> Self
Create a new deterministic RNG for testing
Initializes a KT128 (KangarooTwelve) XOF byte stream from a 256-bit seed.
Suitable for KATs and regression
tests. Unpredictability is only as strong as the seed: this is not a
substitute for Self::new_secure in production.
§Arguments
seed- 32-byte seed; must be chosen explicitly for tests
§Examples
use lib_q_random::LibQRng;
use rand_core::Rng;
let mut rng = LibQRng::new_deterministic([1; 32]);
let mut bytes = [0u8; 32];
rng.fill_bytes(&mut bytes);Sourcepub fn new_deterministic_from_u64(seed: u64) -> Self
pub fn new_deterministic_from_u64(seed: u64) -> Self
Create a deterministic RNG from a u64 test seed (SplitMix64 → KT128).
Sourcepub fn new_deterministic_saturnin(seed: [u8; 32]) -> Result<Self>
pub fn new_deterministic_saturnin(seed: [u8; 32]) -> Result<Self>
Create a deterministic RNG using Saturnin CTR keystream (deterministic-saturnin feature).
Requires alloc. Uses domain crate::kt128_expander::DOMAIN_LIBQ_DET_SATURNIN for the CTR nonce.
§Errors
Returns an error if Saturnin keystream generation fails.
Sourcepub fn new_custom<T: EntropySource + 'static>(entropy_source: T) -> Self
pub fn new_custom<T: EntropySource + 'static>(entropy_source: T) -> Self
Create a new RNG with a custom entropy source
This method allows creating an RNG with a custom entropy source, useful for specialized applications or testing.
§Arguments
entropy_source- Custom entropy source implementation
§Examples
use lib_q_random::LibQRng;
use lib_q_random::entropy::UserEntropySource;
use rand_core::Rng;
let entropy_data = vec![1, 2, 3, 4, 5, 6, 7, 8];
let entropy_source = UserEntropySource::new(entropy_data);
let mut rng = LibQRng::new_custom(entropy_source);Sourcepub fn with_config(config: &RngConfig) -> Result<Self>
pub fn with_config(config: &RngConfig) -> Result<Self>
Create a new RNG with custom configuration
This method allows creating an RNG with specific configuration parameters for specialized use cases.
§Arguments
config- RNG configuration parameters
§Errors
Returns an error if the configuration is invalid or if the RNG cannot be created with the specified parameters.
Sourcepub fn is_deterministic(&self) -> bool
pub fn is_deterministic(&self) -> bool
Check if this RNG is deterministic
Sourcepub fn security_level(&self) -> SecurityLevel
pub fn security_level(&self) -> SecurityLevel
Get the security level of this RNG
Sourcepub fn entropy_source_name(&self) -> &'static str
pub fn entropy_source_name(&self) -> &'static str
Get the entropy source name
Sourcepub fn entropy_source_type(&self) -> EntropySourceType
pub fn entropy_source_type(&self) -> EntropySourceType
Get the entropy source type
Sourcepub fn reseed_counter(&self) -> u32
pub fn reseed_counter(&self) -> u32
Get the reseed counter
Sourcepub fn bytes_generated(&self) -> usize
pub fn bytes_generated(&self) -> usize
Get the bytes generated since last reseed
Sourcepub fn entropy_quality(&self) -> f64
pub fn entropy_quality(&self) -> f64
Get the entropy quality estimate (0.0 to 1.0)
Source§impl LibQRng
impl LibQRng
Sourcepub fn fill<T>(&mut self, dest: &mut [T])
pub fn fill<T>(&mut self, dest: &mut [T])
Fill a slice with random values of any integer type
This method provides a convenient way to fill slices of different integer types with random values, handling the byte conversion internally.
§Examples
use lib_q_random::LibQRng;
let mut rng = LibQRng::new_secure().unwrap();
let mut u16_array = [0u16; 10];
rng.fill(&mut u16_array);Trait Implementations§
Source§impl SecureRng for LibQRng
Available on crate feature alloc only.
impl SecureRng for LibQRng
alloc only.Source§fn fill_bytes_secure(&mut self, dest: &mut [u8]) -> Result<()>
fn fill_bytes_secure(&mut self, dest: &mut [u8]) -> Result<()>
Source§fn next_u32_secure(&mut self) -> Result<u32>
fn next_u32_secure(&mut self) -> Result<u32>
Source§fn next_u64_secure(&mut self) -> Result<u64>
fn next_u64_secure(&mut self) -> Result<u64>
Source§fn initialize(&mut self, entropy: &[u8]) -> Result<()>
fn initialize(&mut self, entropy: &[u8]) -> Result<()>
Source§fn entropy_quality(&self) -> f64
fn entropy_quality(&self) -> f64
Source§fn security_level(&self) -> SecurityLevel
fn security_level(&self) -> SecurityLevel
Source§fn state_size(&self) -> usize
fn state_size(&self) -> usize
Source§impl TryRng for LibQRng
Available on crate feature alloc only.
impl TryRng for LibQRng
alloc only.Source§type Error = Infallible
type Error = Infallible
impl TryCryptoRng for LibQRng
alloc only.Auto Trait Implementations§
impl Freeze for LibQRng
impl !RefUnwindSafe for LibQRng
impl Send for LibQRng
impl Sync for LibQRng
impl Unpin for LibQRng
impl UnsafeUnpin for LibQRng
impl !UnwindSafe for LibQRng
Blanket Implementations§
Source§impl<R> TryRngCore for Rwhere
R: TryRng,
impl<R> TryRngCore for Rwhere
R: TryRng,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<R> RngExt for R
impl<R> RngExt for R
Source§fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
StandardUniform distribution. Read moreSource§fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>
fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>
Source§fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
Source§fn random_bool(&mut self, p: f64) -> bool
fn random_bool(&mut self, p: f64) -> bool
p of being true. Read moreSource§fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
numerator/denominator of being
true. Read more