Skip to content

Commit

Permalink
removed hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
somehybrid committed Apr 8, 2024
1 parent e838055 commit a237204
Show file tree
Hide file tree
Showing 29 changed files with 4,144 additions and 927 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
rustflags = ["-C", "target-cpu=native"]
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

rusty-tags.vi
rusty-tags.emacs
99 changes: 1 addition & 98 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ edition = "2021"
name = "raycrypt"

[dependencies]
zeroize = {version = "1.7",features = ["zeroize_derive"]}
zeroize = { version = "1.7", features = [ "zeroize_derive" ] }
cfg-if = "1.0"
sha2 = "0.10"
getrandom = "0.2"

[dev-dependencies]
hex = "0.4"
serde_json = "1.0"
benchmark-simple = "0.1"
chacha20poly1305 = "0.10"

[[bench]]
name = "bench"
harness = false

[profile.release]
codegen-units = 1
opt-level = 2

[profile.dev]
codegen-units = 16
opt-level = 1
91 changes: 69 additions & 22 deletions benches/bench.rs
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);
}
}
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ requires = ["maturin>=1.1,<2.0"]
build-backend = "maturin"

[project]
name = "chacha20"
name = "raycrypt"
version = "0.1"
description = "A vectorized implementation of the ChaCha stream cipher."
description = "Encrypt at the speed of light"
license = { text = "MIT" }
readme = "README.md"

Expand Down
7 changes: 5 additions & 2 deletions src/aeads.rs
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;
Loading

0 comments on commit a237204

Please sign in to comment.