forked from bnb-chain/tss-lib
/
party_id.go
148 lines (126 loc) · 3.54 KB
/
party_id.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright © 2019 Binance
//
// This file is part of Binance. The full Binance copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
package tss
import (
"fmt"
"math/big"
"sort"
"github.com/Aasifj2/tss-lib/common"
)
type (
// PartyID represents a participant in the TSS protocol rounds.
// Note: The `id` and `moniker` are provided for convenience to allow you to track participants easier.
// The `id` is intended to be a unique string representation of `key` and `moniker` can be anything (even left blank).
PartyID struct {
*MessageWrapper_PartyID
Index int `json:"index"`
}
UnSortedPartyIDs []*PartyID
SortedPartyIDs []*PartyID
)
func (pid *PartyID) ValidateBasic() bool {
return pid != nil && pid.Key != nil && 0 <= pid.Index
}
// --- ProtoBuf Extensions
func (mpid *MessageWrapper_PartyID) KeyInt() *big.Int {
return new(big.Int).SetBytes(mpid.Key)
}
// ----- //
// NewPartyID constructs a new PartyID
// Exported, used in `tss` client. `key` should remain consistent between runs for each party.
func NewPartyID(id, moniker string, key *big.Int) *PartyID {
return &PartyID{
MessageWrapper_PartyID: &MessageWrapper_PartyID{
Id: id,
Moniker: moniker,
Key: key.Bytes(),
},
Index: -1, // not known until sorted
}
}
func (pid PartyID) String() string {
return fmt.Sprintf("{%d,%s}", pid.Index, pid.Moniker)
}
// ----- //
// SortPartyIDs sorts a list of []*PartyID by their keys in ascending order
// Exported, used in `tss` client
func SortPartyIDs(ids UnSortedPartyIDs, startAt ...int) SortedPartyIDs {
sorted := make(SortedPartyIDs, 0, len(ids))
for _, id := range ids {
sorted = append(sorted, id)
}
sort.Sort(sorted)
// assign party indexes
for i, id := range sorted {
frm := 0
if len(startAt) > 0 {
frm = startAt[0]
}
id.Index = i + frm
}
return sorted
}
// GenerateTestPartyIDs generates a list of mock PartyIDs for tests
func GenerateTestPartyIDs(count int, startAt ...int) SortedPartyIDs {
ids := make(UnSortedPartyIDs, 0, count)
key := common.MustGetRandomInt(256)
frm := 0
i := 0 // default `i`
if len(startAt) > 0 {
frm = startAt[0]
i = startAt[0]
}
for ; i < count+frm; i++ {
ids = append(ids, &PartyID{
MessageWrapper_PartyID: &MessageWrapper_PartyID{
Id: fmt.Sprintf("%d", i+1),
Moniker: fmt.Sprintf("P[%d]", i+1),
Key: new(big.Int).Sub(key, big.NewInt(int64(count)-int64(i))).Bytes(),
},
Index: i,
// this key makes tests more deterministic
})
}
return SortPartyIDs(ids, startAt...)
}
func (spids SortedPartyIDs) Keys() []*big.Int {
ids := make([]*big.Int, spids.Len())
for i, pid := range spids {
ids[i] = pid.KeyInt()
}
return ids
}
func (spids SortedPartyIDs) ToUnSorted() UnSortedPartyIDs {
return UnSortedPartyIDs(spids)
}
func (spids SortedPartyIDs) FindByKey(key *big.Int) *PartyID {
for _, pid := range spids {
if pid.KeyInt().Cmp(key) == 0 {
return pid
}
}
return nil
}
func (spids SortedPartyIDs) Exclude(exclude *PartyID) SortedPartyIDs {
newSpIDs := make(SortedPartyIDs, 0, len(spids))
for _, pid := range spids {
if pid.KeyInt().Cmp(exclude.KeyInt()) == 0 {
continue // exclude
}
newSpIDs = append(newSpIDs, pid)
}
return newSpIDs
}
// Sortable
func (spids SortedPartyIDs) Len() int {
return len(spids)
}
func (spids SortedPartyIDs) Less(a, b int) bool {
return spids[a].KeyInt().Cmp(spids[b].KeyInt()) <= 0
}
func (spids SortedPartyIDs) Swap(a, b int) {
spids[a], spids[b] = spids[b], spids[a]
}