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<LibQRng, Error>

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]) -> LibQRng

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) -> LibQRng

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

Source

pub fn new_custom<T>(entropy_source: T) -> LibQRng
where T: EntropySource + 'static,

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<LibQRng, Error>

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<(), Error>

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<(), Error>

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

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

Generate a random u32 with security validation Read more
Source§

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

Generate a random u64 with security validation Read more
Source§

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

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<(), Error>

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

Available on crate feature alloc only.
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, <LibQRng as TryRng>::Error>

Return the next random u32.
Source§

fn try_next_u64(&mut self) -> Result<u64, <LibQRng as TryRng>::Error>

Return the next random u64.
Source§

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

Fill dst entirely with random data.

Auto Trait Implementations§

Blanket Implementations§

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<R> CryptoRng for R
where R: TryCryptoRng<Error = Infallible> + ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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> RngCore for R
where R: Rng,

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> 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<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more