-
Notifications
You must be signed in to change notification settings - Fork 1
/
signserializers.go
122 lines (110 loc) · 3.68 KB
/
signserializers.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
package v1
import (
"bytes"
"encoding/gob"
"github.com/pkg/errors"
"github.com/coinbase/kryptology/pkg/core/curves"
"github.com/coinbase/kryptology/pkg/core/protocol"
"github.com/coinbase/kryptology/pkg/tecdsa/dkls/v1/sign"
)
func newSignProtocolMessage(payload []byte, round string, version uint) *protocol.Message {
return &protocol.Message{
Protocol: protocol.Dkls18Sign,
Version: version,
Payloads: map[string][]byte{payloadKey: payload},
Metadata: map[string]string{"round": round},
}
}
func encodeSignRound1Output(commitment [32]byte, 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(&commitment); err != nil {
return nil, errors.WithStack(err)
}
return newSignProtocolMessage(buf.Bytes(), "1", version), nil
}
func decodeSignRound2Input(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 encodeSignRound2Output(output *sign.SignRound2Output, 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 newSignProtocolMessage(buf.Bytes(), "2", version), nil
}
func decodeSignRound3Input(m *protocol.Message) (*sign.SignRound2Output, 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 := &sign.SignRound2Output{}
if err := dec.Decode(&decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}
func encodeSignRound3Output(output *sign.SignRound3Output, 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 newSignProtocolMessage(buf.Bytes(), "3", version), nil
}
func decodeSignRound4Input(m *protocol.Message) (*sign.SignRound3Output, 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 := &sign.SignRound3Output{}
if err := dec.Decode(&decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}
func encodeSignature(signature *curves.EcdsaSignature, 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(signature); err != nil {
return nil, errors.WithStack(err)
}
return newSignProtocolMessage(buf.Bytes(), "signature", version), nil
}
// DecodeSignature serializes the signature.
func DecodeSignature(m *protocol.Message) (*curves.EcdsaSignature, 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 := &curves.EcdsaSignature{}
if err := dec.Decode(decoded); err != nil {
return nil, errors.WithStack(err)
}
return decoded, nil
}