make edwards an optional feature, and separate benches

This commit is contained in:
Neemek 2025-11-03 18:55:38 +01:00
parent 28f58d7222
commit 3c61f6f043
5 changed files with 59 additions and 39 deletions

View file

@ -8,7 +8,12 @@ crypto-bigint = { version = "0.6.1" }
[features] [features]
rand = ["crypto-bigint/rand"] rand = ["crypto-bigint/rand"]
edwards = []
[[bench]] [[bench]]
name = "benchmarks" name = "montgomery"
path = "bench/bench.rs" path = "bench/montgomery.rs"
[[bench]]
name = "edwards"
path = "bench/edwards.rs"

48
bench/edwards.rs Normal file
View file

@ -0,0 +1,48 @@
#![cfg(feature = "edwards")]
#![feature(test)]
extern crate test;
mod benches {
use test::{black_box, Bencher};
use crypto_bigint::U448;
use diffie_hellman::edwards::{ed448_clamp, ED448_GOLDILOCKS};
use diffie_hellman::traits::{Curve, Point};
#[bench]
fn edwards_point_add(b: &mut Bencher) {
let p = ED448_GOLDILOCKS.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,4 +1,5 @@
#![feature(test)] #![feature(test)]
extern crate test; extern crate test;
mod benches { mod benches {
@ -64,42 +65,5 @@ mod benches {
black_box(p.ct_ladder(&clamp_u256(U256::from_u64(987654321)))) 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,3 +1,5 @@
#![cfg(feature = "edwards")]
use crate::traits::{Curve, Point}; use crate::traits::{Curve, Point};
use crypto_bigint::{AddMod, ConstZero, Integer, InvMod, MulMod, NonZero, SubMod, U448}; use crypto_bigint::{AddMod, ConstZero, Integer, InvMod, MulMod, NonZero, SubMod, U448};
use std::ops::Div; use std::ops::Div;

View file

@ -1,6 +1,7 @@
#[macro_use] #[macro_use]
mod macros; mod macros;
#[cfg(feature = "edwards")]
pub mod edwards; pub mod edwards;
pub mod montgomery; pub mod montgomery;
pub mod traits; pub mod traits;