Skip to content

Commit

Permalink
fix: benches
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr. Capybara committed Oct 27, 2023
1 parent 1b1b3dc commit ae43674
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ gmp-mpfr-sys = "1.4.12"
criterion = "0.3"

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

[[bench]]
Expand Down
19 changes: 10 additions & 9 deletions benches/benchmark_e222_256.rs → benches/benchmark_e222_224.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ use capycrypt::sha3::aux_functions::byte_utils::get_random_bytes;
use criterion::{criterion_group, criterion_main, Criterion};

const SELECTED_CURVE: EdCurves = E222;
const BIT_SECURITY: u64 = 224;

/// Symmetric encrypt and decrypt roundtrip
fn sym_enc(pw: &mut Vec<u8>, mut msg: Message) {
msg.pw_encrypt(&mut pw.clone(), 256);
msg.pw_decrypt(&mut pw.clone(), 256);
msg.pw_encrypt(&mut pw.clone(), BIT_SECURITY);
msg.pw_decrypt(&mut pw.clone(), BIT_SECURITY);
}

/// Asymmetric encrypt and decrypt roundtrip + keygen
fn key_gen_enc_dec(pw: &mut Vec<u8>, mut msg: Message) {
let key_pair = KeyPair::new(pw, "test key".to_string(), SELECTED_CURVE, 256);
msg.key_encrypt(&key_pair.pub_key, 256);
msg.key_decrypt(&key_pair.priv_key, 256);
let key_pair = KeyPair::new(pw, "test key".to_string(), SELECTED_CURVE, BIT_SECURITY);
msg.key_encrypt(&key_pair.pub_key, BIT_SECURITY);
msg.key_decrypt(&key_pair.priv_key, BIT_SECURITY);
}

/// Signature generation + verification roundtrip
pub fn sign_verify(mut key_pair: KeyPair, mut msg: Message) {
msg.sign(&mut key_pair, 512);
msg.verify(&key_pair.pub_key, 512);
msg.sign(&mut key_pair, BIT_SECURITY);
msg.verify(&key_pair.pub_key, BIT_SECURITY);
}

fn bench_sign_verify(c: &mut Criterion) {
Expand All @@ -35,7 +36,7 @@ fn bench_sign_verify(c: &mut Criterion) {
&get_random_bytes(16),
"test key".to_string(),
SELECTED_CURVE,
512,
BIT_SECURITY,
),
Message::new(&mut get_random_bytes(5242880)),
)
Expand All @@ -62,7 +63,7 @@ fn bench_key_gen_enc_dec(c: &mut Criterion) {
&get_random_bytes(32),
"test key".to_string(),
SELECTED_CURVE,
256,
BIT_SECURITY,
)
.priv_key,
Message::new(&mut get_random_bytes(5242880)),
Expand Down
20 changes: 11 additions & 9 deletions benches/benchmark_e521_512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ use capycrypt::sha3::aux_functions::byte_utils::get_random_bytes;
use criterion::{criterion_group, criterion_main, Criterion};

const SELECTED_CURVE: EdCurves = E521;
const BIT_SECURITY: u64 = 512;


/// Symmetric encrypt and decrypt roundtrip
fn sym_enc(pw: &mut Vec<u8>, mut msg: Message) {
msg.pw_encrypt(&mut pw.clone(), 512);
msg.pw_decrypt(&mut pw.clone(), 512);
msg.pw_encrypt(&mut pw.clone(), BIT_SECURITY);
msg.pw_decrypt(&mut pw.clone(), BIT_SECURITY);
}

/// Asymmetric encrypt and decrypt roundtrip + keygen
fn key_gen_enc_dec(pw: &mut Vec<u8>, mut msg: Message) {
let key_pair = KeyPair::new(pw, "test key".to_string(), SELECTED_CURVE, 512);
msg.key_encrypt(&key_pair.pub_key, 512);
msg.key_decrypt(&key_pair.priv_key, 512);
let key_pair = KeyPair::new(pw, "test key".to_string(), SELECTED_CURVE, BIT_SECURITY);
msg.key_encrypt(&key_pair.pub_key, BIT_SECURITY);
msg.key_decrypt(&key_pair.priv_key, BIT_SECURITY);
}

/// Signature generation + verification roundtrip
pub fn sign_verify(mut key_pair: KeyPair, mut msg: Message) {
msg.sign(&mut key_pair, 512);
msg.verify(&key_pair.pub_key, 512);
msg.sign(&mut key_pair, BIT_SECURITY);
msg.verify(&key_pair.pub_key, BIT_SECURITY);
}

fn bench_sign_verify(c: &mut Criterion) {
Expand All @@ -35,7 +37,7 @@ fn bench_sign_verify(c: &mut Criterion) {
&get_random_bytes(16),
"test key".to_string(),
SELECTED_CURVE,
512,
BIT_SECURITY,
),
Message::new(&mut get_random_bytes(5242880)),
)
Expand All @@ -62,7 +64,7 @@ fn bench_key_gen_enc_dec(c: &mut Criterion) {
&get_random_bytes(32),
"test key".to_string(),
SELECTED_CURVE,
512,
BIT_SECURITY,
)
.priv_key,
Message::new(&mut get_random_bytes(5242880)),
Expand Down
8 changes: 6 additions & 2 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ pub fn cshake(x: &mut Vec<u8>, l: u64, n: &str, s: &str, d: u64) -> Vec<u8> {
encoded_n.extend_from_slice(&encoded_s);

let bytepad_w = match d {
224 => 172,
256 => 168,
384 => 152,
512 => 136,
_ => panic!("Value must be either 256 or 512"),
_ => panic!("Unsupported security strength. Must be 224, 384, 256, or 512"),
};

let mut out = byte_pad(&mut encoded_n, bytepad_w);
Expand All @@ -100,9 +102,11 @@ pub fn cshake(x: &mut Vec<u8>, l: u64, n: &str, s: &str, d: u64) -> Vec<u8> {
pub fn kmac_xof(k: &Vec<u8>, x: &Vec<u8>, l: u64, s: &str, d: u64) -> Vec<u8> {
let mut encode_k = encode_string(k);
let bytepad_w = match d {
224 => 172,
256 => 168,
384 => 152,
512 => 136,
_ => panic!("Value must be either 256 or 512"),
_ => panic!("Unsupported security strength. Must be 224, 384, 256, or 512"),
};
let mut bp = byte_pad(&mut encode_k, bytepad_w);
bp.append(&mut x.to_owned());
Expand Down

0 comments on commit ae43674

Please sign in to comment.