Crate rustpq

Crate rustpq 

Source
Expand description

§RustPQ

A pure Rust post-quantum cryptography suite by Sanct.

CI Crates.io Docs.rs License: MIT OR Apache-2.0

§Algorithms

AlgorithmStandardStatus
ML-KEM (Kyber)FIPS 203Implemented
ML-KEM HybridIETF draft-ietf-tls-ecdhe-mlkemImplemented
ML-DSA (Dilithium)FIPS 204Implemented
SLH-DSA (SPHINCS+)FIPS 205Planned

§Features

  • Pure Rust - No unsafe code, memory-safe by design
  • no_std Compatible - Works on embedded devices and bare-metal
  • Constant-time - Resistant to timing attacks via the subtle crate
  • Lightweight - Minimal dependencies

§Installation

[dependencies]
rustpq = "0.3.0"

For hybrid KEMs:

[dependencies]
rustpq = { version = "0.3.0", features = ["x25519-mlkem768"] }

§Usage

§ML-KEM Key Encapsulation

use rustpq::ml_kem::mlkem768::{generate, encapsulate, decapsulate};
use rand::rngs::OsRng;

// Generate a keypair
let (public_key, secret_key) = generate(&mut OsRng);

// Encapsulate: creates shared secret + ciphertext
let (ciphertext, shared_secret_sender) = encapsulate(&public_key, &mut OsRng);

// Decapsulate: recovers shared secret from ciphertext
let shared_secret_receiver = decapsulate(&secret_key, &ciphertext);

assert_eq!(shared_secret_sender.as_bytes(), shared_secret_receiver.as_bytes());

§ML-KEM Hybrid (Post-Quantum + Classical)

Hybrid KEMs combine ML-KEM with traditional ECDH for defense-in-depth. Even if one algorithm is broken, the other provides security.

use rustpq::ml_kem_hybrid::x25519_mlkem768::{generate, encapsulate, decapsulate};
use rand::rngs::OsRng;

// Generate hybrid keypair (X25519 + ML-KEM-768)
let (pk, sk) = generate(&mut OsRng);

// Encapsulate
let (ct, ss_sender) = encapsulate(&pk, &mut OsRng);

// Decapsulate
let ss_receiver = decapsulate(&sk, &ct);

// Get a ready-to-use 32-byte key (SHA3-256 of combined secrets)
let key = ss_sender.derive_key();

// Or access raw concatenated secret for custom KDF
let raw_64_bytes = ss_sender.as_bytes();

§ML-DSA Digital Signatures

use rustpq::ml_dsa::mldsa44::{generate, sign, verify};
use rand::rngs::OsRng;

// Generate a keypair
let (public_key, secret_key) = generate(&mut OsRng);

// Sign a message
let message = b"Hello World";
let context = b""; // Optional context string
let signature = sign(&secret_key, message, context, &mut OsRng).unwrap();

// Verify the signature
assert!(verify(&public_key, message, context, &signature).is_ok());

§Examples

# ML-KEM key encapsulation
cargo run --example basic --features mlkem768

# ML-KEM hybrid (X25519 + ML-KEM-768)
cargo run --example hybrid --features x25519-mlkem768

# End-to-end encryption (hybrid KEM + signatures)
cargo run --example e2ee --features "x25519-mlkem768,mldsa65"

# ML-DSA digital signatures
cargo run --example mldsa --features mldsa44

§Development

# Run all tests
cargo test --all-features

# Run hybrid tests only
cargo test --features "x25519-mlkem768,p256-mlkem768,p384-mlkem1024"

# Benchmarks
cargo bench --features "mlkem512,mlkem768,mlkem1024"
cargo bench --features "x25519-mlkem768,p256-mlkem768,p384-mlkem1024"

# Check for issues
cargo clippy --all-features

§Feature Flags

§ML-KEM (Key Encapsulation)

FeatureAlgorithmSecurityKey SizeCiphertextSecret
mlkem512ML-KEM-512Level 1 (~AES-128)800 B768 B32 B
mlkem768ML-KEM-768Level 3 (~AES-192)1184 B1088 B32 B
mlkem1024ML-KEM-1024Level 5 (~AES-256)1568 B1568 B32 B

§ML-KEM Hybrid (Post-Quantum + Classical)

FeatureHybridSecurityKey SizeCiphertextSecret
x25519-mlkem768X25519 + ML-KEM-768Level 31216 B1120 B64 B
p256-mlkem768P-256 + ML-KEM-768Level 31249 B1153 B64 B
p384-mlkem1024P-384 + ML-KEM-1024Level 51665 B1665 B80 B

Hybrid combiners follow IETF draft-ietf-tls-ecdhe-mlkem. Use derive_key() for a ready-to-use 32-byte key, or as_bytes() for protocol integration or custom KDF.

§ML-DSA (Digital Signatures)

FeatureAlgorithmSecurityKey SizeSignature
mldsa44ML-DSA-44Level 22560 B2420 B
mldsa65ML-DSA-65Level 34032 B3309 B
mldsa87ML-DSA-87Level 54896 B4627 B

§General

FeatureDescription
ml-kemEnable ML-KEM module (default)
ml-kem-hybridEnable ML-KEM Hybrid module
ml-dsaEnable ML-DSA module
stdEnable standard library support
allocEnable allocator support

Default features: ml-kem, mlkem768

§Security

This implementation prioritizes correctness and security:

  • Constant-time operations to prevent timing side-channels
  • Zeroization of sensitive data on drop
  • No unsafe code
  • Hybrid KEMs provide defense-in-depth against both classical and quantum attacks

[!WARNING] This library has not yet been audited. Use at your own risk in production systems.

§License

Licensed under either of:

at your option.

Modules§

ml_kem
ML-KEM (Module-Lattice-Based Key-Encapsulation Mechanism) implementation.