Expand description
lib-Q - Post-Quantum Cryptography Library
A modern, secure cryptography library built exclusively with NIST-approved post-quantum algorithms. Written in Rust with WASM compilation support.
§Architecture Principles
- Zero Dynamic Allocations: Stack-only operations for constrained environments
- Memory Safety: Automatic zeroization of sensitive data using
Zeroizetrait - Constant-Time: Operations designed to prevent timing attacks
- Post-Quantum Only: NIST-approved algorithms for quantum resistance
- Provider Pattern: Pluggable cryptographic implementations
- Unified API: Same interface for Rust crate and WASM usage
§Security Features
- Four-Tier Security: Level 1 (128-bit), Level 3 (192-bit), Level 4 (256-bit), Level 5 (256-bit+)
- Algorithm Diversity: ML-KEM, HQC, ML-DSA, FN-DSA, Saturnin, Romulus (N/M)
- Input Validation: Comprehensive validation of all cryptographic inputs
- Error Handling: Secure error messages that don’t leak sensitive information
- AEAD Layer B: The
libq::aead::context()/ WASM AEAD context path stays Layer A (Resultonly). Forlib_q_core::AeadDecryptSemantic::decrypt_semantic, depend on concrete types fromlib-q-aead,lib-q-saturnin,lib-q-duplex-aead,lib-q-tweak-aead, orlib-q-romulus(seedocs/adr/003-aead-decrypt-layers.md).
§Example Usage
use libq::{
Algorithm,
HashContext,
Utils,
create_hash_context,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize hash context
let mut hash_ctx = create_hash_context();
// Hash data via the umbrella-wired `lib-q-hash` provider
let result = hash_ctx.hash(Algorithm::Shake256, b"Hello, World!");
match result {
Ok(hash) => println!("Hash: {}", Utils::bytes_to_hex(&hash)),
Err(e) => println!("Hash error: {:?}", e),
}
// Generate random bytes
let random_bytes = Utils::random_bytes(32)?;
println!("Random bytes: {}", Utils::bytes_to_hex(&random_bytes));
// Note: Algorithm operations require feature flags:
// - For ML-DSA signatures: enable 'ml-dsa' feature
// - For ML-KEM key exchange: enable 'ml-kem' feature
// - For FN-DSA signatures: enable 'fn-dsa' feature
// - For AEAD: use `libq::aead::context()` and enable `saturnin`, `romulus`, or other AEAD features
Ok(())
}§Feature Flags
std: Enable standard library features (default)no_std: Marker feature; this crate uses#![no_std]whenstdis off, but path dependencies may still enablestd(see crate README). For embedded builds, prefer leaf crates (lib-q-core,lib-q-kem, …) with--no-default-featuresandalloc.wasm: Enable WebAssembly compilation supportml-kem: Enable ML-KEM key encapsulation mechanismml-dsa: Enable ML-DSA digital signature algorithmslh-dsa: Enable SLH-DSA (FIPS 205) algorithm metadata and shared types inlib-q-corefn-dsa: Enable FN-DSA digital signature algorithmsaturnin: Enable Saturnin authenticated encryptionromulus: Enable Romulus-N and Romulus-M AEAD (LWC / SKINNY-128-384+)hqc: Enable HQC key encapsulation mechanism (HQC-128 / HQC-192 / HQC-256)random: Enable lib-q-random for secure random number generationrandom-custom-entropy: Enable custom entropy source supportall-algorithms: Enable all available algorithmszkp: Expose zero-knowledge / STARK API underlibq::zkp(STARK core is always linked)zkp-plonky/zkp-plonky-*: Add the Plonky3-derived STARK stack underlibq::zkp::plonkyzkp-parallel: Rayon-backed parallel proving (STARK)zkp-recursive-experimental: Experimental recursive proof Merkle path (STARK)security-hardened: Enable comprehensive security features
§Security Considerations
This library is designed with security as the primary concern:
- All sensitive data is automatically zeroized when dropped
- Operations are designed to be constant-time where possible
- Input validation is comprehensive and secure
- Error messages don’t leak sensitive information
- Memory allocations are minimized for constrained environments
§WASM Support
The library can be compiled to WebAssembly for use in web applications:
wasm-pack build --target web --out-dir pkgFor getrandom on wasm32-unknown-unknown, use the same flags as CI:
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUSTFLAGS='--cfg getrandom_backend="wasm_js" -C panic=abort'.
This provides a JavaScript API that mirrors the Rust API where wasm-bindgen is enabled.
Modules§
- aead
- AEAD backed by
lib-q-aead(SHAKE256-AEAD and feature-gated algorithms). - wasm
- WebAssembly bindings for lib-Q
- zkp
- Zero-knowledge proof types and functions.
Structs§
- Aead
Context - AEAD context for authenticated encryption operations
- Core
LibQ Crypto Provider - Main lib-Q cryptographic provider
- Custom
Entropy Config - Custom entropy source configuration
- Custom
Entropy Source - Custom entropy source registration
- Entropy
Context - Context data for entropy callbacks
- Entropy
Quality - Entropy quality assessment result
- Entropy
Validator - Comprehensive entropy validator
- Hash
Context - Hash context for hash operations
- KemContext
- KEM context for key encapsulation operations
- LibQ
CbKem Provider - lib-Q Classical McEliece KEM provider implementation
- LibQ
Crypto Provider - Main lib-Q cryptographic provider
- LibQ
Hash Provider - lib-Q hash provider implementation
- LibQ
HqcProvider - HQC provider for libQ integration
- LibQ
KemProvider - lib-Q KEM provider implementation
- LibQRng
- Main libQ random number generator
- LibQ
Signature Provider - lib-Q signature provider implementation
- Security
Validator - Centralized security validator for lib-Q
- Signature
Context - Signature context for digital signature operations
- Utils
- The core API provides a clean interface that:
Enums§
- Algorithm
- Algorithm identifiers for cryptographic operations
- Algorithm
Category - Algorithm categories
- Custom
Entropy Quality - Entropy source quality levels
- Decrypt
Semantic Outcome - Semantic result of an AEAD decrypt after parseable inputs were accepted.
- Error
- The error type for lib-Q operations
- Security
Level - Security levels for cryptographic algorithms
Constants§
Traits§
- Aead
Decrypt Semantic - AEAD implementations that expose a semantic decrypt API (Layer B).
Functions§
- algorithms_
by_ category - Get algorithms by category
- algorithms_
by_ security_ level - Get algorithms by security level
- available_
algorithms - Get available KEM algorithms (no_std version)
- create_
hash_ context - Create a
HashContextwithLibQHashProviderinstalled. - create_
kem_ context - Create a new KEM context
- create_
signature - Legacy boxed
Signaturefactory (stdonly; matcheslib-q-sig/Box<dyn Signature>). Create a signature instance by algorithm name (legacy compatibility) - create_
signature_ context - Create a
SignatureContextwithLibQSignatureProvideralready installed (ML-DSA and SLH-DSA fromlib-q-sigdefaults; FN-DSA when thefn-dsafeature is enabled on this crate). - get_
custom_ entropy_ source_ info - Get information about the currently registered entropy source
- has_
custom_ entropy_ source - Check if a custom entropy source is currently registered
- init
- Initialize the core library
- new_
custom_ rng - Create a new RNG with custom entropy source
- new_
deterministic_ rng - Create a new deterministic RNG instance
- new_
secure_ rng - Create a new secure RNG instance
- register_
custom_ ⚠entropy_ source - Register a custom entropy source for the current thread
- sig_
available_ algorithms - Get available signature algorithms with proper NIST naming
- supported_
algorithms - Get all supported algorithms
- unregister_
custom_ entropy_ source - Unregister the current custom entropy source
- version
- Get library version information
Type Aliases§
- Result
- Result type for lib-Q operations