From a1d96a7d7d8ecaf57b23542900fc91bfa16f08dc Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 25 Aug 2021 15:43:08 +0300 Subject: [PATCH] keys: use elliptic package marshalling functions, #1319 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- pkg/crypto/keys/publickey.go | 23 ++--------------------- pkg/crypto/keys/publickey_test.go | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/pkg/crypto/keys/publickey.go b/pkg/crypto/keys/publickey.go index 311a7a207..876b044c2 100644 --- a/pkg/crypto/keys/publickey.go +++ b/pkg/crypto/keys/publickey.go @@ -143,29 +143,10 @@ func (p *PublicKey) getBytes(compressed bool) []byte { 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 p.Y.Bit(0) == 0 { - prefix = 0x02 - } else { - prefix = 0x03 - } - } else { - prefix = 0x04 - yBytes := p.Y.Bytes() - copy(res[1+coordLen+coordLen-len(yBytes):], yBytes) + return elliptic.MarshalCompressed(p.Curve, p.X, p.Y) } - res[0] = prefix - - return res + return elliptic.Marshal(p.Curve, p.X, p.Y) } // Bytes returns byte array representation of the public key in compressed diff --git a/pkg/crypto/keys/publickey_test.go b/pkg/crypto/keys/publickey_test.go index d84f8c605..ce01217e4 100644 --- a/pkg/crypto/keys/publickey_test.go +++ b/pkg/crypto/keys/publickey_test.go @@ -223,3 +223,17 @@ func BenchmarkPublicEqual(t *testing.B) { _ = 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() + } +}