通过修改lib.rs
文件,将field
和backend
module改为pub。 通过性能对比发现在curve25519-dalek库的代码实现,实际field域内计算速度优于scalar域内的计算性能。
针对field域内的加减乘除和scalar域内的加减乘除运算对应的bench代码如下:
mod scalar_benches {
use super::*;
fn scalar_inversion(c: &mut Criterion) {
c.bench_function("Scalar inversion", |b| {
let s = Scalar::from(897987897u64).invert();
b.iter(|| s.invert());
});
}
fn scalar_mul(c: &mut Criterion) {
c.bench_function("Scalar multiplication", |b| {
let s = Scalar::from(897987897u64).invert();
b.iter(|| s*s);
});
}
fn scalar_add(c: &mut Criterion) {
c.bench_function("Scalar add", |b| {
let s = Scalar::from(897987897u64).invert();
b.iter(|| s+s);
});
}
fn scalar_sub(c: &mut Criterion) {
c.bench_function("Scalar sub", |b| {
let s = Scalar::from(897987897u64).invert();
b.iter(|| s-s);
});
}
fn batch_scalar_inversion(c: &mut Criterion) {
c.bench_function_over_inputs(
"Batch scalar inversion",
|b, &&size| {
let mut rng = OsRng::new().unwrap();
let scalars: Vec = (0..size).map(|_| Scalar::random(&mut rng)).collect();
b.iter(|| {
let mut s = scalars.clone();
Scalar::batch_invert(&mut s);
});
},
&BATCH_SIZES,
);
}
criterion_group! {
name = scalar_benches;
config = Criterion::default();
targets =
scalar_inversion,
scalar_mul,
scalar_add,
scalar_sub,
//batch_scalar_inversion,
}
}
mod field_benches {
use super::*;
fn field_inversion(c: &mut Criterion) {
c.bench_function("field inversion", |b| {
let a: [u8; 32] = [ //0x35863539 as 897987897u64
0x39, 0x35, 0x86, 0x35, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
];
let s = FieldElement::from_bytes(&a).invert();
b.iter(|| s.invert());
});
}
fn field_mul(c: &mut Criterion) {
c.bench_function("field multiplication", |b| {
let a: [u8; 32] = [ //0x35863539 as 897987897u64
0x39, 0x35, 0x86, 0x35, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
];
let s = FieldElement::from_bytes(&a).invert();
b.iter(|| &s * &s );
});
}
fn field_add(c: &mut Criterion) {
c.bench_function("field add", |b| {
let a: [u8; 32] = [ //0x35863539 as 897987897u64
0x39, 0x35, 0x86, 0x35, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
];
let s = FieldElement::from_bytes(&a).invert();
b.iter(|| &s + &s );
});
}
fn field_sub(c: &mut Criterion) {
c.bench_function("field sub", |b| {
let a: [u8; 32] = [ //0x35863539 as 897987897u64
0x39, 0x35, 0x86, 0x35, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
];
let s = FieldElement::from_bytes(&a).invert();
b.iter(|| &s - &s );
});
}
criterion_group! {
name = field_benches;
config = Criterion::default();
targets =
field_inversion,
field_mul,
field_sub,
field_add,
}
}
在1核4G内存Ubuntu16.04系统下运行性能如下:
unning target/release/deps/dalek_benchmarks-53fcb1faec6cb376
Scalar inversion time: [11.761 us 11.819 us 11.893 us]
change: [-13.746% +0.2914% +17.272%] (p = 0.97 > 0.05)
No change in performance detected.
Found 17 outliers among 100 measurements (17.00%)
1 (1.00%) high mild
16 (16.00%) high severe
Scalar multiplication time: [170.81 ns 193.62 ns 218.47 ns]
change: [+2.0752% +17.017% +34.861%] (p = 0.03 < 0.05)
Performance has regressed.
Found 5 outliers among 100 measurements (5.00%)
3 (3.00%) high mild
2 (2.00%) high severe
Scalar add time: [63.678 ns 64.160 ns 64.790 ns]
Found 18 outliers among 100 measurements (18.00%)
18 (18.00%) high severe
Scalar sub time: [63.023 ns 63.360 ns 63.790 ns]
Found 17 outliers among 100 measurements (17.00%)
1 (1.00%) high mild
16 (16.00%) high severe
field inversion time: [3.6348 us 3.6528 us 3.6763 us]
change: [-9.9310% +2.1958% +16.053%] (p = 0.74 > 0.05)
No change in performance detected.
Found 17 outliers among 100 measurements (17.00%)
17 (17.00%) high severe
field multiplication time: [27.161 ns 27.300 ns 27.479 ns]
change: [-11.422% +1.2224% +15.799%] (p = 0.86 > 0.05)
No change in performance detected.
Found 19 outliers among 100 measurements (19.00%)
2 (2.00%) high mild
17 (17.00%) high severe
field sub time: [11.746 ns 11.810 ns 11.889 ns]
Found 17 outliers among 100 measurements (17.00%)
17 (17.00%) high severe
field add time: [11.541 ns 11.729 ns 11.965 ns]
Found 22 outliers among 100 measurements (22.00%)
1 (1.00%) high mild
21 (21.00%) high severe