Skip to content

Commit

Permalink
supplement user key generation performance
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Jun 13, 2022
1 parent d6a464f commit ccdb7b0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
16 changes: 16 additions & 0 deletions sm9/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 0 additions & 10 deletions sm9/sm9.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 22 additions & 11 deletions sm9/sm9_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ccdb7b0

Please sign in to comment.