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

DKG trustless key sharding #152

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ linters-settings:
alias: grpcretry
- pkg : github.com/kraken-hpc/go-fork
alias: fork
- pkg: github.com/libp2p/go-libp2p-pubsub
alias: pubsub
- pkg: github.com/armon/go-metrics/prometheus
alias: gmprometheus
- pkg: github.com/mitchellh/go-homedir
Expand Down
4 changes: 2 additions & 2 deletions cmd/horcrux/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.
if signMode == string(signer.SignModeThreshold) {
// Threshold Mode Config
cosignersFlag, _ := cmdFlags.GetStringSlice(flagCosigner)
threshold, _ := cmdFlags.GetInt(flagThreshold)
threshold, _ := cmdFlags.GetUint8(flagThreshold)
raftTimeout, _ := cmdFlags.GetString(flagRaftTimeout)
grpcTimeout, _ := cmdFlags.GetString(flagGRPCTimeout)
cosigners, err := signer.CosignersFromFlag(cosignersFlag)
Expand Down Expand Up @@ -141,7 +141,7 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.
`cosigners in format tcp://{cosigner-addr}:{p2p-port}
(e.g. --cosigner tcp://horcrux-1:2222 --cosigner tcp://horcrux-2:2222 --cosigner tcp://horcrux-3:2222)`)

f.IntP(flagThreshold, "t", 0, "number of shards required for threshold signature")
f.Uint8P(flagThreshold, "t", 0, "number of shards required for threshold signature")

f.StringP(
flagDebugAddr, "d", "",
Expand Down
116 changes: 116 additions & 0 deletions cmd/horcrux/cmd/dkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cmd

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/strangelove-ventures/horcrux/signer"
)

const flagID = "id"

// dkgCmd is a cobra command for performing
// a DKG key ceremony as a participating cosigner.
func dkgCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "dkg",
Args: cobra.NoArgs,
Short: `Perform DKG key sharding ceremony (no trusted "dealer")`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
flags := cmd.Flags()

id, _ := flags.GetUint8(flagID)
chainID, _ := flags.GetString(flagChainID)
threshold := config.Config.ThresholdModeConfig.Threshold
shards := uint8(len(config.Config.ThresholdModeConfig.Cosigners))

var errs []error

if id == 0 {
errs = append(errs, fmt.Errorf("id must not be zero"))
}

if id > shards {
errs = append(errs, fmt.Errorf("id must not be greater than total shards"))
}

if chainID == "" {
errs = append(errs, fmt.Errorf("chain-id flag must not be empty"))
}

if threshold == 0 {
errs = append(errs, fmt.Errorf("threshold flag must be > 0, <= --shards, and > --shards/2"))
}

if shards == 0 {
errs = append(errs, fmt.Errorf("shards flag must be greater than zero"))
}

if threshold > shards {
errs = append(errs, fmt.Errorf(
"threshold cannot be greater than total shards, got [threshold](%d) > [shards](%d)",
threshold, shards,
))
}

if threshold <= shards/2 {
errs = append(errs, fmt.Errorf("threshold must be greater than total shards "+
"divided by 2, got [threshold](%d) <= [shards](%d) / 2", threshold, shards))
}

if len(errs) > 0 {
return errors.Join(errs...)
}

keyFile, err := config.KeyFileExistsCosignerRSA()
if err != nil {
return err
}

key, err := signer.LoadCosignerRSAKey(keyFile)
if err != nil {
return fmt.Errorf("error reading cosigner key (%s): %w", keyFile, err)
}

// silence usage after all input has been validated
cmd.SilenceUsage = true

shard, err := signer.NetworkDKG(cmd.Context(), config.Config.ThresholdModeConfig.Cosigners, id, key, threshold)
if err != nil {
return err
}

shardBz, err := shard.MarshalJSON()
if err != nil {
return err
}

out := config.HomeDir

if config.Config.PrivValKeyDir != nil {
out = *config.Config.PrivValKeyDir
}

filename := filepath.Join(out, fmt.Sprintf("%s_shard.json", chainID))

if err := os.WriteFile(filename, shardBz, 0600); err != nil {
return err
}

fmt.Fprintf(cmd.OutOrStdout(), "Created Ed25519 Shard %s\n", filename)

return nil
},
}

f := cmd.Flags()
f.Uint8(flagID, 0, "cosigner shard ID as participant in DKG ceremony")
_ = cmd.MarkFlagRequired(flagID)
f.String(flagChainID, "", "key shards will sign for this chain ID")
_ = cmd.MarkFlagRequired(flagChainID)

return cmd
}
6 changes: 3 additions & 3 deletions cmd/horcrux/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ type (
}

v2CosignerConfig struct {
Threshold int `json:"threshold" yaml:"threshold"`
Threshold uint8 `json:"threshold" yaml:"threshold"`
Shares int `json:"shares" yaml:"shares"`
P2PListen string `json:"p2p-listen" yaml:"p2p-listen"`
Peers []struct {
ShareID int `json:"share-id" yaml:"share-id"`
ShareID uint8 `json:"share-id" yaml:"share-id"`
P2PAddr string `json:"p2p-addr" yaml:"p2p-addr"`
} `json:"peers" yaml:"peers"`
Timeout string `json:"rpc-timeout" yaml:"rpc-timeout"`
Expand All @@ -66,7 +66,7 @@ type (
PubKey cometcrypto.PubKey `json:"pub_key"`
ShareKey []byte `json:"secret_share"`
RSAKey rsa.PrivateKey `json:"rsa_key"`
ID int `json:"id"`
ID uint8 `json:"id"`
RSAPubs []*rsa.PublicKey `json:"rsa_pubs"`
}
)
Expand Down
1 change: 1 addition & 0 deletions cmd/horcrux/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func rootCmd() *cobra.Command {
cmd.AddCommand(addressCmd())
cmd.AddCommand(createCosignerEd25519ShardsCmd())
cmd.AddCommand(createCosignerRSAShardsCmd())
cmd.AddCommand(dkgCmd())
cmd.AddCommand(leaderElectionCmd())
cmd.AddCommand(getLeaderCmd())
cmd.AddCommand(stateCmd())
Expand Down
2 changes: 1 addition & 1 deletion cmd/horcrux/cmd/shards.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/strangelove-ventures/horcrux/signer"
)

func createCosignerDirectoryIfNecessary(out string, id int) (string, error) {
func createCosignerDirectoryIfNecessary(out string, id uint8) (string, error) {
dir := filepath.Join(out, fmt.Sprintf("cosigner_%d", id))
dirStat, err := os.Stat(dir)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/horcrux/cmd/threshold.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewThresholdValidator(
key,
pubKeys,
p2pListen,
uint8(thresholdCfg.Threshold),
thresholdCfg.Threshold,
)

// Validated prior in ValidateThresholdModeConfig
Expand Down
82 changes: 74 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ require (
github.com/Jille/raftadmin v1.2.0
github.com/armon/go-metrics v0.4.1
github.com/avast/retry-go v3.0.0+incompatible
github.com/avast/retry-go/v4 v4.3.4
github.com/cometbft/cometbft v0.37.0
github.com/cosmos/cosmos-sdk v0.47.1
github.com/gogo/protobuf v1.3.2
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/hashicorp/raft v1.5.0
github.com/hashicorp/raft-boltdb/v2 v2.2.2
github.com/kraken-hpc/go-fork v0.1.1
github.com/libp2p/go-libp2p v0.27.3
github.com/libp2p/go-libp2p-pubsub v0.9.3
github.com/mitchellh/go-homedir v1.1.0
github.com/ory/dockertest v3.3.5+incompatible
github.com/prometheus/client_golang v1.14.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.14.0
github.com/stretchr/testify v1.8.2
github.com/taurusgroup/frost-ed25519 v0.0.0-20210707140332-5abc84a4dba7
github.com/tendermint/go-amino v0.16.0
gitlab.com/unit410/edwards25519 v0.0.0-20220725154547-61980033348e
gitlab.com/unit410/threshold-ed25519 v0.0.0-20220725172740-6ee731f539ac
Expand All @@ -43,6 +47,7 @@ require (
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d // indirect
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
github.com/boltdb/bolt v1.3.1 // indirect
Expand All @@ -52,7 +57,9 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cometbft/cometbft-db v0.7.0 // indirect
github.com/confio/ics23/go v0.9.0 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
Expand All @@ -61,6 +68,7 @@ require (
github.com/cosmos/ledger-cosmos-go v0.12.2 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
Expand All @@ -69,17 +77,26 @@ require (
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
github.com/elastic/gosigar v0.14.2 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/flynn/noise v1.0.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/frankban/quicktest v1.14.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/pprof v0.0.0-20230405160723-4a4c7d95572b // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
Expand All @@ -92,34 +109,76 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.1 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/golang-lru/v2 v2.0.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hdevalence/ed25519consensus v0.1.0 // indirect
github.com/huandu/skiplist v1.2.0 // indirect
github.com/huin/goupnp v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.16.3 // indirect
github.com/klauspost/compress v1.16.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/koron/go-ssdp v0.0.4 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-cidranger v1.1.0 // indirect
github.com/libp2p/go-flow-metrics v0.1.0 // indirect
github.com/libp2p/go-libp2p-asn-util v0.3.0 // indirect
github.com/libp2p/go-msgio v0.3.0 // indirect
github.com/libp2p/go-nat v0.1.0 // indirect
github.com/libp2p/go-netroute v0.2.1 // indirect
github.com/libp2p/go-reuseport v0.2.0 // indirect
github.com/libp2p/go-yamux/v4 v4.0.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/miekg/dns v1.1.53 // indirect
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multiaddr v0.9.0 // indirect
github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multicodec v0.8.1 // indirect
github.com/multiformats/go-multihash v0.2.1 // indirect
github.com/multiformats/go-multistream v0.4.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/onsi/ginkgo/v2 v2.9.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
github.com/opencontainers/runc v1.1.3 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // 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.5 // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-19 v0.3.2 // indirect
github.com/quic-go/qtls-go1-20 v0.2.2 // indirect
github.com/quic-go/quic-go v0.33.0 // indirect
github.com/quic-go/webtransport-go v0.5.2 // indirect
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand All @@ -131,17 +190,24 @@ require (
github.com/zondax/hid v0.9.1 // indirect
github.com/zondax/ledger-go v0.14.1 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/dig v1.16.1 // indirect
go.uber.org/fx v1.19.2 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.1.7 // indirect
nhooyr.io/websocket v1.8.7 // indirect
pgregory.net/rapid v0.5.5 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Expand Down
Loading