-
Notifications
You must be signed in to change notification settings - Fork 136
/
commit.go
128 lines (108 loc) · 2.95 KB
/
commit.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
package frost
import (
"encoding/binary"
"errors"
"io"
"sort"
"github.com/cloudflare/circl/group"
)
type Nonce struct {
ID uint16
hiding, binding group.Scalar
}
func (s Suite) nonceGenerate(rnd io.Reader, secret group.Scalar) (group.Scalar, error) {
k := make([]byte, 32)
_, err := io.ReadFull(rnd, k)
if err != nil {
return nil, err
}
secretEnc, err := secret.MarshalBinary()
if err != nil {
return nil, err
}
return s.hasher.h4(append(append([]byte{}, k...), secretEnc...)), nil
}
type Commitment struct {
ID uint16
hiding, binding group.Element
}
func (c Commitment) MarshalBinary() ([]byte, error) {
bytes := (&[2]byte{})[:]
binary.BigEndian.PutUint16(bytes, c.ID)
h, err := c.hiding.MarshalBinaryCompress()
if err != nil {
return nil, err
}
b, err := c.binding.MarshalBinaryCompress()
if err != nil {
return nil, err
}
return append(append(bytes, h...), b...), nil
}
func encodeComs(coms []*Commitment) ([]byte, error) {
sort.SliceStable(coms, func(i, j int) bool { return coms[i].ID < coms[j].ID })
var out []byte
for i := range coms {
cEnc, err := coms[i].MarshalBinary()
if err != nil {
return nil, err
}
out = append(out, cEnc...)
}
return out, nil
}
type bindingFactor struct {
ID uint16
factor group.Scalar
}
func (s Suite) getBindingFactorFromID(bindingFactors []bindingFactor, id uint) (group.Scalar, error) {
for i := range bindingFactors {
if uint(bindingFactors[i].ID) == id {
return bindingFactors[i].factor, nil
}
}
return nil, errors.New("frost: id not found")
}
func (s Suite) getBindingFactors(coms []*Commitment, msg []byte) ([]bindingFactor, error) {
msgHash := s.hasher.h3(msg)
commitEncoded, err := encodeComs(coms)
if err != nil {
return nil, err
}
commitEncodedHash := s.hasher.h3(commitEncoded)
rhoInputPrefix := append(append([]byte{}, msgHash...), commitEncodedHash...)
bindingFactors := make([]bindingFactor, len(coms))
id := (&[2]byte{})[:]
for i := range coms {
binary.BigEndian.PutUint16(id, coms[i].ID)
bf := s.hasher.h1(append(append([]byte{}, rhoInputPrefix...), id...))
bindingFactors[i] = bindingFactor{ID: coms[i].ID, factor: bf}
}
return bindingFactors, nil
}
func (s Suite) getGroupCommitment(coms []*Commitment, bindingFactors []bindingFactor) (group.Element, error) {
gc := s.g.NewElement()
tmp := s.g.NewElement()
for i := range coms {
bf, err := s.getBindingFactorFromID(bindingFactors, uint(coms[i].ID))
if err != nil {
return nil, err
}
tmp.Mul(coms[i].binding, bf)
tmp.Add(tmp, coms[i].hiding)
gc.Add(gc, tmp)
}
return gc, nil
}
func (s Suite) getChallenge(groupCom group.Element, pubKey *PublicKey, msg []byte) (group.Scalar, error) {
gcEnc, err := groupCom.MarshalBinaryCompress()
if err != nil {
return nil, err
}
pkEnc, err := pubKey.key.MarshalBinaryCompress()
if err != nil {
return nil, err
}
chInput := append(append(append([]byte{}, gcEnc...), pkEnc...), msg...)
return s.hasher.h2(chInput), nil
}