Skip to main content

LibQRng

Struct LibQRng 

Source
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

Source

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);
Source

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);
Source

pub fn new_deterministic_from_u64(seed: u64) -> Self

Create a deterministic RNG from a u64 test seed (SplitMix64 → KT128).

Source

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.

Source

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);
Source

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.

Source

pub fn is_deterministic(&self) -> bool

Check if this RNG is deterministic

Source

pub fn security_level(&self) -> SecurityLevel

Get the security level of this RNG

Source

pub fn entropy_source_name(&self) -> &'static str

Get the entropy source name

Source

pub fn entropy_source_type(&self) -> EntropySourceType

Get the entropy source type

Source

pub fn reseed_counter(&self) -> u32

Get the reseed counter

Source

pub fn bytes_generated(&self) -> usize

Get the bytes generated since last reseed

Source

pub fn is_secure(&self) -> bool

Check if this RNG is cryptographically secure

Source

pub fn entropy_quality(&self) -> f64

Get the entropy quality estimate (0.0 to 1.0)

Source§

impl LibQRng

Source

pub fn fill<T>(&mut self, dest: &mut [T])
where T: Copy + Default,

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 Display for LibQRng

Available on crate feature alloc only.
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl SecureRng for LibQRng

Available on crate feature alloc only.
Source§

fn fill_bytes_secure(&mut self, dest: &mut [u8]) -> Result<()>

Fill the buffer with cryptographically secure random bytes Read more
Source§

fn next_u32_secure(&mut self) -> Result<u32>

Generate a random u32 with security validation Read more
Source§

fn next_u64_secure(&mut self) -> Result<u64>

Generate a random u64 with security validation Read more
Source§

fn initialize(&mut self, entropy: &[u8]) -> Result<()>

Initialize the RNG with entropy (if supported) Read more
Source§

fn is_secure(&self) -> bool

Check if the RNG is cryptographically secure Read more
Source§

fn entropy_quality(&self) -> f64

Get the entropy quality estimate (0.0 to 1.0) Read more
Source§

fn security_level(&self) -> SecurityLevel

Get the RNG’s security level Read more
Source§

fn reseed(&mut self) -> Result<()>

Reseed the RNG with fresh entropy Read more
Source§

fn state_size(&self) -> usize

Get the RNG’s internal state size Read more
Source§

fn reseed_interval(&self) -> Option<usize>

Get the RNG’s reseed interval Read more
Source§

impl TryRng for LibQRng

Available on crate feature alloc only.
Source§

type Error = Infallible

The type returned in the event of a RNG error. Read more
Source§

fn try_next_u32(&mut self) -> Result<u32, Self::Error>

Return the next random u32.
Source§

fn try_next_u64(&mut self) -> Result<u64, Self::Error>

Return the next random u64.
Source§

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error>

Fill dst entirely with random data.
Source§

impl TryCryptoRng for LibQRng

Available on crate feature alloc only.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<R> TryRngCore for R
where R: TryRng,

Source§

type Error = <R as TryRng>::Error

👎Deprecated since 0.10.0:

use TryRng instead

Error type.
Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<R> Rng for R
where R: TryRng<Error = Infallible> + ?Sized,

Source§

fn next_u32(&mut self) -> u32

Return the next random u32.
Source§

fn next_u64(&mut self) -> u64

Return the next random u64.
Source§

fn fill_bytes(&mut self, dst: &mut [u8])

Fill dest with random data. Read more
Source§

impl<R> RngExt for R
where R: Rng + ?Sized,

Source§

fn random<T>(&mut self) -> T

Return a random value via the StandardUniform distribution. Read more
Source§

fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>

Return an iterator over random variates Read more
Source§

fn random_range<T, R>(&mut self, range: R) -> T
where T: SampleUniform, R: SampleRange<T>,

Generate a random value in the given range. Read more
Source§

fn random_bool(&mut self, p: f64) -> bool

Return a bool with a probability p of being true. Read more
Source§

fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool

Return a bool with a probability of numerator/denominator of being true. Read more
Source§

fn sample<T, D>(&mut self, distr: D) -> T
where D: Distribution<T>,

Sample a new value, using the given distribution. Read more
Source§

fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>
where D: Distribution<T>, Self: Sized,

Create an iterator that generates values using the given distribution. Read more
Source§

fn fill<T>(&mut self, dest: &mut [T])
where T: Fill,

Fill any type implementing Fill with random data Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<R> CryptoRng for R
where R: TryCryptoRng<Error = Infallible> + ?Sized,

Source§

impl<R> RngCore for R
where R: Rng,