# Rusty Crystals A Rust implementation of the ML-DSA (CRYSTALS-Dilithium) and SLH-DSA (SPHINCS+) post-quantum digital signature schemes with hierarchical deterministic (HD) wallet support. This workspace provides post-quantum cryptographic primitives and HD wallet functionality compatible with BIP-32, BIP-39, and BIP-44 standards. ## Security Features This implementation provides enterprise-grade memory security: - **Compile-time copy prevention on secrets** - `SensitiveBytes32`, `SensitiveBytes64`, `Keypair`, `SecretKey`, `ExtendedPrivKey`, and `WormholePair` deliberately do **not** implement `Clone`. Secret material can only be moved. Any duplication must be explicit via `to_bytes()`/`from_bytes()` round-trips, making the copy visible at the call site. - **Automatic memory zeroization** - Source arrays passed to constructors and all wrapper contents are zeroed on drop (`ZeroizeOnDrop`). Mnemonic strings passed to `mnemonic_to_seed` are wrapped in `Zeroizing` and scrubbed on **every** exit path, including parse failure and panic unwind. - **Explicit sensitive data handling** - APIs require `(&mut entropy).into()` syntax, making sensitive operations obvious and zeroing the caller's buffer at the point of transfer. - **Bounded derivation paths** - `derive_key_from_seed` and `generate_wormhole_from_seed` reject paths whose byte length exceeds `MAX_DERIVATION_PATH_BYTES` (256) or whose segment count exceeds `MAX_DERIVATION_DEPTH` (16) **before** any allocation or HMAC work, preventing DoS via attacker-controlled deep paths. ```rust // Secure by design - entropy is zeroized after conversion let mut entropy = [0u8; 32]; getrandom::getrandom(&mut entropy).unwrap(); let keypair = ml_dsa_87::Keypair::generate(&mut (&mut entropy).into()); // entropy is now [0,0,0,...] - no sensitive data left in memory // If you genuinely need a second copy of a keypair, you must opt in explicitly: let kp_bytes = keypair.to_bytes(); let keypair2 = ml_dsa_87::Keypair::from_bytes(&kp_bytes)?; // Prefer passing &Keypair instead of duplicating secrets. ``` This eliminates entire classes of security vulnerabilities related to sensitive data handling in cryptographic applications. ## Overview This workspace contains three crates: - **`qp-rusty-crystals-dilithium`** - ML-DSA digital signature implementation - **`qp-rusty-crystals-sphincs`** - SLH-DSA digital signature implementation - **`qp-rusty-crystals-hdwallet`** - HD wallet for post-quantum keys ## Usage ### ML-DSA Digital Signatures ```toml [dependencies] # Default feature enables ML-DSA-87. Add ml-dsa-44 / ml-dsa-65 as needed. qp-rusty-crystals-dilithium = "3.0.1" getrandom = "0.2" # For secure entropy generation if needed ``` **Security Note**: When generating entropy for cryptographic operations, always use cryptographically secure random sources. Never use predictable strings, timestamps, or user input as entropy. ```rust use qp_rusty_crystals_dilithium::ml_dsa_87; // Generate secure entropy let mut entropy = [0u8; 32]; getrandom::getrandom(&mut entropy).expect("Failed to generate entropy"); // Generate keypair let keypair = ml_dsa_87::Keypair::generate(&mut (&mut entropy).into()); // Sign message let message = b"Hello, post-quantum world!"; let signature = keypair.sign(message, None, None).expect("Signing should succeed"); // Verify signature let is_valid = keypair.verify(message, signature.as_slice(), None); ``` ### HD Wallet ```toml [dependencies] qp-rusty-crystals-hdwallet = "1.0.0" ``` ```rust use qp_rusty_crystals_hdwallet::{generate_mnemonic, HDLattice}; // Generate secure seed for mnemonic let mut seed = [0u8; 32]; getrandom::getrandom(&mut seed).expect("Failed to generate seed"); // Generate mnemonic (returned as a self-wiping Zeroizing) let mnemonic = generate_mnemonic(&mut (&mut seed).into())?; // Create HD wallet let hd_wallet = HDLattice::from_mnemonic(&mnemonic, None)?; // Derive keys using BIP-44 path let keys = hd_wallet.generate_derived_keys("44'/0'/0'/0'/0'")?; ``` ## Crates ### qp-rusty-crystals-dilithium ML-DSA digital signature implementation: - **ML-DSA-44, ML-DSA-65, ML-DSA-87** - All security levels - **NIST Compliant** - Verified against official test vectors - **Pure Rust** - Memory-safe, no unsafe code - **High Performance** - Optimized implementation ### qp-rusty-crystals-sphincs SLH-DSA (FIPS 205) digital signature implementation. See [sphincs/README.md](sphincs/README.md). - **All twelve parameter sets** - SHA2 and SHAKE, categories 1, 3 and 5, small and fast - **Pure, pre-hash and internal interfaces** - **NIST Compliant** - Passes all NIST ACVP vectors for SLH-DSA - **Pure Rust** - `no_std`, no unsafe code ### qp-rusty-crystals-hdwallet Post-quantum HD wallet: - **BIP-39 Compatible** - Mnemonic phrase generation/restoration - **BIP-32 Derivation** - Hierarchical deterministic keys - **BIP-44 Paths** - Standard derivation paths - **Hardened Keys Only** - Secure post-quantum derivation - **SLH-DSA keys** - With the `slh-dsa` feature, one derivation module per FIPS 205 parameter set ## Testing Run all tests with: ```bash cargo test --workspace # Also exercise ML-DSA-44/65 ACVP suites: cargo test -p qp-rusty-crystals-dilithium --all-features ``` For test coverage: ```bash cargo install cargo-tarpaulin cargo tarpaulin --workspace ``` ### NIST KAT tests ACVP known-answer vectors for ML-DSA-44/65/87 live under `dilithium/tests/acvp_vectors/` (see `NIST_VALIDATION.md`). PQCrystals-style NIST KATs (`PQCsignKAT_Dilithium{2,3,5}.rsp`) cover ML-DSA-44/65/87 via `test_nist_kat_ml_dsa_{44,65,87}` in `tests/src/verify_integration_tests.rs` (keygen, verify, and hedged sign bit-exact against the C reference). See `test_vectors/README_TEST_VECTORS.md` for regeneration steps. ACVP known-answer vectors for all twelve SLH-DSA parameter sets live under `sphincs/tests/acvp_vectors/`. The key generation and signing vectors only run in release builds: ```bash cargo test -p qp-rusty-crystals-sphincs --release ``` ## Deep Wiki Use AI to explore this repository interactively! [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/Quantus-Network/qp-rusty-crystals) ## Code Coverage This repository has 100% code coverage for all critical logic and functionality. ```./coverage.sh``` ## License | Crate | License | |-------|---------| | `qp-rusty-crystals-dilithium` | [Apache-2.0](LICENSE) | | `qp-rusty-crystals-hdwallet` | [Apache-2.0](LICENSE) | | `qp-rusty-crystals-sphincs` | [Apache-2.0](LICENSE) | Versions of `qp-rusty-crystals-hdwallet` up to and including 4.1.1 are GPL-3.0-only: their `hderive` module was derived from tiny-hderive, which is licensed under GPL-3.0. That code has since been replaced by a re-implementation written from a specification. Versions of `qp-rusty-crystals-dilithium` up to and including 4.1.1 are GPL-3.0-only: they contained code from Quantum Blockchains' GPL-3.0 Rust port of pq-crystals. That code has since been replaced by a re-implementation from the pq-crystals reference. ## Acknowledgements The ML-DSA arithmetic, packing and Keccak code is a Rust port of the [pq-crystals](https://github.com/pq-crystals/dilithium) reference implementation, which its authors release as public domain (CC0) or under Apache-2.0. The SLH-DSA code follows [slhdsa-c](https://github.com/pq-code-package/slhdsa-c), which its authors license under Apache-2.0, ISC or MIT. Up to and including version 4.1.1, the hdwallet `hderive` module was ported from [nam-tiny-hderive](https://github.com/namada-net/tiny-hderive), the Namada fork of [tiny-hderive](https://github.com/maciejhirsz/tiny-hderive). The current module is a re-implementation written from a specification.