Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

国密库签名验签性能太低 #31

Open
hupeng1234 opened this issue Dec 31, 2020 · 1 comment
Open

国密库签名验签性能太低 #31

hupeng1234 opened this issue Dec 31, 2020 · 1 comment

Comments

@hupeng1234
Copy link

1.3.2版本的国密签名验签比标准库ecda低20倍左右,而网安的是ecdsa的2倍速。
image

@hupeng1234
Copy link
Author

package main

import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"fmt"
wanganSM2 "github.com/Hyperledger-TWGC/ccs-gm/sm2"
twGCSM2 "github.com/Hyperledger-TWGC/tjfoc-gm/sm2"
"github.com/tjfoc/gmsm/sm2"
"github.com/tjfoc/gmsm/sm3"
"log"
"testing"
)

func BenchmarkSM2(t *testing.B) {
//t.ReportAllocs()
msg := []byte("hello")
priv, err := sm2.GenerateKey() // 生成密钥对
if err != nil {
log.Fatal(err)
}
t.ResetTimer()
for i := 0; i < t.N; i++ {
sign, err := priv.Sign(rand.Reader, msg, nil) // 签名
if err != nil {
log.Fatal(err)
}
priv.Verify(msg, sign) // 密钥验证
// if ok != true {
// fmt.Printf("Verify error\n")
// } else {
// fmt.Printf("Verify ok\n")
// }
}
}

func BenchmarkTWGCSM2(t *testing.B) {
msg := []byte("test")
priv, err := twGCSM2.GenerateKey(nil) // 生成密钥对
if err != nil {
t.Fatal(err)
}
t.ResetTimer()
for i := 0; i < t.N; i++ {
sign, err := priv.Sign(nil, msg, nil) // 签名
if err != nil {
t.Fatal(err)
}
priv.Verify(msg, sign) // 密钥验证
}
}

func BenchmarkWSWananSignAndVerify(b *testing.B) {
hashed := []byte("hello")
priv, _ := wanganSM2.GenerateKey(rand.Reader)

for i := 0; i < b.N; i++ {
	r, s, err := wanganSM2.Sign(rand.Reader, priv, hashed)
	if err != nil {
		panic(err)
	}
	ok := wanganSM2.Verify(&priv.PublicKey, hashed, r, s)
	if !ok {
		panic("xxx")
	}

}

}

func BenchmarkWSWananSignAndVerify2(b *testing.B) {
hashed := []byte("hello")
priv, _ := wanganSM2.GenerateKey(rand.Reader)

for i := 0; i < b.N; i++ {
	signature, err := priv.Sign(rand.Reader, hashed, nil)
	if err != nil {
		panic(err)
	}
	pub := priv.PublicKey
	ok := pub.Verify(hashed, signature)
	if !ok {
		panic("xxx")
	}

}

}

func BenchmarkEcdsaSignVerify(b *testing.B) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
panic(err)
}
msg := []byte("hello")
b.ResetTimer()
for i := 0; i < b.N; i++ {
r, s, err := ecdsa.Sign(rand.Reader, key, msg)
if err != nil {
panic(err)
}
ok := ecdsa.Verify(&key.PublicKey, msg, r, s)
if !ok {
panic(fmt.Sprintf("ecdsa failed to verify signature at index: %d", i))
}
}
}

func BenchmarkSM2SignVerify(b *testing.B) {
key, err := sm2.GenerateKey()
if err != nil {
panic(err)
}
msg := []byte("hello")
b.ResetTimer()
for i := 0; i < b.N; i++ {
r, s, err := sm2.Sm2Sign(key, msg, nil)
if err != nil {
panic(err)
}
ok := sm2.Sm2Verify(&key.PublicKey, msg, nil, r, s)
if !ok {
panic(fmt.Sprintf("sm2 failed to verify signature at index: %d", i))
}
}
}

func BenchmarkTjfocSM2SignVerify(b *testing.B) {
key, err := twGCSM2.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
msg := []byte("hello")
b.ResetTimer()
for i := 0; i < b.N; i++ {
r, s, err := twGCSM2.Sm2Sign(key, msg, nil, rand.Reader)
if err != nil {
panic(err)
}
ok := twGCSM2.Sm2Verify(&key.PublicKey, msg, nil, r, s)
if !ok {
panic(fmt.Sprintf("sm2 failed to verify signature at index: %d", i))
}
}
}

func BenchmarkEcdsaSignVerify2(b *testing.B) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
panic(err)
}
msg := []byte("hello")
b.ResetTimer()
for i := 0; i < b.N; i++ {
h := sm3.New()
h.Write(msg)
digest := h.Sum(nil)[:32]
r, s, err := ecdsa.Sign(rand.Reader, key, digest)
if err != nil {
panic(err)
}

	h = sm3.New()
	h.Write(msg)
	digest = h.Sum(nil)[:32]
	ok := ecdsa.Verify(&key.PublicKey, digest, r, s)
	if !ok {
		panic(fmt.Sprintf("!!! ecdsa failed to verify signature at index: %d", i))
	}
}

}

func BenchmarkSM2SignVerify2(b *testing.B) {
key, err := sm2.GenerateKey()
if err != nil {
panic(err)
}
msg := []byte("hello")
b.ResetTimer()
for i := 0; i < b.N; i++ {
signature, err := SM2Sign(key, msg)
if err != nil {
panic(err)
}
ok, err := SM2Verify(&key.PublicKey, msg, signature)
if err != nil {
panic(err)
}
if !ok {
panic(fmt.Sprintf("!!! sm2 failed to verify signature at index: %d", i))
}
}
}

func BenchmarkSha256(b *testing.B) {
msg := []byte("hello")
for i := 0; i < b.N; i++ {
h := sha256.New()
h.Write(msg)
h.Sum(nil)
}
}

func BenchmarkSM3(b *testing.B) {
msg := []byte("hello")
for i := 0; i < b.N; i++ {
h := sm3.New()
h.Write(msg)
h.Sum(nil)
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant