Skip to content

Commit

Permalink
Merge pull request #3112 from keep-network/ecdsa-dkg-protocol
Browse files Browse the repository at this point in the history
ECDSA DKG test loop: Actual protocol logic
  • Loading branch information
Beau Shinkle authored Aug 5, 2022
2 parents 20c3e0e + a5d462e commit 76d2469
Show file tree
Hide file tree
Showing 21 changed files with 4,746 additions and 238 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
replace github.com/urfave/cli => github.com/keep-network/cli v1.20.0

require (
github.com/binance-chain/tss-lib v1.3.3
github.com/btcsuite/btcd v0.22.1
github.com/btcsuite/btcd/btcec/v2 v2.2.0
github.com/ethereum/go-ethereum v1.10.19
Expand Down Expand Up @@ -33,6 +34,7 @@ require (

require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
Expand All @@ -42,6 +44,7 @@ require (
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/decred/dcrd/dcrec/edwards/v2 v2.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/elastic/gosigar v0.12.0 // indirect
Expand Down Expand Up @@ -129,6 +132,7 @@ require (
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/opencontainers/runtime-spec v1.0.2 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/otiai10/primes v0.0.0-20180210170552-f6d2a1ba97c4 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
Expand Down
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bou.ke/monkey v1.0.1 h1:zEMLInw9xvNakzUUPjfS4Ds6jYPqCFx3m7bRmG5NH2U=
bou.ke/monkey v1.0.1/go.mod h1:FgHuK96Rv2Nlf+0u1OOVDpCMdsWyOFmeeketDHE7LIg=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.31.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
Expand Down Expand Up @@ -69,6 +71,8 @@ github.com/VictoriaMetrics/fastcache v1.6.0/go.mod h1:0qHz5QP0GMX4pfmMA/zt5RgfNu
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 h1:w1UutsfOrms1J05zt7ISrnJIXKzwaspym5BTKGx93EI=
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412/go.mod h1:WPjqKcmVOxf0XSf3YxCJs6N6AOSrOx3obionmG7T0y0=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down Expand Up @@ -108,12 +112,15 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/binance-chain/tss-lib v1.3.3 h1:fW9O2ynr4j4apsx/2J39kv/xw/ia23mwmZI4gnmo48c=
github.com/binance-chain/tss-lib v1.3.3/go.mod h1:xfM6gCPA61WIV5q5tK9Acdv46n1QJLhXnZ4eD17hJpI=
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g=
github.com/btcsuite/btcd v0.0.0-20190213025234-306aecffea32/go.mod h1:DrZx5ec/dmnfpw9KyYoQyYo7d0KEvTkk/5M/vbZjAr8=
github.com/btcsuite/btcd v0.0.0-20190523000118-16327141da8c/go.mod h1:3J08xEfcugPacsc34/LKRU2yO7YmuT8yt28J8k2+rrI=
github.com/btcsuite/btcd v0.0.0-20190605094302-a0d1e3e36d50/go.mod h1:3J08xEfcugPacsc34/LKRU2yO7YmuT8yt28J8k2+rrI=
github.com/btcsuite/btcd v0.0.0-20190629003639-c26ffa870fd8/go.mod h1:3J08xEfcugPacsc34/LKRU2yO7YmuT8yt28J8k2+rrI=
github.com/btcsuite/btcd v0.0.0-20190824003749-130ea5bddde3/go.mod h1:3J08xEfcugPacsc34/LKRU2yO7YmuT8yt28J8k2+rrI=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btcd v0.21.0-beta/go.mod h1:ZSWyehm27aAuS9bvkATT+Xte3hjHZ+MRgMY/8NJ7K94=
Expand Down Expand Up @@ -201,6 +208,8 @@ github.com/deckarep/golang-set v1.8.0 h1:sk9/l/KqpunDwP7pSjUg0keiOOLEnOBHzykLrsP
github.com/deckarep/golang-set v1.8.0/go.mod h1:5nI87KwE7wgsBU1F4GKAw2Qod7p5kyS383rP6+o6qqo=
github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0=
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
github.com/decred/dcrd/dcrec/edwards/v2 v2.0.0 h1:E5KszxGgpjpmW8vN811G6rBAZg0/S/DftdGqN4FW5x4=
github.com/decred/dcrd/dcrec/edwards/v2 v2.0.0/go.mod h1:d0H8xGMWbiIQP7gN3v2rByWUcuZPm9YsgmnfoxgbINc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218=
Expand Down Expand Up @@ -1277,6 +1286,11 @@ github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTm
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/mint v1.2.4 h1:DxYL0itZyPaR5Z9HILdxSoHx+gNs6Yx+neOGS3IVUk0=
github.com/otiai10/mint v1.2.4/go.mod h1:d+b7n/0R3tdyUYYylALXpWQ/kTN+QobSq/4SRGBkR3M=
github.com/otiai10/primes v0.0.0-20180210170552-f6d2a1ba97c4 h1:blMAhTXF6uL1+e3eVSajjLT43Cc0U8mU1gcigbbolJM=
github.com/otiai10/primes v0.0.0-20180210170552-f6d2a1ba97c4/go.mod h1:UmSP7QeU3XmAdGu5+dnrTJqjBc+IscpVZkQzk473cjM=
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/paulbellamy/ratecounter v0.2.0/go.mod h1:Hfx1hDpSGoqxkVVpBi/IlYD7kChlfo5C6hzIHwPqfFE=
Expand Down Expand Up @@ -1774,6 +1788,7 @@ golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190610200419-93c9922d18ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
38 changes: 27 additions & 11 deletions pkg/chain/ethereum/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,24 +252,37 @@ func (tc *TbtcChain) RestoreRewardEligibility() error {
return err
}

// TODO: Implement a real SelectGroup function.
// SelectGroup returns the group members for the group generated by
// the given seed. This function can return an error if the beacon chain's
// state does not allow for group selection at the moment.
func (tc *TbtcChain) SelectGroup(seed *big.Int) ([]chain.Address, error) {
_, operatorPublicKey, err := tc.OperatorKeyPair()
groupSize := big.NewInt(int64(tc.GetConfig().GroupSize))
seedBytes := [32]byte{}
seed.FillBytes(seedBytes[:])

// TODO: Replace with a call to the WalletRegistry.selectGroup function.
operatorsIDs, err := tc.sortitionPool.SelectGroup(groupSize, seedBytes)
if err != nil {
return nil, err
return nil, fmt.Errorf(
"cannot select group in the sortition pool: [%v]",
err,
)
}

operatorAddress, err := tc.Signing().PublicKeyToAddress(operatorPublicKey)
operatorsAddresses, err := tc.sortitionPool.GetIDOperators(operatorsIDs)
if err != nil {
return nil, err
return nil, fmt.Errorf(
"cannot convert operators' IDs to addresses: [%v]",
err,
)
}

groupOperators := make([]chain.Address, tc.GetConfig().GroupSize)
for i := range groupOperators {
groupOperators[i] = operatorAddress
result := make([]chain.Address, len(operatorsAddresses))
for i := range result {
result[i] = chain.Address(operatorsAddresses[i].String())
}

return groupOperators, nil
return result, nil
}

// TODO: Implement a real OnDKGStarted function.
Expand Down Expand Up @@ -299,8 +312,11 @@ func (mwr *mockWalletRegistry) OnDKGStarted(
for {
select {
case block := <-blocksChan:
// Generate an event every 500th block.
if block%500 == 0 {
// Generate an event every 500th block starting from block 250.
// The shift is done in order to avoid overlapping with beacon
// DKG test loop.
shift := uint64(250)
if block >= shift && (block-shift)%500 == 0 {
// The seed is keccak256(block).
blockBytes := make([]byte, 8)
binary.BigEndian.PutUint64(blockBytes, block)
Expand Down
52 changes: 47 additions & 5 deletions pkg/ecdsa/dkg/dkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,50 @@ package dkg

import (
"fmt"
"math/big"
"time"

"github.com/ipfs/go-log"

"github.com/keep-network/keep-core/pkg/chain"
"github.com/keep-network/keep-core/pkg/net"
"github.com/keep-network/keep-core/pkg/protocol/group"
"github.com/keep-network/keep-core/pkg/protocol/state"
)

// ExecutorConfig carries the config for an Executor.
type ExecutorConfig struct {
TssPreParamsPoolSize int
TssPreParamsPoolGenerationTimeout time.Duration
}

// Executor represents an ECDSA distributed key generation process executor.
type Executor struct {
logger log.StandardLogger
tssPreParamsPool *tssPreParamsPool
}

// NewExecutor creates a new Executor instance.
func NewExecutor(
logger log.StandardLogger,
config *ExecutorConfig,
) *Executor {
return &Executor{
logger: logger,
tssPreParamsPool: newTssPreParamsPool(
logger,
config.TssPreParamsPoolSize,
config.TssPreParamsPoolGenerationTimeout,
),
}
}

// Execute runs the ECDSA distributed key generation protocol, given a
// broadcast channel to mediate with, a block counter used for time tracking,
// a member index to use in the group, dishonest threshold, and block height
// when DKG protocol should start.
func Execute(
logger log.StandardLogger,
func (e *Executor) Execute(
seed *big.Int,
startBlockNumber uint64,
memberIndex group.MemberIndex,
groupSize int,
Expand All @@ -23,24 +54,26 @@ func Execute(
channel net.BroadcastChannel,
membershipValidator *group.MembershipValidator,
) (*Result, uint64, error) {
logger.Debugf("[member:%v] initializing member", memberIndex)
e.logger.Debugf("[member:%v] initializing member", memberIndex)

registerUnmarshallers(channel)

member := newMember(
logger,
e.logger,
memberIndex,
groupSize,
dishonestThreshold,
membershipValidator,
seed.Text(16), // TODO: Should change on retry.,
e.tssPreParamsPool.get(),
)

initialState := &ephemeralKeyPairGenerationState{
channel: channel,
member: member.initializeEphemeralKeysGeneration(),
}

stateMachine := state.NewMachine(logger, channel, blockCounter, initialState)
stateMachine := state.NewMachine(e.logger, channel, blockCounter, initialState)

lastState, endBlockNumber, err := stateMachine.Execute(startBlockNumber)
if err != nil {
Expand All @@ -62,4 +95,13 @@ func registerUnmarshallers(channel net.BroadcastChannel) {
channel.SetUnmarshaler(func() net.TaggedUnmarshaler {
return &ephemeralPublicKeyMessage{}
})
channel.SetUnmarshaler(func() net.TaggedUnmarshaler {
return &tssRoundOneMessage{}
})
channel.SetUnmarshaler(func() net.TaggedUnmarshaler {
return &tssRoundTwoMessage{}
})
channel.SetUnmarshaler(func() net.TaggedUnmarshaler {
return &tssRoundThreeMessage{}
})
}
3 changes: 3 additions & 0 deletions pkg/ecdsa/dkg/gen/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package gen

//go:generate sh -c "protoc --proto_path=$GOPATH/src:. --gogoslick_out=. */*.proto"
Loading

0 comments on commit 76d2469

Please sign in to comment.