From abeebc678c21c2566d2a76c8943b4c50c2a8e589 Mon Sep 17 00:00:00 2001 From: r4f43l Date: Sat, 23 Dec 2023 21:51:02 +0100 Subject: [PATCH] Refactor cosigner package and add ted25519SignerSoft type --- pkg/cosigner/cosigner.go | 3 ++- pkg/cosigner/{local_cosigner.go => local.go} | 0 .../{remote_cosigner.go => remote.go} | 7 ++++--- pkg/tss/threshold-ed25519_signer_soft.go | 20 +++++++++++-------- signer/threshold_validator.go | 1 + 5 files changed, 19 insertions(+), 12 deletions(-) rename pkg/cosigner/{local_cosigner.go => local.go} (100%) rename pkg/cosigner/{remote_cosigner.go => remote.go} (92%) diff --git a/pkg/cosigner/cosigner.go b/pkg/cosigner/cosigner.go index 7a6b0f03..3564990a 100644 --- a/pkg/cosigner/cosigner.go +++ b/pkg/cosigner/cosigner.go @@ -17,6 +17,7 @@ import ( "github.com/strangelove-ventures/horcrux/signer/proto" ) +/* // threshold-ed25519 type MPC struct { // our own cosigner @@ -33,7 +34,7 @@ type Localcosigner interface { type Remotecosigner interface { // TODO - add methods } - +*/ // CosignerSignRequest is sent to a co-signer to obtain their signature for the SignBytes // The SignBytes should be a serialized block type CosignerSignRequest struct { diff --git a/pkg/cosigner/local_cosigner.go b/pkg/cosigner/local.go similarity index 100% rename from pkg/cosigner/local_cosigner.go rename to pkg/cosigner/local.go diff --git a/pkg/cosigner/remote_cosigner.go b/pkg/cosigner/remote.go similarity index 92% rename from pkg/cosigner/remote_cosigner.go rename to pkg/cosigner/remote.go index abdb3c90..13c4b093 100644 --- a/pkg/cosigner/remote_cosigner.go +++ b/pkg/cosigner/remote.go @@ -16,7 +16,8 @@ import ( // var _ Cosigner = &RemoteCosigner{} // RemoteCosigner uses CosignerGRPC to request signing from a remote cosigner -// Remote Cosigner are the CLIENT! +// Remote Cosigner are CLIENTS! to every other cosigner, including the the nodes local cosigner +// It calls the GRPC server of the other cosigner type RemoteCosigner struct { id int address string @@ -28,7 +29,7 @@ type RemoteCosigner struct { func InitRemoteCosigner(id int, address string, client proto.CosignerClient) *RemoteCosigner { cosigner := &RemoteCosigner{ id: id, - address: address, + address: address, // address is the P2P URL of the remote cosigner Client: client, } @@ -37,7 +38,7 @@ func InitRemoteCosigner(id int, address string, client proto.CosignerClient) *Re // NewRemoteCosigner returns a newly initialized RemoteCosigner func NewRemoteCosigner(id int, address string) (*RemoteCosigner, error) { - client, err := getGRPCClient(address) + client, err := getGRPCClient(address) // address is the P2P URL of the cosigner server to dial if err != nil { return nil, err } diff --git a/pkg/tss/threshold-ed25519_signer_soft.go b/pkg/tss/threshold-ed25519_signer_soft.go index d2450809..d05c718a 100644 --- a/pkg/tss/threshold-ed25519_signer_soft.go +++ b/pkg/tss/threshold-ed25519_signer_soft.go @@ -31,7 +31,10 @@ func CreateEd25519ThresholdSignShards(pv privval.FilePVKey, threshold, shards ui return out } -type ThresholdSignerSoft struct { +// ted25519SignerSoft is a threshold signer that uses the threshold-ed25519 library +// to perform the signing operations. +// Its only responsibility is to sign a payload and combine signatures +type ted25519SignerSoft struct { privateKeyShard []byte pubKey []byte threshold uint8 @@ -39,7 +42,7 @@ type ThresholdSignerSoft struct { id uint8 } -func NewThresholdEd25519SignerSoft(config *config.RuntimeConfig, id int, chainID string) (*ThresholdSignerSoft, error) { +func NewThresholdEd25519SignerSoft(config *config.RuntimeConfig, id int, chainID string) (*ted25519SignerSoft, error) { keyFile, err := config.KeyFileExistsCosigner(chainID) if err != nil { return nil, err @@ -54,7 +57,7 @@ func NewThresholdEd25519SignerSoft(config *config.RuntimeConfig, id int, chainID return nil, fmt.Errorf("key shard Index (%d) in (%s) does not match cosigner Index (%d)", key.ID, keyFile, id) } - s := ThresholdSignerSoft{ + s := ted25519SignerSoft{ privateKeyShard: key.PrivateShard, pubKey: key.PubKey.Bytes(), threshold: uint8(config.Config.ThresholdModeConfig.Threshold), @@ -65,12 +68,13 @@ func NewThresholdEd25519SignerSoft(config *config.RuntimeConfig, id int, chainID return &s, nil } -func (s *ThresholdSignerSoft) GetPubKey() []byte { +func (s *ted25519SignerSoft) GetPubKey() []byte { return s.pubKey } -func (s *ThresholdSignerSoft) Sign(nonces []types.Nonce, payload []byte) ([]byte, error) { - nonceShare, noncePub, err := s.sumNonces(nonces) +func (s *ted25519SignerSoft) Sign(nonces []types.Nonce, payload []byte) ([]byte, error) { + // sum the nonces to get the ephemeral public key and share + nonceShare, noncePub, err := sumNonces(nonces) if err != nil { return nil, fmt.Errorf("failed to combine nonces: %w", err) } @@ -80,7 +84,7 @@ func (s *ThresholdSignerSoft) Sign(nonces []types.Nonce, payload []byte) ([]byte return append(noncePub, sig...), nil } -func (s *ThresholdSignerSoft) sumNonces(nonces []types.Nonce) (tsed25519.Scalar, tsed25519.Element, error) { +func sumNonces(nonces []types.Nonce) (tsed25519.Scalar, tsed25519.Element, error) { shareParts := make([]tsed25519.Scalar, len(nonces)) publicKeys := make([]tsed25519.Element, len(nonces)) @@ -132,7 +136,7 @@ func (ng NonceGenerator) GenerateNonces(threshold, total uint8) (types.Nonces, e } // CombineSignatures combines partial signatures into a full signature -func (s *ThresholdSignerSoft) CombineSignatures(signatures []types.PartialSignature) ([]byte, error) { +func (s *ted25519SignerSoft) CombineSignatures(signatures []types.PartialSignature) ([]byte, error) { sigIds := make([]int, len(signatures)) shareSigs := make([][]byte, len(signatures)) var ephPub []byte diff --git a/signer/threshold_validator.go b/signer/threshold_validator.go index e6b457a4..4b8172bd 100644 --- a/signer/threshold_validator.go +++ b/signer/threshold_validator.go @@ -651,6 +651,7 @@ func (pv *ThresholdValidator) Sign(ctx context.Context, chainID string, block ty numPeers := len(pv.peerCosigners) total := uint8(numPeers + 1) + // More or less everything belov here shoud be moved to a "cosigners" peerStartTime := time.Now() cosignersOrderedByFastest := pv.cosignerHealth.GetFastest()