supplement user key generation performance

This commit is contained in:
Sun Yimin 2022-06-13 17:35:46 +08:00 committed by GitHub
parent d6a464f470
commit ccdb7b0568
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 21 deletions

View File

@ -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.

View File

@ -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)

View File

@ -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) {