-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
somehybrid
committed
Apr 8, 2024
1 parent
e838055
commit a237204
Showing
29 changed files
with
4,144 additions
and
927 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[build] | ||
rustflags = ["-C", "target-cpu=native"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,95 @@ | ||
use benchmark_simple::*; | ||
use chacha20poly1305::{ | ||
aead::{Aead, AeadCore, KeyInit, OsRng}, | ||
ChaCha20Poly1305, Nonce | ||
}; | ||
use raycrypt::aeads::aegis256::encrypt; | ||
use raycrypt::aeads::chachapoly1305::encrypt as chapoly; | ||
use serde_json::ser::CharEscape; | ||
use raycrypt::aegis256::encrypt; | ||
use raycrypt::chachapoly1305::ChaCha20Poly1305; | ||
use raycrypt::xchachapoly1305::XChaCha20Poly1305; | ||
use raycrypt::ciphers::chacha::ChaCha20; | ||
|
||
fn chapoly(key: &[u8], msg: &[u8], nonce: &[u8], ad: &[u8]) -> Vec<u8> { | ||
ChaCha20Poly1305::new(key).encrypt(msg, nonce, ad) | ||
} | ||
|
||
fn xchapoly(key: &[u8], msg: &[u8], nonce: &[u8], ad: &[u8]) -> Vec<u8> { | ||
XChaCha20Poly1305::new(key).encrypt(msg, nonce, ad) | ||
} | ||
|
||
fn chacha(key: &[u8], msg: &[u8], nonce: &[u8]) -> Vec<u8> { | ||
ChaCha20::new(key).encrypt(msg, nonce) | ||
} | ||
|
||
#[inline(always)] | ||
fn test_aegis(key: &[u8], nonce: &[u8], msg: &[u8]) { | ||
encrypt::<16>(key, msg, nonce, &[0u8]); | ||
} | ||
|
||
#[inline(always)] | ||
fn test_chapoly(key: &[u8], nonce: &[u8], msg: &[u8]) { | ||
chapoly(key.to_vec(), msg, nonce, &[0u8], None); | ||
chapoly(key, msg, nonce, &[0u8]); | ||
} | ||
|
||
#[inline(always)] | ||
fn test_chacha(key: &[u8], nonce: &[u8], msg: &[u8]) { | ||
chacha(key, msg, nonce); | ||
} | ||
|
||
fn test_rustcrypto(key: &[u8], nonce: &[u8], msg: &[u8]) { | ||
let key = chacha20poly1305::Key::from_slice(&[0u8; 32]); | ||
let nonce = chacha20poly1305::Nonce::from_slice(&[0u8; 12]); | ||
let state = ChaCha20Poly1305::new(key); | ||
state.encrypt(nonce, msg).unwrap(); | ||
#[inline(always)] | ||
fn test_xchapoly(key: &[u8], nonce: &[u8], msg: &[u8]) { | ||
xchapoly(key, msg, nonce, &[0u8]); | ||
} | ||
|
||
fn main() { | ||
let bench = Bench::new(); | ||
let mut m = vec![0u8; 16384]; | ||
let mut k = vec![0u8; 32]; | ||
let mut nonce = k.clone(); | ||
let m = vec![0u8; 16384]; | ||
let k = vec![0u8; 32]; | ||
let nonce = k.clone(); | ||
|
||
let options = &Options { | ||
iterations: 100, | ||
warmup_iterations: 50, | ||
iterations: 1000, | ||
warmup_iterations: 100, | ||
min_samples: 5, | ||
max_samples: 10, | ||
max_rsd: 1.0, | ||
..Default::default() | ||
}; | ||
|
||
let res = bench.run(&options, || test_aegis(&k, &nonce, &m)); | ||
println!("{}", res.throughput(m.len() as u128)); | ||
println!("aegis256: {}", res.throughput(m.len() as u128)); | ||
|
||
let res = bench.run(&options, || test_chapoly(&k, &nonce, &m)); | ||
println!("{}", res.throughput(m.len() as u128)); | ||
println!("chacha20poly1305: {}", res.throughput(m.len() as u128)); | ||
|
||
let res = bench.run(&options, || test_xchapoly(&k, &nonce, &m)); | ||
println!("xchacha20poly1305: {}", res.throughput(m.len() as u128)); | ||
|
||
let res = bench.run(&options, || test_chacha(&k, &nonce, &m)); | ||
println!("chacha20: {}", res.throughput(m.len() as u128)); | ||
|
||
#[cfg(target_arch = "x86_64")] | ||
unsafe { | ||
use core::arch::x86_64::__rdtscp; | ||
|
||
let mut tmp = [0u8; 32]; | ||
|
||
let a = __rdtscp(tmp.as_mut_ptr() as *mut u32); | ||
test_aegis(&k, &nonce, &m); | ||
let b = __rdtscp(tmp.as_mut_ptr() as *mut u32); | ||
|
||
println!("aegis256: CPU cycles {}", b - a); | ||
|
||
let a = __rdtscp(tmp.as_mut_ptr() as *mut u32); | ||
test_chapoly(&k, &nonce, &m); | ||
let b = __rdtscp(tmp.as_mut_ptr() as *mut u32); | ||
|
||
println!("chacha20poly1305: CPU cycles {}", b - a); | ||
|
||
let a = __rdtscp(tmp.as_mut_ptr() as *mut u32); | ||
test_xchapoly(&k, &nonce, &m); | ||
let b = __rdtscp(tmp.as_mut_ptr() as *mut u32); | ||
|
||
println!("xchacha20poly1305: CPU cycles {}", b - a); | ||
|
||
let res = bench.run(&options, || test_rustcrypto(&k, &nonce, &m)); | ||
println!("{}", res.throughput(m.len() as u128)); | ||
let a = __rdtscp(tmp.as_mut_ptr() as *mut u32); | ||
test_chacha(&k, &nonce, &m); | ||
let b = __rdtscp(tmp.as_mut_ptr() as *mut u32); | ||
println!("chacha20: CPU cycles {}", b - a); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
pub mod chachapoly1305; | ||
pub mod xchachapoly1305; | ||
pub mod aegis256; | ||
mod chachapoly1305; | ||
mod xchachapoly1305; | ||
|
||
pub use chachapoly1305::ChaCha20Poly1305; | ||
pub use xchachapoly1305::XChaCha20Poly1305; |
Oops, something went wrong.