-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelegate.go
136 lines (111 loc) · 3.65 KB
/
delegate.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
package main
import (
"math/big"
"time"
"github.com/fatih/color"
)
// #############################################################################
func (p *Party) RunParallelDelegate(R *HashMapValues, pool *WorkerPool, fn WorkerFunc, ctx WorkerCtx) {
res := pool.Run(fn, ctx)
for i := 0; i < len(res); i++ {
data, ok := res[i].data.(DHOutput)
Assert(ok)
R.DHData[res[i].id] = HashMapValue{DHElement{}, data.S}
R.EncData[res[i].id] = data.Ct
}
}
// #############################################################################
func (d *Delegate) Init(id, n, nBits int, dPath, lPath string, ctx *EGContext) {
d.party.Init(id, n, nBits, dPath, lPath, ctx)
d.alpha = d.party.ctx.ecc.RandomScalar()
d.aesKey = RandomBytes(32)
d.party.ctx.ecc.EC_BaseMultiply(d.alpha, &d.L)
}
func (d *Delegate) DelegateStart(M *HashMapValues, sum bool) {
color.Set(d.party.log_color)
defer color.Unset()
defer Timer(time.Now(), d.party.log, "DelegateStart")
*M = NewHashMap(d.party.nBits)
unmodified := GetBitMap(M.Size())
var ctxSum BlindCtxSum
var ctxInt BlindCtxInt
if sum {
ctxSum = BlindCtxSum{ctx: &d.party.ctx, alpha: d.alpha, pk: d.party.agg_pk, sk: d.party.partial_sk, h2c: d.party.h2c}
} else {
ctxInt = BlindCtxInt{ctx: &d.party.ctx.ecc, alpha: d.alpha, sk: d.aesKey, h2c: d.party.h2c}
}
pool := NewWorkerPool(uint64(len(d.party.X)))
for w, v := range d.party.X {
idx := GetIndex(w, M.nBits)
if !unmodified.CheckedRemove(idx) {
continue
}
pool.InChan <- WorkerInput{data: BlindInput{w, v}, id: idx}
}
filled := uint64(M.Size()) - unmodified.GetCardinality()
pool.nJobs = filled
if sum {
d.party.RunParallelDelegate(M, pool, BlindEGWorker, ctxSum)
} else {
d.party.RunParallelDelegate(M, pool, BlindAESWorker, ctxInt)
}
d.party.log.Printf("Filled %d slots (%.3f x expected)\n", filled, float64(filled)/E_FullSlots(float64(M.Size()), float64(len(d.party.X))))
pool = NewWorkerPool(unmodified.GetCardinality())
k := unmodified.Iterator()
for k.HasNext() {
pool.InChan <- WorkerInput{id: k.Next(), data: RandomizeInput{}}
}
if sum {
d.party.RunParallelDelegate(M, pool, RandomizeEGDelegateWorker, ctxSum)
} else {
d.party.RunParallelDelegate(M, pool, RandomizeAESDelegateWorker, ctxInt)
}
d.party.log.Printf("Randomized %d unmodified slots\n", unmodified.GetCardinality())
}
func (d *Delegate) DelegateFinish(R *HashMapFinal, sum bool) (int, *EGCiphertext) {
color.Set(d.party.log_color)
defer Timer(time.Now(), d.party.log, "DelegateFinish")
sz := len(R.Q)
pool := NewWorkerPool(uint64(sz))
for i := 0; i < sz; i++ {
pool.InChan <- WorkerInput{id: uint64(i), data: UnblindInput{Q: R.Q[i], AES: R.AES[i]}}
}
var res []WorkerOutput
var ctSum EGCiphertext
count := 0
if sum {
res = pool.Run(UnblindEGWorker, BlindCtxSum{ctx: &d.party.ctx, alpha: d.alpha, pk: d.party.agg_pk, sk: d.party.partial_sk, h2c: d.party.h2c})
first := true
for i := 0; i < len(res); i++ {
data, _ := res[i].data.(*EGCiphertext)
if data != nil {
count += 1
if first {
ctSum = *data
first = false
} else {
d.party.ctx.EG_AddInplace(&ctSum, data)
}
}
}
} else {
res = pool.Run(UnblindAESWorker, BlindCtxInt{ctx: &d.party.ctx.ecc, alpha: d.alpha, sk: d.aesKey, h2c: d.party.h2c})
for i := 0; i < len(res); i++ {
data, _ := res[i].data.(string)
if data != "" {
count += 1
}
}
}
if sum {
return count, &ctSum
}
return count, nil
}
func (d *Delegate) JointDecryption(ctSum *EGCiphertext, partials [][]DHElement) big.Int {
color.Set(d.party.log_color)
defer Timer(time.Now(), d.party.log, "JointDecryption")
var result big.Int
d.party.ctx.EGMP_AggDecrypt(partials, &result, ctSum)
return result
}