Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fasmat committed Jun 13, 2024
1 parent 7decac8 commit f98d823
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 104 deletions.
1 change: 1 addition & 0 deletions activation/wire/malfeasance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wire
// ATXVersion is an identifier to allow for different versions of ATXs to be part of a proof.
type ATXVersion uint8

// TODO(mafa): this is for proofs that involve ATXs from different versions. This is not yet implemented.
const (
ATXVersion1 ATXVersion = 1
ATXVersion2 ATXVersion = 2
Expand Down
44 changes: 18 additions & 26 deletions activation/wire/malfeasance_double_marry.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,32 +135,21 @@ func certificateProof(certs MarriageCertificates, index uint64) ([]types.Hash32,
return proofHashes, nil
}

func (p ProofDoubleMarry) Valid(edVerifier *signing.EdVerifier) (bool, error) {
func (p ProofDoubleMarry) Valid(edVerifier *signing.EdVerifier) error {
if p.Proofs[0].ATXID == p.Proofs[1].ATXID {
return false, errors.New("proofs have the same ATX ID")
return errors.New("proofs have the same ATX ID")
}

if p.Proofs[0].NodeID != p.Proofs[1].NodeID {
return false, errors.New("proofs have different node IDs")
return errors.New("proofs have different node IDs")
}

atx1Valid, err := p.Proofs[0].Valid(edVerifier)
if err != nil {
return false, fmt.Errorf("proof 1 is invalid: %w", err)
}
if !atx1Valid {
return false, nil
if err := p.Proofs[0].Valid(edVerifier); err != nil {
return fmt.Errorf("proof 1 is invalid: %w", err)
}

atx2Valid, err := p.Proofs[1].Valid(edVerifier)
if err != nil {
return false, fmt.Errorf("proof 2 is invalid: %w", err)
if err := p.Proofs[1].Valid(edVerifier); err != nil {
return fmt.Errorf("proof 2 is invalid: %w", err)
}
if !atx2Valid {
return false, nil
}

return true, nil
return nil
}

type MarryProof struct {
Expand All @@ -185,14 +174,14 @@ type MarryProof struct {
Signature types.EdSignature
}

func (p MarryProof) Valid(edVerifier *signing.EdVerifier) (bool, error) {
func (p MarryProof) Valid(edVerifier *signing.EdVerifier) error {
if !edVerifier.Verify(signing.ATX, p.SmesherID, p.ATXID.Bytes(), p.Signature) {
return false, errors.New("invalid ATX signature")
return errors.New("invalid ATX signature")
}

// TODO(mafa): check domain
if !edVerifier.Verify(signing.ATX, p.NodeID, p.SmesherID.Bytes(), p.CertificateSignature) {
return false, errors.New("invalid certificate signature")
return errors.New("invalid certificate signature")
}

proof := make([][]byte, len(p.MarriageProof))
Expand All @@ -207,10 +196,10 @@ func (p MarryProof) Valid(edVerifier *signing.EdVerifier) (bool, error) {
atxTreeHash,
)
if err != nil {
return false, fmt.Errorf("validate marriage proof: %w", err)
return fmt.Errorf("validate marriage proof: %w", err)
}
if !ok {
return false, nil
return errors.New("invalid marriage proof")
}

mc := MarriageCertificate{
Expand All @@ -230,7 +219,10 @@ func (p MarryProof) Valid(edVerifier *signing.EdVerifier) (bool, error) {
atxTreeHash,
)
if err != nil {
return false, fmt.Errorf("validate certificate proof: %w", err)
return fmt.Errorf("validate certificate proof: %w", err)
}
if !ok {
return errors.New("invalid certificate proof")
}
return ok, nil
return nil
}
63 changes: 26 additions & 37 deletions activation/wire/malfeasance_double_marry_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wire

import (
"slices"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -33,9 +34,7 @@ func Test_DoubleMarryProof(t *testing.T) {
require.NoError(t, err)

verifier := signing.NewEdVerifier()
ok, err := proof.Valid(verifier)
require.NoError(t, err)
require.True(t, ok)
require.NoError(t, proof.Valid(verifier))
})

t.Run("does not contain same certificate owner", func(t *testing.T) {
Expand Down Expand Up @@ -72,9 +71,8 @@ func Test_DoubleMarryProof(t *testing.T) {
}

verifier := signing.NewEdVerifier()
ok, err := proof.Valid(verifier)
err = proof.Valid(verifier)
require.ErrorContains(t, err, "proofs have different node IDs")
require.False(t, ok)
})

t.Run("same ATX ID", func(t *testing.T) {
Expand All @@ -98,9 +96,8 @@ func Test_DoubleMarryProof(t *testing.T) {
}

verifier := signing.NewEdVerifier()
ok, err := proof.Valid(verifier)
err = proof.Valid(verifier)
require.ErrorContains(t, err, "same ATX ID")
require.False(t, ok)
})

t.Run("invalid marriage proof", func(t *testing.T) {
Expand All @@ -111,8 +108,8 @@ func Test_DoubleMarryProof(t *testing.T) {
atx1.Sign(sig)

atx2 := newActivationTxV2(
WithMarriageCertificate(otherSig, sig.NodeID()),
WithMarriageCertificate(sig, sig.NodeID()),
WithMarriageCertificate(otherSig, otherSig.NodeID()),
WithMarriageCertificate(sig, otherSig.NodeID()),
)
atx2.Sign(otherSig)

Expand All @@ -132,7 +129,7 @@ func Test_DoubleMarryProof(t *testing.T) {
ATXID: atx1.ID(),
NodeID: sig.NodeID(),
MarriageRoot: types.Hash32(atx1.Marriages.Root()),
MarriageProof: proof1,
MarriageProof: slices.Clone(proof1),
CertificateSignature: atx1.Marriages[0].Signature,
CertificateIndex: 0,
CertificateProof: certProof1,
Expand All @@ -143,7 +140,7 @@ func Test_DoubleMarryProof(t *testing.T) {
ATXID: atx2.ID(),
NodeID: sig.NodeID(),
MarriageRoot: types.Hash32(atx2.Marriages.Root()),
MarriageProof: proof2,
MarriageProof: slices.Clone(proof2),
CertificateSignature: atx2.Marriages[1].Signature,
CertificateIndex: 1,
CertificateProof: certProof2,
Expand All @@ -155,15 +152,13 @@ func Test_DoubleMarryProof(t *testing.T) {

verifier := signing.NewEdVerifier()
proof.Proofs[0].MarriageProof[0] = types.RandomHash()
ok, err := proof.Valid(verifier)
require.NoError(t, err)
require.False(t, ok)
err = proof.Valid(verifier)
require.ErrorContains(t, err, "proof 1 is invalid: invalid marriage proof")

proof.Proofs[0].MarriageProof[0] = proof1[0]
proof.Proofs[1].MarriageProof[0] = types.RandomHash()
ok, err = proof.Valid(verifier)
require.NoError(t, err)
require.False(t, ok)
err = proof.Valid(verifier)
require.ErrorContains(t, err, "proof 2 is invalid: invalid marriage proof")
})

t.Run("invalid certificate proof", func(t *testing.T) {
Expand All @@ -174,8 +169,8 @@ func Test_DoubleMarryProof(t *testing.T) {
atx1.Sign(sig)

atx2 := newActivationTxV2(
WithMarriageCertificate(otherSig, sig.NodeID()),
WithMarriageCertificate(sig, sig.NodeID()),
WithMarriageCertificate(otherSig, otherSig.NodeID()),
WithMarriageCertificate(sig, otherSig.NodeID()),
)
atx2.Sign(otherSig)

Expand All @@ -198,7 +193,7 @@ func Test_DoubleMarryProof(t *testing.T) {
MarriageProof: proof1,
CertificateSignature: atx1.Marriages[0].Signature,
CertificateIndex: 0,
CertificateProof: certProof1,
CertificateProof: slices.Clone(certProof1),
SmesherID: atx1.SmesherID,
Signature: atx1.Signature,
},
Expand All @@ -209,7 +204,7 @@ func Test_DoubleMarryProof(t *testing.T) {
MarriageProof: proof2,
CertificateSignature: atx2.Marriages[1].Signature,
CertificateIndex: 1,
CertificateProof: certProof2,
CertificateProof: slices.Clone(certProof2),
SmesherID: atx2.SmesherID,
Signature: atx2.Signature,
},
Expand All @@ -218,15 +213,13 @@ func Test_DoubleMarryProof(t *testing.T) {

verifier := signing.NewEdVerifier()
proof.Proofs[0].CertificateProof[0] = types.RandomHash()
ok, err := proof.Valid(verifier)
require.NoError(t, err)
require.False(t, ok)
err = proof.Valid(verifier)
require.ErrorContains(t, err, "proof 1 is invalid: invalid certificate proof")

proof.Proofs[0].CertificateProof[0] = certProof1[0]
proof.Proofs[1].CertificateProof[0] = types.RandomHash()
ok, err = proof.Valid(verifier)
require.NoError(t, err)
require.False(t, ok)
err = proof.Valid(verifier)
require.ErrorContains(t, err, "proof 2 is invalid: invalid certificate proof")
})

t.Run("invalid atx signature", func(t *testing.T) {
Expand All @@ -248,15 +241,13 @@ func Test_DoubleMarryProof(t *testing.T) {
verifier := signing.NewEdVerifier()

proof.Proofs[0].Signature = types.RandomEdSignature()
ok, err := proof.Valid(verifier)
err = proof.Valid(verifier)
require.ErrorContains(t, err, "proof 1 is invalid: invalid ATX signature")
require.False(t, ok)

proof.Proofs[0].Signature = atx1.Signature
proof.Proofs[1].Signature = types.RandomEdSignature()
ok, err = proof.Valid(verifier)
err = proof.Valid(verifier)
require.ErrorContains(t, err, "proof 2 is invalid: invalid ATX signature")
require.False(t, ok)
})

t.Run("invalid certificate signature", func(t *testing.T) {
Expand All @@ -267,8 +258,8 @@ func Test_DoubleMarryProof(t *testing.T) {
atx1.Sign(sig)

atx2 := newActivationTxV2(
WithMarriageCertificate(otherSig, sig.NodeID()),
WithMarriageCertificate(sig, sig.NodeID()),
WithMarriageCertificate(otherSig, otherSig.NodeID()),
WithMarriageCertificate(sig, otherSig.NodeID()),
)
atx2.Sign(otherSig)

Expand All @@ -278,14 +269,12 @@ func Test_DoubleMarryProof(t *testing.T) {
verifier := signing.NewEdVerifier()

proof.Proofs[0].CertificateSignature = types.RandomEdSignature()
ok, err := proof.Valid(verifier)
err = proof.Valid(verifier)
require.ErrorContains(t, err, "proof 1 is invalid: invalid certificate signature")
require.False(t, ok)

proof.Proofs[0].CertificateSignature = atx1.Marriages[1].Signature
proof.Proofs[1].CertificateSignature = types.RandomEdSignature()
ok, err = proof.Valid(verifier)
err = proof.Valid(verifier)
require.ErrorContains(t, err, "proof 2 is invalid: invalid certificate signature")
require.False(t, ok)
})
}
39 changes: 19 additions & 20 deletions activation/wire/malfeasance_double_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,32 +89,24 @@ func publishEpochProof(atx *ActivationTxV2) ([]types.Hash32, error) {

// Valid returns true if the proof is valid. It verifies that the two proofs have the same publish epoch, smesher ID,
// and a valid signature but different ATX IDs as well as that the provided merkle proofs are valid.
func (p ProofDoublePublish) Valid(edVerifier *signing.EdVerifier) (bool, error) {
func (p ProofDoublePublish) Valid(edVerifier *signing.EdVerifier) error {
if p.Proofs[0].ATXID == p.Proofs[1].ATXID {
return false, errors.New("proofs have the same ATX ID")
return errors.New("proofs have the same ATX ID")
}

if p.Proofs[0].SmesherID != p.Proofs[1].SmesherID {
return false, errors.New("proofs have different smesher IDs")
return errors.New("proofs have different smesher IDs")
}

if p.Proofs[0].PubEpoch != p.Proofs[1].PubEpoch {
return false, errors.New("proofs have different publish epochs")
return errors.New("proofs have different publish epochs")
}

atx1Valid, err := p.Proofs[0].Valid(edVerifier)
if err != nil {
return false, fmt.Errorf("proof 1 is invalid: %w", err)
if err := p.Proofs[0].Valid(edVerifier); err != nil {
return fmt.Errorf("proof 1 is invalid: %w", err)
}
if !atx1Valid {
return false, nil
if err := p.Proofs[1].Valid(edVerifier); err != nil {
return fmt.Errorf("proof 2 is invalid: %w", err)
}

atx2Valid, err := p.Proofs[1].Valid(edVerifier)
if err != nil {
return false, fmt.Errorf("proof 2 is invalid: %w", err)
}
return atx2Valid, nil
return nil
}

// PublishProof proofs that an ATX was published with a given publish epoch by a given smesher.
Expand All @@ -134,21 +126,28 @@ type PublishProof struct {
}

// Valid returns true if the proof is valid. It verifies that the signature is valid and that the merkle proof is valid.
func (p PublishProof) Valid(edVerifier *signing.EdVerifier) (bool, error) {
func (p PublishProof) Valid(edVerifier *signing.EdVerifier) error {
if !edVerifier.Verify(signing.ATX, p.SmesherID, p.ATXID.Bytes(), p.Signature) {
return false, errors.New("invalid signature")
return errors.New("invalid signature")
}
proof := make([][]byte, len(p.Proof))
for i, h := range p.Proof {
proof[i] = h.Bytes()
}
epoch := make([]byte, 32)
binary.LittleEndian.PutUint32(epoch, p.PubEpoch.Uint32())
return merkle.ValidatePartialTree(
ok, err := merkle.ValidatePartialTree(
[]uint64{uint64(PublishEpochIndex)},
[][]byte{epoch},
proof,
p.ATXID.Bytes(),
atxTreeHash,
)
if err != nil {
return fmt.Errorf("validate publish epoch proof: %w", err)
}
if !ok {
return errors.New("invalid publish epoch proof")
}
return nil
}
Loading

0 comments on commit f98d823

Please sign in to comment.