tjfoc / gmsm

GM SM2/3/4 library based on Golang (基于Go语言的国密SM2/SM3/SM4算法库)
http://www.wutongchain.com
Apache License 2.0
1.75k stars 583 forks source link

SM2性能这么差? #177

Open liyimeng opened 1 year ago

liyimeng commented 1 year ago

根据这个文档 https://docs.chainmaker.org.cn/v2.1.0/html/tech/密码算法引擎.html

密码库 签名 验证 加密 解密
同济库 3194 691 680 984
北大gmssl 32793 17258 7633 8902
腾讯国密 43951 26937 29283 17054

本库的性能还不到其他库的10%,有没有什么办法提高SM2性能?

superajun-wsj commented 1 year ago

腾讯国密有Go版本吗?

duxin40 commented 1 year ago

同问

cuipeiyu commented 1 year ago

指的是这个吧 https://github.com/Tencent/TencentKonaSMSuite

cuipeiyu commented 1 year ago

指的是这个吧 https://github.com/Tencent/TencentKonaSMSuite

cuipeiyu commented 1 year ago

指的是这个吧 https://github.com/Tencent/TencentKonaSMSuite

aveyuan commented 1 year ago

是因为这个库是纯GO实现,北大是CGO,腾讯是JAVA,语言的区别?

superajun-wsj commented 1 year ago

存计算类的,感觉Go与C不应该这么大差距,同时也应该只比Java峰值性能差一点才正常

aveyuan commented 1 year ago

存计算类的,感觉Go与C不应该这么大差距,同时也应该只比Java峰值性能差一点才正常

卧槽,我刚刚自己试了试,跑Mark。几百。这,,,,,

Encode

Running tool: C:\Program Files\Go\bin\go.exe test -benchmem -run=^$ -bench ^BenchmarkEncSm2$ learning-test/sm2

goos: windows
goarch: amd64
pkg: learning-test/sm2
cpu: AMD Ryzen 7 5700U with Radeon Graphics         
BenchmarkEncSm2-16           933       1253870 ns/op       74418 B/op       1536 allocs/op
PASS
ok      learning-test/sm2   1.329s

再看看Decode

Running tool: C:\Program Files\Go\bin\go.exe test -benchmem -run=^$ -bench ^BenchmarkDecSm2$ learning-test/sm2

goos: windows
goarch: amd64
pkg: learning-test/sm2
cpu: AMD Ryzen 7 5700U with Radeon Graphics         
BenchmarkDecSm2-16          1164       1021186 ns/op       65469 B/op       1352 allocs/op
PASS
ok      learning-test/sm2   1.319s
aveyuan commented 1 year ago

我测试了对称加密,可以达到100w

goos: windows
goarch: amd64
pkg: encrypt/cecsm
cpu: AMD Ryzen 7 5700U with Radeon Graphics         
BenchmarkDecode-16       1000000          1141 ns/op         368 B/op         11 allocs/op
PASS
ok      encrypt/cecsm   1.198s
johnshajiang commented 11 months ago

根据这个文档 https://docs.chainmaker.org.cn/v2.1.0/html/tech/密码算法引擎.html

这个文档里面所说的腾讯国密库,应该是指的TencentSM,而不是[TencentKonaSMSuite]。 TencentSM是一个C语实现的国密密码学算法库,并没有开源;TencentKonaSMSuite是纯Java实现的国密密码算法与安全协议协议的组件。

[TencentKonaSMSuite]: https://github.com/Tencent/TencentKonaSMSuite

emmansun commented 8 months ago

关于加密软件性能,我尝试从以下几个方面解释:

当然性能是一方面,安全性也非常重要,实现最好是做到常量时间运行,也就是说运行性能和密钥等机密信息的长短、大小等不产生关系,以防侧信道攻击。

所以,选择密码软件,最好能进行相关方面的测试,开源是好的选择,因为你能看到实现细节、判断实现的安全性(当然需要一定的知识)。闭源软件,一个是进行针对性测试,另外一个,如有条件,购买之前最好请第三方进行独立审计,或者有“权威”机构的检测报告。

johnshajiang commented 8 months ago

@emmansun 请问,是否研究过[Tongsuo]的SM2实现? 它新近发布的8.4.0版本就对SM2有较大的性能改进。相关的PR应该是:https://github.com/Tongsuo-Project/Tongsuo/pull/446

[Tongsuo]: https://github.com/Tongsuo-Project/Tongsuo/

emmansun commented 8 months ago

@johnshajiang 没有看过。Go语言的话可以考虑使用emmansun/gmsm。

liyimeng commented 8 months ago

@emmansun 感谢分享!emmansun/gmsm的性能是不是比这个有改进?

emmansun commented 8 months ago

@emmansun 感谢分享!emmansun/gmsm的性能是不是比这个有改进?

这是发布v0.24.1时跑的结果,分别是SM2和NIST P256,测试代码在https://github.com/emmansun/gmsm/blob/main/sm2/sm2_test.go 中

go: v1.21.1
goos: windows
goarch: amd64
pkg: github.com/emmansun/gmsm/sm2
cpu: Intel(R) Core(TM) i5-9500 CPU @ 3.00GHz
BenchmarkSign_SM2
BenchmarkSign_SM2-6
   80709         14215 ns/op        1544 B/op         22 allocs/op
BenchmarkVerify_SM2
BenchmarkVerify_SM2-6
   19902         60295 ns/op         848 B/op         17 allocs/op   
BenchmarkSign_P256
BenchmarkSign_P256-6
   51289         23286 ns/op        2440 B/op         33 allocs/op
BenchmarkVerify_P256
BenchmarkVerify_P256-6
   17524         67679 ns/op         944 B/op         20 allocs/op

你可以通过SM2性能优化(续)了解一下大概,当然有兴趣也可以直接浏览实现代码。

MaoJianwei commented 8 months ago

很棒的库!希望能做更多的封装,让使用更方便一些。类似tjfoc/gmsm这个库里面的sm4.SM4Cbc等全局函数,一次调用就可以完成加密,减少手工padding、创建cipher对象等重复工作:) @emmansun

@emmansun 感谢分享!emmansun/gmsm的性能是不是比这个有改进?

这是发布v0.24.1时跑的结果,分别是SM2和NIST P256,测试代码在https://github.com/emmansun/gmsm/blob/main/sm2/sm2_test.go 中

go: v1.21.1
goos: windows
goarch: amd64
pkg: github.com/emmansun/gmsm/sm2
cpu: Intel(R) Core(TM) i5-9500 CPU @ 3.00GHz
BenchmarkSign_SM2
BenchmarkSign_SM2-6
   80709       14215 ns/op        1544 B/op         22 allocs/op
BenchmarkVerify_SM2
BenchmarkVerify_SM2-6
   19902       60295 ns/op         848 B/op         17 allocs/op   
BenchmarkSign_P256
BenchmarkSign_P256-6
   51289       23286 ns/op        2440 B/op         33 allocs/op
BenchmarkVerify_P256
BenchmarkVerify_P256-6
   17524       67679 ns/op         944 B/op         20 allocs/op

你可以通过SM2性能优化(续)了解一下大概,当然有兴趣也可以直接浏览实现代码。

liyimeng commented 8 months ago

@emmansun 再次感谢分享,已经加星!