simplify
This commit is contained in:
parent
fc51ea1057
commit
beead2b394
4 changed files with 21 additions and 45 deletions
|
|
@ -17,3 +17,4 @@ path = "bench/montgomery.rs"
|
|||
[[bench]]
|
||||
name = "edwards"
|
||||
path = "bench/edwards.rs"
|
||||
required-features = ["edwards"]
|
||||
|
|
|
|||
|
|
@ -4,45 +4,37 @@
|
|||
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::edwards::{ED448_GOLDILOCKS, ed448_clamp};
|
||||
use diffie_hellman::traits::{Curve, Point};
|
||||
use test::{Bencher, black_box};
|
||||
|
||||
#[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))
|
||||
})
|
||||
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())
|
||||
})
|
||||
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))))
|
||||
})
|
||||
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))))
|
||||
})
|
||||
b.iter(|| black_box(p.ct_ladder(&ed448_clamp(U448::from_u64(987654321)))))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@
|
|||
extern crate test;
|
||||
|
||||
mod benches {
|
||||
use test::{black_box, Bencher};
|
||||
use diffie_hellman::{Public, Secret, U256};
|
||||
use diffie_hellman::montgomery::{clamp_u256, CURVE_25519};
|
||||
use diffie_hellman::montgomery::{CURVE_25519, clamp_u256};
|
||||
use diffie_hellman::traits::{Curve, Point};
|
||||
use diffie_hellman::{Public, Secret, U256};
|
||||
use test::{Bencher, black_box};
|
||||
|
||||
#[bench]
|
||||
fn diffie_hellman(b: &mut Bencher) {
|
||||
|
|
@ -22,9 +22,7 @@ mod benches {
|
|||
fn public_key_derivation(b: &mut Bencher) {
|
||||
let a = Secret::from(U256::from_u32(123456789));
|
||||
|
||||
b.iter(|| {
|
||||
black_box(Public::from(&a))
|
||||
})
|
||||
b.iter(|| black_box(Public::from(&a)))
|
||||
}
|
||||
|
||||
#[bench]
|
||||
|
|
@ -32,36 +30,27 @@ mod benches {
|
|||
let p = CURVE_25519.point(U256::from_u32(123456789));
|
||||
let q = p.double();
|
||||
|
||||
b.iter(|| {
|
||||
black_box(p.add(&q, &p))
|
||||
})
|
||||
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())
|
||||
})
|
||||
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))))
|
||||
})
|
||||
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))))
|
||||
})
|
||||
b.iter(|| black_box(p.ct_ladder(&clamp_u256(U256::from_u64(987654321)))))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use crate::traits::{Curve, Point};
|
|||
use crypto_bigint::{AddMod, Encoding, NonZero, SubMod, U256};
|
||||
use crypto_bigint::{Integer, InvMod, MulMod};
|
||||
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`
|
||||
#[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)
|
||||
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 x = m & (a.x ^ b.x);
|
||||
let z = m & (a.z ^ b.z);
|
||||
|
||||
*a = MontgomeryPoint {
|
||||
x: a.x ^ x,
|
||||
z: a.z ^ z,
|
||||
curve: a.curve,
|
||||
};
|
||||
a.x ^= x;
|
||||
b.x ^= x;
|
||||
|
||||
*b = MontgomeryPoint {
|
||||
x: b.x ^ x,
|
||||
z: b.z ^ z,
|
||||
curve: b.curve,
|
||||
}
|
||||
a.z ^= z;
|
||||
b.z ^= z;
|
||||
}
|
||||
|
||||
impl<N> Display for MontgomeryPoint<N>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue