keys: use elliptic package marshalling functions, #1319

name                       old time/op    new time/op    delta
PublicBytes-8                81.4ns ± 6%    71.2ns ± 8%  -12.56%  (p=0.000 n=10+10)
PublicUncompressedBytes-8    93.2ns ±17%    72.5ns ±14%  -22.25%  (p=0.000 n=10+10)

name                       old alloc/op   new alloc/op   delta
PublicBytes-8                 80.0B ± 0%     48.0B ± 0%  -40.00%  (p=0.000 n=10+10)
PublicUncompressedBytes-8     80.0B ± 0%     48.0B ± 0%  -40.00%  (p=0.000 n=10+10)

name                       old allocs/op  new allocs/op  delta
PublicBytes-8                  2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.000 n=10+10)
PublicUncompressedBytes-8      2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.000 n=10+10)
This commit is contained in:
Roman Khimov 2021-08-25 15:43:08 +03:00
parent 217d7bdf44
commit a1d96a7d7d
2 changed files with 16 additions and 21 deletions

View file

@ -143,29 +143,10 @@ func (p *PublicKey) getBytes(compressed bool) []byte {
return []byte{0x00} return []byte{0x00}
} }
var resLen = 1 + coordLen
if !compressed {
resLen += coordLen
}
var res = make([]byte, resLen)
var prefix byte
xBytes := p.X.Bytes()
copy(res[1+coordLen-len(xBytes):], xBytes)
if compressed { if compressed {
if p.Y.Bit(0) == 0 { return elliptic.MarshalCompressed(p.Curve, p.X, p.Y)
prefix = 0x02
} else {
prefix = 0x03
}
} else {
prefix = 0x04
yBytes := p.Y.Bytes()
copy(res[1+coordLen+coordLen-len(yBytes):], yBytes)
} }
res[0] = prefix return elliptic.Marshal(p.Curve, p.X, p.Y)
return res
} }
// Bytes returns byte array representation of the public key in compressed // Bytes returns byte array representation of the public key in compressed

View file

@ -223,3 +223,17 @@ func BenchmarkPublicEqual(t *testing.B) {
_ = k11.Equal(k2) _ = k11.Equal(k2)
} }
} }
func BenchmarkPublicBytes(t *testing.B) {
k := getPubKey(t)
for n := 0; n < t.N; n++ {
_ = k.Bytes()
}
}
func BenchmarkPublicUncompressedBytes(t *testing.B) {
k := getPubKey(t)
for n := 0; n < t.N; n++ {
_ = k.Bytes()
}
}