-
Notifications
You must be signed in to change notification settings - Fork 136
/
commit.go
134 lines (115 loc) · 3.03 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
129
130
131
132
133
134
package frost
import (
"errors"
"fmt"
"io"
"sort"
"github.com/cloudflare/circl/group"
)
type Nonce struct {
ID group.Scalar
hiding, binding group.Scalar
}
func (s Suite) nonceGenerate(rnd io.Reader, secret group.Scalar) (group.Scalar, error) {
randomBytes := make([]byte, 32)
_, err := io.ReadFull(rnd, randomBytes)
if err != nil {
return nil, err
}
secretEnc, err := secret.MarshalBinary()
if err != nil {
return nil, err
}
return s.hasher.h3(append(randomBytes, secretEnc...)), nil
}
type Commitment struct {
ID group.Scalar
hiding, binding group.Element
}
func (c Commitment) MarshalBinary() ([]byte, error) {
id, err := c.ID.MarshalBinary()
if err != nil {
return nil, err
}
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(id, h...), b...), nil
}
func encodeCommitments(coms []*Commitment) ([]byte, error) {
sort.SliceStable(coms, func(i, j int) bool {
return coms[i].ID.(fmt.Stringer).String() < coms[j].ID.(fmt.Stringer).String()
})
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 group.Scalar
factor group.Scalar
}
func (s Suite) getBindingFactorFromID(bindingFactors []bindingFactor, id group.Scalar) (group.Scalar, error) {
for i := range bindingFactors {
if bindingFactors[i].ID.IsEqual(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.h4(msg)
encodeComs, err := encodeCommitments(coms)
if err != nil {
return nil, err
}
encodeComsHash := s.hasher.h5(encodeComs)
rhoInputPrefix := append(msgHash, encodeComsHash...)
bindingFactors := make([]bindingFactor, len(coms))
for i := range coms {
id, err := coms[i].ID.MarshalBinary()
if err != nil {
return nil, err
}
rhoInput := append(append([]byte{}, rhoInputPrefix...), id...)
bf := s.hasher.h1(rhoInput)
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, 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
}