Skip to content

Commit

Permalink
fix: expose key share in frost
Browse files Browse the repository at this point in the history
  • Loading branch information
logicalangel committed May 21, 2024
1 parent fd2aef4 commit c5395d2
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 10 deletions.
8 changes: 8 additions & 0 deletions internal/app/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ func Broker() {
ethRPC := ethereum.New()
pos.New(ethRPC)

//frostService := frost.New()

Check failure on line 29 in internal/app/broker.go

View workflow job for this annotation

GitHub Actions / build

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 29 in internal/app/broker.go

View workflow job for this annotation

GitHub Actions / build

commentFormatting: put a space between `//` and comment text (gocritic)

//scheduler := scheduler.New(

Check failure on line 31 in internal/app/broker.go

View workflow job for this annotation

GitHub Actions / build

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 31 in internal/app/broker.go

View workflow job for this annotation

GitHub Actions / build

commentFormatting: put a space between `//` and comment text (gocritic)
// scheduler.WithFrostEvents(frostService),
//)

server.New(
websocket.WithWebsocket(),
)

//scheduler.Start()

Check failure on line 39 in internal/app/broker.go

View workflow job for this annotation

GitHub Actions / build

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 39 in internal/app/broker.go

View workflow job for this annotation

GitHub Actions / build

commentFormatting: put a space between `//` and comment text (gocritic)
}
6 changes: 6 additions & 0 deletions internal/config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ type EthLog struct {
Events []Event `yaml:"events"`
}

// Frost struct represent all task detail of its plugin.
type Frost struct {
Schedule time.Duration `yaml:"schedule"`
}

// Plugins struct holds all applications plugin configs.
type Plugins struct {
EthLog *EthLog `yaml:"logs"`
Uniswap *Uniswap `yaml:"uniswap"`
Frost *Frost `yaml:"frost"`
Correctness []string `yaml:"correctness"`
}

Expand Down
1 change: 1 addition & 0 deletions internal/consts/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ const (
ChannelPriceReport Channels = "unchained:price_report"
ChannelEventLog Channels = "unchained:event_log"
ChannelCorrectnessReport Channels = "unchained:correctness_report"
ChannelFrostSignerList Channels = "unchained:frost_signer_list"
)
2 changes: 2 additions & 0 deletions internal/consts/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ const (

OpCodeCorrectnessReport OpCode = 10
OpCodeCorrectnessReportBroadcast OpCode = 11

OpCodeSendSignerList OpCode = 12
)
5 changes: 1 addition & 4 deletions internal/crypto/frost/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (s *DistributedSigner) Update(msg *dkg.Round1Data) ([]*dkg.Round2Data, erro
return round2Data, nil
}

// Finalize function will confirm the identity key about other parties updates.
func (s *DistributedSigner) Finalize(msg *dkg.Round2Data) error {
if msg.ReceiverIdentifier.Equal(s.currentParticipant.Identifier) == 0 {
return nil
Expand All @@ -51,8 +52,6 @@ func (s *DistributedSigner) Finalize(msg *dkg.Round2Data) error {
return nil
}

// This will, for each participant, return their secret key (which is a share of the global secret signing key),
// the corresponding verification key, and the global public key.
participantsSecretKey, _, groupPublicKeyGeneratedInDKG, err := s.currentParticipant.Finalize(
s.accumulatedMessages,
s.ackMessages,
Expand Down Expand Up @@ -90,7 +89,5 @@ func NewIdentity(id int, signerCount int, minSigningCount int) (*dkg.Round1Data,
panic("this is just a test, and it failed")
}

// signer.accumulatedMessages = append(signer.accumulatedMessages, round1Data)

return round1Data, &signer
}
19 changes: 13 additions & 6 deletions internal/crypto/frost/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package frost

import (
"errors"

"github.com/TimeleapLabs/unchained/internal/consts"
"github.com/TimeleapLabs/unchained/internal/utils"
"github.com/bytemare/frost"
Expand All @@ -16,17 +15,18 @@ type MessageSigner struct {
participant *frost.Participant
}

func (s *MessageSigner) Confirm(commitment *frost.Commitment) error {
// Confirm function will set other parties confirms.
func (s *MessageSigner) Confirm(commitment *frost.Commitment) (*frost.SignatureShare, error) {
s.commitments = append(s.commitments, commitment)

if len(s.commitments.Participants()) < s.partySize {
return nil
return nil, nil
}

signatureShare, err := s.participant.Sign(s.data, s.commitments)
if err != nil {
utils.Logger.With("err", err).Error("cant sign message")
return consts.ErrCantSign
return nil, consts.ErrCantSign
}

if !s.participant.VerifySignatureShare(
Expand All @@ -36,12 +36,19 @@ func (s *MessageSigner) Confirm(commitment *frost.Commitment) error {
s.commitments,
s.data,
) {
return consts.ErrCantVerify
return nil, consts.ErrCantVerify
}

return nil
return signatureShare, nil
}

func (s *MessageSigner) AggregateSignatures(signatureShares []*frost.SignatureShare) ([]byte, error) {
signature := s.participant.Aggregate(s.commitments, s.data, signatureShares)

return signature.Encode(), nil
}

// NewSigner create a new signing state for a message.
func (s *DistributedSigner) NewSigner(data []byte) (*MessageSigner, *frost.Commitment, error) {
if s.finalParticipant == nil {
return nil, nil, consts.ErrSignerIsNotReady
Expand Down
21 changes: 21 additions & 0 deletions internal/model/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
sia "github.com/pouya-eghbali/go-sia/v2/pkg"
)

type Signers []Signer

type Signer struct {
Name string
EvmAddress string
Expand Down Expand Up @@ -34,6 +36,25 @@ func (s *Signature) FromSia(sia sia.Sia) *Signature {
return s
}

func (s Signers) Sia() sia.Sia {
return new(sia.ArraySia[Signer]).
AddArray8(s, func(s *sia.ArraySia[Signer], item Signer) {
s.EmbedBytes(item.Sia().Bytes())
})
}

//func (s Signers) FromBytes(payload []byte) Signers {

Check failure on line 46 in internal/model/bls.go

View workflow job for this annotation

GitHub Actions / build

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 46 in internal/model/bls.go

View workflow job for this annotation

GitHub Actions / build

commentFormatting: put a space between `//` and comment text (gocritic)
// signers := Signers{}
//
// siaArray := sia.ArraySia[Signer]{
// sia.NewFromBytes(payload),
// }
//
// ReadArray8(func(s *sia.ArraySia[Signer]) Signer {
// signers = append(signers, Signer)
// })
//}

func (s *Signer) Sia() sia.Sia {
return sia.New().
AddString8(s.Name).
Expand Down
27 changes: 27 additions & 0 deletions internal/scheduler/frost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package scheduler

import (
"github.com/TimeleapLabs/unchained/internal/service/frost"
)

// FrostSync is a scheduler for syncing signer of Frost and keep task's dependencies.
type FrostSync struct {
frostService frost.Service
}

// Run will trigger by the scheduler and process the Frost sync.
func (e *FrostSync) Run() {
err := e.frostService.PushSigners()
if err != nil {
panic(err)
}
}

// NewFrostSync will create a new FrostSync task.
func NewFrostSync(frostService frost.Service) *FrostSync {
e := FrostSync{
frostService: frostService,
}

return &e
}
12 changes: 12 additions & 0 deletions internal/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"
"time"

"github.com/TimeleapLabs/unchained/internal/service/frost"

"github.com/TimeleapLabs/unchained/internal/utils"

"github.com/TimeleapLabs/unchained/internal/config"
Expand Down Expand Up @@ -66,6 +68,16 @@ func WithUniswapEvents(uniswapService uniswapService.Service) func(s *Scheduler)
}
}

// WithFrostEvents adds frost sync event task to the scheduler.
func WithFrostEvents(frostService frost.Service) func(s *Scheduler) {
return func(s *Scheduler) {
if config.App.Plugins.Frost == nil {
return
}
s.AddTask(config.App.Plugins.Frost.Schedule, NewFrostSync(frostService))
}
}

// AddTask adds a new task to the scheduler.
func (s *Scheduler) AddTask(duration time.Duration, task Task) {
utils.Logger.
Expand Down
14 changes: 14 additions & 0 deletions internal/service/frost/frost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package frost

type Service interface {
PushSigners() error
}

type service struct {
reserveSigners []bool

Check failure on line 8 in internal/service/frost/frost.go

View workflow job for this annotation

GitHub Actions / build

field `reserveSigners` is unused (unused)

Check failure on line 8 in internal/service/frost/frost.go

View workflow job for this annotation

GitHub Actions / build

field `reserveSigners` is unused (unused)
currentSigners []bool
}

func New() Service {
return &service{}
}
40 changes: 40 additions & 0 deletions internal/service/frost/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package frost

import (
"encoding/json"
"github.com/TimeleapLabs/unchained/internal/transport/server/pubsub"

"github.com/TimeleapLabs/unchained/internal/consts"
"github.com/TimeleapLabs/unchained/internal/model"
"github.com/TimeleapLabs/unchained/internal/transport/server/websocket/store"
"github.com/TimeleapLabs/unchained/internal/utils"
"github.com/gorilla/websocket"
)

// PushSigners starts calculating of Frost signers by sending signers list to the Broker.
func (s *service) PushSigners() error {
signers := []model.Signer{}
store.Signers.Range(func(_ *websocket.Conn, value model.Signer) bool {
signers = append(signers, value)
return true
})

signersBytes, err := json.Marshal(signers)
if err != nil {
utils.Logger.With("Error", err).Error("Cant marshal signers list")
return consts.ErrInternalError
}

pubsub.Publish(consts.ChannelFrostSignerList, consts.OpCodeSendSignerList, signersBytes)

return nil
}

// SyncSigners Get list of signers and check power of voting them and generate a new list (if there is difference) of signers which have power.
func (s *service) SyncSigners(signers []model.Signer) error {

Check failure on line 34 in internal/service/frost/sync.go

View workflow job for this annotation

GitHub Actions / build

unused-parameter: parameter 'signers' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 34 in internal/service/frost/sync.go

View workflow job for this annotation

GitHub Actions / build

unused-parameter: parameter 'signers' seems to be unused, consider removing or renaming it as _ (revive)
// TODO: get power of list items and delete no power ones.

// TODO: check the final list with previous one, and replace it if it have difference

return nil
}
2 changes: 2 additions & 0 deletions internal/transport/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func NewRPC(handler handler.Handler) {
case consts.OpCodeCorrectnessReportBroadcast:
handler.CorrectnessReport(ctx, payload[1:])

case consts.OpCodeSendSignerList:
handler.InitFrostSigner(ctx, payload[1:])
default:
utils.Logger.
With("Code", payload[0]).
Expand Down
16 changes: 16 additions & 0 deletions internal/transport/client/handler/frost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package handler

import (
"context"
)

func (h *consumer) InitFrostSigner(ctx context.Context, message []byte) {

Check failure on line 7 in internal/transport/client/handler/frost.go

View workflow job for this annotation

GitHub Actions / build

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 7 in internal/transport/client/handler/frost.go

View workflow job for this annotation

GitHub Actions / build

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
// packet := new([]model.Signer).FromBytes(message)

}

func (w worker) InitFrostSigner(ctx context.Context, message []byte) {

Check failure on line 12 in internal/transport/client/handler/frost.go

View workflow job for this annotation

GitHub Actions / build

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 12 in internal/transport/client/handler/frost.go

View workflow job for this annotation

GitHub Actions / build

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
//packet := new(model.Signers).FromBytes(message)

Check failure on line 13 in internal/transport/client/handler/frost.go

View workflow job for this annotation

GitHub Actions / build

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 13 in internal/transport/client/handler/frost.go

View workflow job for this annotation

GitHub Actions / build

commentFormatting: put a space between `//` and comment text (gocritic)
//TODO implement me
panic("implement me")
}
2 changes: 2 additions & 0 deletions internal/transport/client/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ type Handler interface {
CorrectnessReport(ctx context.Context, message []byte)
EventLog(ctx context.Context, message []byte)
PriceReport(ctx context.Context, message []byte)

InitFrostSigner(ctx context.Context, message []byte)
}

0 comments on commit c5395d2

Please sign in to comment.