use origin_crypto_sdk::chacha20_blake3::ChaCha20Blake3;
use origin_crypto_sdk::types::XNonce;
fn main() {
println!("=== ChaCha20-BLAKE3 Encryption (Committing AEAD) ===\n");
let key: [u8; 32] = [0x42u8; 32]; let nonce = XNonce::random(); assert_eq!(nonce.as_bytes().len(), XNonce::LEN);
println!("Key: {} bytes", key.len());
println!("Nonce: {} bytes (XNonce)", nonce.as_bytes().len());
let plaintext = b"Sensitive data that must remain confidential";
let aad = b"file-version:1|algorithm:chacha20-blake3";
let ciphertext =
ChaCha20Blake3::encrypt(&key, nonce.as_bytes(), plaintext, aad).expect("encryption failed");
println!("\nPlaintext: {} bytes", plaintext.len());
println!(
"Ciphertext: {} bytes (includes 32-byte auth tag)",
ciphertext.len()
);
let decrypted = ChaCha20Blake3::decrypt(&key, nonce.as_bytes(), &ciphertext, aad)
.expect("decryption failed");
assert_eq!(plaintext.as_slice(), decrypted.as_slice());
println!("\nRoundtrip: PASSED");
println!("Decrypted: {}", String::from_utf8_lossy(&decrypted));
let wrong_aad = b"file-version:2|algorithm:chacha20-blake3";
match ChaCha20Blake3::decrypt(&key, nonce.as_bytes(), &ciphertext, wrong_aad) {
Ok(_) => println!("\nWrong AAD: PASSED (BUG — should have failed!)"),
Err(_) => println!("\nWrong AAD: correctly REJECTED"),
}
let mut tampered = ciphertext.clone();
tampered[0] ^= 0xFF;
match ChaCha20Blake3::decrypt(&key, nonce.as_bytes(), &tampered, aad) {
Ok(_) => println!("Tampered CT: PASSED (BUG — should have failed!)"),
Err(_) => println!("Tampered CT: correctly REJECTED"),
}
}