election_test.go 23.1 KB
Newer Older
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
1
2
3
4
package election

import (
	"bsc-shamir/crypto/common"
Mikkel Wienberg Madsen's avatar
Mikkel Wienberg Madsen committed
5
6
	"bsc-shamir/crypto/pedersen"
	"bsc-shamir/crypto/sigma"
7
	"bsc-shamir/math/zn"
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
8
	"bsc-shamir/network"
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
9
10
11
12
	"crypto/rand"
	"io/ioutil"
	"log"
	"math/big"
13
	"os"
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
14
15
16
17
18
19
20
	"strconv"
	"testing"
	"time"

	"github.com/google/uuid"
)

Thomas Hoffmann's avatar
Thomas Hoffmann committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
func primes(bit string) (string, string) {
	switch bit {
	case "16":
		return "C047", "6023"
	case "32":
		return "E0885DBF", "70442EDF"
	case "64":
		return "D74913FAF3F964EF", "6BA489FD79FCB277"
	case "128":
		return "F213D1D3FCB695A327DACA080380F5B7", "7909E8E9FE5B4AD193ED650401C07ADB"
	case "256":
		return "FB6E2B44AC03CE014942C38631E31810C18B97B8C1110DB6DC698CB71AE9A65F", "7DB715A25601E700A4A161C318F18C0860C5CBDC608886DB6E34C65B8D74D32F"
	case "512":
		return "E5F3ADA593237B17A587FCC4AF32F72DC75CAA3E97C256F599303EB1B855CC4229E0C80B20A361C44CF4F45E28828A734BA1AEFF0D90C6880E6A9DF640D058F7", "72F9D6D2C991BD8BD2C3FE6257997B96E3AE551F4BE12B7ACC981F58DC2AE62114F064059051B0E2267A7A2F14414539A5D0D77F86C8634407354EFB20682C7B"
	default:
		return "", ""
	}
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
40
41
42
43
/////////////////////
///Setup functions///
/////////////////////

44
45
46
47
48
49
50
51
52
53
54
55
func benchConfig() *common.Params {
	config := &common.Params{
		P:  big.NewInt(3767033279),
		Q:  big.NewInt(1883516639),
		Zp: zn.NewRing(big.NewInt(3767033279)),
		Zq: zn.NewRing(big.NewInt(1883516639)),
		G:  big.NewInt(2),
		H:  big.NewInt(3),
	}
	return config
}

Thomas Hoffmann's avatar
Thomas Hoffmann committed
56
func createConfig(p, q string) *common.Params {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
57
	return common.NewParams(p, q, "2", "3", 16)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
58
59
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
60
61
62
63
64
65
66
67
func setupVote(votecount int) []*big.Int {
	votes := make([]*big.Int, votecount)
	for i := 0; i < votecount; i++ {
		votes[i], _ = rand.Int(rand.Reader, big.NewInt(1))
	}
	return votes
}

68
func createAllBallots(votes []*big.Int, servercount int, reqserver int, xs []*big.Int, params *common.Params) []map[UniqueID]*Ballot {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
69
70
71
	ballots := make([]map[UniqueID]*Ballot, len(votes))
	for votenum := 0; votenum < len(votes); votenum++ {
		for servernum := 0; servernum < servercount; servernum++ {
72
			ballots[votenum] = CreateBenchBallots(reqserver, xs, votes[votenum], params)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
73
74
75
76
77
78
79
80
81
82
83
84
85
		}
	}
	return ballots
}

func createXS(n int) []*big.Int {
	xs := make([]*big.Int, n)
	for i := 1; i <= n; i++ {
		xs[i-1] = big.NewInt(int64(i))
	}
	return xs
}

86
87
88
89
90
func CreateBenchBallots(t int, xs []*big.Int, vote *big.Int, params *common.Params) map[UniqueID]*Ballot {
	binder := params.Zq.GetRandomElement()
	shares, commits := pedersen.NewParams(params).Create(t, xs, vote, binder)
	proof := sigma.NewParams(params).Prove(vote, binder, commits[0])

Mikkel Wienberg Madsen's avatar
Mikkel Wienberg Madsen committed
91
92
	timestamp := time.Now()
	id := uuid.New()
93

Mikkel Wienberg Madsen's avatar
Mikkel Wienberg Madsen committed
94
	ballots := make(map[UniqueID]*Ballot)
95
96
	for _, share := range shares {
		ballots[UniqueID(share.X.String())] = &Ballot{
Mikkel Wienberg Madsen's avatar
Mikkel Wienberg Madsen committed
97
98
			ID:        id,
			Timestamp: timestamp,
99
100
101
			Share:     share,
			Commits:   commits,
			Proofs:    proof,
Mikkel Wienberg Madsen's avatar
Mikkel Wienberg Madsen committed
102
103
		}
	}
104

Mikkel Wienberg Madsen's avatar
Mikkel Wienberg Madsen committed
105
106
107
	return ballots
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
108
109
110
111
112
113
///////////////////////
///Client benchmarks///
///////////////////////

var result []map[UniqueID]*Ballot

114
func benchmarkBallots(params *common.Params, votecount, serverCount, reqServer int, b *testing.B) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
115
116
117
118
119
120
121
122
	var r []map[UniqueID]*Ballot
	//Setup
	b.StopTimer()
	votes := setupVote(votecount)
	xs := createXS(serverCount)
	b.StartTimer()
	//Actual benchmark
	for i := 0; i < b.N; i++ {
123
		r = createAllBallots(votes, serverCount, reqServer, xs, params)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
124
125
126
127
128
	}
	result = r
}

//Variable n
Thomas Hoffmann's avatar
Thomas Hoffmann committed
129
func BenchmarkClientServer(b *testing.B) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
130
	log.SetOutput(ioutil.Discard)
131
132
133
134
135
136
137
138
139
140
	config := benchConfig()
	b.Run("9 of  9 servers", func(b *testing.B) { benchmarkBallots(config, 1, 9, 9, b) })
	b.Run("9 of 10 servers", func(b *testing.B) { benchmarkBallots(config, 1, 10, 9, b) })
	b.Run("9 of 11 servers", func(b *testing.B) { benchmarkBallots(config, 1, 11, 9, b) })
	b.Run("9 of 12 servers", func(b *testing.B) { benchmarkBallots(config, 1, 12, 9, b) })
	b.Run("9 of 13 servers", func(b *testing.B) { benchmarkBallots(config, 1, 13, 9, b) })
	b.Run("9 of 14 servers", func(b *testing.B) { benchmarkBallots(config, 1, 14, 9, b) })
	b.Run("9 of 15 servers", func(b *testing.B) { benchmarkBallots(config, 1, 15, 9, b) })
	b.Run("9 of 16 servers", func(b *testing.B) { benchmarkBallots(config, 1, 16, 9, b) })
	b.Run("9 of 17 servers", func(b *testing.B) { benchmarkBallots(config, 1, 17, 9, b) })
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
141
142
143
}

//Variable t
Thomas Hoffmann's avatar
Thomas Hoffmann committed
144
func BenchmarkClientThreshold(b *testing.B) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
145
	log.SetOutput(ioutil.Discard)
146
147
148
149
150
151
152
153
154
155
	config := benchConfig()
	b.Run(" 9 of 17 servers", func(b *testing.B) { benchmarkBallots(config, 1, 17, 9, b) })
	b.Run("10 of 17 servers", func(b *testing.B) { benchmarkBallots(config, 1, 17, 10, b) })
	b.Run("11 of 17 servers", func(b *testing.B) { benchmarkBallots(config, 1, 17, 11, b) })
	b.Run("12 of 17 servers", func(b *testing.B) { benchmarkBallots(config, 1, 17, 12, b) })
	b.Run("13 of 17 servers", func(b *testing.B) { benchmarkBallots(config, 1, 17, 13, b) })
	b.Run("14 of 17 servers", func(b *testing.B) { benchmarkBallots(config, 1, 17, 14, b) })
	b.Run("15 of 17 servers", func(b *testing.B) { benchmarkBallots(config, 1, 17, 15, b) })
	b.Run("16 of 17 servers", func(b *testing.B) { benchmarkBallots(config, 1, 17, 16, b) })
	b.Run("17 of 17 servers", func(b *testing.B) { benchmarkBallots(config, 1, 17, 17, b) })
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
156
157
158
159
160
161
}

///////////////////////////////
///Server - Tally benchmarks///
///////////////////////////////

162
func benchmarkTally(params *common.Params, votecount, serverCount, reqServer int, b *testing.B) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
163
164
	//Setup
	b.StopTimer()
Thomas Hoffmann's avatar
Thomas Hoffmann committed
165
	box := NewBallotBox(big.NewInt(1), params)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
166
167
168

	id := UniqueID("1")
	for i := 0; i < votecount; i++ {
169
		_ = box.Put(CreateBenchBallots(reqServer, createXS(serverCount), big.NewInt(1), params)[id])
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
170
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
171
172
173
174
175
176
177

	ballotList := box.ballotList()
	ids := make([]uuid.UUID, 0, len(ballotList))
	for _, id := range ballotList {
		ids = append(ids, id)
	}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
178
179
180
	//Actual benchmark
	b.StartTimer()
	for i := 0; i < b.N; i++ {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
181
		box.Tally(ids)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
182
183
184
	}
}

Thomas Hoffmann's avatar
Thomas Hoffmann committed
185
func BenchmarkTallyVote(b *testing.B) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
186
	log.SetOutput(ioutil.Discard)
187
188
189
190
191
192
193
	config := benchConfig()
	b.Run("1      Votes", func(b *testing.B) { benchmarkTally(config, 1, 5, 3, b) })
	b.Run("10     Votes", func(b *testing.B) { benchmarkTally(config, 10, 5, 3, b) })
	b.Run("100    Votes", func(b *testing.B) { benchmarkTally(config, 100, 5, 3, b) })
	b.Run("1000   Votes", func(b *testing.B) { benchmarkTally(config, 1000, 5, 3, b) })
	b.Run("10000  Votes", func(b *testing.B) { benchmarkTally(config, 10000, 5, 3, b) })
	b.Run("100000 Votes", func(b *testing.B) { benchmarkTally(config, 100000, 5, 3, b) })
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
194
}
Thomas Hoffmann's avatar
Thomas Hoffmann committed
195
func BenchmarkTallyServer(b *testing.B) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
196
	log.SetOutput(ioutil.Discard)
197
198
199
200
201
202
203
204
205
206
	config := benchConfig()
	b.Run("9 of 9  servers", func(b *testing.B) { benchmarkTally(config, 10, 9, 9, b) })
	b.Run("9 of 10 servers", func(b *testing.B) { benchmarkTally(config, 10, 10, 9, b) })
	b.Run("9 of 11 servers", func(b *testing.B) { benchmarkTally(config, 10, 11, 9, b) })
	b.Run("9 of 12 servers", func(b *testing.B) { benchmarkTally(config, 10, 12, 9, b) })
	b.Run("9 of 13 servers", func(b *testing.B) { benchmarkTally(config, 10, 13, 9, b) })
	b.Run("9 of 14 servers", func(b *testing.B) { benchmarkTally(config, 10, 14, 9, b) })
	b.Run("9 of 15 servers", func(b *testing.B) { benchmarkTally(config, 10, 15, 9, b) })
	b.Run("9 of 16 servers", func(b *testing.B) { benchmarkTally(config, 10, 16, 9, b) })
	b.Run("9 of 17 servers", func(b *testing.B) { benchmarkTally(config, 10, 17, 9, b) })
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
207
}
Thomas Hoffmann's avatar
Thomas Hoffmann committed
208
func BenchmarkTallyThreshold(b *testing.B) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
209
	log.SetOutput(ioutil.Discard)
210
211
212
213
214
215
216
217
218
219
	config := benchConfig()
	b.Run("9  of 17 servers", func(b *testing.B) { benchmarkTally(config, 10, 17, 9, b) })
	b.Run("10 of 17 servers", func(b *testing.B) { benchmarkTally(config, 10, 17, 10, b) })
	b.Run("11 of 17 servers", func(b *testing.B) { benchmarkTally(config, 10, 17, 11, b) })
	b.Run("12 of 17 servers", func(b *testing.B) { benchmarkTally(config, 10, 17, 12, b) })
	b.Run("13 of 17 servers", func(b *testing.B) { benchmarkTally(config, 10, 17, 13, b) })
	b.Run("14 of 17 servers", func(b *testing.B) { benchmarkTally(config, 10, 17, 14, b) })
	b.Run("15 of 17 servers", func(b *testing.B) { benchmarkTally(config, 10, 17, 15, b) })
	b.Run("16 of 17 servers", func(b *testing.B) { benchmarkTally(config, 10, 17, 16, b) })
	b.Run("17 of 17 servers", func(b *testing.B) { benchmarkTally(config, 10, 17, 17, b) })
Thomas Hoffmann's avatar
Thomas Hoffmann committed
220
221
222
}
func BenchmarkTallyOptimal(b *testing.B) {
	log.SetOutput(ioutil.Discard)
223
224
225
226
227
228
229
230
	config := benchConfig()
	b.Run("5  of 9  servers", func(b *testing.B) { benchmarkTally(config, 10, 9, 5, b) })
	b.Run("6  of 11 servers", func(b *testing.B) { benchmarkTally(config, 10, 11, 6, b) })
	b.Run("7  of 13 servers", func(b *testing.B) { benchmarkTally(config, 10, 13, 7, b) })
	b.Run("8  of 15 servers", func(b *testing.B) { benchmarkTally(config, 10, 15, 8, b) })
	b.Run("9  of 17 servers", func(b *testing.B) { benchmarkTally(config, 10, 17, 9, b) })
	b.Run("10 of 19 servers", func(b *testing.B) { benchmarkTally(config, 10, 19, 10, b) })
	b.Run("11 of 21 servers", func(b *testing.B) { benchmarkTally(config, 10, 21, 11, b) })
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
231
232
}

Thomas Hoffmann's avatar
Thomas Hoffmann committed
233
/////////////////////////////////
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
234
///Server - Combine benchmarks///
Thomas Hoffmann's avatar
Thomas Hoffmann committed
235
/////////////////////////////////
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
236
237
var resultcombine *big.Int

238
func benchmarkCombine(params *common.Params, votecount, serverCount, reqServer int, b *testing.B) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
239
240
241
242
243
244
	log.SetOutput(ioutil.Discard)
	var r *big.Int
	//Setup
	b.StopTimer()
	votes := setupVote(votecount)
	xs := createXS(serverCount)
245
	ballots := createAllBallots(votes, serverCount, reqServer, xs, params)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
246
247
248
	ballotBoxes := make([]*BallotBox, serverCount)
	tallyBoxes := make([]*TallyBox, serverCount)
	for i := 0; i < serverCount; i++ {
249
		tallyBoxes[i] = NewTallyBox(params)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
250
		ballotBoxes[i] = NewBallotBox(xs[i], params)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
251
252
253
254
255
256
257
	}

	for i, ballotbox := range ballotBoxes {
		ID := UniqueID(strconv.Itoa(i + 1))
		for _, ballot := range ballots {
			_ = ballotbox.Put(ballot[ID])
		}
Anders Jensen Løvig's avatar
Merge    
Anders Jensen Løvig committed
258

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
259
260
261
262
263
		ballotList := ballotbox.ballotList()
		ids := make([]uuid.UUID, 0, len(ballotList))
		for _, id := range ballotList {
			ids = append(ids, id)
		}
Anders Jensen Løvig's avatar
Merge    
Anders Jensen Løvig committed
264

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
265
		tally := ballotbox.Tally(ids)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281

		for i := 0; i < reqServer; i++ {
			_ = tallyBoxes[0].Put(tally)
		}
	}

	//Actual benchmark
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		r = tallyBoxes[0].Combine()
	}
	resultcombine = r
}

func BenchmarkCombineVote(b *testing.B) {
	log.SetOutput(ioutil.Discard)
282
283
284
285
286
287
	config := benchConfig()
	b.Run("10     Votes", func(b *testing.B) { benchmarkCombine(config, 10, 5, 3, b) })
	b.Run("100    Votes", func(b *testing.B) { benchmarkCombine(config, 100, 5, 3, b) })
	b.Run("1000   Votes", func(b *testing.B) { benchmarkCombine(config, 1000, 5, 3, b) })
	b.Run("10000  Votes", func(b *testing.B) { benchmarkCombine(config, 10000, 5, 3, b) })
	b.Run("100000 Votes", func(b *testing.B) { benchmarkCombine(config, 100000, 5, 3, b) })
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
288
289
290
}
func BenchmarkCombineServer(b *testing.B) {
	log.SetOutput(ioutil.Discard)
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
	config := benchConfig()
	b.Run("15 of 15 servers", func(b *testing.B) { benchmarkCombine(config, 10, 15, 15, b) })
	b.Run("15 of 16 servers", func(b *testing.B) { benchmarkCombine(config, 10, 16, 15, b) })
	b.Run("15 of 17 servers", func(b *testing.B) { benchmarkCombine(config, 10, 17, 15, b) })
	b.Run("15 of 18 servers", func(b *testing.B) { benchmarkCombine(config, 10, 18, 15, b) })
	b.Run("15 of 19 servers", func(b *testing.B) { benchmarkCombine(config, 10, 19, 15, b) })
	b.Run("15 of 20 servers", func(b *testing.B) { benchmarkCombine(config, 10, 20, 15, b) })
	b.Run("15 of 21 servers", func(b *testing.B) { benchmarkCombine(config, 10, 21, 15, b) })
	b.Run("15 of 22 servers", func(b *testing.B) { benchmarkCombine(config, 10, 22, 15, b) })
	b.Run("15 of 23 servers", func(b *testing.B) { benchmarkCombine(config, 10, 23, 15, b) })
	b.Run("15 of 24 servers", func(b *testing.B) { benchmarkCombine(config, 10, 24, 15, b) })
	b.Run("15 of 25 servers", func(b *testing.B) { benchmarkCombine(config, 10, 25, 15, b) })
	b.Run("15 of 26 servers", func(b *testing.B) { benchmarkCombine(config, 10, 26, 15, b) })
	b.Run("15 of 27 servers", func(b *testing.B) { benchmarkCombine(config, 10, 27, 15, b) })
	b.Run("15 of 28 servers", func(b *testing.B) { benchmarkCombine(config, 10, 28, 15, b) })
	b.Run("15 of 29 servers", func(b *testing.B) { benchmarkCombine(config, 10, 29, 15, b) })
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
307
}
Thomas Hoffmann's avatar
Thomas Hoffmann committed
308
func BenchmarkCombineThreshold(b *testing.B) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
309
	log.SetOutput(ioutil.Discard)
310
311
312
313
314
315
316
317
318
319
	config := benchConfig()
	b.Run("9  of 17 servers", func(b *testing.B) { benchmarkCombine(config, 10, 17, 9, b) })
	b.Run("10 of 17 servers", func(b *testing.B) { benchmarkCombine(config, 10, 17, 10, b) })
	b.Run("11 of 17 servers", func(b *testing.B) { benchmarkCombine(config, 10, 17, 11, b) })
	b.Run("12 of 17 servers", func(b *testing.B) { benchmarkCombine(config, 10, 17, 12, b) })
	b.Run("13 of 17 servers", func(b *testing.B) { benchmarkCombine(config, 10, 17, 13, b) })
	b.Run("14 of 17 servers", func(b *testing.B) { benchmarkCombine(config, 10, 17, 14, b) })
	b.Run("15 of 17 servers", func(b *testing.B) { benchmarkCombine(config, 10, 17, 15, b) })
	b.Run("16 of 17 servers", func(b *testing.B) { benchmarkCombine(config, 10, 17, 16, b) })
	b.Run("17 of 17 servers", func(b *testing.B) { benchmarkCombine(config, 10, 17, 17, b) })
Thomas Hoffmann's avatar
Thomas Hoffmann committed
320

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
321
322
}

323
324
325
326
327
328
329
330
331
332
333
334
335
func BenchmarkCombineOptimal(b *testing.B) {
	log.SetOutput(ioutil.Discard)
	config := benchConfig()
	b.Run("5  of 9  servers", func(b *testing.B) { benchmarkCombine(config, 10, 9, 5, b) })
	b.Run("6  of 11 servers", func(b *testing.B) { benchmarkCombine(config, 10, 11, 6, b) })
	b.Run("7  of 13 servers", func(b *testing.B) { benchmarkCombine(config, 10, 13, 7, b) })
	b.Run("8  of 15 servers", func(b *testing.B) { benchmarkCombine(config, 10, 15, 8, b) })
	b.Run("9  of 17 servers", func(b *testing.B) { benchmarkCombine(config, 10, 17, 9, b) })
	b.Run("10 of 19 servers", func(b *testing.B) { benchmarkCombine(config, 10, 19, 10, b) })
	b.Run("11 of 21 servers", func(b *testing.B) { benchmarkCombine(config, 10, 21, 11, b) })

}

Thomas Hoffmann's avatar
Thomas Hoffmann committed
336
337
338
339
340
341
342
343
///////////////////////
///Config benchmarks///
///////////////////////

//Vælg største 16-bit, 32-bit, ..., 512-bit primtal

func BenchmarkClientConfig(b *testing.B) {
	log.SetOutput(ioutil.Discard)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
344
	config := createConfig(primes("16"))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
345
	b.Run(" 16-bit", func(b *testing.B) { benchmarkBallots(config, 10, 17, 9, b) })
Thomas Hoffmann's avatar
Thomas Hoffmann committed
346
	config = createConfig(primes("32"))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
347
	b.Run(" 32-bit", func(b *testing.B) { benchmarkBallots(config, 10, 17, 9, b) })
Thomas Hoffmann's avatar
Thomas Hoffmann committed
348
	config = createConfig(primes("64"))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
349
	b.Run(" 64-bit", func(b *testing.B) { benchmarkBallots(config, 10, 17, 9, b) })
Thomas Hoffmann's avatar
Thomas Hoffmann committed
350
	config = createConfig(primes("128"))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
351
	b.Run("128-bit", func(b *testing.B) { benchmarkBallots(config, 10, 17, 9, b) })
Thomas Hoffmann's avatar
Thomas Hoffmann committed
352
	config = createConfig(primes("256"))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
353
	b.Run("256-bit", func(b *testing.B) { benchmarkBallots(config, 10, 17, 9, b) })
Thomas Hoffmann's avatar
Thomas Hoffmann committed
354
	config = createConfig(primes("512"))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
355
356
357
358
359
	b.Run("512-bit", func(b *testing.B) { benchmarkBallots(config, 10, 17, 9, b) })
}

func BenchmarkTallyConfig(b *testing.B) {
	log.SetOutput(ioutil.Discard)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
360
	config := createConfig(primes("16"))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
361
	b.Run(" 16-bit", func(b *testing.B) { benchmarkTally(config, 10, 17, 9, b) })
Thomas Hoffmann's avatar
Thomas Hoffmann committed
362
363
364
365
366
367
368
369
370
371
	config = createConfig(primes("32"))
	b.Run(" 32-bit", func(b *testing.B) { benchmarkTally(config, 10, 17, 9, b) })
	config = createConfig(primes("64"))
	b.Run(" 64-bit", func(b *testing.B) { benchmarkTally(config, 10, 17, 9, b) })
	config = createConfig(primes("128"))
	b.Run("128-bit", func(b *testing.B) { benchmarkTally(config, 10, 17, 9, b) })
	config = createConfig(primes("256"))
	b.Run("256-bit", func(b *testing.B) { benchmarkTally(config, 10, 17, 9, b) })
	config = createConfig(primes("512"))
	b.Run("512-bit", func(b *testing.B) { benchmarkTally(config, 10, 17, 9, b) })
Thomas Hoffmann's avatar
Thomas Hoffmann committed
372
373
374
375
376
}

func BenchmarkCombineConfig(b *testing.B) {
	log.SetOutput(ioutil.Discard)
	var config *common.Params
Thomas Hoffmann's avatar
Thomas Hoffmann committed
377
	config = createConfig(primes("16"))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
378
	b.Run(" 16-bit", func(b *testing.B) { benchmarkCombine(config, 10, 17, 9, b) })
Thomas Hoffmann's avatar
Thomas Hoffmann committed
379
	config = createConfig(primes("32"))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
380
	b.Run(" 32-bit", func(b *testing.B) { benchmarkCombine(config, 10, 17, 9, b) })
Thomas Hoffmann's avatar
Thomas Hoffmann committed
381
	config = createConfig(primes("64"))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
382
	b.Run(" 64-bit", func(b *testing.B) { benchmarkCombine(config, 10, 17, 9, b) })
Thomas Hoffmann's avatar
Thomas Hoffmann committed
383
	config = createConfig(primes("128"))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
384
	b.Run("128-bit", func(b *testing.B) { benchmarkCombine(config, 10, 17, 9, b) })
Thomas Hoffmann's avatar
Thomas Hoffmann committed
385
	config = createConfig(primes("256"))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
386
	b.Run("256-bit", func(b *testing.B) { benchmarkCombine(config, 10, 17, 9, b) })
Thomas Hoffmann's avatar
Thomas Hoffmann committed
387
	config = createConfig(primes("512"))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
388
389
390
	b.Run("512-bit", func(b *testing.B) { benchmarkCombine(config, 10, 17, 9, b) })
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
391
392
// Actual tests
func setupElection() *Election {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
393
394
395
396
397
	// netConfig, err := network.LoadConfig()
	netConfig := &network.Config{}
	netConfig.RootPath = "../config/cert/root.crt"
	netConfig.CertPath = "../config/cert/server/s2.crt"
	netConfig.KeyPath = "../config/cert/server/s2.key"
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
398
	config := &Config{
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
399
		Server:          network.NewServer(netConfig),
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
400
401
402
403
404
405
		ServerID:        "1",
		Voters:          5,
		Servers:         3,
		RequiredServers: 2,
		Deadline:        time.Now().Add(1 * time.Minute),
		ResultCallback:  func(result *Result) {},
406
		CloseSleep:      0 * time.Second, // Disable close delay for testing
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
407
408
409
410
	}
	return NewElection(config)
}

411
412
func testElection(t *testing.T, expectedPhase Phase, fun func(*Election) <-chan time.Time) *Election {
	log.SetOutput(ioutil.Discard)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
413
414
415
	e := setupElection()

	select {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
416
417
	case <-time.After(2 * time.Second): // Give time for test
	case <-fun(e):
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
418
419
420
421
422
423
424
425
426
427
	}

	if e.Status.Phase != expectedPhase {
		t.Errorf("Unexpected phase\n  Expected: %s\n  Actual:   %s\n", expectedPhase, e.Status.Phase)
	}

	return e
}

func TestCloseDeadline(t *testing.T) {
428
429
	_ = testElection(t, PhaseClosed, func(e *Election) <-chan time.Time {
		e.Deadline = time.Now().Add(1000 * time.Millisecond)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
430
		_ = e.Start()
431
		return time.After(1200 * time.Millisecond)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
432
	})
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
433
434
435
}

func TestCloseAgreement(t *testing.T) {
436
	_ = testElection(t, PhaseTallying, func(e *Election) <-chan time.Time {
437
		e.Status.Phase = PhaseCollect
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
438

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
439
		ballots := CreateBallots(3, createXS(3), big.NewInt(1))
440
		e.handleBallot(UniqueID(strconv.Itoa(1)), ballots["1"])
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
441

442
		e.handleClosing("2")
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
443
444
445
446
447

		b := ballots["2"]
		_ = e.Agreement.AddList("2", map[string]uuid.UUID{
			b.Hash(): b.ID,
		})
448
		e.handleClosing("3")
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
449

450
		return time.After(100 * time.Millisecond)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
451
452
453
454
	})
}

func TestCloseAllVotes(t *testing.T) {
455
	_ = testElection(t, PhaseClosed, func(e *Election) <-chan time.Time {
456
		e.Status.Phase = PhaseCollect
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
457

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
458
459
		for i := 1; i <= e.Participants.Voters; i++ {
			ballots := CreateBallots(3, createXS(1), big.NewInt(1))
460
			e.handleBallot(UniqueID(strconv.Itoa(i)), ballots["1"])
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
461
		}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
462

463
		return time.After(100 * time.Millisecond)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
464
465
466
467
	})
}

func TestTallyDeadline(t *testing.T) {
468
	_ = testElection(t, PhaseTallying, func(e *Election) <-chan time.Time {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
469
470
471
		e.Deadline = time.Now().Add(1 * time.Second)
		_ = e.Start()

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
472
		ballots := CreateBallots(3, createXS(3), big.NewInt(1))
473
		e.handleBallot(UniqueID(strconv.Itoa(1)), ballots["1"])
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
474
475
476

		// By closing another server, we should go directly to tallying when
		// deadline is reached
477
		e.handleClosing("2")
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
478
479
480
481
		b := ballots["2"]
		_ = e.Agreement.AddList("2", map[string]uuid.UUID{
			b.Hash(): b.ID,
		})
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
482

483
		return time.After(1200 * time.Millisecond)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
484
485
486
487
	})
}

func TestNoVotesResult(t *testing.T) {
488
489
	_ = testElection(t, PhaseResult, func(e *Election) <-chan time.Time {
		done := make(chan time.Time, 1)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
490
491
492
493
		e.resultCallback = func(result *Result) {
			if result != nil {
				t.Error("Got a result wile none was expected")
			}
494
			done <- time.Now()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
495
		}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
496
497
		e.Deadline = time.Now().Add(1000 * time.Millisecond)
		_ = e.Start()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
498

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
499
		e.handleClosing("2")
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
500

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
501
502
		return done
	})
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
503
504
505
}

func TestTallyAfterClose(t *testing.T) {
506
	_ = testElection(t, PhaseTallying, func(e *Election) <-chan time.Time {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
507
508
509
		e.Deadline = time.Now().Add(1000 * time.Millisecond)
		_ = e.Start()

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
510
		ballots := CreateBallots(3, createXS(3), big.NewInt(1))
511
		e.handleBallot(UniqueID(strconv.Itoa(1)), ballots["1"])
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
512

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
513
514
515
516
		b := ballots["2"]
		_ = e.Agreement.AddList("2", map[string]uuid.UUID{
			b.Hash(): b.ID,
		})
517
		e.handleClosing("2")
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
518

519
		return time.After(1200 * time.Millisecond)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
520
521
522
523
524
	})
}

func TestOnlyVoteOnce(t *testing.T) {
	e := setupElection()
525
	e.Status.Phase = PhaseCollect
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
526
527
528

	ballots := CreateBallots(3, createXS(1), big.NewInt(1))

529
530
	e.handleBallot("4", ballots["1"])
	e.handleBallot("4", ballots["1"])
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548

	if e.ballotBox.Size() != 1 {
		t.Errorf("Expected ballot box size 1, got %d", e.ballotBox.Size())
	}
}

func TestHandleTally(t *testing.T) {
	ballots := CreateBallots(3, createXS(5), big.NewInt(1))
	tally1 := &Tally{
		ID:      uuid.New(),
		Share:   ballots["3"].Share,
		Commits: ballots["3"].Commits,
	}
	tally2 := &Tally{
		ID:      uuid.New(),
		Share:   ballots["4"].Share,
		Commits: ballots["4"].Commits,
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
549
550
551
	ballotList := map[string]uuid.UUID{
		ballots["1"].Hash(): ballots["1"].ID,
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
552

553
	_ = testElection(t, PhaseResult, func(e *Election) <-chan time.Time {
554
		e.Status.Phase = PhaseCollect
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
555

556
557
		e.handleBallot("4", ballots["1"])
		e.handleTally("2", tally1)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
558
559
560
		if e.tallyBox.Size() != 0 {
			t.Errorf("Expected 0 tally, got %d", e.tallyBox.Size())
		}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
561

562
		e.handleClosing("2")
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
563
		_ = e.Agreement.AddList("2", ballotList)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
564

565
566
		e.handleTally("2", tally1)
		e.handleTally("2", tally1)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
567
568
569
570
		if e.tallyBox.Size() != 1 {
			t.Errorf("Expected 1 tally, got %d", e.tallyBox.Size())
		}

571
		e.handleClosing("3")
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
572
		_ = e.Agreement.AddList("3", ballotList)
573
		e.handleTally("3", tally2)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
574

575
576
577
578
579
580
		return time.After(500 * time.Millisecond)
	})
}

func TestCloseTimeout(t *testing.T) {
	_ = testElection(t, PhaseClosed, func(e *Election) <-chan time.Time {
581
		e.Status.Phase = PhaseCollect
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
		e.Config.CloseSleep = 1 * time.Second

		// This should start a go-routine waiting for late ballots
		e.nextPhase(ReasonAgreement)

		if e.Status.Phase != PhaseCloseWait {
			t.Errorf("Not waiting for late ballots")
		}

		return time.After(1200 * time.Millisecond)
	})
}

func TestCloseTimeoutTally(t *testing.T) {
	_ = testElection(t, PhaseTallying, func(e *Election) <-chan time.Time {
Thomas Hoffmann's avatar
Thomas Hoffmann committed
597
		log.SetOutput(os.Stdout)
598
		e.Status.Phase = PhaseCollect
599
600
		e.Config.CloseSleep = 1 * time.Second

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
601
		ballots := CreateBallots(3, createXS(3), big.NewInt(1))
602
		e.handleBallot(UniqueID(strconv.Itoa(1)), ballots["1"])
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
603
604
605

		_ = e.Agreement.AddList("2", map[string]uuid.UUID{ballots["2"].Hash(): ballots["2"].ID})
		e.handleClosing("2")
606
607
608
609

		// This should start a go-routine waiting for late ballots
		e.nextPhase(ReasonAgreement)

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
610
611
612
		// This prevent us from wait for ballot lists
		e.Config.CloseSleep = 0

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
613
614
		_ = e.Agreement.AddList("3", map[string]uuid.UUID{ballots["3"].Hash(): ballots["3"].ID})
		e.handleClosing("3")
615
616
617
618
619
620
621
622
623
624
		if e.Status.Phase != PhaseCloseWait {
			t.Errorf("Not waiting for late ballots")
		}

		return time.After(1200 * time.Millisecond)
	})
}

func TestCloseTimeoutSkipTally(t *testing.T) {
	_ = testElection(t, PhaseResult, func(e *Election) <-chan time.Time {
625
		e.Status.Phase = PhaseCollect
626
627
628
629
630
631
632
		e.Config.CloseSleep = 1 * time.Second

		e.handleClosing("3")

		// This should start a go-routine waiting for late ballots
		e.nextPhase(ReasonAgreement)

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
633
634
		e.Config.CloseSleep = 0

635
636
637
638
639
640
641
642
643
644
645
		e.handleClosing("4")
		if e.Status.Phase != PhaseCloseWait {
			t.Errorf("Not waiting for late ballots")
		}

		return time.After(1200 * time.Millisecond)
	})
}

func TestCloseTimeoutLateBallot(t *testing.T) {
	_ = testElection(t, PhaseClosed, func(e *Election) <-chan time.Time {
646
		e.Status.Phase = PhaseCollect
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
		e.Config.CloseSleep = 500 * time.Millisecond

		for i := 1; i < e.Participants.Voters; i++ {
			ballots := CreateBallots(3, createXS(1), big.NewInt(1))
			e.handleBallot(UniqueID(strconv.Itoa(i)), ballots["1"])
		}

		// This should start a go-routine waiting for late ballots
		e.nextPhase(ReasonAgreement)

		// Send the last ballot
		ballots := CreateBallots(3, createXS(1), big.NewInt(1))
		e.handleBallot(UniqueID(strconv.Itoa(e.Participants.Voters)), ballots["1"])

		time.Sleep(10 * time.Millisecond)

		if e.Status.Phase == PhaseCloseWait {
			t.Errorf("Should skip waiting when all ballots received")
		}

		return time.After(1000 * time.Millisecond)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
668
669
	})
}