Skip to content

Commit

Permalink
Refactor cosigner package and add ted25519SignerSoft type
Browse files Browse the repository at this point in the history
  • Loading branch information
nitronit committed Dec 23, 2023
1 parent 7f94824 commit abeebc6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
3 changes: 2 additions & 1 deletion pkg/cosigner/cosigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/strangelove-ventures/horcrux/signer/proto"
)

/*
// threshold-ed25519
type MPC struct {
// our own cosigner
Expand All @@ -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 {
Expand Down
File renamed without changes.
7 changes: 4 additions & 3 deletions pkg/cosigner/remote_cosigner.go → pkg/cosigner/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}

Expand All @@ -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
}
Expand Down
20 changes: 12 additions & 8 deletions pkg/tss/threshold-ed25519_signer_soft.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ 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
total uint8
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
Expand All @@ -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),
Expand All @@ -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)
}
Expand All @@ -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))

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions signer/threshold_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit abeebc6

Please sign in to comment.