ever-crypto 0.1.0

A simple Rust library providing easy-to-use APIs for XChaCha20Poly1305 and Kyber1024 post-quantum cryptography at the same time. Based on chacha20poly1305 and pqcrypto-kyber.
Documentation
//! Benchmark example for Ever Crypto library
//! 
//! This example demonstrates the performance characteristics of:
//! 1. XChaCha20Poly1305 encryption/decryption
//! 2. Kyber1024 key generation and encapsulation

use ever_crypto::{
    XChaChaCrypto, XChaChaKey, XChaChaNonce,
    KyberCrypto,
    Result,
};
use std::time::Instant;

const BENCHMARK_ITERATIONS: usize = 1000;
const MESSAGE_SIZE: usize = 1024; // 1KB messages

fn main() -> Result<()> {
    println!("=== Ever Crypto Library Benchmark ===\n");
    
    // Benchmark XChaCha20Poly1305
    println!("1. XChaCha20Poly1305 Benchmark");
    benchmark_xchacha()?;
    
    // Benchmark Kyber1024
    println!("\n2. Kyber1024 Benchmark");
    benchmark_kyber()?;
    
    // Benchmark Hybrid Encryption
    println!("\n3. Hybrid Encryption Benchmark");
    benchmark_hybrid()?;
    
    println!("\n=== Benchmark completed! ===");
    Ok(())
}

fn benchmark_xchacha() -> Result<()> {
    let key = XChaChaKey::generate();
    let nonce = XChaChaNonce::generate();
    let plaintext = vec![0u8; MESSAGE_SIZE];
    
    // Benchmark encryption
    let start = Instant::now();
    let mut ciphertext = Vec::new();
    for _ in 0..BENCHMARK_ITERATIONS {
        ciphertext = XChaChaCrypto::encrypt(&key, &nonce, &plaintext, None)?;
    }
    let encryption_time = start.elapsed();
    
    // Benchmark decryption
    let start = Instant::now();
    for _ in 0..BENCHMARK_ITERATIONS {
        let _decrypted = XChaChaCrypto::decrypt(&key, &nonce, &ciphertext, None)?;
    }
    let decryption_time = start.elapsed();
    
    let encryption_throughput = (BENCHMARK_ITERATIONS * MESSAGE_SIZE) as f64 / encryption_time.as_secs_f64() / 1_000_000.0;
    let decryption_throughput = (BENCHMARK_ITERATIONS * MESSAGE_SIZE) as f64 / decryption_time.as_secs_f64() / 1_000_000.0;
    
    println!("  Encryption: {:.2} MB/s ({:.2} μs per operation)", 
             encryption_throughput, 
             encryption_time.as_micros() as f64 / BENCHMARK_ITERATIONS as f64);
    println!("  Decryption: {:.2} MB/s ({:.2} μs per operation)", 
             decryption_throughput, 
             decryption_time.as_micros() as f64 / BENCHMARK_ITERATIONS as f64);
    
    Ok(())
}

fn benchmark_kyber() -> Result<()> {
    // Benchmark key generation
    let start = Instant::now();
    let mut keypair = KyberCrypto::generate_keypair();
    for _ in 1..BENCHMARK_ITERATIONS {
        keypair = KyberCrypto::generate_keypair();
    }
    let keygen_time = start.elapsed();
    
    // Benchmark encapsulation
    let start = Instant::now();
    let mut result = KyberCrypto::encapsulate(&keypair.public_key);
    for _ in 1..BENCHMARK_ITERATIONS {
        result = KyberCrypto::encapsulate(&keypair.public_key);
    }
    let encapsulation_time = start.elapsed();
    
    // Benchmark decapsulation
    let (_, ciphertext) = result;
    let start = Instant::now();
    for _ in 0..BENCHMARK_ITERATIONS {
        let _shared_secret = KyberCrypto::decapsulate(&ciphertext, &keypair.secret_key);
    }
    let decapsulation_time = start.elapsed();
    
    println!("  Key Generation: {:.2} ms per operation", 
             keygen_time.as_millis() as f64 / BENCHMARK_ITERATIONS as f64);
    println!("  Encapsulation: {:.2} μs per operation", 
             encapsulation_time.as_micros() as f64 / BENCHMARK_ITERATIONS as f64);
    println!("  Decapsulation: {:.2} μs per operation", 
             decapsulation_time.as_micros() as f64 / BENCHMARK_ITERATIONS as f64);
    
    Ok(())
}

fn benchmark_hybrid() -> Result<()> {
    let plaintext = vec![0u8; MESSAGE_SIZE];
    
    // Benchmark full hybrid encryption
    let start = Instant::now();
    for _ in 0..BENCHMARK_ITERATIONS {
        // Key establishment
        let alice_keypair = KyberCrypto::generate_keypair();
        let (shared_secret, _kyber_ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
        
        // Data encryption
        let xchacha_key = XChaChaKey::from_bytes(shared_secret.as_bytes())?;
        let (_nonce, _ciphertext) = XChaChaCrypto::encrypt_with_random_nonce(
            &xchacha_key,
            &plaintext,
            None,
        )?;
    }
    let hybrid_time = start.elapsed();
    
    let hybrid_throughput = (BENCHMARK_ITERATIONS * MESSAGE_SIZE) as f64 / hybrid_time.as_secs_f64() / 1_000_000.0;
    
    println!("  Hybrid Encryption: {:.2} MB/s ({:.2} ms per operation)", 
             hybrid_throughput, 
             hybrid_time.as_millis() as f64 / BENCHMARK_ITERATIONS as f64);
    
    Ok(())
}