From 25a3450f37b909e48f30a33146be990f35e3250c Mon Sep 17 00:00:00 2001 From: programmer10110 Date: Sun, 26 Jul 2020 20:46:39 +0300 Subject: [PATCH] fix calculating participantID --- dkg/dkg.go | 31 +++++++++++++++++++++++++------ main.go | 16 +++++++++++++++- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/dkg/dkg.go b/dkg/dkg.go index 7684935..588f0e0 100644 --- a/dkg/dkg.go +++ b/dkg/dkg.go @@ -3,6 +3,7 @@ package dkg import ( "errors" "fmt" + "math" "sort" "sync" @@ -37,7 +38,6 @@ func Init() *DKG { d.deals = make(map[string]*dkg.Deal) d.commits = make(map[string][]kyber.Point) - d.responses = newMessageStore(9) return &d } @@ -56,10 +56,33 @@ func (d *DKG) StorePubKey(participant string, pk kyber.Point) bool { }) } +func (d *DKG) calcParticipantID() int { + for idx, p := range d.pubkeys { + if p.PK.Equal(d.pubKey) { + return idx + } + } + return -1 +} + func (d *DKG) InitDKGInstance(t int) (err error) { sort.Sort(d.pubkeys) - d.instance, err = dkg.NewDistKeyGenerator(d.suite, d.secKey, d.pubkeys.GetPKs(), t) + publicKeys := d.pubkeys.GetPKs() + + participantsCount := len(publicKeys) + + participantID := d.calcParticipantID() + + if participantID < 0 { + return fmt.Errorf("failed to determine participant index") + } + + d.ParticipantID = participantID + + d.responses = newMessageStore(int(math.Pow(float64(participantsCount)-1, 2))) + + d.instance, err = dkg.NewDistKeyGenerator(d.suite, d.secKey, publicKeys, t) if err != nil { return err } @@ -82,10 +105,6 @@ func (d *DKG) GetDeals() (map[int]*dkg.Deal, error) { if err != nil { return nil, err } - for _, deal := range deals { - d.ParticipantID = int(deal.Index) // Same for each deal. - break - } return deals, nil } diff --git a/main.go b/main.go index ac4b9be..a775989 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,15 @@ type Transport struct { nodes []*dkglib.DKG } +func (t *Transport) getNodeByParticipantID(id int) *dkglib.DKG { + for _, node := range t.nodes { + if node.ParticipantID == id { + return node + } + } + return nil +} + func (t *Transport) BroadcastPK(participant string, pk kyber.Point) { for idx, node := range t.nodes { if ok := node.StorePubKey(participant, pk); !ok { @@ -39,7 +48,12 @@ func (t *Transport) BroadcastCommits(participant string, commits []kyber.Point) func (t *Transport) BroadcastDeals(participant string, deals map[int]*dkg.Deal) { for index, deal := range deals { - t.nodes[index].StoreDeal(participant, deal) + dstNode := t.getNodeByParticipantID(index) + if dstNode == nil { + fmt.Printf("Node with index #%d not found\n", index) + continue + } + dstNode.StoreDeal(participant, deal) } }