This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
sig-threshold.go
240 lines (197 loc) · 5.4 KB
/
sig-threshold.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package event
import (
"encoding/json"
"fmt"
"math/big"
"strconv"
"strings"
"github.com/decentralized-identity/kerigo/pkg/derivation"
"github.com/pkg/errors"
)
type SigThreshold struct {
conditions [][]*big.Rat
}
// MarshalJSON is used to correctly output the JSON since this field can
// contain a single int, a list, or a list of lists
func (s *SigThreshold) MarshalJSON() ([]byte, error) {
switch len(s.conditions) {
case 0:
return json.Marshal("0")
case 1:
if len(s.conditions[0]) == 1 {
return json.Marshal(s.conditions[0][0])
}
return json.Marshal(s.conditions[0])
}
return json.Marshal(s.conditions)
}
func (s *SigThreshold) UnmarshalJSON(in []byte) error {
// check the smiplest first - that the input is a simple string
if string(in[:1]) == `"` {
parsed := ""
err := json.Unmarshal(in, &parsed)
if err != nil {
return nil
}
tholdint, err := strconv.Atoi(parsed)
if err != nil {
return err
}
s.conditions = [][]*big.Rat{{big.NewRat(int64(tholdint), 1)}}
return nil
}
// it's either a list, or list of lists
tholds := []interface{}{}
err := json.Unmarshal(in, &tholds)
if err != nil {
return err
}
if len(tholds) == 0 {
s.conditions = [][]*big.Rat{}
} else {
// if the first item is a string, we will treat this as a weighted thresholed
if _, ok := tholds[0].(string); ok {
conditions := []string{}
for _, t := range tholds {
if ts, ok := t.(string); ok {
conditions = append(conditions, ts)
}
}
converted, err := parseConditions(conditions)
if err != nil {
return err
}
s.conditions = append(s.conditions, converted)
}
// if the first item is a slice, we will treat this as a multi-weighted
if _, ok := tholds[0].([]interface{}); ok {
for _, t := range tholds {
if ti, ok := t.([]interface{}); ok {
conditions := []string{}
for _, iface := range ti {
if ts, ok := iface.(string); ok {
conditions = append(conditions, ts)
}
}
converted, err := parseConditions(conditions)
if err != nil {
return err
}
s.conditions = append(s.conditions, converted)
}
}
}
}
return nil
}
// parseConditions iterates over string representation of fractional
// weights and parses them to bit.Rat
func parseConditions(conditions []string) ([]*big.Rat, error) {
converted := []*big.Rat{}
for _, c := range conditions {
thold := new(big.Rat)
_, err := fmt.Sscan(c, thold)
if err != nil {
return nil, fmt.Errorf("unable to parse condition %s: %s", c, err)
}
if thold.Sign() == -1 {
return nil, errors.New("thresholds must be >= 0")
}
converted = append(converted, thold)
}
return converted, nil
}
// Satisfied takes the provided signature derivations and checkes each weighted
// threshold
func (s *SigThreshold) Satisfied(sigs []derivation.Derivation) bool {
if !s.Weighted() {
required := 0
if len(s.conditions) == 1 && len(s.conditions[0]) == 1 {
required = int(s.conditions[0][0].Num().Int64())
}
return len(sigs) >= required
}
for _, c := range s.conditions {
weight := big.NewRat(0, 1)
for _, s := range sigs {
// if our index is outside of the weighted list it doesn't count
if int(s.KeyIndex) >= len(c) {
continue
}
weight.Add(weight, c[int(s.KeyIndex)])
}
// if we failed to meet this weight, return false
if weight.Cmp(big.NewRat(1, 1)) == -1 {
return false
}
}
return true
}
// String returns the threshold as a raw string representation
// sufficient for use in the next digest commitment
func (s *SigThreshold) String() string {
var b strings.Builder
for i, conditions := range s.conditions {
if i != 0 {
b.Write([]byte(`&`))
}
for j, c := range conditions {
if j == 0 {
fmt.Fprintf(&b, "%s", c.RatString())
} else {
fmt.Fprintf(&b, ",%s", c.RatString())
}
}
}
return b.String()
}
// returns true if this is a weighted threshold - i.e. there
// is one or more lists of weights
func (s *SigThreshold) Weighted() bool {
if len(s.conditions) < 1 || (len(s.conditions) == 1 && len(s.conditions[0]) == 1) {
return false
}
return true
}
// New returns a signing threshold requiring 'threshold' signatures
func NewSigThreshold(threshold int64) (*SigThreshold, error) {
if threshold < 0 {
return nil, errors.New("threshold must be >= 0")
}
return &SigThreshold{[][]*big.Rat{{big.NewRat(threshold, 1)}}}, nil
}
// NewWeighted creates a new sighing threshold with a weighted multisig
func NewWeighted(conditions ...string) (*SigThreshold, error) {
sum := new(big.Rat)
converted, err := parseConditions(conditions)
if err != nil {
return nil, err
}
for _, c := range converted {
sum.Add(sum, c)
}
if sum.Cmp(big.NewRat(1, 1)) == -1 {
return nil, errors.New("threshold not satifiable: sum of weights must be greather than 1")
}
return &SigThreshold{conditions: [][]*big.Rat{converted}}, nil
}
// NewMultiWeighted creates a new signing threshold with multiple
// conditions
func NewMultiWeighted(conditions ...[]string) (*SigThreshold, error) {
thresholds := [][]*big.Rat{}
for _, c := range conditions {
tot := new(big.Rat)
converted, err := parseConditions(c)
if err != nil {
return nil, err
}
for _, frac := range converted {
tot = tot.Add(tot, frac)
}
if tot.Cmp(big.NewRat(1, 1)) == -1 {
return nil, fmt.Errorf("threshold not statisfiable (%s)", c)
}
thresholds = append(thresholds, converted)
}
return &SigThreshold{conditions: thresholds}, nil
}