-
Notifications
You must be signed in to change notification settings - Fork 1
/
dkgserializers.go
450 lines (406 loc) · 15.4 KB
/
dkgserializers.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package v1
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/pkg/errors"
"github.com/coinbase/kryptology/pkg/core/curves"
"github.com/coinbase/kryptology/pkg/core/protocol"
"github.com/coinbase/kryptology/pkg/ot/base/simplest"
"github.com/coinbase/kryptology/pkg/ot/extension/kos"
v0 "github.com/coinbase/kryptology/pkg/tecdsa/dkls/v0"
"github.com/coinbase/kryptology/pkg/tecdsa/dkls/v1/dkg"
"github.com/coinbase/kryptology/pkg/zkp/schnorr"
)
const payloadKey = "direct"
func newDkgProtocolMessage(payload []byte, round string, version uint) *protocol.Message {
return &protocol.Message{
Protocol: protocol.Dkls18Dkg,
Version: version,
Payloads: map[string][]byte{payloadKey: payload},
Metadata: map[string]string{"round": round},
}
}
func registerTypes() {
gob.Register(&curves.ScalarK256{})
gob.Register(&curves.PointK256{})
gob.Register(&curves.ScalarP256{})
gob.Register(&curves.PointP256{})
}
func encodeDkgRound1Output(commitment [32]byte, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
registerTypes()
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(&commitment); err != nil {
return nil, errors.WithStack(err)
}
return newDkgProtocolMessage(buf.Bytes(), "1", version), nil
}
func decodeDkgRound2Input(m *protocol.Message) ([32]byte, error) {
if m.Version != protocol.Version1 {
return [32]byte{}, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := [32]byte{}
if err := dec.Decode(&decoded); err != nil {
return [32]byte{}, errors.WithStack(err)
}
return decoded, nil
}
func encodeDkgRound2Output(output *dkg.Round2Output, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(output); err != nil {
return nil, errors.WithStack(err)
}
return newDkgProtocolMessage(buf.Bytes(), "2", version), nil
}
func decodeDkgRound3Input(m *protocol.Message) (*dkg.Round2Output, error) {
if m.Version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := new(dkg.Round2Output)
if err := dec.Decode(decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}
func encodeDkgRound3Output(proof *schnorr.Proof, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(proof); err != nil {
return nil, errors.WithStack(err)
}
return newDkgProtocolMessage(buf.Bytes(), "3", version), nil
}
func decodeDkgRound4Input(m *protocol.Message) (*schnorr.Proof, error) {
if m.Version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := new(schnorr.Proof)
if err := dec.Decode(decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}
func encodeDkgRound4Output(proof *schnorr.Proof, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(proof); err != nil {
return nil, errors.WithStack(err)
}
return newDkgProtocolMessage(buf.Bytes(), "4", version), nil
}
func decodeDkgRound5Input(m *protocol.Message) (*schnorr.Proof, error) {
if m.Version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := new(schnorr.Proof)
if err := dec.Decode(decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}
func encodeDkgRound5Output(proof *schnorr.Proof, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(proof); err != nil {
return nil, errors.WithStack(err)
}
return newDkgProtocolMessage(buf.Bytes(), "5", version), nil
}
func decodeDkgRound6Input(m *protocol.Message) (*schnorr.Proof, error) {
if m.Version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := new(schnorr.Proof)
if err := dec.Decode(decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}
func encodeDkgRound6Output(choices []simplest.ReceiversMaskedChoices, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(choices); err != nil {
return nil, errors.WithStack(err)
}
return newDkgProtocolMessage(buf.Bytes(), "6", version), nil
}
func decodeDkgRound7Input(m *protocol.Message) ([]simplest.ReceiversMaskedChoices, error) {
if m.Version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := []simplest.ReceiversMaskedChoices{}
if err := dec.Decode(&decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}
func encodeDkgRound7Output(challenge []simplest.OtChallenge, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(challenge); err != nil {
return nil, errors.WithStack(err)
}
return newDkgProtocolMessage(buf.Bytes(), "7", version), nil
}
func decodeDkgRound8Input(m *protocol.Message) ([]simplest.OtChallenge, error) {
if m.Version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := []simplest.OtChallenge{}
if err := dec.Decode(&decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}
func encodeDkgRound8Output(responses []simplest.OtChallengeResponse, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(responses); err != nil {
return nil, errors.WithStack(err)
}
return newDkgProtocolMessage(buf.Bytes(), "8", version), nil
}
func decodeDkgRound9Input(m *protocol.Message) ([]simplest.OtChallengeResponse, error) {
if m.Version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := []simplest.OtChallengeResponse{}
if err := dec.Decode(&decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}
func encodeDkgRound9Output(opening []simplest.ChallengeOpening, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(opening); err != nil {
return nil, errors.WithStack(err)
}
return newDkgProtocolMessage(buf.Bytes(), "9", version), nil
}
func decodeDkgRound10Input(m *protocol.Message) ([]simplest.ChallengeOpening, error) {
if m.Version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := []simplest.ChallengeOpening{}
if err := dec.Decode(&decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}
// EncodeAliceDkgOutput serializes Alice DKG output based on the protocol version.
func EncodeAliceDkgOutput(result *dkg.AliceOutput, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
registerTypes()
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(result); err != nil {
return nil, errors.WithStack(err)
}
return newDkgProtocolMessage(buf.Bytes(), "alice-output", version), nil
}
// DecodeAliceDkgResult deserializes Alice DKG output.
func DecodeAliceDkgResult(m *protocol.Message) (*dkg.AliceOutput, error) {
if m.Version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
registerTypes()
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := new(dkg.AliceOutput)
if err := dec.Decode(&decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}
// EncodeBobDkgOutput serializes Bob DKG output based on the protocol version.
func EncodeBobDkgOutput(result *dkg.BobOutput, version uint) (*protocol.Message, error) {
if version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
registerTypes()
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(result); err != nil {
return nil, errors.WithStack(err)
}
return newDkgProtocolMessage(buf.Bytes(), "bob-output", version), nil
}
// DecodeBobDkgResult deserializes Bob DKG output.
func DecodeBobDkgResult(m *protocol.Message) (*dkg.BobOutput, error) {
if m.Version != protocol.Version1 {
return nil, errors.New("only version 1 is supported")
}
buf := bytes.NewBuffer(m.Payloads[payloadKey])
dec := gob.NewDecoder(buf)
decoded := new(dkg.BobOutput)
if err := dec.Decode(&decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}
// ConvertAliceDkgOutputToV1 converts the V0 output to V1 output.
// The V0 version of DKls `gob` encoded entire `Alice` object and returned it as DKG state and returned this state and
// the public key to the caller as the serialized version of DKG.
// In contrast, the V1 version of DKLs `gob` encodes only what is need for signing algorithm. Therefore, this function
// first decodes the V0 dkg result to Alice object and a public key. Then extracts the required data out of the Alice
// object and creates V1 Dkg output object.
// Note that in addition to extracting the required data, this function also performs type conversions on curve Scalar
// and Points. The reason is that between V0 and V1, the interface and data types for curve computation has changed.
//
// Furthermore, the new encoded value is represented as a `protocol.Message` which contains versioning and other
// metadata about the serialized values. This serialized value will be the input to the sign function.
//
// In summary, the following data mapping and conversion is performed.
// - v0.alice.Pk -> Converted to v1.PublicKey (a curve Point)
// - v0.alice.SkA -> Converted to v1.SecretKeyShare (a scalar value)
// - v0.alice.Receiver.Packed -> Converted to v1.SeedOtResult.PackedRandomChoiceBits (the random choice bits in OT)
// - v0.alice.Receiver.Packed -> Converted to v1.SeedOtResult.RandomChoiceBits (the random choice bits in OT in unpacked form)
// - v0.alice.Receiver.Rho -> Converted to v1.SeedOtResult.OneTimePadDecryptionKey (the Rho value in the paper)
func ConvertAliceDkgOutputToV1(params *v0.Params, dkgResult []byte) (*protocol.Message, error) {
alice, err := v0.DecodeAlice(params, dkgResult)
if err != nil {
return nil, errors.WithStack(err)
}
var curve *curves.Curve
if params.Curve.Params().Name == curves.K256Name {
curve = curves.K256()
} else if params.Curve.Params().Name == curves.P256Name {
curve = curves.P256()
} else {
return nil, fmt.Errorf("unsupported curve %s", params.Curve.Params().Name)
}
publicKey, err := curve.Point.Set(alice.Pk.X, alice.Pk.Y)
if err != nil {
return nil, errors.WithStack(err)
}
secretKey, err := curve.Scalar.SetBigInt(alice.SkA)
if err != nil {
return nil, errors.WithStack(err)
}
packedChoiceBits := make([]byte, len(alice.Receiver.Packed))
copy(packedChoiceBits, alice.Receiver.Packed[:])
randomChoiceBits := make([]int, kos.Kappa)
for i := 0; i < len(randomChoiceBits); i++ {
randomChoiceBits[i] = int(simplest.ExtractBitFromByteVector(packedChoiceBits, i))
}
decryptionPads := make([]simplest.OneTimePadDecryptionKey, kos.Kappa)
for i := 0; i < kos.Kappa; i++ {
for j := 0; j < simplest.DigestSize; j++ {
decryptionPads[i][j] = alice.Receiver.Rho[i][j]
}
}
dkgConvertedResult := &dkg.AliceOutput{
PublicKey: publicKey,
SecretKeyShare: secretKey,
SeedOtResult: &simplest.ReceiverOutput{
PackedRandomChoiceBits: packedChoiceBits,
RandomChoiceBits: randomChoiceBits,
OneTimePadDecryptionKey: decryptionPads,
},
}
return EncodeAliceDkgOutput(dkgConvertedResult, protocol.Version1)
}
// ConvertBobDkgOutputToV1 converts the V0 output to V1 output.
// The V0 version of DKls `gob` encoded entire `Bob` object and returned it as DKG state and returned this state and
// the public key to the caller as the serialized version of DKG.
// In contrast, the V1 version of DKLs `gob` encodes only what is need for signing algorithm. Therefore, this function
// first decodes the V0 dkg result to Bob object and a public key. Then extracts the required data out of the Bob
// object and creates V1 Dkg output object.
// Note that in addition to extracting the required data, this function also performs type conversions on curve Scalar
// and Points. The reason is that between V0 and V1, the interface and data types for curve computation has changed.
//
// Furthermore, the new encoded value is represented as a `protocol.Message` which contains versioning and other
// metadata about the serialized values. This serialized value will be the input to the sign function.
//
// In summary, the following data mapping and conversion is performed.
// - v0.bob.Pk -> Converted to v1.PublicKey (a curve Point)
// - v0.bob.SkA -> Converted to v1.SecretKeyShare (a scalar value)
// - v0.bob.Sender.Rho -> Converted to v1.SeedOtResult.OneTimePadEncryptionKeys (the Rho value in the paper)
func ConvertBobDkgOutputToV1(params *v0.Params, dkgResult []byte) (*protocol.Message, error) {
bob, err := v0.DecodeBob(params, dkgResult)
if err != nil {
return nil, errors.WithStack(err)
}
var curve *curves.Curve
if params.Curve.Params().Name == curves.K256Name {
curve = curves.K256()
} else if params.Curve.Params().Name == curves.P256Name {
curve = curves.P256()
} else {
return nil, fmt.Errorf("unsupported curve %s", params.Curve.Params().Name)
}
publicKey, err := curve.Point.Set(bob.Pk.X, bob.Pk.Y)
if err != nil {
return nil, errors.WithStack(err)
}
secretKey, err := curve.Scalar.SetBigInt(bob.SkB)
if err != nil {
return nil, errors.WithStack(err)
}
encryptionPads := make([]simplest.OneTimePadEncryptionKeys, kos.Kappa)
for i := 0; i < kos.Kappa; i++ {
for k := 0; k < 2; k++ { // simplest.keyCount
for j := 0; j < simplest.DigestSize; j++ {
encryptionPads[i][k][j] = bob.Sender.Rho[i][k][j]
}
}
}
dkgConvertedResult := &dkg.BobOutput{
PublicKey: publicKey,
SecretKeyShare: secretKey,
SeedOtResult: &simplest.SenderOutput{
OneTimePadEncryptionKeys: encryptionPads,
},
}
return EncodeBobDkgOutput(dkgConvertedResult, protocol.Version1)
}