export more, benchmarks

This commit is contained in:
Neemek 2025-10-09 21:28:56 +02:00
parent 2c7a005d7c
commit 321635a80a
4 changed files with 71 additions and 3 deletions

View file

@ -2,8 +2,10 @@
<module type="EMPTY_MODULE" version="4"> <module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/bench/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/bench/target" />
<excludeFolder url="file://$MODULE_DIR$/target" /> <excludeFolder url="file://$MODULE_DIR$/target" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />

View file

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

64
bench/src/lib.rs Normal file
View file

@ -0,0 +1,64 @@
#![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))))
})
}
}

View file

@ -1,4 +1,4 @@
use crypto_bigint::{ConstChoice, Encoding, NonZero, U256}; pub use crypto_bigint::{ConstChoice, Encoding, NonZero, U256};
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::ops::{Div, Mul}; use std::ops::{Div, Mul};
@ -31,7 +31,7 @@ impl MontgomeryCurve {
} }
} }
fn clamp_u256(x: U256) -> U256 { pub fn clamp_u256(x: U256) -> U256 {
let mut bytes: [u8; 32] = x.to_le_bytes(); let mut bytes: [u8; 32] = x.to_le_bytes();
// clamp value // clamp value