#sm2 #elliptic-curve #performance #standard

efficient-sm2

中国标准签名算法(SM2)的Rust库

7个版本

0.2.4 2024年7月19日
0.2.3 2023年9月1日
0.2.1 2023年8月24日
0.2.0 2023年2月17日
0.0.1 2020年12月18日

#156 in 密码学

Download history 8/week @ 2024-04-26 1/week @ 2024-05-10 1/week @ 2024-05-17 2/week @ 2024-05-31 3/week @ 2024-06-07 3/week @ 2024-06-14 6/week @ 2024-06-21 2/week @ 2024-06-28 4/week @ 2024-07-05 4/week @ 2024-07-12 139/week @ 2024-07-19 25/week @ 2024-07-26 2/week @ 2024-08-02 3/week @ 2024-08-09

169 每月下载次数
用于 3 个crate(2个直接)

Apache-2.0

1.5MB
18K SLoC

软件按“原样”提供,姚鹏飞和作者不就本软件提供任何明示或暗示的保证,包括但不限于适用性和对特定目的的适用性。在任何情况下,姚鹏飞或作者不对任何特殊、直接、间接或后果性损害或任何由于使用、数据丢失或利润损失而产生的任何损害负责,无论此类损害是基于合同、疏忽或其他侵权行为,无论此类损害是否与使用或性能本软件有关。

efficient-sm2

此仓库是纯Rust编写的SM2签名/验证算法,性能优于其他SM2库。此外,您可以更改结构体 CURVE_PARAMS 为其他椭圆曲线的参数,并享受更好的性能。

用法

use rand::RngCore;

fn main() {
    let test_word = b"hello world";

    let mut private_key = [0; 32];
    rand::thread_rng().fill_bytes(&mut private_key);

    let key_pair = efficient_sm2::KeyPair::new(&private_key).unwrap();

    // signing in sm2
    let sig = key_pair.sign(test_word).unwrap();

    // verification sm2 signature
    sig.verify(&key_pair.public_key(), test_word).unwrap();
}

基准测试

 cargo +nightly bench --workspace --features internal_benches

结果

test ec::signing::sign_bench::es_sign_bench                ... bench:      52,532 ns/iter (+/- 730)
test ec::signing::sign_bench::es_sign_without_sm3_bench    ... bench:      27,474 ns/iter (+/- 336)
test ec::signing::sign_bench::es_verify_bench              ... bench:     117,975 ns/iter (+/- 992)
test ec::signing::sign_bench::libsm_sign_bench             ... bench:     105,183 ns/iter (+/- 1,347)
test ec::signing::sign_bench::libsm_sign_without_sm3_bench ... bench:      82,389 ns/iter (+/- 1,235)
test ec::signing::sign_bench::libsm_verify_bench           ... bench:     562,687 ns/iter (+/- 43,833)
test sm2p256::sm2_bench::add_mod_bench                     ... bench:           1 ns/iter (+/- 0)
test sm2p256::sm2_bench::base_point_mul_bench              ... bench:       8,387 ns/iter (+/- 70)
test sm2p256::sm2_bench::big_number_bench                  ... bench:         177 ns/iter (+/- 0)
test sm2p256::sm2_bench::libsm_mul_mod_bench               ... bench:          99 ns/iter (+/- 7)
test sm2p256::sm2_bench::mont_pro_bench                    ... bench:          29 ns/iter (+/- 0)
test sm2p256::sm2_bench::point_add_bench                   ... bench:         269 ns/iter (+/- 3)
test sm2p256::sm2_bench::point_double_bench                ... bench:         267 ns/iter (+/- 4)
test sm2p256::sm2_bench::point_mul_bench                   ... bench:      90,584 ns/iter (+/- 2,239)
test sm2p256::sm2_bench::shl_bak_bench                     ... bench:          43 ns/iter (+/- 0)
test sm2p256::sm2_bench::shl_bench                         ... bench:           5 ns/iter (+/- 0)
test sm2p256::sm2_bench::sub_mod_bench                     ... bench:           2 ns/iter (+/- 0)

ps. 基准测试环境

  • cpu: amd r7 4800-h
  • memory: 32g
  • os: ubuntu 20.04

依赖项

~1.1–1.7MB
~34K SLoC