-
Notifications
You must be signed in to change notification settings - Fork 172
/
keygen_rot.go
237 lines (192 loc) · 6.77 KB
/
keygen_rot.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
package drlwe
import (
"errors"
"github.com/tuneinsight/lattigo/v4/ring"
"github.com/tuneinsight/lattigo/v4/rlwe"
"github.com/tuneinsight/lattigo/v4/rlwe/ringqp"
"github.com/tuneinsight/lattigo/v4/utils"
)
// RTGShare is represent a Party's share in the RTG protocol.
type RTGShare struct {
Value [][]ringqp.Poly
}
// RTGCRP is a type for common reference polynomials in the RTG protocol.
type RTGCRP [][]ringqp.Poly
// RTGProtocol is the structure storing the parameters for the collective rotation-keys generation.
type RTGProtocol struct {
params rlwe.Parameters
buff [2]ringqp.Poly
gaussianSamplerQ *ring.GaussianSampler
}
// ShallowCopy creates a shallow copy of RTGProtocol in which all the read-only data-structures are
// shared with the receiver and the temporary buffers are reallocated. The receiver and the returned
// RTGProtocol can be used concurrently.
func (rtg *RTGProtocol) ShallowCopy() *RTGProtocol {
prng, err := utils.NewPRNG()
if err != nil {
panic(err)
}
params := rtg.params
return &RTGProtocol{
params: rtg.params,
buff: [2]ringqp.Poly{params.RingQP().NewPoly(), params.RingQP().NewPoly()},
gaussianSamplerQ: ring.NewGaussianSampler(prng, params.RingQ(), params.Sigma(), int(6*params.Sigma())),
}
}
// NewRTGProtocol creates a RTGProtocol instance.
func NewRTGProtocol(params rlwe.Parameters) *RTGProtocol {
rtg := new(RTGProtocol)
rtg.params = params
prng, err := utils.NewPRNG()
if err != nil {
panic(err)
}
rtg.gaussianSamplerQ = ring.NewGaussianSampler(prng, params.RingQ(), params.Sigma(), int(6*params.Sigma()))
rtg.buff = [2]ringqp.Poly{params.RingQP().NewPoly(), params.RingQP().NewPoly()}
return rtg
}
// AllocateShare allocates a party's share in the RTG protocol.
func (rtg *RTGProtocol) AllocateShare() (rtgShare *RTGShare) {
rtgShare = new(RTGShare)
params := rtg.params
decompRNS := rtg.params.DecompRNS(params.QCount()-1, params.PCount()-1)
decompPw2 := rtg.params.DecompPw2(params.QCount()-1, params.PCount()-1)
rtgShare.Value = make([][]ringqp.Poly, decompRNS)
for i := 0; i < decompRNS; i++ {
rtgShare.Value[i] = make([]ringqp.Poly, decompPw2)
for j := 0; j < decompPw2; j++ {
rtgShare.Value[i][j] = rtg.params.RingQP().NewPoly()
}
}
return
}
// SampleCRP samples a common random polynomial to be used in the RTG protocol from the provided
// common reference string.
func (rtg *RTGProtocol) SampleCRP(crs CRS) RTGCRP {
params := rtg.params
decompRNS := rtg.params.DecompRNS(params.QCount()-1, params.PCount()-1)
decompPw2 := rtg.params.DecompPw2(params.QCount()-1, params.PCount()-1)
crp := make([][]ringqp.Poly, decompRNS)
us := ringqp.NewUniformSampler(crs, *params.RingQP())
for i := 0; i < decompRNS; i++ {
crp[i] = make([]ringqp.Poly, decompPw2)
for j := 0; j < decompPw2; j++ {
crp[i][j] = rtg.params.RingQP().NewPoly()
us.Read(crp[i][j])
}
}
return RTGCRP(crp)
}
// GenShare generates a party's share in the RTG protocol.
func (rtg *RTGProtocol) GenShare(sk *rlwe.SecretKey, galEl uint64, crp RTGCRP, shareOut *RTGShare) {
ringQ := rtg.params.RingQ()
ringQP := rtg.params.RingQP()
levelQ := sk.LevelQ()
levelP := sk.LevelP()
hasModulusP := levelP > -1
galElInv := ring.ModExp(galEl, ringQ.NthRoot-1, ringQ.NthRoot)
ringQ.PermuteNTT(sk.Value.Q, galElInv, rtg.buff[1].Q)
if hasModulusP {
ringQ.PermuteNTT(sk.Value.P, galElInv, rtg.buff[1].P)
ringQ.MulScalarBigint(sk.Value.Q, ringQP.RingP.ModulusAtLevel[levelP], rtg.buff[0].Q)
} else {
levelP = 0
ring.CopyLvl(levelQ, sk.Value.Q, rtg.buff[0].Q)
}
RNSDecomp := len(shareOut.Value)
BITDecomp := len(shareOut.Value[0])
var index int
for j := 0; j < BITDecomp; j++ {
for i := 0; i < RNSDecomp; i++ {
// e
rtg.gaussianSamplerQ.Read(shareOut.Value[i][j].Q)
if hasModulusP {
ringQP.ExtendBasisSmallNormAndCenter(shareOut.Value[i][j].Q, levelP, nil, shareOut.Value[i][j].P)
}
ringQP.NTTLazyLvl(levelQ, levelP, shareOut.Value[i][j], shareOut.Value[i][j])
ringQP.MFormLvl(levelQ, levelP, shareOut.Value[i][j], shareOut.Value[i][j])
// a is the CRP
// e + sk_in * (qiBarre*qiStar) * 2^w
// (qiBarre*qiStar)%qi = 1, else 0
for k := 0; k < levelP+1; k++ {
index = i*(levelP+1) + k
// Handles the case where nb pj does not divides nb qi
if index >= levelQ+1 {
break
}
qi := ringQ.Modulus[index]
tmp0 := rtg.buff[0].Q.Coeffs[index]
tmp1 := shareOut.Value[i][j].Q.Coeffs[index]
for w := 0; w < ringQ.N; w++ {
tmp1[w] = ring.CRed(tmp1[w]+tmp0[w], qi)
}
}
// sk_in * (qiBarre*qiStar) * 2^w - a*sk + e
ringQP.MulCoeffsMontgomeryAndSubLvl(levelQ, levelP, crp[i][j], rtg.buff[1], shareOut.Value[i][j])
}
ringQ.MulScalar(rtg.buff[0].Q, 1<<rtg.params.Pow2Base(), rtg.buff[0].Q)
}
}
// AggregateShares aggregates two share in the Rotation Key Generation protocol.
func (rtg *RTGProtocol) AggregateShares(share1, share2, shareOut *RTGShare) {
ringQP := rtg.params.RingQP()
levelQ := share1.Value[0][0].Q.Level()
var levelP int
if share1.Value[0][0].P != nil {
levelP = share1.Value[0][0].P.Level()
}
RNSDecomp := len(shareOut.Value)
BITDecomp := len(shareOut.Value[0])
for i := 0; i < RNSDecomp; i++ {
for j := 0; j < BITDecomp; j++ {
ringQP.AddLvl(levelQ, levelP, share1.Value[i][j], share2.Value[i][j], shareOut.Value[i][j])
}
}
}
// GenRotationKey finalizes the RTG protocol and populates the input RotationKey with the computed collective SwitchingKey.
func (rtg *RTGProtocol) GenRotationKey(share *RTGShare, crp RTGCRP, rotKey *rlwe.SwitchingKey) {
RNSDecomp := len(share.Value)
BITDecomp := len(share.Value[0])
for i := 0; i < RNSDecomp; i++ {
for j := 0; j < BITDecomp; j++ {
rotKey.Value[i][j].Value[0].Copy(share.Value[i][j])
rotKey.Value[i][j].Value[1].Copy(crp[i][j])
}
}
}
// MarshalBinary encode the target element on a slice of byte.
func (share *RTGShare) MarshalBinary() (data []byte, err error) {
data = make([]byte, 2+share.Value[0][0].MarshalBinarySize64()*len(share.Value)*len(share.Value[0]))
if len(share.Value) > 0xFF {
return []byte{}, errors.New("RKGShare: uint8 overflow on length")
}
data[0] = uint8(len(share.Value))
data[1] = uint8(len(share.Value[0]))
ptr := 2
var inc int
for i := range share.Value {
for _, el := range share.Value[i] {
if inc, err = el.Encode64(data[ptr:]); err != nil {
return []byte{}, err
}
ptr += inc
}
}
return data, nil
}
// UnmarshalBinary decodes a slice of bytes on the target element.
func (share *RTGShare) UnmarshalBinary(data []byte) (err error) {
share.Value = make([][]ringqp.Poly, data[0])
ptr := 2
var inc int
for i := range share.Value {
share.Value[i] = make([]ringqp.Poly, data[1])
for j := range share.Value[i] {
if inc, err = share.Value[i][j].Decode64(data[ptr:]); err != nil {
return err
}
ptr += inc
}
}
return nil
}