1use ever_crypto::{
8 XChaChaCrypto, XChaChaKey, XChaChaNonce,
9 KyberCrypto,
10 Result,
11};
12use std::time::Instant;
13
14const BENCHMARK_ITERATIONS: usize = 1000;
15const MESSAGE_SIZE: usize = 1024; fn main() -> Result<()> {
18 println!("=== Ever Crypto Library Benchmark ===\n");
19
20 println!("1. XChaCha20Poly1305 Benchmark");
22 benchmark_xchacha()?;
23
24 println!("\n2. Kyber1024 Benchmark");
26 benchmark_kyber()?;
27
28 println!("\n3. Hybrid Encryption Benchmark");
30 benchmark_hybrid()?;
31
32 println!("\n=== Benchmark completed! ===");
33 Ok(())
34}
35
36fn benchmark_xchacha() -> Result<()> {
37 let key = XChaChaKey::generate();
38 let nonce = XChaChaNonce::generate();
39 let plaintext = vec![0u8; MESSAGE_SIZE];
40
41 let start = Instant::now();
43 let mut ciphertext = Vec::new();
44 for _ in 0..BENCHMARK_ITERATIONS {
45 ciphertext = XChaChaCrypto::encrypt(&key, &nonce, &plaintext, None)?;
46 }
47 let encryption_time = start.elapsed();
48
49 let start = Instant::now();
51 for _ in 0..BENCHMARK_ITERATIONS {
52 let _decrypted = XChaChaCrypto::decrypt(&key, &nonce, &ciphertext, None)?;
53 }
54 let decryption_time = start.elapsed();
55
56 let encryption_throughput = (BENCHMARK_ITERATIONS * MESSAGE_SIZE) as f64 / encryption_time.as_secs_f64() / 1_000_000.0;
57 let decryption_throughput = (BENCHMARK_ITERATIONS * MESSAGE_SIZE) as f64 / decryption_time.as_secs_f64() / 1_000_000.0;
58
59 println!(" Encryption: {:.2} MB/s ({:.2} μs per operation)",
60 encryption_throughput,
61 encryption_time.as_micros() as f64 / BENCHMARK_ITERATIONS as f64);
62 println!(" Decryption: {:.2} MB/s ({:.2} μs per operation)",
63 decryption_throughput,
64 decryption_time.as_micros() as f64 / BENCHMARK_ITERATIONS as f64);
65
66 Ok(())
67}
68
69fn benchmark_kyber() -> Result<()> {
70 let start = Instant::now();
72 let mut keypair = KyberCrypto::generate_keypair();
73 for _ in 1..BENCHMARK_ITERATIONS {
74 keypair = KyberCrypto::generate_keypair();
75 }
76 let keygen_time = start.elapsed();
77
78 let start = Instant::now();
80 let mut result = KyberCrypto::encapsulate(&keypair.public_key);
81 for _ in 1..BENCHMARK_ITERATIONS {
82 result = KyberCrypto::encapsulate(&keypair.public_key);
83 }
84 let encapsulation_time = start.elapsed();
85
86 let (_, ciphertext) = result;
88 let start = Instant::now();
89 for _ in 0..BENCHMARK_ITERATIONS {
90 let _shared_secret = KyberCrypto::decapsulate(&ciphertext, &keypair.secret_key);
91 }
92 let decapsulation_time = start.elapsed();
93
94 println!(" Key Generation: {:.2} ms per operation",
95 keygen_time.as_millis() as f64 / BENCHMARK_ITERATIONS as f64);
96 println!(" Encapsulation: {:.2} μs per operation",
97 encapsulation_time.as_micros() as f64 / BENCHMARK_ITERATIONS as f64);
98 println!(" Decapsulation: {:.2} μs per operation",
99 decapsulation_time.as_micros() as f64 / BENCHMARK_ITERATIONS as f64);
100
101 Ok(())
102}
103
104fn benchmark_hybrid() -> Result<()> {
105 let plaintext = vec![0u8; MESSAGE_SIZE];
106
107 let start = Instant::now();
109 for _ in 0..BENCHMARK_ITERATIONS {
110 let alice_keypair = KyberCrypto::generate_keypair();
112 let (shared_secret, _kyber_ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
113
114 let xchacha_key = XChaChaKey::from_bytes(shared_secret.as_bytes())?;
116 let (_nonce, _ciphertext) = XChaChaCrypto::encrypt_with_random_nonce(
117 &xchacha_key,
118 &plaintext,
119 None,
120 )?;
121 }
122 let hybrid_time = start.elapsed();
123
124 let hybrid_throughput = (BENCHMARK_ITERATIONS * MESSAGE_SIZE) as f64 / hybrid_time.as_secs_f64() / 1_000_000.0;
125
126 println!(" Hybrid Encryption: {:.2} MB/s ({:.2} ms per operation)",
127 hybrid_throughput,
128 hybrid_time.as_millis() as f64 / BENCHMARK_ITERATIONS as f64);
129
130 Ok(())
131}