-
Notifications
You must be signed in to change notification settings - Fork 75
/
round_1.go
350 lines (314 loc) · 8.62 KB
/
round_1.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
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
// Copyright © 2022 AMIS Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package signSix
import (
"errors"
"math/big"
"github.com/getamis/alice/crypto/birkhoffinterpolation"
pt "github.com/getamis/alice/crypto/ecpointgrouplaw"
"github.com/getamis/alice/crypto/homo/paillier"
"github.com/getamis/alice/crypto/tss"
"github.com/getamis/alice/crypto/tss/ecdsa/cggmp"
"github.com/getamis/alice/crypto/utils"
paillierzkproof "github.com/getamis/alice/crypto/zkproof/paillier"
"github.com/getamis/alice/types"
"github.com/getamis/sirius/log"
)
const (
BYTELENGTHKAPPA = 32
)
var (
ErrNotEnoughRanks = errors.New("not enough ranks")
parameter = paillierzkproof.NewS256()
)
type round1Data struct {
beta *big.Int
s *big.Int
r *big.Int
D *big.Int
F *big.Int
countSigma *big.Int
betahat *big.Int
shat *big.Int
rhat *big.Int
Dhat []byte
Fhat *big.Int
gammaCiphertext *big.Int
kCiphertext *big.Int
Z1 *pt.ECPoint
Z2 *pt.ECPoint
round2Msg types.Message
}
type round1Handler struct {
bkMulShare *big.Int
pubKey *pt.ECPoint
paillierKey *paillier.Paillier
bkpartialPubKey *pt.ECPoint
msg []byte
k *big.Int
rho *big.Int
kCiphertext *big.Int
gamma *big.Int
nu *big.Int
ySecret *big.Int
gammaCiphertext *big.Int
Z1 *pt.ECPoint
Z2 *pt.ECPoint
b *big.Int
Gamma *pt.ECPoint
msgGamma *pt.EcPointMessage
bks map[string]*birkhoffinterpolation.BkParameter
bkShare *big.Int
peerManager types.PeerManager
peerNum uint32
peers map[string]*peer
own *peer
}
func newRound1Handler(threshold uint32, ssid []byte, share *big.Int, ySecret *big.Int, pubKey *pt.ECPoint, partialPubKey, allY map[string]*pt.ECPoint, bks map[string]*birkhoffinterpolation.BkParameter, paillierKey *paillier.Paillier, ped map[string]*paillierzkproof.PederssenOpenParameter, msg []byte, peerManager types.PeerManager) (*round1Handler, error) {
curve := pubKey.GetCurve()
curveN := curve.Params().N
// Establish BK Coefficient:
selfId := peerManager.SelfID()
ownBK := bks[peerManager.SelfID()]
bkss := birkhoffinterpolation.BkParameters{
ownBK,
}
ids := []string{
selfId,
}
for id, bk := range bks {
if id == selfId {
continue
}
bkss = append(bkss, bk)
ids = append(ids, id)
}
err := bkss.CheckValid(threshold, curveN)
if err != nil {
return nil, err
}
// Build peers
bkcoefficient, err := bkss.ComputeBkCoefficient(threshold, curveN)
if err != nil {
return nil, err
}
peers := make(map[string]*peer, peerManager.NumPeers())
for i, id := range ids {
if id == selfId {
continue
}
peers[id] = newPeer(id, ssid, bks[id], bkcoefficient[i], ped[id], partialPubKey[id], allY[id])
}
bkShare := new(big.Int).Mul(share, bkcoefficient[0])
bkShare.Mod(bkShare, curveN)
// Build and send round1 message
// k, γ in F_q
k, err := utils.RandomInt(curveN)
if err != nil {
return nil, err
}
b, err := utils.RandomInt(curveN)
if err != nil {
return nil, err
}
// Gi = enc_i(γ, mu), and Ki = enc(k, ρ)
kCiphertext, rho, err := paillierKey.EncryptWithOutputSalt(k)
if err != nil {
return nil, err
}
// Set own data
own := newPeer(selfId, ssid, ownBK, bkcoefficient[0], ped[selfId], partialPubKey[selfId], allY[selfId])
Z1 := pt.ScalarBaseMult(curve, b)
Z2 := pt.ScalarBaseMult(curve, k)
Z2, err = Z2.Add(own.allY.ScalarMult(b))
if err != nil {
return nil, err
}
// γ in F_q
gamma, err := utils.RandomInt(curveN)
if err != nil {
return nil, err
}
gammaCiphertext, nu, err := paillierKey.EncryptWithOutputSalt(gamma)
if err != nil {
return nil, err
}
Gamma := pt.ScalarBaseMult(curve, gamma)
msgGamma, err := Gamma.ToEcPointMessage()
if err != nil {
return nil, err
}
return &round1Handler{
bkMulShare: bkShare,
pubKey: pubKey,
paillierKey: paillierKey,
bkpartialPubKey: own.partialPubKey.ScalarMult(own.bkcoefficient),
msg: msg,
k: k,
rho: rho,
gamma: gamma,
Gamma: Gamma,
msgGamma: msgGamma,
Z1: Z1,
Z2: Z2,
b: b,
nu: nu,
ySecret: ySecret,
gammaCiphertext: gammaCiphertext,
kCiphertext: kCiphertext,
bks: bks,
bkShare: bkShare,
peerManager: peerManager,
peerNum: peerManager.NumPeers(),
peers: peers,
own: own,
}, nil
}
func (p *round1Handler) MessageType() types.MessageType {
return types.MessageType(Type_Round1)
}
func (p *round1Handler) GetRequiredMessageCount() uint32 {
return p.peerNum
}
func (p *round1Handler) IsHandled(logger log.Logger, id string) bool {
peer, ok := p.peers[id]
if !ok {
logger.Warn("Peer not found")
return false
}
return peer.Messages[p.MessageType()] != nil
}
func (p *round1Handler) HandleMessage(logger log.Logger, message types.Message) error {
msg := getMessage(message)
id := msg.GetId()
peer, ok := p.peers[id]
if !ok {
logger.Warn("Peer not found")
return tss.ErrPeerNotFound
}
round1 := msg.GetRound1()
ownPed := p.own.para
peerPed := peer.para
n := peerPed.GetN()
kciphertext := new(big.Int).SetBytes(round1.KCiphertext)
A := peer.allY
B, err := round1.Z1.ToPoint()
if err != nil {
return err
}
X, err := round1.Z2.ToPoint()
if err != nil {
return err
}
// verify Proof_enc
err = round1.Psi.Verify(parameter, peer.ssidWithBk, kciphertext, n, A, B, X, ownPed)
if err != nil {
return err
}
return peer.AddMessage(msg)
}
func (p *round1Handler) Finalize(logger log.Logger) (types.Handler, error) {
for _, peer := range p.peers {
msg := getMessage(peer.GetMessage(types.MessageType(Type_Round1)))
round1 := msg.GetRound1()
beta, r, s, D, F, phiProof, err := cggmp.MtaWithProofAff_p(p.own.ssidWithBk, peer.para, p.paillierKey, round1.KCiphertext, p.gamma, p.nu, p.gammaCiphertext)
if err != nil {
return nil, err
}
// psihat share proof: M(prove,Πaff-g,(sid,i),(Iε,Jε,Dˆj,i,Kj,Fˆj,i,Xi);(xi,βˆi,j,sˆi,j,rˆi,j)).
betahat, countSigma, rhat, shat, Dhat, Fhat, psihatProof, err := cggmp.MtaWithProofAff_g(p.own.ssidWithBk, peer.para, p.paillierKey, round1.KCiphertext, p.bkMulShare, p.bkpartialPubKey)
if err != nil {
return nil, err
}
Z1, err := round1.Z1.ToPoint()
if err != nil {
return nil, err
}
Z2, err := round1.Z2.ToPoint()
if err != nil {
return nil, err
}
peer.round1Data = &round1Data{
beta: beta,
r: r,
s: s,
D: D,
F: F,
gammaCiphertext: new(big.Int).SetBytes(round1.GammaCiphertext),
kCiphertext: new(big.Int).SetBytes(round1.KCiphertext),
countSigma: countSigma,
betahat: betahat,
rhat: rhat,
shat: shat,
Dhat: Dhat,
Fhat: Fhat,
Z1: Z1,
Z2: Z2,
// logstar proof for the secret gamma, mu: M(prove,Πlog,(sid,i),(Iε,Gi,Γi,g);(γi,νi)).
round2Msg: &Message{
Id: p.own.Id,
Type: Type_Round2,
Body: &Message_Round2{
Round2: &Round2Msg{
D: D.Bytes(),
F: F.Bytes(),
Dhat: Dhat,
Fhat: Fhat.Bytes(),
Psi: phiProof,
Psihat: psihatProof,
},
},
},
}
}
for id, peer := range p.peers {
p.peerManager.MustSend(id, peer.round1Data.round2Msg)
}
return newRound2Handler(p)
}
func getMessage(messsage types.Message) *Message {
return messsage.(*Message)
}
func (p *round1Handler) sendRound1Messages() error {
n := p.paillierKey.GetN()
msgZ1, err := p.Z1.ToEcPointMessage()
if err != nil {
return err
}
msgZ2, err := p.Z2.ToEcPointMessage()
if err != nil {
return err
}
for _, peer := range p.peers {
// Compute proof psi_{j,i}^0
psi, err := paillierzkproof.NewEncryptRangeWithELMessage(parameter, p.own.ssidWithBk, p.k, p.rho, p.ySecret, p.b, p.kCiphertext, n, p.own.allY, p.Z1, p.Z2, peer.para)
if err != nil {
return err
}
p.peerManager.MustSend(peer.Id, &Message{
Id: p.own.Id,
Type: Type_Round1,
Body: &Message_Round1{
Round1: &Round1Msg{
KCiphertext: p.kCiphertext.Bytes(),
GammaCiphertext: p.gammaCiphertext.Bytes(),
Z1: msgZ1,
Z2: msgZ2,
Psi: psi,
},
},
})
}
return nil
}