mirror of
https://github.com/hanzoai/crypto.git
synced 2026-09-30 21:37:30 +00:00
cargo could not read the manifest at all:
error inheriting `aes-gcm` from workspace root manifest's
workspace.dependencies.aes-gcm: failed to find a workspace root
All 25 dependencies are declared `workspace = true`, which resolves only
against a [workspace.dependencies] table, and the crate was extracted from a
monorepo without one. The versions here are the ones Cargo.lock had already
resolved, so this records what the crate was building against and chooses
nothing new — `cargo build --locked` leaves Cargo.lock untouched.
That got the manifest parsed and the library building, which exposed three
callers written against an ml-kem/ml-dsa that predates the pinned versions:
Encapsulate and Decapsulate are private at ml_kem's root and public at
ml_kem::kem, and MlKem768::generate needs KemCore in scope.
ml-dsa has no Ml_Dsa65, no VerificationKey and no SigningKey::generate.
Key generation is the KeyGen trait (MlDsa65::key_gen), the pair hands out
signing_key()/verifying_key(), and signing goes through signature::Signer
and Verifier.
ml-dsa is on rand_core 0.9 while ml-kem is on 0.6, so one thread_rng cannot
serve both. The ml-dsa side takes OsRng from ml-dsa's own re-export, via
unwrap_err, because OsRng is fallible in 0.9 and key_gen wants an
infallible CryptoRng. No new dependency.
integration_test's message list mixed [u8; 9], [u8; 27], [u8; 0] and
[u8; 1000] in one untyped array, which has no single element type. Typed
as [&[u8]; 4].
Both examples RUN, not merely compile: ML-KEM encapsulation round-trips,
ML-DSA signatures verify, HQC round-trips. 12 tests pass.
Worth knowing separately: src/ml_kem.rs, src/ml_dsa.rs, src/hqc.rs, src/pqc/
and src/hybrid/ are in no build target — lib.rs declares only symmetric, hash,
error and prelude. Those wrappers are dead files and would not compile as they
stand (src/ml_dsa.rs imports the same Ml_Dsa65 and VerificationKey that do not
exist). Post-quantum is reachable today only as the re-exported upstream
crates, without this crate's SecurityLevel/zeroizing layer over it.
113 lines
3.8 KiB
Rust
113 lines
3.8 KiB
Rust
//! Basic usage example of Hanzo Crypto
|
|
|
|
use hanzo_crypto::prelude::*;
|
|
use hanzo_crypto::{ml_kem, ml_dsa, blake3, chacha20poly1305, aes_gcm};
|
|
|
|
fn main() -> Result<()> {
|
|
println!("Hanzo Crypto Example\n");
|
|
|
|
// ML-KEM (NIST FIPS 203) - Post-quantum key encapsulation
|
|
println!("1. ML-KEM-768 (formerly Kyber):");
|
|
{
|
|
use ml_kem::kem::{Decapsulate, Encapsulate};
|
|
use ml_kem::{KemCore, MlKem768};
|
|
|
|
let mut rng = rand::thread_rng();
|
|
let (dk, ek) = MlKem768::generate(&mut rng);
|
|
|
|
let (ct, ss_sender) = ek.encapsulate(&mut rng).unwrap();
|
|
let ss_receiver = dk.decapsulate(&ct).unwrap();
|
|
|
|
assert_eq!(ss_sender, ss_receiver);
|
|
println!(" ✓ Key encapsulation successful");
|
|
}
|
|
|
|
// ML-DSA (NIST FIPS 204) - Post-quantum signatures
|
|
println!("\n2. ML-DSA-65 (formerly Dilithium):");
|
|
{
|
|
// ml-dsa is on rand_core 0.9 while ml-kem above is on 0.6, so the two
|
|
// blocks cannot share one RNG. OsRng is fallible there; unwrap_err gives
|
|
// the infallible CryptoRng key_gen asks for.
|
|
use ml_dsa::signature::rand_core::{OsRng, TryRngCore};
|
|
use ml_dsa::signature::{Signer, Verifier};
|
|
use ml_dsa::{KeyGen, MlDsa65};
|
|
|
|
let mut rng = OsRng.unwrap_err();
|
|
let keypair = MlDsa65::key_gen(&mut rng);
|
|
|
|
let message = b"Hello, post-quantum world!";
|
|
let signature = keypair.signing_key().sign(message);
|
|
|
|
keypair.verifying_key().verify(message, &signature).unwrap();
|
|
println!(" ✓ Signature verification successful");
|
|
}
|
|
|
|
// BLAKE3 hashing
|
|
println!("\n3. BLAKE3 Hashing:");
|
|
{
|
|
let data = b"Hash this data with BLAKE3";
|
|
let hash = blake3::hash(data);
|
|
println!(" Hash: {}", hash.to_hex());
|
|
println!(" ✓ BLAKE3 hash computed");
|
|
}
|
|
|
|
// ChaCha20-Poly1305 authenticated encryption
|
|
println!("\n4. ChaCha20-Poly1305:");
|
|
{
|
|
use chacha20poly1305::{
|
|
aead::{Aead, AeadCore, KeyInit, OsRng},
|
|
ChaCha20Poly1305,
|
|
};
|
|
|
|
let key = ChaCha20Poly1305::generate_key(&mut OsRng);
|
|
let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
|
|
|
|
let cipher = ChaCha20Poly1305::new(&key);
|
|
let plaintext = b"Secret message";
|
|
let ciphertext = cipher.encrypt(&nonce, plaintext.as_ref()).unwrap();
|
|
let decrypted = cipher.decrypt(&nonce, ciphertext.as_ref()).unwrap();
|
|
|
|
assert_eq!(plaintext.as_ref(), decrypted);
|
|
println!(" ✓ Encryption/decryption successful");
|
|
}
|
|
|
|
// AES-256-GCM authenticated encryption
|
|
println!("\n5. AES-256-GCM:");
|
|
{
|
|
use aes_gcm::{
|
|
aead::{Aead, AeadCore, KeyInit, OsRng},
|
|
Aes256Gcm,
|
|
};
|
|
|
|
let key = Aes256Gcm::generate_key(&mut OsRng);
|
|
let nonce = Aes256Gcm::generate_nonce(&mut OsRng);
|
|
|
|
let cipher = Aes256Gcm::new(&key);
|
|
let plaintext = b"Another secret";
|
|
let ciphertext = cipher.encrypt(&nonce, plaintext.as_ref()).unwrap();
|
|
let decrypted = cipher.decrypt(&nonce, ciphertext.as_ref()).unwrap();
|
|
|
|
assert_eq!(plaintext.as_ref(), decrypted);
|
|
println!(" ✓ AES-256-GCM successful");
|
|
}
|
|
|
|
// HQC code-based cryptography
|
|
println!("\n6. HQC-192:");
|
|
{
|
|
use hanzo_crypto::hqc::hqc192::*;
|
|
// as_bytes on a shared secret comes from this trait, not the type.
|
|
use pqcrypto_traits::kem::SharedSecret;
|
|
|
|
let (pk, sk) = keypair();
|
|
let (ss1, ct) = encapsulate(&pk);
|
|
let ss2 = decapsulate(&ct, &sk);
|
|
|
|
assert_eq!(ss1.as_bytes(), ss2.as_bytes());
|
|
println!(" ✓ HQC key encapsulation successful");
|
|
}
|
|
|
|
println!("\n✅ All cryptographic operations completed successfully!");
|
|
println!(" This library is ARM-compatible (M1/M2/M3 Mac ready)");
|
|
|
|
Ok(())
|
|
} |