-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.go
323 lines (279 loc) · 6.66 KB
/
messages.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package frost
import (
"encoding/base64"
"encoding/json"
"errors"
"github.com/bartke/frost/party"
"github.com/bartke/frost/polynomial"
"github.com/bartke/frost/ristretto"
"github.com/bartke/frost/zk"
)
func decodeScalar(encoded string, scalar *ristretto.Scalar) error {
bytes, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
return err
}
_, err = scalar.SetCanonicalBytes(bytes)
return err
}
type Header struct {
// Type is the message type
Type MessageType
// From returns the party.ID of the party who sent this message.
// Cannot be 0
From party.ID
// To is the party.ID of the party the message is addressed to.
// If the message is intended for broadcast, the ID returned is 0 (invalid),
// therefore, you should call IsBroadcast() first.
To party.ID
}
func (h *Header) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type string `json:"type"`
From string `json:"from"`
To string `json:"to"`
}{
Type: base64.StdEncoding.EncodeToString([]byte{byte(h.Type)}),
From: base64.StdEncoding.EncodeToString(h.From.Bytes()),
To: base64.StdEncoding.EncodeToString(h.To.Bytes()),
})
}
func (h *Header) UnmarshalJSON(data []byte) error {
aux := &struct {
Type string `json:"type"`
From string `json:"from"`
To string `json:"to"`
}{}
if err := json.Unmarshal(data, aux); err != nil {
return err
}
typeBytes, err := base64.StdEncoding.DecodeString(aux.Type)
if err != nil {
return err
}
h.Type = MessageType(typeBytes[0])
fromBytes, err := base64.StdEncoding.DecodeString(aux.From)
if err != nil {
return err
}
h.From, err = party.FromBytes(fromBytes)
if err != nil {
return err
}
toBytes, err := base64.StdEncoding.DecodeString(aux.To)
if err != nil {
return err
}
h.To, err = party.FromBytes(toBytes)
return err
}
type Message struct {
Header
KeyGen1 *KeyGen1
KeyGen2 *KeyGen2
Sign1 *Sign1
Sign2 *Sign2
}
var ErrInvalidMessage = errors.New("invalid message")
type MessageType uint8
// MessageType s must be increasing.
const (
MessageTypeNone MessageType = iota
MessageTypeKeyGen1
MessageTypeKeyGen2
MessageTypeSign1
MessageTypeSign2
)
func (m *Message) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Header Header `json:"header"`
KeyGen1 *KeyGen1 `json:"keygen1,omitempty"`
KeyGen2 *KeyGen2 `json:"keygen2,omitempty"`
Sign1 *Sign1 `json:"sign1,omitempty"`
Sign2 *Sign2 `json:"sign2,omitempty"`
}{
Header: m.Header,
KeyGen1: m.KeyGen1,
KeyGen2: m.KeyGen2,
Sign1: m.Sign1,
Sign2: m.Sign2,
})
}
func (m *Message) UnmarshalJSON(data []byte) error {
aux := &struct {
Header Header `json:"header"`
KeyGen1 *KeyGen1 `json:"keygen1,omitempty"`
KeyGen2 *KeyGen2 `json:"keygen2,omitempty"`
Sign1 *Sign1 `json:"sign1,omitempty"`
Sign2 *Sign2 `json:"sign2,omitempty"`
}{}
if err := json.Unmarshal(data, aux); err != nil {
return err
}
m.Header = aux.Header
m.KeyGen1 = aux.KeyGen1
m.KeyGen2 = aux.KeyGen2
m.Sign1 = aux.Sign1
m.Sign2 = aux.Sign2
return nil
}
type KeyGen1 struct {
Proof *zk.Schnorr
Commitments *polynomial.Exponent
}
func NewKeyGen1(from party.ID, proof *zk.Schnorr, commitments *polynomial.Exponent) *Message {
return &Message{
Header: Header{
Type: MessageTypeKeyGen1,
From: from,
},
KeyGen1: &KeyGen1{
Proof: proof,
Commitments: commitments,
},
}
}
func (m *KeyGen1) MarshalJSON() ([]byte, error) {
proofBytes, err := m.Proof.MarshalBinary()
if err != nil {
return nil, err
}
commitmentsBytes, err := m.Commitments.MarshalBinary()
if err != nil {
return nil, err
}
return json.Marshal(&struct {
Proof string `json:"proof"`
Commitments string `json:"commitments"`
}{
Proof: base64.StdEncoding.EncodeToString(proofBytes),
Commitments: base64.StdEncoding.EncodeToString(commitmentsBytes),
})
}
func (m *KeyGen1) UnmarshalJSON(data []byte) error {
aux := &struct {
Proof string `json:"proof"`
Commitments string `json:"commitments"`
}{}
if err := json.Unmarshal(data, aux); err != nil {
return err
}
proofBytes, err := base64.StdEncoding.DecodeString(aux.Proof)
if err != nil {
return err
}
m.Proof = &zk.Schnorr{}
if err := m.Proof.UnmarshalBinary(proofBytes); err != nil {
return err
}
commitmentsBytes, err := base64.StdEncoding.DecodeString(aux.Commitments)
if err != nil {
return err
}
m.Commitments = &polynomial.Exponent{}
return m.Commitments.UnmarshalBinary(commitmentsBytes)
}
type KeyGen2 struct {
// Share is a Shamir additive share for the destination party
Share ristretto.Scalar
}
func NewKeyGen2(from, to party.ID, share *ristretto.Scalar) *Message {
return &Message{
Header: Header{
Type: MessageTypeKeyGen2,
From: from,
To: to,
},
KeyGen2: &KeyGen2{Share: *share},
}
}
func (m *KeyGen2) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Share string `json:"share"`
}{
Share: base64.StdEncoding.EncodeToString(m.Share.Bytes()),
})
}
func (m *KeyGen2) UnmarshalJSON(data []byte) error {
aux := &struct {
Share string `json:"share"`
}{}
if err := json.Unmarshal(data, aux); err != nil {
return err
}
if err := decodeScalar(aux.Share, &m.Share); err != nil {
return err
}
return nil
}
type Sign1 struct {
// Di = [di] B
// Ei = [ei] B
Di, Ei ristretto.Element
}
func NewSign1(from party.ID, commitmentD, commitmentE *ristretto.Element) *Message {
return &Message{
Header: Header{
Type: MessageTypeSign1,
From: from,
},
Sign1: &Sign1{
Di: *commitmentD,
Ei: *commitmentE,
},
}
}
func (m *Sign1) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Di ristretto.Element `json:"di"`
Ei ristretto.Element `json:"ei"`
}{
Di: m.Di,
Ei: m.Ei,
})
}
func (m *Sign1) UnmarshalJSON(data []byte) error {
aux := &struct {
Di ristretto.Element `json:"di"`
Ei ristretto.Element `json:"ei"`
}{}
if err := json.Unmarshal(data, aux); err != nil {
return err
}
m.Di = aux.Di
m.Ei = aux.Ei
return nil
}
type Sign2 struct {
// Zi is a ristretto.Scalar.
// It represents the sender's share of the 's' part of the final signature
Zi ristretto.Scalar
}
func NewSign2(from party.ID, signatureShare *ristretto.Scalar) *Message {
return &Message{
Header: Header{
Type: MessageTypeSign2,
From: from,
},
Sign2: &Sign2{Zi: *signatureShare},
}
}
func (m *Sign2) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Zi string `json:"zi"`
}{
Zi: base64.StdEncoding.EncodeToString(m.Zi.Bytes()),
})
}
func (m *Sign2) UnmarshalJSON(data []byte) error {
aux := &struct {
Zi string `json:"zi"`
}{}
if err := json.Unmarshal(data, aux); err != nil {
return err
}
if err := decodeScalar(aux.Zi, &m.Zi); err != nil {
return err
}
return nil
}