This commit is contained in:
Neemek 2025-11-04 09:50:39 +01:00
parent fc51ea1057
commit beead2b394
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E
4 changed files with 21 additions and 45 deletions

View file

@ -17,3 +17,4 @@ path = "bench/montgomery.rs"
[[bench]] [[bench]]
name = "edwards" name = "edwards"
path = "bench/edwards.rs" path = "bench/edwards.rs"
required-features = ["edwards"]

View file

@ -4,45 +4,37 @@
extern crate test; extern crate test;
mod benches { mod benches {
use test::{black_box, Bencher};
use crypto_bigint::U448; use crypto_bigint::U448;
use diffie_hellman::edwards::{ed448_clamp, ED448_GOLDILOCKS}; use diffie_hellman::edwards::{ED448_GOLDILOCKS, ed448_clamp};
use diffie_hellman::traits::{Curve, Point}; use diffie_hellman::traits::{Curve, Point};
use test::{Bencher, black_box};
#[bench] #[bench]
fn edwards_point_add(b: &mut Bencher) { fn edwards_point_add(b: &mut Bencher) {
let p = ED448_GOLDILOCKS.generator(); let p = ED448_GOLDILOCKS.generator();
let q = p.double(); let q = p.double();
b.iter(|| { b.iter(|| black_box(p.add(&q, &p)))
black_box(p.add(&q, &p))
})
} }
#[bench] #[bench]
fn edwards_point_double(b: &mut Bencher) { fn edwards_point_double(b: &mut Bencher) {
let p = ED448_GOLDILOCKS.generator(); let p = ED448_GOLDILOCKS.generator();
b.iter(|| { b.iter(|| black_box(p.double()))
black_box(p.double())
})
} }
#[bench] #[bench]
fn edwards_ladder(b: &mut Bencher) { fn edwards_ladder(b: &mut Bencher) {
let p = ED448_GOLDILOCKS.point(U448::from_u32(123456789)); let p = ED448_GOLDILOCKS.point(U448::from_u32(123456789));
b.iter(|| { b.iter(|| black_box(p.ladder(&ed448_clamp(U448::from_u64(987654321)))))
black_box(p.ladder(&ed448_clamp(U448::from_u64(987654321))))
})
} }
#[bench] #[bench]
fn constant_time_edwards_ladder(b: &mut Bencher) { fn constant_time_edwards_ladder(b: &mut Bencher) {
let p = ED448_GOLDILOCKS.point(U448::from_u32(123456789)); let p = ED448_GOLDILOCKS.point(U448::from_u32(123456789));
b.iter(|| { b.iter(|| black_box(p.ct_ladder(&ed448_clamp(U448::from_u64(987654321)))))
black_box(p.ct_ladder(&ed448_clamp(U448::from_u64(987654321))))
})
} }
} }

View file

@ -3,10 +3,10 @@
extern crate test; extern crate test;
mod benches { mod benches {
use test::{black_box, Bencher}; use diffie_hellman::montgomery::{CURVE_25519, clamp_u256};
use diffie_hellman::{Public, Secret, U256};
use diffie_hellman::montgomery::{clamp_u256, CURVE_25519};
use diffie_hellman::traits::{Curve, Point}; use diffie_hellman::traits::{Curve, Point};
use diffie_hellman::{Public, Secret, U256};
use test::{Bencher, black_box};
#[bench] #[bench]
fn diffie_hellman(b: &mut Bencher) { fn diffie_hellman(b: &mut Bencher) {
@ -22,9 +22,7 @@ mod benches {
fn public_key_derivation(b: &mut Bencher) { fn public_key_derivation(b: &mut Bencher) {
let a = Secret::from(U256::from_u32(123456789)); let a = Secret::from(U256::from_u32(123456789));
b.iter(|| { b.iter(|| black_box(Public::from(&a)))
black_box(Public::from(&a))
})
} }
#[bench] #[bench]
@ -32,36 +30,27 @@ mod benches {
let p = CURVE_25519.point(U256::from_u32(123456789)); let p = CURVE_25519.point(U256::from_u32(123456789));
let q = p.double(); let q = p.double();
b.iter(|| { b.iter(|| black_box(p.add(&q, &p)))
black_box(p.add(&q, &p))
})
} }
#[bench] #[bench]
fn montgomery_point_double(b: &mut Bencher) { fn montgomery_point_double(b: &mut Bencher) {
let p = CURVE_25519.point(U256::from_u32(123456789)); let p = CURVE_25519.point(U256::from_u32(123456789));
b.iter(|| { b.iter(|| black_box(p.double()))
black_box(p.double())
})
} }
#[bench] #[bench]
fn montgomery_ladder(b: &mut Bencher) { fn montgomery_ladder(b: &mut Bencher) {
let p = CURVE_25519.point(U256::from_u32(123456789)); let p = CURVE_25519.point(U256::from_u32(123456789));
b.iter(|| { b.iter(|| black_box(p.ladder(&clamp_u256(U256::from_u64(987654321)))))
black_box(p.ladder(&clamp_u256(U256::from_u64(987654321))))
})
} }
#[bench] #[bench]
fn constant_time_montgomery_ladder(b: &mut Bencher) { fn constant_time_montgomery_ladder(b: &mut Bencher) {
let p = CURVE_25519.point(U256::from_u32(123456789)); let p = CURVE_25519.point(U256::from_u32(123456789));
b.iter(|| { b.iter(|| black_box(p.ct_ladder(&clamp_u256(U256::from_u64(987654321)))))
black_box(p.ct_ladder(&clamp_u256(U256::from_u64(987654321))))
})
} }
} }

View file

@ -2,7 +2,7 @@ use crate::traits::{Curve, Point};
use crypto_bigint::{AddMod, Encoding, NonZero, SubMod, U256}; use crypto_bigint::{AddMod, Encoding, NonZero, SubMod, U256};
use crypto_bigint::{Integer, InvMod, MulMod}; use crypto_bigint::{Integer, InvMod, MulMod};
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::ops::{Div, Mul}; use std::ops::{BitXor, Div, Mul};
/// A curve of the form `By^2 = x^3 + Ax^2 + x` /// A curve of the form `By^2 = x^3 + Ax^2 + x`
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
@ -162,24 +162,18 @@ ladder_impl!(MontgomeryPoint, swap_montgomery);
fn swap_montgomery<N>(a: &mut MontgomeryPoint<N>, b: &mut MontgomeryPoint<N>, c: u8, max: N) fn swap_montgomery<N>(a: &mut MontgomeryPoint<N>, b: &mut MontgomeryPoint<N>, c: u8, max: N)
where where
N: AddMod + SubMod + MulMod + InvMod<Output = N> + Copy + Integer + std::ops::Div<Output = N>, N: BitXor + Copy + Mul<Output = N> + From<u8> + Div<Output = N> + Integer + InvMod<Output = N>,
{ {
let m = max * N::from(c); let m = max * N::from(c);
let x = m & (a.x ^ b.x); let x = m & (a.x ^ b.x);
let z = m & (a.z ^ b.z); let z = m & (a.z ^ b.z);
*a = MontgomeryPoint { a.x ^= x;
x: a.x ^ x, b.x ^= x;
z: a.z ^ z,
curve: a.curve,
};
*b = MontgomeryPoint { a.z ^= z;
x: b.x ^ x, b.z ^= z;
z: b.z ^ z,
curve: b.curve,
}
} }
impl<N> Display for MontgomeryPoint<N> impl<N> Display for MontgomeryPoint<N>