From ccdb7b056864701db900ceb6a8a1a9b8a7a9c47e Mon Sep 17 00:00:00 2001 From: Sun Yimin Date: Mon, 13 Jun 2022 17:35:46 +0800 Subject: [PATCH] supplement user key generation performance --- sm9/README.md | 16 ++++++++++++++++ sm9/sm9.go | 10 ---------- sm9/sm9_key.go | 33 ++++++++++++++++++++++----------- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/sm9/README.md b/sm9/README.md index 51846e1e..6143cfe9 100644 --- a/sm9/README.md +++ b/sm9/README.md @@ -37,4 +37,20 @@ This part codes mainly refer two projects: cpu: Intel(R) Core(TM) i5-9500 CPU @ 3.00GHz BenchmarkDecrypt-6 507 2345492 ns/op 202360 B/op 5228 allocs/op +**SM9 Generate User Sign Private Key Benchmark** + + goos: windows + goarch: amd64 + pkg: github.com/emmansun/gmsm/sm9 + cpu: Intel(R) Core(TM) i5-9500 CPU @ 3.00GHz + BenchmarkGenerateSignPrivKey-6 8078 147638 ns/op 3176 B/op 47 allocs/op + +**SM9 Generate User Encrypt Private Key Benchmark** + + goos: windows + goarch: amd64 + pkg: github.com/emmansun/gmsm/sm9 + cpu: Intel(R) Core(TM) i5-9500 CPU @ 3.00GHz + BenchmarkGenerateEncryptPrivKey-6 3445 326796 ns/op 3433 B/op 47 allocs/op + To further improve `Verify()/Decrypt()` performance, need to improve `Pair()` method performance. diff --git a/sm9/sm9.go b/sm9/sm9.go index ace90237..38954f02 100644 --- a/sm9/sm9.go +++ b/sm9/sm9.go @@ -260,16 +260,6 @@ func (pub *SignMasterPublicKey) Verify(uid []byte, hid byte, hash, sig []byte) b return VerifyASN1(pub, uid, hid, hash, sig) } -func (pub *EncryptMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) *G1 { - var buffer []byte - buffer = append(buffer, uid...) - buffer = append(buffer, hid) - h1 := hashH1(buffer) - p := new(G1).ScalarBaseMult(h1) - p.Add(p, pub.MasterPublicKey) - return p -} - func (pub *EncryptMasterPublicKey) Pair() *GT { pub.pairOnce.Do(func() { pub.basePoint = Pair(pub.MasterPublicKey, Gen2) diff --git a/sm9/sm9_key.go b/sm9/sm9_key.go index 946ddf7b..bf8023c8 100644 --- a/sm9/sm9_key.go +++ b/sm9/sm9_key.go @@ -105,6 +105,17 @@ func (master *SignMasterPrivateKey) Public() *SignMasterPublicKey { return &master.SignMasterPublicKey } +// GenerateUserPublicKey generate user sign public key +func (pub *SignMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) *G2 { + var buffer []byte + buffer = append(buffer, uid...) + buffer = append(buffer, hid) + h1 := hashH1(buffer) + p := new(G2).ScalarBaseMult(h1) + p.Add(p, pub.MasterPublicKey) + return p +} + // MarshalASN1 marshal sign master public key to asn.1 format data according // SM9 cryptographic algorithm application specification func (pub *SignMasterPublicKey) MarshalASN1() ([]byte, error) { @@ -132,17 +143,6 @@ func (pub *SignMasterPublicKey) UnmarshalASN1(der []byte) error { return nil } -// GenerateUserPublicKey generate user sign public key -func (pub *SignMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) *G2 { - var buffer []byte - buffer = append(buffer, uid...) - buffer = append(buffer, hid) - h1 := hashH1(buffer) - p := new(G2).ScalarBaseMult(h1) - p.Add(p, pub.MasterPublicKey) - return p -} - // MasterPublic returns the master public key corresponding to priv. func (priv *SignPrivateKey) MasterPublic() *SignMasterPublicKey { return &priv.SignMasterPublicKey @@ -243,6 +243,17 @@ func (master *EncryptMasterPrivateKey) UnmarshalASN1(der []byte) error { return nil } +// GenerateUserPublicKey generate user encrypt public key +func (pub *EncryptMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) *G1 { + var buffer []byte + buffer = append(buffer, uid...) + buffer = append(buffer, hid) + h1 := hashH1(buffer) + p := new(G1).ScalarBaseMult(h1) + p.Add(p, pub.MasterPublicKey) + return p +} + // MarshalASN1 marshal encrypt master public key to asn.1 format data according // SM9 cryptographic algorithm application specification func (pub *EncryptMasterPublicKey) MarshalASN1() ([]byte, error) {