curve25519-dalek中field域内和scalar域内的运算性能对比

本文对比了Curve25519-Dalek库中field域和scalar域的计算性能,发现field域计算表现更优。通过对加减乘除等基本运算的benchmark测试,具体分析了不同场景下的性能差异。

通过修改lib.rs文件,将fieldbackendmodule改为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 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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值