Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.9-Ignored-DKG-Phases #154

Merged
merged 4 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions pkgs/board/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewBoard(

// PushDeals implements a kyber DKG Board interface to broadcast deal bundle
func (b *Board) PushDeals(bundle *dkg.DealBundle) {
b.logger.Debug("Pushing deal bundle: ", zap.Int("num of deals", len(bundle.Deals)))
b.logger.Info("Pushing deal bundle: ", zap.Int("num of deals", len(bundle.Deals)))

byts, err := wire2.EncodeDealBundle(bundle)
if err != nil {
Expand All @@ -60,8 +60,25 @@ func (b *Board) IncomingDeal() <-chan dkg.DealBundle {
}

// PushResponses implements a kyber DKG Board interface to broadcast responses

// A response bundle is returned if there is any invalid or
// missing deals.
func (b *Board) PushResponses(bundle *dkg.ResponseBundle) {
// dont push responses to nodes, allowing them to finish with error
b.logger.Info("Pushing response bundle: ", zap.Int("num of responses", len(bundle.Responses)))
byts, err := wire2.EncodeResponseBundle(bundle)
if err != nil {
b.logger.Error("error encoding response bundle", zap.Error(err))
return
}
msg := &wire2.KyberMessage{
Type: wire2.KyberResponseBundleMessageType,
Data: byts,
}

if err := b.broadcastF(msg); err != nil {
b.logger.Error("error broadcasting response bundle", zap.Error(err))
return
}
}

// IncomingResponse implements a kyber DKG Board interface function
Expand All @@ -71,7 +88,21 @@ func (b *Board) IncomingResponse() <-chan dkg.ResponseBundle {

// PushJustifications implements a kyber DKG interface to broadcast justifications
func (b *Board) PushJustifications(bundle *dkg.JustificationBundle) {
// dont push justifications to nodes, allowing them to finish with error
b.logger.Info("Pushing justifications bundle: ", zap.Int("num of justifications", len(bundle.Justifications)))
byts, err := wire2.EncodeJustificationBundle(bundle)
if err != nil {
b.logger.Error("error encoding justifications bundle", zap.Error(err))
return
}
msg := &wire2.KyberMessage{
Type: wire2.KyberJustificationBundleMessageType,
Data: byts,
}

if err := b.broadcastF(msg); err != nil {
b.logger.Error("error broadcasting justifications bundle", zap.Error(err))
return
}
}

// IncomingJustification implements a kyber DKG Board interface function
Expand Down
53 changes: 41 additions & 12 deletions pkgs/initiator/initiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ import (

"github.com/attestantio/go-eth2-client/spec/phase0"
eth2_key_manager_core "github.com/bloxapp/eth2-key-manager/core"
kyber_bls12381 "github.com/drand/kyber-bls12381"
kyber_dkg "github.com/drand/kyber/share/dkg"
"github.com/ethereum/go-ethereum/common"
"github.com/herumi/bls-eth-go-binary/bls"
"github.com/imroc/req/v3"
"go.uber.org/zap"

spec "github.com/ssvlabs/dkg-spec"
spec_crypto "github.com/ssvlabs/dkg-spec/crypto"
"github.com/ssvlabs/ssv-dkg/pkgs/consts"
"github.com/ssvlabs/ssv-dkg/pkgs/crypto"
"github.com/ssvlabs/ssv-dkg/pkgs/utils"
"github.com/ssvlabs/ssv-dkg/pkgs/wire"
"go.uber.org/zap"
)

type VerifyMessageSignatureFunc func(pub *rsa.PublicKey, msg, sig []byte) error
Expand Down Expand Up @@ -442,7 +444,7 @@ func (c *Initiator) CreateCeremonyResults(
nonce uint64,
amount phase0.Gwei,
) (*wire.DepositDataCLI, *wire.KeySharesCLI, []*wire.SignedProof, error) {
dkgResults, err := parseDKGResultsFromBytes(resultsBytes)
dkgResults, err := c.parseDKGResultsFromBytes(resultsBytes)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -580,20 +582,16 @@ func (c *Initiator) processDKGResultResponse(dkgResults []*spec.Result,
return depositDataJson, keyshares, nil
}

func parseDKGResultsFromBytes(responseResult [][]byte) (dkgResults []*spec.Result, finalErr error) {
func (c *Initiator) parseDKGResultsFromBytes(responseResult [][]byte) (dkgResults []*spec.Result, finalErr error) {
for i := 0; i < len(responseResult); i++ {
msg := responseResult[i]
tsp := &wire.SignedTransport{}
if err := tsp.UnmarshalSSZ(msg); err != nil {
finalErr = errors.Join(finalErr, err)
continue
}
if tsp.Message.Type == wire.ErrorMessageType {
finalErr = errors.Join(finalErr, fmt.Errorf("%s", string(tsp.Message.Data)))
continue
}
if tsp.Message.Type != wire.OutputMessageType {
finalErr = errors.Join(finalErr, fmt.Errorf("wrong DKG result message type: exp %s, got %s ", wire.OutputMessageType.String(), tsp.Message.Type.String()))
if err := verifyMessageType(tsp, wire.OutputMessageType); err != nil {
finalErr = errors.Join(finalErr, err)
continue
}
result := &spec.Result{}
Expand Down Expand Up @@ -786,8 +784,8 @@ func (c *Initiator) processPongMessage(res wire.PongResult) error {
return fmt.Errorf("operator returned error: %s", errString)
}
// Validate that incoming message is an pong message
if signedPongMsg.Message.Type != wire.PongMessageType {
return fmt.Errorf("wrong incoming message type from operator")
if err := verifyMessageType(signedPongMsg, wire.PongMessageType); err != nil {
return err
}
pong := &wire.Pong{}
if err := pong.UnmarshalSSZ(signedPongMsg.Message.Data); err != nil {
Expand Down Expand Up @@ -931,7 +929,7 @@ func (c *Initiator) createBulkResults(resultsBytes [][][]byte, signedMsg, msgIDM
bulkKeyShares := []*wire.KeySharesCLI{}
bulkProofs := [][]*wire.SignedProof{}
for _, ceremonyResult := range resultsBytes {
dkgResults, err := parseDKGResultsFromBytes(ceremonyResult)
dkgResults, err := c.parseDKGResultsFromBytes(ceremonyResult)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -971,3 +969,34 @@ func (c *Initiator) createBulkResults(resultsBytes [][][]byte, signedMsg, msgIDM
return bulkDepositData, bulkKeyShares, bulkProofs, nil
}

func verifyMessageType(tsp *wire.SignedTransport, expectedType wire.TransportType) error {
if tsp.Message.Type != expectedType {
if tsp.Message.Type == wire.ErrorMessageType {
return fmt.Errorf("dkg protocol failed with %s", string(tsp.Message.Data))
}
if tsp.Message.Type == wire.KyberMessageType {
kyberMsg := &wire.KyberMessage{}
if err := kyberMsg.UnmarshalSSZ(tsp.Message.Data); err != nil {
return err
}
switch kyberMsg.Type {
// if we are not in fastsync, we expect only complaints
case wire.KyberResponseBundleMessageType:
bundle, err := wire.DecodeResponseBundle(kyberMsg.Data)
if err != nil {
return err
}
return fmt.Errorf("dkg protocol failed with response complaints: %v", bundle)
case wire.KyberJustificationBundleMessageType:
bundle, err := wire.DecodeJustificationBundle(kyberMsg.Data, kyber_bls12381.NewBLS12381Suite().G1().(kyber_dkg.Suite))
if err != nil {
return err
}
return fmt.Errorf("dkg protocol failed with justification message, which is unexpected: %v", bundle)
default:
return fmt.Errorf("received message with wrong type %s ", kyberMsg.Type)
}
}
}
return nil
}
135 changes: 135 additions & 0 deletions pkgs/initiator/initiator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@ import (
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/bloxapp/eth2-key-manager/core"
"github.com/bloxapp/ssv/logging"
kyber_bls12381 "github.com/drand/kyber-bls12381"
"github.com/drand/kyber/share/dkg"
kyber_dkg "github.com/drand/kyber/share/dkg"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/herumi/bls-eth-go-binary/bls"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

e2m_core "github.com/bloxapp/eth2-key-manager/core"
spec "github.com/ssvlabs/dkg-spec"
spec_crypto "github.com/ssvlabs/dkg-spec/crypto"
"github.com/ssvlabs/dkg-spec/testing/stubs"
"github.com/ssvlabs/ssv-dkg/pkgs/crypto"
"github.com/ssvlabs/ssv-dkg/pkgs/initiator"
"github.com/ssvlabs/ssv-dkg/pkgs/utils"
"github.com/ssvlabs/ssv-dkg/pkgs/utils/test_utils"
"github.com/ssvlabs/ssv-dkg/pkgs/validator"
"github.com/ssvlabs/ssv-dkg/pkgs/wire"
Expand Down Expand Up @@ -418,3 +423,133 @@ func must[T any](v T, err error) T {
}
return v
}

func TestDKGFailWithOperatorsMisbehave(t *testing.T) {
err := logging.SetGlobalLogger("debug", "capital", "console", nil)
require.NoError(t, err)
logger := zap.L().Named("operator-tests")
ops := wire.OperatorsCLI{}
version := "test.version"
stubClient := &stubs.Client{
CallContractF: func(call ethereum.CallMsg) ([]byte, error) {
return nil, nil
},
}
srv1 := test_utils.CreateTestOperatorFromFile(t, 1, "../../examples/operator1", version, operatorCert, operatorKey, stubClient)
srv2 := test_utils.CreateTestOperatorFromFile(t, 2, "../../examples/operator2", version, operatorCert, operatorKey, stubClient)
srv3 := test_utils.CreateTestOperatorFromFile(t, 3, "../../examples/operator3", version, operatorCert, operatorKey, stubClient)
srv4 := test_utils.CreateTestOperatorFromFile(t, 4, "../../examples/operator4", version, operatorCert, operatorKey, stubClient)

ops = append(
ops,
wire.OperatorCLI{Addr: srv1.HttpSrv.URL, ID: 1, PubKey: &srv1.PrivKey.PublicKey},
wire.OperatorCLI{Addr: srv2.HttpSrv.URL, ID: 2, PubKey: &srv2.PrivKey.PublicKey},
wire.OperatorCLI{Addr: srv3.HttpSrv.URL, ID: 3, PubKey: &srv3.PrivKey.PublicKey},
wire.OperatorCLI{Addr: srv4.HttpSrv.URL, ID: 4, PubKey: &srv4.PrivKey.PublicKey},
)
withdraw := common.HexToAddress("0x0000000000000000000000000000000000000009")
owner := common.HexToAddress("0x0000000000000000000000000000000000000007")
ids := []uint64{1, 2, 3, 4}
t.Run("operator cheat with deal bundle", func(t *testing.T) {
intr, err := initiator.New(ops, logger, "test.version", rootCert, false)
require.NoError(t, err)
id := spec.NewID()

ops, err := initiator.ValidatedOperatorData(ids, intr.Operators)
require.NoError(t, err)
threshold := utils.GetThreshold(ids)
init := &spec.Init{
Operators: ops,
T: uint64(threshold),
WithdrawalCredentials: withdraw.Bytes(),
Fork: e2m_core.NetworkFromString("mainnet").GenesisForkVersion(),
Owner: owner,
Nonce: 0,
Amount: uint64(spec_crypto.MIN_ACTIVATION_BALANCE),
}

exchangeMsgs, _, err := intr.SendInitMsg(id, init, ops)
require.NoError(t, err)
kyberMsgs, _, err := intr.SendExchangeMsgs(id, exchangeMsgs, ops)
require.NoError(t, err)

tsp := &wire.SignedTransport{}
err = tsp.UnmarshalSSZ(kyberMsgs[1])
require.NoError(t, err)

kyberMsg := &wire.KyberMessage{}
err = kyberMsg.UnmarshalSSZ(tsp.Message.Data)
require.NoError(t, err)

// decode deal bundle
d, err := wire.DecodeDealBundle(kyberMsg.Data, kyber_bls12381.NewBLS12381Suite().G1().(kyber_dkg.Suite))
require.NoError(t, err)

// try to cheat
cheatDealShare, err := hex.DecodeString("a262a2a96d170658f68cf2450106949e92b9c415c3add2dbb8ce1a09886cf24f12f4f2ff1043b372090b06ce9a328a6f64b4279125902a244a22aa4ae30e16249186b1ae3a2c2c6a29634215a84e86fae66862013d8db1cdc930a8b1502750d8")
require.NoError(t, err)
d.Deals[0].EncryptedShare = cheatDealShare
bundle := &dkg.DealBundle{
DealerIndex: d.DealerIndex,
Deals: d.Deals,
Public: d.Public,
SessionID: d.SessionID,
Signature: d.Signature,
}

byts, err := wire.EncodeDealBundle(bundle)
require.NoError(t, err)

// send corrupted kyber message to get justification error from protocol
msg := &wire.KyberMessage{
Type: wire.KyberDealBundleMessageType,
Data: byts,
}
byts, err = msg.MarshalSSZ()
require.NoError(t, err)

trsp := &wire.Transport{
Type: wire.KyberMessageType,
Identifier: id,
Data: byts,
Version: intr.Version,
}
bts, err := trsp.MarshalSSZ()
require.NoError(t, err)

// Sign message with RSA private key
sign, err := srv1.Srv.State.Sign(bts)
require.NoError(t, err)

pub, err := spec_crypto.EncodeRSAPublicKey(&srv1.Srv.State.PrivateKey.PublicKey)
require.NoError(t, err)

signed := &wire.SignedTransport{
Message: trsp,
Signer: pub,
Signature: sign,
}
final, err := signed.MarshalSSZ()
kyberMsgs[srv1.ID] = final
require.NoError(t, err)

dkgResult, errs, err := intr.SendKyberMsgs(id, kyberMsgs, ops)
require.NoError(t, err)

for _, err := range errs {
require.NoError(t, err)
}
var finalResults [][]byte
for _, res := range dkgResult {
finalResults = append(finalResults, res)
}

_, _, _, err = intr.CreateCeremonyResults(finalResults, id, init.Operators, init.WithdrawalCredentials, nil, init.Fork, init.Owner, init.Nonce, phase0.Gwei(init.Amount))
require.ErrorContains(t, err, "protocol failed with response complaints")
})

srv1.HttpSrv.Close()
srv2.HttpSrv.Close()
srv3.HttpSrv.Close()
srv4.HttpSrv.Close()
}
16 changes: 16 additions & 0 deletions pkgs/wire/types_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"strings"

spec "github.com/ssvlabs/dkg-spec"
spec_crypto "github.com/ssvlabs/dkg-spec/crypto"
)

// Proof for a DKG ceremony
Expand Down Expand Up @@ -191,6 +192,21 @@ func (o OperatorsCLI) Clone() OperatorsCLI {
return clone
}

func (o OperatorsCLI) ToSpecOperators() ([]*spec.Operator, error) {
Copy link
Contributor

@MatusKysel MatusKysel Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is this used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover, removed

specOps := make([]*spec.Operator, 0)
for _, op := range o {
pub, err := spec_crypto.EncodeRSAPublicKey(op.PubKey)
if err != nil {
return nil, err
}
specOps = append(specOps, &spec.Operator{
ID: op.ID,
PubKey: pub,
})
}
return specOps, nil
}

type operatorCLIJSON struct {
Addr string `json:"ip"`
ID uint64 `json:"id"`
Expand Down
Loading