curve25519-dalek中field域内和scalar域内的运算性能对比
生活随笔
收集整理的這篇文章主要介紹了
curve25519-dalek中field域内和scalar域内的运算性能对比
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
通過修改lib.rs文件,將field和backendmodule改為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<Scalar> = (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 897987897u640x39, 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 897987897u640x39, 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 897987897u640x39, 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 897987897u640x39, 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 mild16 (16.00%) high severeScalar 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 mild2 (2.00%) high severeScalar add time: [63.678 ns 64.160 ns 64.790 ns] Found 18 outliers among 100 measurements (18.00%)18 (18.00%) high severeScalar sub time: [63.023 ns 63.360 ns 63.790 ns] Found 17 outliers among 100 measurements (17.00%)1 (1.00%) high mild16 (16.00%) high severefield 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 severefield 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 mild17 (17.00%) high severefield sub time: [11.746 ns 11.810 ns 11.889 ns] Found 17 outliers among 100 measurements (17.00%)17 (17.00%) high severefield add time: [11.541 ns 11.729 ns 11.965 ns] Found 22 outliers among 100 measurements (22.00%)1 (1.00%) high mild21 (21.00%) high severe總結
以上是生活随笔為你收集整理的curve25519-dalek中field域内和scalar域内的运算性能对比的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 螺丝孔的视觉定位检测软硬件方案--康耐德
- 下一篇: 请教大侠如何用c语言编写搬家的简单程序