diff --git a/shared/bls/bls.go b/shared/bls/bls.go index 89326c7fae..c240cc73cc 100644 --- a/shared/bls/bls.go +++ b/shared/bls/bls.go @@ -153,10 +153,8 @@ func (p *PublicKey) Marshal() []byte { // Copy the public key to a new pointer reference. func (p *PublicKey) Copy() (*PublicKey, error) { - rawBytes := p.p.Serialize() - newKey := &bls12.PublicKey{} - err := newKey.Deserialize(rawBytes) - return &PublicKey{p: newKey}, err + np := *p.p + return &PublicKey{p: &np}, nil } // Aggregate two public keys. diff --git a/shared/bls/bls_test.go b/shared/bls/bls_test.go index eca0d320ce..d50f956c90 100644 --- a/shared/bls/bls_test.go +++ b/shared/bls/bls_test.go @@ -278,3 +278,15 @@ func TestSignatureFromBytes(t *testing.T) { }) } } + +func TestPublicKey_Copy(t *testing.T) { + pubkeyA := bls.RandKey().PublicKey() + pubkeyBytes := pubkeyA.Marshal() + + pubkeyB, _ := pubkeyA.Copy() + pubkeyB.Aggregate(bls.RandKey().PublicKey()) + + if !bytes.Equal(pubkeyA.Marshal(), pubkeyBytes) { + t.Fatal("Pubkey was mutated after copy") + } +} \ No newline at end of file