-
Notifications
You must be signed in to change notification settings - Fork 5
/
ss.go
289 lines (237 loc) · 8.29 KB
/
ss.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
package mpc
import (
"math/big"
mpc_core "github.com/hhcho/mpc-core"
"github.com/ldsec/lattigo/v2/utils"
// "go.dedis.ch/onet/v3/log"
"github.com/hhcho/sfgwas/crypto"
"github.com/ldsec/lattigo/v2/dckks"
"github.com/ldsec/lattigo/v2/ring"
//"github.com/hhcho/mpc-core"
"github.com/ldsec/lattigo/v2/ckks"
//"math/bits"
)
func (mpcObj *MPC) SSMultMat(a, b mpc_core.RMat) mpc_core.RMat {
ar, am := mpcObj.BeaverPartitionMat(a)
br, bm := mpcObj.BeaverPartitionMat(b)
ab := mpcObj.BeaverMultMat(ar, am, br, bm)
return mpcObj.BeaverReconstructMat(ab)
}
func (mpcObj *MPC) SSMultElemVecScalar(a mpc_core.RVec, b mpc_core.RElem) mpc_core.RVec {
ar, am := mpcObj.BeaverPartitionVec(a)
br, bm := mpcObj.BeaverPartition(b)
x := mpc_core.InitRVec(mpcObj.rtype.Zero(), len(a))
for i := range x {
x[i] = mpcObj.BeaverMult(ar[i], am[i], br, bm)
}
return mpcObj.BeaverReconstructVec(x)
}
func (mpcObj *MPC) SSSquareElemVec(a mpc_core.RVec) mpc_core.RVec {
ar, am := mpcObj.BeaverPartitionVec(a)
x := mpcObj.BeaverMultElemVec(ar, am, ar, am)
return mpcObj.BeaverReconstructVec(x)
}
func (mpcObj *MPC) SSMultElemVec(a, b mpc_core.RVec) mpc_core.RVec {
ar, am := mpcObj.BeaverPartitionVec(a)
br, bm := mpcObj.BeaverPartitionVec(b)
x := mpcObj.BeaverMultElemVec(ar, am, br, bm)
return mpcObj.BeaverReconstructVec(x)
}
func (mpcObj *MPC) SSMultElemMat(a, b mpc_core.RMat) mpc_core.RMat {
ar, am := mpcObj.BeaverPartitionMat(a)
br, bm := mpcObj.BeaverPartitionMat(b)
x := mpcObj.BeaverMultElemMat(ar, am, br, bm)
return mpcObj.BeaverReconstructMat(x)
}
// Row-major
func (mpcObj *MPC) SSToCMat(cryptoParams *crypto.CryptoParams, rm mpc_core.RMat) (cm crypto.CipherMatrix) {
if mpcObj.GetPid() == 0 {
cm = make(crypto.CipherMatrix, 1)
cm[0] = make(crypto.CipherVector, 1)
return
}
rtype := rm.Type().Zero()
if rtype.TypeID() != mpc_core.LElem256UniqueID && rtype.TypeID() != mpc_core.LElem128UniqueID {
panic("SSToCMat only supported for LElem128 or LElem256")
}
slots := cryptoParams.GetSlots()
numCtxRow := len(rm)
nElemCol := len(rm[0])
numCtxCol := 1 + ((nElemCol - 1) / slots)
// Pad RVec
//rvNew := mpc_core.InitRVec(rtype.Zero(), cryptoParams.GetSlots()*numCtxCol)
//for i := range rvNew {
// rvNew[i] = rv[i % len(rv)]
//}
//rv = rvNew
bound := rtype.Modulus()
bound.Quo(bound, big.NewInt(4*int64(mpcObj.GetNParty()-1)))
boundHalf := new(big.Int).Rsh(bound, 1)
mask := make(mpc_core.RMat, len(rm))
boundElem := rtype.FromBigInt(bound)
for i := range rm {
mask[i] = make(mpc_core.RVec, len(rm[0]))
for j := range rm[0] {
// log.LLvl1(time.Now().Format(time.RFC3339), "ss to cvec bound: ", bound)
tmp := ring.RandInt(bound)
mask[i][j] = rtype.FromBigInt(tmp)
if tmp.Cmp(boundHalf) >= 0 {
mask[i][j] = mask[i][j].Sub(boundElem)
}
}
}
rmMask := rm.Copy()
rmMask.Sub(mask)
rmMask = mpcObj.RevealSymMat(rmMask)
var share mpc_core.RMat
if mpcObj.GetPid() == mpcObj.GetHubPid() { // share = (x - r) + r_i
share = rmMask
share.Add(mask)
} else { // share = r_i
share = mask
}
pm := make(crypto.PlainMatrix, numCtxRow)
for i := range pm {
pm[i] = make(crypto.PlainVector, numCtxCol)
cryptoParams.WithEncoder(func(encoder ckks.Encoder) error {
start := 0
end := slots
for j := 0; j < numCtxCol; j++ {
if end > nElemCol {
end = nElemCol
}
pm[i][j] = encoder.EncodeRVecNew(share[i][start:end], uint64(end-start), mpcObj.GetFracBits())
start += slots
end += slots
}
return nil
})
}
cm = crypto.EncryptPlaintextMatrix(cryptoParams, pm)
return mpcObj.Network.AggregateCMat(cryptoParams, cm)
}
func (mpcObj *MPC) SSToCVec(cryptoParams *crypto.CryptoParams, rv mpc_core.RVec) (cv crypto.CipherVector) {
return mpcObj.SSToCMat(cryptoParams, mpc_core.RMat{rv})[0]
}
func (mpcObj *MPC) SStoCiphertext(cryptoParams *crypto.CryptoParams, rv mpc_core.RVec) *ckks.Ciphertext {
return mpcObj.SSToCVec(cryptoParams, rv)[0]
}
func (mpcObj *MPC) CMatToSS(cryptoParams *crypto.CryptoParams, rtype mpc_core.RElem, cm crypto.CipherMatrix, sourcePid, numCtxRow, numCtxCol, nElemRow int) (rm mpc_core.RMat) {
slots := cryptoParams.GetSlots()
fracBits := mpcObj.GetFracBits()
rm = mpc_core.InitRMat(rtype.Zero(), numCtxRow, nElemRow)
if mpcObj.GetPid() == 0 {
return
}
if sourcePid > 0 {
cm = mpcObj.Network.BroadcastCMat(cryptoParams, cm, sourcePid, numCtxRow, numCtxCol)
}
cm, levelStart := crypto.FlattenLevels(cryptoParams, cm)
ctMask := crypto.CopyEncryptedMatrix(cm)
paramN := cryptoParams.Params.N()
dckksContext := dckks.NewContext(cryptoParams.Params)
shareDecrypt := make([][]*ring.Poly, numCtxRow)
for i := range shareDecrypt {
shareDecrypt[i] = make([]*ring.Poly, numCtxCol)
for j := range shareDecrypt[i] {
shareDecrypt[i][j] = dckksContext.RingQ.NewPolyLvl(levelStart)
}
}
maskBigint := make([][][]*big.Int, numCtxRow)
for i := range maskBigint {
maskBigint[i] = make([][]*big.Int, numCtxCol)
for j := range maskBigint[i] {
maskBigint[i][j] = make([]*big.Int, paramN)
}
}
context := dckksContext.RingQ
prng, _ := utils.NewPRNG()
sampler := ring.NewGaussianSampler(prng)
bound := ring.NewUint(context.Modulus[0])
for i := 1; i < levelStart+1; i++ {
bound.Mul(bound, ring.NewUint(context.Modulus[i]))
}
bound.Quo(bound, ring.NewUint(2*uint64(mpcObj.GetNParty()-1)))
//fmt.Println("CMatToSS: Bound bit length ", bound.BitLen())
// Check if there is enough space in ct for masks
//if bound.Cmp(##) < 0 {
// panic(fmt.Sprintf("Attempted SS conversion on a ciphertext without enough levels -> %d", levelStart))
//}
boundHalf := new(big.Int).Rsh(bound, 1)
var sign int
for k := range cm {
for i := range cm[k] {
for j := range maskBigint[k][i] {
// TODO: check relation between coeff size and decoded output size
m := ring.RandInt(bound)
sign = m.Cmp(boundHalf)
if sign == 1 || sign == 0 {
m.Sub(m, bound)
}
maskBigint[k][i][j] = m
}
}
}
for k := range cm {
for i := range cm[k] {
// h0 = mask (at level min)
context.SetCoefficientsBigintLvl(levelStart, maskBigint[k][i], shareDecrypt[k][i])
context.NTTLvl(levelStart, shareDecrypt[k][i], shareDecrypt[k][i])
ctMask[k][i].SetValue([]*ring.Poly{shareDecrypt[k][i].CopyNew(), context.NewPoly()})
// h0 = sk*c1 + mask
context.MulCoeffsMontgomeryAndAddLvl(levelStart, cryptoParams.Sk.Value, cm[k][i].Value()[1], shareDecrypt[k][i])
// h0 = sk*c1 + mask + e0
tmp := sampler.ReadNew(dckksContext.RingQ, 3.19, 19)
dckksContext.RingQ.NTT(tmp, tmp)
context.AddLvl(levelStart, shareDecrypt[k][i], tmp, shareDecrypt[k][i])
}
}
// TODO: communicate in one batch
agg := make([][]*ring.Poly, len(shareDecrypt))
for i := range shareDecrypt {
agg[i] = mpcObj.Network.AggregateRefreshShareVec(shareDecrypt[i], levelStart)
}
pt := make(crypto.PlainMatrix, numCtxRow)
ptMask := make(crypto.PlainMatrix, numCtxRow)
for i := range cm {
pt[i] = make(crypto.PlainVector, numCtxCol)
ptMask[i] = make(crypto.PlainVector, numCtxCol)
for j := range cm[i] {
ctOut := cm[i][j].CopyNew().Ciphertext()
context.AddLvl(levelStart, ctOut.Value()[0], agg[i][j], ctOut.Value()[0])
pt[i][j] = ctOut.Plaintext()
ptMask[i][j] = ctMask[i][j].Plaintext()
}
}
rm = mpc_core.InitRMat(rtype.Zero(), numCtxRow, nElemRow)
cryptoParams.WithEncoder(func(encoder ckks.Encoder) error {
for i := range pt {
for j := range pt[i] {
var rvOut mpc_core.RVec
if mpcObj.GetPid() == mpcObj.GetHubPid() {
rvOut = encoder.DecodeRVec(rtype, pt[i][j], uint64(slots), fracBits)
} else {
rvOut = mpc_core.InitRVec(rtype.Zero(), slots)
}
rvMask := encoder.DecodeRVec(rtype, ptMask[i][j], uint64(slots), fracBits)
rvOut.Sub(rvMask)
start := j * slots
end := start + slots
if end > nElemRow {
end = nElemRow
}
for k := 0; k < end-start; k++ {
rm[i][start+k] = rvOut[k]
}
}
}
return nil
})
return
}
func (mpcObj *MPC) CVecToSS(cryptoParams *crypto.CryptoParams, rtype mpc_core.RElem, cv crypto.CipherVector, sourcePid, numCtx, nElem int) (rm mpc_core.RVec) {
return mpcObj.CMatToSS(cryptoParams, rtype, crypto.CipherMatrix{cv}, sourcePid, 1, numCtx, nElem)[0]
}
func (mpcObj *MPC) CiphertextToSS(cryptoParams *crypto.CryptoParams, rtype mpc_core.RElem, ct *ckks.Ciphertext, sourcePid, N int) (rv mpc_core.RVec) {
return mpcObj.CVecToSS(cryptoParams, rtype, crypto.CipherVector{ct}, sourcePid, 1, N)
}