-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
544 lines (475 loc) · 16.5 KB
/
client.go
File metadata and controls
544 lines (475 loc) · 16.5 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"math/big"
"math/rand"
"net/http"
"time"
"github.com/fentec-project/bn256"
"github.com/fentec-project/gofe/data"
"github.com/fentec-project/gofe/innerprod/fullysec"
"github.com/pkg/errors"
)
// RegistrationInfo - registration info
type RegistrationInfo struct {
ClientSequenceID int `json:"clientSequenceId"`
PoolLabel string `json:"poolLabel"`
RegistrationExpiry string `json:"registrationExpiry"`
Status string `json:"status"`
SlotLabels []string `json:"slotLabels"`
InnerVector []int `json:"innerVector"`
}
// APIResponseRegistrationInfo - API response for RegistrationInfo
type APIResponseRegistrationInfo struct {
Data RegistrationInfo `json:"data"`
ErrorDetails *map[string]interface{} `json:"errorDetails"`
ErrorMessage *map[string]interface{} `json:"errorMessage"`
Status string `json:"status"`
}
// PoolDataPayload - pool data payload
type PoolDataPayload struct {
Status string `json:"status"`
CreationTime *string `json:"creationTime"`
RegistrationTime *string `json:"registrationTime"`
FinalizationTime *string `json:"finalizationTime"`
CalculationTime *string `json:"calculationTime"`
PoolLabel string `json:"poolLabel"`
PoolExpiry string `json:"poolExpiry"`
PublicKeys *[]string `json:"publicKeys"`
CypherTexts *[][]string `json:"cypherTexts"`
DecryptionKeys *[][]string `json:"decryptionKeys"`
Histogram *[]int `json:"histogram"`
SlotLabels []string `json:"slotLabels"`
InnerVector []int `json:"innerVector"`
}
// APIResponsePoolDataPayload - API response for PoolDataPayload
type APIResponsePoolDataPayload struct {
Data PoolDataPayload `json:"data"`
ErrorDetails *string `json:"errorDetails"`
ErrorMessage *string `json:"errorMessage"`
Status string `json:"status"`
}
// APIResponsePoolDataPayloadArray - API response for PoolDataPayload
type APIResponsePoolDataPayloadArray struct {
Data []PoolDataPayload `json:"data"`
ErrorDetails *string `json:"errorDetails"`
ErrorMessage *string `json:"errorMessage"`
Status string `json:"status"`
}
// PublicKeyShareRequest - public key share
type PublicKeyShareRequest struct {
ClientSequenceID int `json:"clientSequenceId"`
PoolLabel string `json:"poolLabel"`
RegistrationExpiry string `json:"registrationExpiry"`
KeyShare string `json:"keyShare"`
}
// CypherAndDKRequest - cyphertext vector and partial decryption key
type CypherAndDKRequest struct {
ClientSequenceID int `json:"clientSequenceId"`
PoolLabel string `json:"poolLabel"`
CypherText []string `json:"cypherText"`
DecryptionKeyShare []string `json:"decryptionKeyShare"`
}
// HistogramPayload - histogram payload
type HistogramPayload struct {
Secret string `json:"secret"`
PoolLabel string `json:"poolLabel"`
Histogram []int `json:"histogram"`
}
func register(host string, regInfo *RegistrationInfo) error {
APIUrl := host + "/ace/register"
var apiResponse APIResponseRegistrationInfo
response, err := http.Post(APIUrl, "application/json", nil)
if err != nil {
return fmt.Errorf("The HTTP request failed with error %s", err)
}
apiResponseData, _ := ioutil.ReadAll(response.Body)
// fmt.Printf("REGISTER RESPONSE: %v\n", string(apiResponseData))
err = json.Unmarshal(apiResponseData, &apiResponse)
if err != nil {
return fmt.Errorf("Cannot unmarshal APIResponse: %s", err)
}
*regInfo = apiResponse.Data
return nil
}
func status(host string, regInfo RegistrationInfo, poolDataPayload *PoolDataPayload) error {
APIUrl := host + "/ace/status/" + regInfo.PoolLabel
var apiResponse APIResponsePoolDataPayload
response, err := http.Get(APIUrl)
if err != nil {
return fmt.Errorf("The HTTP request failed with error %s", err)
}
apiResponseData, _ := ioutil.ReadAll(response.Body)
// fmt.Printf("STATUS RESPONSE: %s\n", string(apiResponseData))
err = json.Unmarshal(apiResponseData, &apiResponse)
if err != nil {
return fmt.Errorf("Cannot unmarshal APIResponse: %s", err)
}
*poolDataPayload = apiResponse.Data
return nil
}
func postPublicKeyShare(host string, publicKeyShareReq PublicKeyShareRequest, poolDataPayload *PoolDataPayload) error {
APIUrl := host + "/ace/public-key-share"
var apiResponse APIResponsePoolDataPayload
jsonValue, _ := json.Marshal(publicKeyShareReq)
// fmt.Printf("PUBLIC: %s\n", jsonValue)
response, err := http.Post(APIUrl, "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
return fmt.Errorf("The HTTP request failed with error %s", err)
}
apiResponseData, _ := ioutil.ReadAll(response.Body)
// fmt.Printf("POST PUBLIC KEY SHARE RESPONSE: %s\n", string(apiResponseData))
err = json.Unmarshal(apiResponseData, &apiResponse)
if err != nil {
return fmt.Errorf("Cannot unmarshal APIResponse: %s", err)
}
*poolDataPayload = apiResponse.Data
return nil
}
func postCypherAndDecryptionKey(host string, cypherAndDKReq CypherAndDKRequest, poolDataPayload *PoolDataPayload) error {
APIUrl := host + "/ace/cyphertext-and-dk"
var apiResponse APIResponsePoolDataPayload
jsonValue, _ := json.Marshal(cypherAndDKReq)
response, err := http.Post(APIUrl, "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
return fmt.Errorf("The HTTP request failed with error %s", err)
}
apiResponseData, _ := ioutil.ReadAll(response.Body)
// fmt.Printf("POST CYPHER AND DECRYPT KEY REQUEST: %s\n", string(jsonValue))
// fmt.Printf("POST CYPHER AND DECRYPT KEY RESPONSE: %s\n", string(apiResponseData))
err = json.Unmarshal(apiResponseData, &apiResponse)
if err != nil {
return fmt.Errorf("Cannot unmarshal APIResponse: %s", err)
}
*poolDataPayload = apiResponse.Data
return nil
}
func postHistogram(host string, histogramPayload HistogramPayload, poolDataPayload *PoolDataPayload) error {
// register
APIUrl := host + "/ace/histogram"
var apiResponse APIResponsePoolDataPayload
jsonValue, _ := json.Marshal(histogramPayload)
response, err := http.Post(APIUrl, "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
return fmt.Errorf("The HTTP request failed with error %s", err)
}
apiResponseData, _ := ioutil.ReadAll(response.Body)
// fmt.Printf("HIST REQUEST: %s\n", string(jsonValue))
// fmt.Printf("HIST RESPONSE: %s\n", string(apiResponseData))
err = json.Unmarshal(apiResponseData, &apiResponse)
if err != nil {
return fmt.Errorf("Cannot unmarshal APIResponse: %s", err)
}
*poolDataPayload = apiResponse.Data
return nil
}
func listFinalized(host string, poolDataPayloadArray *[]PoolDataPayload) error {
// register
APIUrl := host + "/ace/pools?status=FINALIZED"
// var registrationInfo RegistrationInfo
var apiResponse APIResponsePoolDataPayloadArray
response, err := http.Get(APIUrl)
if err != nil {
return fmt.Errorf("The HTTP request failed with error %s", err)
}
apiResponseData, _ := ioutil.ReadAll(response.Body)
// fmt.Printf("POLL RESPONSE: %s\n", string(apiResponseData))
err = json.Unmarshal(apiResponseData, &apiResponse)
if err != nil {
return fmt.Errorf("Cannot unmarshal APIResponse: %s", err)
}
*poolDataPayloadArray = apiResponse.Data
return nil
}
// Client - Participating client wrapper
type Client struct {
Encryptor *fullysec.DMCFEClient
}
func (c *Client) init(sequence int) error {
encryptor, err := fullysec.NewDMCFEClient(sequence)
if err != nil {
return errors.Wrap(err, "could not instantiate fullysec")
}
c.Encryptor = encryptor
return nil
}
func (c *Client) getPublicKeyEncoding() string {
return c.g1Base64Encoding(c.Encryptor.ClientPubKey)
}
func (c *Client) setShare(encodedPublicKeys []string) error {
var err error
publicKeys := make([]*bn256.G1, len(encodedPublicKeys))
for i := 0; i < len(encodedPublicKeys); i++ {
publicKeys[i], err = g1Base64Decoding(encodedPublicKeys[i])
if err != nil {
return err
}
}
err = c.Encryptor.SetShare(publicKeys)
return err
}
func (c *Client) encryptData(data []int, labels []string) ([]string, error) {
ciphers := make([]string, len(data))
for i := 0; i < len(data); i++ {
g1, err := c.Encryptor.Encrypt(big.NewInt(int64(data[i])), labels[i])
if err != nil {
return nil, err
}
ciphers[i] = c.g1Base64Encoding(g1)
}
return ciphers, nil
}
func (c *Client) deriveKeyShare(vector []int) ([]string, error) {
keyShare, err := c.Encryptor.DeriveKeyShare(toVector(vector))
if err != nil {
return nil, err
}
result := make([]string, len(keyShare))
for i := 0; i < len(keyShare); i++ {
result[i] = g2Base64Encoding(keyShare[i])
}
return result, nil
}
func (c *Client) g1Base64Encoding(g1 *bn256.G1) string {
return base64.StdEncoding.EncodeToString(g1.Marshal())
}
func decryptHistogram(ciphers [][]string, keyShares [][]string, labels []string, vector []int) ([]int, error) {
var err error
y := toVector(vector)
b := big.NewInt(int64(len(vector)))
keys := make([]data.VectorG2, len(keyShares))
for i := 0; i < len(keyShares); i++ {
keys[i] = make(data.VectorG2, len(keyShares[i]))
for j := 0; j < len(keyShares[i]); j++ {
keys[i][j], err = g2Base64Decoding(keyShares[i][j])
if err != nil {
return nil, err
}
}
}
histogram := make([]int, len(labels))
for i := 0; i < len(labels); i++ {
ciphersi := make([]*bn256.G1, len(vector))
for j := 0; j < len(vector); j++ {
ciphersi[j], err = g1Base64Decoding(ciphers[j][i])
if err != nil {
return nil, err
}
}
value, err := fullysec.DMCFEDecrypt(ciphersi, keys, y, labels[i], b)
if err != nil {
return nil, err
}
histogram[i] = int(value.Int64())
}
return histogram, nil
}
func g2Base64Encoding(g2 *bn256.G2) string {
return base64.StdEncoding.EncodeToString(g2.Marshal())
}
func g1Base64Decoding(b64 string) (*bn256.G1, error) {
bytes, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
return nil, err
}
g1 := new(bn256.G1)
_, err = g1.Unmarshal(bytes)
if err != nil {
return nil, err
}
return g1, nil
}
func g2Base64Decoding(b64 string) (*bn256.G2, error) {
bytes, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
return nil, err
}
g2 := new(bn256.G2)
_, err = g2.Unmarshal(bytes)
if err != nil {
return nil, err
}
return g2, nil
}
func toVector(vector []int) data.Vector {
components := make([]*big.Int, len(vector))
for i := 0; i < len(vector); i++ {
components[i] = big.NewInt(int64(vector[i]))
}
return data.NewVector(components)
}
// Exposure simulation
func simulateExposure(size int) []int {
exposure := make([]int, size)
index := rand.Intn(size)
exposure[index] = 1
return exposure
}
func simulateClient(host string) {
// fmt.Println("Starting the client...")
// Initial delay
startDelay := time.Duration(rand.Intn(500))
time.Sleep(startDelay * time.Millisecond)
var regInfo RegistrationInfo
var statusData PoolDataPayload
var publicKeyShareReq PublicKeyShareRequest
var cypherAndDKReq CypherAndDKRequest
// Registration
err := register(host, ®Info)
if err != nil {
fmt.Printf("Error while registring: %v. Terminating client.", err)
return
}
// Client initialization
client := new(Client)
err = client.init(regInfo.ClientSequenceID)
if err != nil {
fmt.Printf("Error in client initialization: %v. Terminating client.\n", err)
return
}
// fmt.Println("REGISTRED")
// fmt.Println(regInfo)
publicKeyShareReq.ClientSequenceID = regInfo.ClientSequenceID
publicKeyShareReq.PoolLabel = regInfo.PoolLabel
publicKeyShareReq.RegistrationExpiry = regInfo.RegistrationExpiry
// Set public key share
publicKeyShareReq.KeyShare = client.getPublicKeyEncoding() // fmt.Sprintf("<KEY-SHARE-%d>", regInfo.ClientSequenceID)
// Sending public key share to central server
err = postPublicKeyShare(host, publicKeyShareReq, &statusData)
if err != nil {
fmt.Println("Error while posting public key share status. Terminating client.")
return
}
// fmt.Println("PUBLIC KEY SHARE SUBMITTED")
// fmt.Println(statusData.Status)
// Waiting for collection of all key shares on server
cnt := 0
const pollingIterationsLimit = 100
for statusData.Status != "ENCRYPTION" {
cnt = cnt + 1
if cnt > pollingIterationsLimit {
fmt.Printf("Too many polling iterrations (%d). Terminating client.", cnt)
return
}
err = status(host, regInfo, &statusData)
if err != nil {
fmt.Println("Error while checking status. Terminating client.")
return
}
// fmt.Println(statusData)
if statusData.Status == "EXPIRED" {
fmt.Println("Pool expired. Terminating client.")
return
}
delay := time.Duration(500 + rand.Intn(2000))
// fmt.Println("POLL DELAY: %f", delay)
time.Sleep(delay * time.Millisecond)
}
client.setShare(*statusData.PublicKeys)
// fmt.Println("PUBLIC KEY OBTAINED")
// fmt.Println(statusData.Status)
cypherAndDKReq.ClientSequenceID = regInfo.ClientSequenceID
cypherAndDKReq.PoolLabel = regInfo.PoolLabel
// Simulate exposure
exposure := simulateExposure(len(statusData.SlotLabels))
fmt.Printf("CLIENT %d: Simulating exposure %v\n", regInfo.ClientSequenceID, exposure)
cypherAndDKReq.CypherText, err = client.encryptData(exposure, statusData.SlotLabels)
// []int{1, 0, 0, 0, 0},
// []string{"CY", "PH", "ER", "TE", "XT"}
if err != nil {
fmt.Printf("CLIENT %d: Error encrypting client data: %v. Terminating client", regInfo.ClientSequenceID, err)
return
}
cypherAndDKReq.DecryptionKeyShare, err = client.deriveKeyShare(statusData.InnerVector)
if err != nil {
fmt.Printf("CLIENT %d: Error deriving client key share: %v. Terminating client", regInfo.ClientSequenceID, err)
return
}
err = postCypherAndDecryptionKey(host, cypherAndDKReq, &statusData)
if err != nil {
fmt.Printf("CLIENT %d: Error while posting cyphertexts and decrypt keys status: %v. Terminating client", regInfo.ClientSequenceID, err)
return
}
// fmt.Println("ENCRYPTION SUBMITTED")
// fmt.Println(statusData.Status)
fmt.Printf("CLIENT %d: DONE\n", regInfo.ClientSequenceID)
}
func simulateAnalyticsServer(host string, secret string) error {
var poolDataPayloadArray []PoolDataPayload
var statusData PoolDataPayload
var histogramPayload HistogramPayload
histogramPayload.Secret = secret
fmt.Println("Analytics server simulation started")
for {
fmt.Println("POLLING ...")
err := listFinalized(host, &poolDataPayloadArray)
if err != nil {
fmt.Printf("Error while polling status: %v. Terminating client.", err)
return err
}
// fmt.Printf("FINALIZED:\n")
for i := 0; i < len(poolDataPayloadArray); i++ {
histogramPayload.PoolLabel = poolDataPayloadArray[i].PoolLabel
// Decrypt histogram
histogram, err := decryptHistogram(
*poolDataPayloadArray[i].CypherTexts,
*poolDataPayloadArray[i].DecryptionKeys,
poolDataPayloadArray[i].SlotLabels,
poolDataPayloadArray[i].InnerVector)
if err != nil {
fmt.Printf("%v x %v\n", len(*poolDataPayloadArray[i].CypherTexts), len((*poolDataPayloadArray[i].CypherTexts)[0]))
fmt.Printf("%v\n", len(*poolDataPayloadArray[i].DecryptionKeys))
// fmt.Println(*poolDataPayloadArray[i].DecryptionKeys)
fmt.Println(poolDataPayloadArray[i].SlotLabels)
fmt.Println(poolDataPayloadArray[i].InnerVector)
fmt.Printf("Error decrypting histogram: %v. Terminating client.\n", err)
return err
}
if err != nil {
fmt.Printf("Histogram calculation failed: %v\n", err)
return err
}
// fmt.Printf("%d: \n%v\n%v\n", i, *cypherTextPtr, *decryptionKeysPtr)
// fmt.Printf("Histogram: %v\n", histogram)
histogramPayload.Histogram = histogram
err = postHistogram(host, histogramPayload, &statusData)
if err != nil {
fmt.Printf("Error while posting histogram status: %v. Terminating analytics client.\n", err)
return err
}
fmt.Printf("HISTOGRAM: %v\n", histogram)
// fmt.Printf("HISTOGRAM SUBMITTED: %v\n", histogramPayload)
// fmt.Println(statusData.Status)
}
delay := time.Duration(2000)
time.Sleep(delay * time.Millisecond)
}
}
func main() {
rand.Seed(time.Now().UnixNano())
modePtr := flag.String("mode", "CLIENT", "Client operation mode: CLIENT or ANALYTICS")
hostPtr := flag.String("host", "http://localhost:9500", "URL of central server")
secretPtr := flag.String("secret", "", "Secret for posting histograms")
flag.Parse()
mode := *modePtr
host := *hostPtr
secret := *secretPtr
// fmt.Printf("Running in %s mode. Connected to %s\n", mode, host)
if mode == "CLIENT" {
simulateClient(host)
return
}
if mode == "ANALYTICS" {
err := simulateAnalyticsServer(host, secret)
if err != nil {
fmt.Printf("Error in analytics server: %s. Terminating client.", err)
}
return
}
fmt.Printf("ERROR: Wrong mode: %s\n", mode)
}