vouch-core 4.2.0

Vouch Protocol canonical core: JCS canonicalization, Ed25519 + ML-DSA proofs, did:key/multikey, Data Integrity (eddsa-jcs-2022), credentials, delegation, revocation. One byte-exact implementation shared by every language SDK via WASM and UniFFI/C-FFI.
Documentation
//! Where the core gets random bytes.
//!
//! With `std` they come from the operating system (getrandom; on WASM, the
//! JavaScript crypto API). A `no_std` build has no source the core can trust
//! on every board, so anything that needs fresh randomness returns an error
//! there, and the caller passes the random bytes in instead (a seed for
//! [`crate::keys::Ed25519KeyPair::from_seed`], a host nonce, a `stateKey`).
//! Verification never needs randomness, so it works the same in both builds.

#[allow(unused_imports)]
use crate::prelude::*;

/// Fill `buf` from the operating system's random source.
#[cfg(feature = "std")]
pub(crate) fn fill(buf: &mut [u8]) -> core::result::Result<(), getrandom::Error> {
    getrandom::getrandom(buf)
}

/// A random generator for crates that take one (`rand_core`).
#[cfg(feature = "std")]
pub(crate) fn rng() -> crate::Result<rand_core::OsRng> {
    Ok(rand_core::OsRng)
}

/// The refusal a `no_std` build gives where it would need fresh randomness.
#[cfg(not(feature = "std"))]
#[derive(Debug, Clone, Copy)]
pub(crate) struct NoRandomSource;

#[cfg(not(feature = "std"))]
impl core::fmt::Display for NoRandomSource {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str("no random source in a no_std build: pass the random bytes in")
    }
}

#[cfg(not(feature = "std"))]
pub(crate) fn fill(_buf: &mut [u8]) -> core::result::Result<(), NoRandomSource> {
    Err(NoRandomSource)
}

/// A generator that can never be made, so code written against
/// `rand_core::RngCore` still builds without `std` and never runs.
#[cfg(not(feature = "std"))]
pub(crate) enum NoRng {}

#[cfg(not(feature = "std"))]
impl rand_core::RngCore for NoRng {
    fn next_u32(&mut self) -> u32 {
        match *self {}
    }
    fn next_u64(&mut self) -> u64 {
        match *self {}
    }
    fn fill_bytes(&mut self, _dest: &mut [u8]) {
        match *self {}
    }
    fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> core::result::Result<(), rand_core::Error> {
        match *self {}
    }
}

#[cfg(not(feature = "std"))]
impl rand_core::CryptoRng for NoRng {}

#[cfg(not(feature = "std"))]
pub(crate) fn rng() -> crate::Result<NoRng> {
    Err(crate::CoreError::Crypto(format!("rng: {NoRandomSource}")))
}