organize benches, refactor

This commit is contained in:
Neemek 2025-11-03 18:40:20 +01:00
parent 0702313755
commit 28f58d7222
8 changed files with 144 additions and 191 deletions

87
bench/Cargo.lock generated
View file

@ -1,87 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "autocfg"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
[[package]]
name = "bench"
version = "0.1.0"
dependencies = [
"diffie-hellman",
]
[[package]]
name = "cfg-if"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
[[package]]
name = "crypto-bigint"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96272c2ff28b807e09250b180ad1fb7889a3258f7455759b5c3c58b719467130"
dependencies = [
"num-traits",
"rand_core",
"subtle",
]
[[package]]
name = "diffie-hellman"
version = "0.1.0"
dependencies = [
"crypto-bigint",
]
[[package]]
name = "getrandom"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.177"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976"
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "subtle"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"

View file

@ -1,8 +0,0 @@
[package]
name = "bench"
version = "0.1.0"
edition = "2024"
[dependencies]
diffie-hellman = { path = ".." }

105
bench/bench.rs Normal file
View file

@ -0,0 +1,105 @@
#![feature(test)]
extern crate test;
mod benches {
use test::{black_box, Bencher};
use crypto_bigint::U448;
use diffie_hellman::{Public, Secret, U256};
use diffie_hellman::edwards::{ed448_clamp, ED448_GOLDILOCKS};
use diffie_hellman::montgomery::{clamp_u256, CURVE_25519};
use diffie_hellman::traits::{Curve, Point};
#[bench]
fn diffie_hellman(b: &mut Bencher) {
let a = Secret::from(U256::from_u32(123456789));
let p = Public::from(&Secret::from(U256::from_u64(987654321)));
b.iter(|| {
black_box(a.diffie_hellman(&p));
})
}
#[bench]
fn public_key_derivation(b: &mut Bencher) {
let a = Secret::from(U256::from_u32(123456789));
b.iter(|| {
black_box(Public::from(&a))
})
}
#[bench]
fn montgomery_point_add(b: &mut Bencher) {
let p = CURVE_25519.point(U256::from_u32(123456789));
let q = p.double();
b.iter(|| {
black_box(p.add(&q, &p))
})
}
#[bench]
fn montgomery_point_double(b: &mut Bencher) {
let p = CURVE_25519.point(U256::from_u32(123456789));
b.iter(|| {
black_box(p.double())
})
}
#[bench]
fn montgomery_ladder(b: &mut Bencher) {
let p = CURVE_25519.point(U256::from_u32(123456789));
b.iter(|| {
black_box(p.ladder(&clamp_u256(U256::from_u64(987654321))))
})
}
#[bench]
fn constant_time_montgomery_ladder(b: &mut Bencher) {
let p = CURVE_25519.point(U256::from_u32(123456789));
b.iter(|| {
black_box(p.ct_ladder(&clamp_u256(U256::from_u64(987654321))))
})
}
#[bench]
fn edwards_point_add(b: &mut Bencher) {
let p = CURVE_25519.generator();
let q = p.double();
b.iter(|| {
black_box(p.add(&q, &p))
})
}
#[bench]
fn edwards_point_double(b: &mut Bencher) {
let p = ED448_GOLDILOCKS.generator();
b.iter(|| {
black_box(p.double())
})
}
#[bench]
fn edwards_ladder(b: &mut Bencher) {
let p = ED448_GOLDILOCKS.point(U448::from_u32(123456789));
b.iter(|| {
black_box(p.ladder(&ed448_clamp(U448::from_u64(987654321))))
})
}
#[bench]
fn constant_time_edwards_ladder(b: &mut Bencher) {
let p = ED448_GOLDILOCKS.point(U448::from_u32(123456789));
b.iter(|| {
black_box(p.ct_ladder(&ed448_clamp(U448::from_u64(987654321))))
})
}
}

View file

@ -1,2 +0,0 @@
[toolchain]
channel = "nightly"

View file

@ -1,64 +0,0 @@
#![feature(test)]
extern crate test;
mod benches {
use test::{black_box, Bencher};
use diffie_hellman::{clamp_u256, Public, Secret, CURVE_25519, U256};
#[bench]
fn diffie_hellman(b: &mut Bencher) {
let a = Secret::from(U256::from_u32(123456789));
let p = Public::from(&Secret::from(U256::from_u64(987654321)));
b.iter(|| {
black_box(a.diffie_hellman(&p));
})
}
#[bench]
fn public_key_derivation(b: &mut Bencher) {
let a = Secret::from(U256::from_u32(123456789));
b.iter(|| {
black_box(Public::from(&a))
})
}
#[bench]
fn montgomery_point_add(b: &mut Bencher) {
let p = CURVE_25519.point(U256::from_u32(123456789));
let q = p.double();
b.iter(|| {
black_box(p.add(q, p))
})
}
#[bench]
fn montgomery_point_double(b: &mut Bencher) {
let p = CURVE_25519.point(U256::from_u32(123456789));
b.iter(|| {
black_box(p.double())
})
}
#[bench]
fn montgomery_ladder(b: &mut Bencher) {
let p = CURVE_25519.point(U256::from_u32(123456789));
b.iter(|| {
black_box(p.ladder(clamp_u256(U256::from_u64(987654321))))
})
}
#[bench]
fn constant_time_montgomery_ladder(b: &mut Bencher) {
let p = CURVE_25519.point(U256::from_u32(123456789));
b.iter(|| {
black_box(p.ct_ladder(clamp_u256(U256::from_u64(987654321))))
})
}
}