-
Notifications
You must be signed in to change notification settings - Fork 2
/
proposal.go
307 lines (238 loc) · 6.18 KB
/
proposal.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
package Schultz
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"fmt"
"math/rand"
"sort"
"strings"
"time"
"../../utils/conv"
polycommit "../../utils/polycommit/pbc"
"../../utils/polyring"
"../../utils/vector"
"github.com/ncw/gmp"
log "github.com/sirupsen/logrus"
)
type OldNodeID int32
type NewNodeID int32
type PointsOnBlindingPoly struct {
points map[NewNodeID]*gmp.Int
}
func (pz PointsOnBlindingPoly) Equal(other PointsOnBlindingPoly) bool {
if len(pz.points) != len(other.points) {
return false
}
for id, p := range pz.points {
if pOther, ok := other.points[id]; ok {
if 0 != p.Cmp(pOther) {
return false
}
} else {
return false
}
}
return true
}
func (pz PointsOnBlindingPoly) GobEncode() ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.Encode(pz.points); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (pz *PointsOnBlindingPoly) GobDecode(buf []byte) error {
r := bytes.NewBuffer(buf)
dec := gob.NewDecoder(r)
if err := dec.Decode(&pz.points); err != nil {
return err
}
return nil
}
func (pz PointsOnBlindingPoly) Bytes() []byte {
var buf bytes.Buffer
// To store the keys in slice in sorted order
var keys []NewNodeID
for k := range pz.points {
keys = append(keys, k)
}
sort.SliceStable(keys, func(i, j int) bool { return keys[i] < keys[j] })
for _, k := range keys {
buf.Write(pz.points[k].Bytes())
}
return buf.Bytes()
}
func (pz PointsOnBlindingPoly) String() string {
s := ""
for id, point := range pz.points {
s += fmt.Sprintf("%d => %s, ", id, point.String())
}
return s
}
type Proposal struct {
commQ polycommit.PolyCommit
// one for each new node
commRs map[NewNodeID]polycommit.PolyCommit
// one for each old node
pointToPeers map[OldNodeID]PointsOnBlindingPoly
}
func (p Proposal) GobEncode() ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.Encode(p.commQ); err != nil {
return nil, err
}
if err := enc.Encode(p.commRs); err != nil {
return nil, err
}
if err := enc.Encode(p.pointToPeers); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (p *Proposal) GobDecode(buf []byte) error {
r := bytes.NewBuffer(buf)
dec := gob.NewDecoder(r)
if err := dec.Decode(&p.commQ); err != nil {
return err
}
if err := dec.Decode(&p.commRs); err != nil {
return err
}
return dec.Decode(&p.pointToPeers)
}
func (p Proposal) Equal(other Proposal) bool {
if !p.commQ.Equals(other.commQ) {
return false
}
if len(p.commRs) != len(other.commRs) {
return false
}
for i, v := range p.commRs {
if !v.Equals(other.commRs[i]) {
return false
}
}
if len(p.pointToPeers) != len(p.pointToPeers) {
return false
}
for id, pp := range p.pointToPeers {
if pOther, ok := other.pointToPeers[id]; ok {
if !pp.Equal(pOther) {
return false
}
} else {
return false
}
}
return true
}
func (p Proposal) Hash() [32]byte {
hash := sha256.New()
hash.Write(p.commQ.Bytes())
{
// To store the keys in slice in sorted order
var keys []NewNodeID
for k := range p.commRs {
keys = append(keys, k)
}
sort.SliceStable(keys, func(i, j int) bool { return keys[i] < keys[j] })
for _, k := range keys {
hash.Write(p.commRs[k].Bytes())
}
}
{
// To store the keys in slice in sorted order
var keys []OldNodeID
for k := range p.pointToPeers {
keys = append(keys, k)
}
sort.SliceStable(keys, func(i, j int) bool { return keys[i] < keys[j] })
for _, k := range keys {
hash.Write(p.pointToPeers[k].Bytes())
}
}
var result [32]byte
copy(result[:], hash.Sum(nil))
return result
}
func (p Proposal) ToBytes() []byte {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.Encode(p); err != nil {
panic(err.Error())
}
return buf.Bytes()
}
func (p Proposal) String() string {
s := fmt.Sprintf("Comm(Q): %s\n", p.commQ.String())
for _, comm := range p.commRs {
s += fmt.Sprintf("-- Comm(Rs): %s\n", comm.String())
}
for i, peer := range p.pointToPeers {
s += fmt.Sprintf("points to peer %d: %s\n", i, peer.String())
}
return strings.TrimSuffix(s, "\n")
}
func (p Proposal) Print() {
fmt.Println(p.String())
}
func GenerateProposal(pp PublicParameter) Proposal {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
Q, err := polyring.NewRand(pp.degree, r, pp.prime)
if err != nil {
panic(err.Error())
}
// indices of old group nodes
oldGroupIndices := vector.FromInt64(pp.oldGroup...).GetPtr()
// make it zero know
Q.GetPtrToConstant().SetUint64(0)
// commit to it!
commQ := polycommit.NewPolyCommit(Q)
// blinding polynomials
blindingPolys := make(map[NewNodeID]polyring.Polynomial, len(pp.newGroup))
commBlindingPolyList := make(map[NewNodeID]polycommit.PolyCommit, len(pp.newGroup))
for _, newNodeId := range pp.newGroup {
blindingPolyForI, err := polyring.NewRand(pp.degree-1, r, pp.prime)
if err != nil {
panic(err.Error())
}
// Rk = Rk * (x-newNodeId)
blindingPolyForI.MulSelf(polyring.FromVec(int64(-newNodeId), 1))
blindingPolyForI.Mod(pp.prime)
// store the blinding poly
blindingPolys[NewNodeID(newNodeId)] = blindingPolyForI
// commitment to the blinding polynomials
commBlindingPolyList[NewNodeID(newNodeId)] = polycommit.NewPolyCommit(blindingPolyForI)
}
// for each old group member, evaluate points on blinding polynomials
pointsOnQ := vector.New(len(pp.oldGroup))
Q.EvalModArray(oldGroupIndices, pp.prime, pointsOnQ.GetPtr())
proposal := Proposal{
commQ,
commBlindingPolyList,
make(map[OldNodeID]PointsOnBlindingPoly, len(pp.oldGroup)),
}
// j is the id for an old group member
for _, j := range pp.oldGroup {
nodeJ := gmp.NewInt(int64(j))
Qj := gmp.NewInt(0)
Q.EvalMod(nodeJ, pp.prime, Qj)
if !commQ.VerifyEval(conv.GmpInt2BigInt(nodeJ), conv.GmpInt2BigInt(Qj)) {
log.Fatalf("Q(j) not on Q, which is a bug")
}
BlidingPointsForJ := make(map[NewNodeID]*gmp.Int, len(pp.newGroup))
for nodeK, Rk := range blindingPolys {
Rkj := gmp.NewInt(0)
Rk.EvalMod(nodeJ, pp.prime, Rkj)
BlidingPointsForJ[NewNodeID(nodeK)] = gmp.NewInt(0)
BlidingPointsForJ[NewNodeID(nodeK)].Add(Qj, Rkj)
}
proposal.pointToPeers[OldNodeID(j)] = PointsOnBlindingPoly{
points: BlidingPointsForJ,
}
}
return proposal
}