From 32a064aa31e013bc8b70fcd10d809b6a755d6815 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 12 Feb 2020 18:08:33 +0300 Subject: [PATCH] keys: add Cmp method to PublicKey It can be used by code that doesn't operate with PublicKeys, but still needs to be able to compare keys for some purposes. --- pkg/crypto/keys/publickey.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/pkg/crypto/keys/publickey.go b/pkg/crypto/keys/publickey.go index be1435fea..b22e2f9f3 100644 --- a/pkg/crypto/keys/publickey.go +++ b/pkg/crypto/keys/publickey.go @@ -22,17 +22,7 @@ type PublicKeys []*PublicKey func (keys PublicKeys) Len() int { return len(keys) } func (keys PublicKeys) Swap(i, j int) { keys[i], keys[j] = keys[j], keys[i] } func (keys PublicKeys) Less(i, j int) bool { - if keys[i].X.Cmp(keys[j].X) == -1 { - return true - } - if keys[i].X.Cmp(keys[j].X) == 1 { - return false - } - if keys[i].X.Cmp(keys[j].X) == 0 { - return false - } - - return keys[i].Y.Cmp(keys[j].Y) == -1 + return keys[i].Cmp(keys[j]) == -1 } // DecodeBytes decodes a PublicKeys from the given slice of bytes. @@ -75,6 +65,15 @@ func (p *PublicKey) Equal(key *PublicKey) bool { return p.X.Cmp(key.X) == 0 && p.Y.Cmp(key.Y) == 0 } +// Cmp compares two keys. +func (p *PublicKey) Cmp(key *PublicKey) int { + xCmp := p.X.Cmp(key.X) + if xCmp != 0 { + return xCmp + } + return p.Y.Cmp(key.Y) +} + // NewPublicKeyFromString returns a public key created from the // given hex string. func NewPublicKeyFromString(s string) (*PublicKey, error) {