/
shuffling_protocol.go
292 lines (224 loc) · 9.89 KB
/
shuffling_protocol.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
// Package protocols contains Shuffling protocol which permits to rerandomize and shuffle a list of DP responses.
// The El-Gamal encrypted DP response should be encrypted by the collective public key of the cothority.
// In that case, each cothority server (node) can homomorphically rerandomize and shuffle the DP responses.
// This is done by creating a circuit between the servers. The DP response is sent through this circuit and
// each server applies its transformation on it and forwards it to the next node in the circuit
// until it comes back to the server who started the protocol.
package protocols
import (
"errors"
"time"
"github.com/lca1/drynx/lib"
"github.com/lca1/unlynx/lib"
"go.dedis.ch/kyber/v3"
"go.dedis.ch/onet/v3"
"go.dedis.ch/onet/v3/log"
"go.dedis.ch/onet/v3/network"
)
// ShufflingProtocolName is the registered name for the neff shuffle protocol.
const ShufflingProtocolName = "Shuffling"
func init() {
network.RegisterMessage(libdrynx.ShufflingMessage{})
network.RegisterMessage(libdrynx.ShufflingBytesMessage{})
network.RegisterMessage(SBLengthMessage{})
onet.GlobalProtocolRegister(ShufflingProtocolName, NewShufflingProtocol)
}
// Messages
//______________________________________________________________________________________________________________________
// SBLengthMessage is a message containing the lengths to read a shuffling message in bytes
type SBLengthMessage struct {
Sender string
GacbLength int
AabLength int
PgaebLength int
}
// Structs
//______________________________________________________________________________________________________________________
// ShufflingBytesStruct contains a shuffling message in bytes
type shufflingBytesStruct struct {
*onet.TreeNode
libdrynx.ShufflingBytesMessage
}
// SbLengthStruct contains a length message
type sbLengthStruct struct {
*onet.TreeNode
SBLengthMessage
}
// Protocol
//______________________________________________________________________________________________________________________
// ShufflingProtocol hold the state of a shuffling protocol instance.
type ShufflingProtocol struct {
*onet.TreeNodeInstance
// Protocol feedback channel
FeedbackChannel chan []libunlynx.ProcessResponse
// Protocol communication channels
LengthNodeChannel chan sbLengthStruct
PreviousNodeInPathChannel chan shufflingBytesStruct
ExecTimeStart time.Duration
ExecTime time.Duration
// Protocol state data
nextNodeInCircuit *onet.TreeNode
TargetOfShuffle *[]libunlynx.ProcessResponse
CollectiveKey kyber.Point //only use in order to test the protocol
Proofs int
Precomputed []libunlynx.CipherVectorScalar
Query *libdrynx.SurveyQuery
// Protocol proof data
MapPIs map[string]onet.ProtocolInstance
}
// NewShufflingProtocol constructs neff shuffle protocol instances.
func NewShufflingProtocol(n *onet.TreeNodeInstance) (onet.ProtocolInstance, error) {
dsp := &ShufflingProtocol{
TreeNodeInstance: n,
FeedbackChannel: make(chan []libunlynx.ProcessResponse),
}
if err := dsp.RegisterChannel(&dsp.PreviousNodeInPathChannel); err != nil {
return nil, errors.New("couldn't register data reference channel: " + err.Error())
}
if err := dsp.RegisterChannel(&dsp.LengthNodeChannel); err != nil {
return nil, errors.New("couldn't register data reference channel: " + err.Error())
}
var i int
var node *onet.TreeNode
var nodeList = n.Tree().List()
for i, node = range nodeList {
if n.TreeNode().Equal(node) {
dsp.nextNodeInCircuit = nodeList[(i+1)%len(nodeList)]
break
}
}
return dsp, nil
}
// Start is called at the root node and starts the execution of the protocol.
func (p *ShufflingProtocol) Start() error {
roundTotalStart := libunlynx.StartTimer(p.Name() + "_Shuffling(START)")
if p.TargetOfShuffle == nil {
return errors.New("no map given as shuffling target")
}
p.ExecTimeStart = 0
p.ExecTime = 0
startT := time.Now()
nbrProcessResponses := len(*p.TargetOfShuffle)
log.Lvl2("[SHUFFLING PROTOCOL] <Drynx> Server", p.ServerIdentity(), " started a Shuffling Protocol (", nbrProcessResponses, " responses)")
shuffleTarget := *p.TargetOfShuffle
if len(shuffleTarget) == 1 { //cannot shuffle 1 -> add a dummy response with 0s
pr := libunlynx.ProcessResponse{}
pr.GroupByEnc = shuffleTarget[0].GroupByEnc
pr.WhereEnc = shuffleTarget[0].WhereEnc
pr.AggregatingAttributes = make(libunlynx.CipherVector, len(shuffleTarget[0].AggregatingAttributes))
for i := range shuffleTarget[0].AggregatingAttributes {
pr.AggregatingAttributes[i] = libunlynx.IntToCipherText(int64(0))
}
shuffleTarget = append(shuffleTarget, pr)
}
collectiveKey := p.Aggregate()
if p.CollectiveKey != nil {
//test
collectiveKey = p.CollectiveKey
log.Lvl2("Key used is ", collectiveKey)
}
roundShufflingStart := libunlynx.StartTimer(p.Name() + "_Shuffling(START-noProof)")
if p.Precomputed != nil {
log.Lvl2("[SHUFFLING PROTOCOL] <Drynx> Server", p.ServerIdentity(), " uses pre-computation in shuffling")
}
shuffledData, pi, beta := libdrynx.ShuffleSequence(shuffleTarget, nil, collectiveKey, p.Precomputed)
libunlynx.EndTimer(roundShufflingStart)
roundShufflingStartProof := libunlynx.StartTimer(p.Name() + "_Shuffling(START-Proof)")
if p.Proofs != 0 {
go func() {
log.Lvl2("[SHUFFLING PROTOCOL] <Drynx> Server", p.ServerIdentity(), "creates shuffling proof")
proof := libdrynx.ShufflingProofCreation(shuffleTarget, shuffledData, libunlynx.SuiTe.Point().Base(), collectiveKey, beta, pi)
pi := p.MapPIs["shuffle/"+p.ServerIdentity().String()]
pi.(*ProofCollectionProtocol).Proof = libdrynx.ProofRequest{ShuffleProof: libdrynx.NewShuffleProofRequest(&proof, p.Query.SurveyID, p.ServerIdentity().String(), "", p.Query.Query.RosterVNs, p.Private(), nil)}
go pi.Dispatch()
go pi.Start()
<-pi.(*ProofCollectionProtocol).FeedbackChannel
}()
}
libunlynx.EndTimer(roundShufflingStartProof)
libunlynx.EndTimer(roundTotalStart)
p.ExecTimeStart += time.Since(startT)
//sendingStart := libdrynx.StartTimer(p.Name() + "_Sending")
message := libdrynx.ShufflingBytesMessage{}
var cgaLength, eaaLength, egaLength int
message.Data, cgaLength, eaaLength, egaLength = (&libdrynx.ShufflingMessage{Data: shuffledData}).ToBytes()
sendingStart := libunlynx.StartTimer(p.Name() + "_Sending")
p.sendToNext(&SBLengthMessage{p.ServerIdentity().String(), cgaLength, eaaLength, egaLength})
p.sendToNext(&message)
libunlynx.EndTimer(sendingStart)
return nil
}
// Dispatch is called on each tree node. It waits for incoming messages and handles them.
func (p *ShufflingProtocol) Dispatch() error {
defer p.Done()
shufflingLength := <-p.LengthNodeChannel
receiving := libunlynx.StartTimer(p.Name() + "_Receiving")
tmp := <-p.PreviousNodeInPathChannel
libunlynx.EndTimer(receiving)
sm := libdrynx.ShufflingMessage{}
sm.FromBytes(tmp.Data, shufflingLength.GacbLength, shufflingLength.AabLength, shufflingLength.PgaebLength)
shufflingTarget := sm.Data
startT := time.Now()
roundTotalComputation := libunlynx.StartTimer(p.Name() + "_Shuffling(DISPATCH)")
collectiveKey := p.Aggregate() //shuffling is by default done with collective authority key
if p.CollectiveKey != nil {
//test
collectiveKey = p.CollectiveKey
log.Lvl2("Key used: ", collectiveKey)
}
if p.Precomputed != nil {
log.Lvl2("[SHUFFLING PROTOCOL] <Drynx> Server", p.ServerIdentity(), " uses pre-computation in shuffling")
}
shuffledData := shufflingTarget
var pi []int
var beta [][]kyber.Scalar
if !p.IsRoot() {
roundShuffle := libunlynx.StartTimer(p.Name() + "_Shuffling(DISPATCH-noProof)")
shuffledData, pi, beta = libdrynx.ShuffleSequence(shufflingTarget, nil, collectiveKey, p.Precomputed)
libunlynx.EndTimer(roundShuffle)
roundShuffleProof := libunlynx.StartTimer(p.Name() + "_Shuffling(DISPATCH-Proof)")
if p.Proofs != 0 {
go func(shufflingTarget []libunlynx.ProcessResponse, shuffledData []libunlynx.ProcessResponse) {
log.Lvl2("[SHUFFLING PROTOCOL] <Drynx> Server", p.ServerIdentity(), "creates shuffling proof")
proof := libdrynx.ShufflingProofCreation(shufflingTarget, shuffledData, libunlynx.SuiTe.Point().Base(), collectiveKey, beta, pi)
pi := p.MapPIs["shuffle/"+p.ServerIdentity().String()]
pi.(*ProofCollectionProtocol).Proof = libdrynx.ProofRequest{ShuffleProof: libdrynx.NewShuffleProofRequest(&proof, p.Query.SurveyID, p.ServerIdentity().String(), "", p.Query.Query.RosterVNs, p.Private(), nil)}
go pi.Dispatch()
go pi.Start()
<-pi.(*ProofCollectionProtocol).FeedbackChannel
}(shufflingTarget, shuffledData)
}
libunlynx.EndTimer(roundShuffleProof)
}
shufflingTarget = shuffledData
if p.IsRoot() {
log.Lvl2("[SHUFFLING PROTOCOL] <Drynx> Server", p.ServerIdentity(), " completed shuffling (", len(shufflingTarget), " responses)")
} else {
log.Lvl2("[SHUFFLING PROTOCOL] <Drynx> Server", p.ServerIdentity(), " carried on shuffling.")
}
libunlynx.EndTimer(roundTotalComputation)
// If this tree node is the root, then protocol reached the end.
if p.IsRoot() {
p.ExecTime += time.Since(startT)
p.FeedbackChannel <- shufflingTarget
} else {
message := libdrynx.ShufflingBytesMessage{}
var cgaLength, eaaLength, egaLength int
message.Data, cgaLength, eaaLength, egaLength = (&libdrynx.ShufflingMessage{Data: shuffledData}).ToBytes()
sending := libunlynx.StartTimer(p.Name() + "_Sending")
p.sendToNext(&SBLengthMessage{p.ServerIdentity().String(), cgaLength, eaaLength, egaLength})
p.sendToNext(&message)
libunlynx.EndTimer(sending)
}
return nil
}
// Sends the message msg to the next node in the circuit based on the next TreeNode in Tree.List() If not visited yet.
// If the message already visited the next node, doesn't send and returns false. Otherwise, return true.
func (p *ShufflingProtocol) sendToNext(msg interface{}) {
err := p.SendTo(p.nextNodeInCircuit, msg)
if err != nil {
log.Lvl2("Had an error sending a message: ", err)
}
}
// Conversion
//______________________________________________________________________________________________________________________