/
round_4.go
312 lines (261 loc) · 7.64 KB
/
round_4.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
/*
* Copyright (C) 2020-2021 AnySwap Ltd. All rights reserved.
* Copyright (C) 2020-2021 haijun.cai@anyswap.exchange
*
* This library is free software; you can redistribute it and/or
* modify it under the Apache License, Version 2.0.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package signing
import (
"crypto/sha512"
"errors"
"fmt"
"github.com/anyswap/FastMulThreshold-DSA/smpc-lib/crypto/ed"
"github.com/anyswap/FastMulThreshold-DSA/smpc-lib/crypto/ed_ristretto"
"github.com/anyswap/FastMulThreshold-DSA/smpc-lib/smpc"
"github.com/gtank/merlin"
r255 "github.com/gtank/ristretto255"
)
// for ed25519 and sr25519, calculate the challenge k
func CalKValue(keyType string, message, pkFinal, RFinal []byte) ([32]byte, error){
var k [32]byte
if keyType == smpc.SR25519 {
transcript := merlin.NewTranscript("SigningContext")
transcript.AppendMessage([]byte(""), []byte("substrate"))
transcript.AppendMessage([]byte("sign-bytes"), message)
transcript.AppendMessage([]byte("proto-name"), []byte("Schnorr-sig"))
transcript.AppendMessage([]byte("sign:pk"), pkFinal)
transcript.AppendMessage([]byte("sign:R"), RFinal)
outK := transcript.ExtractBytes([]byte("sign:c"), 64)
var kHelper [64]byte
copy(kHelper[:], outK[:])
ed_ristretto.ScReduce(&k, &kHelper)
}else{
// 2.6 calculate k=H(FinalRBytes||pk||M)
var kDigest [64]byte
h := sha512.New()
_, err := h.Write(RFinal)
if err != nil {
return k, err
}
_, err = h.Write(pkFinal)
if err != nil {
return k, err
}
_, err = h.Write(message)
if err != nil {
return k, err
}
h.Sum(kDigest[:0])
ed.ScReduce(&k, &kDigest)
}
return k, nil
}
// Start verify CR DR xkR,calc lambda1 s
func (round *round4) Start() error {
if round.started {
fmt.Printf("============= ed sign,round4.start fail =======\n")
return errors.New("ed sign,round4 already started")
}
round.number = 4
round.started = true
round.ResetOK()
curIndex, err := round.GetDNodeIDIndex(round.kgid)
if err != nil {
return err
}
var FinalRBytes [32]byte
if round.temp.keyType == smpc.SR25519 {
FinalR := new(r255.Element)
for k := range round.idsign {
msg1, ok := round.temp.signRound1Messages[k].(*SignRound1Message)
if !ok {
return errors.New("get cr fail")
}
msg3, ok := round.temp.signRound3Messages[k].(*SignRound3Message)
if !ok {
return errors.New("get dr fail")
}
CRFlag := ed.Verify(msg1.CR, msg3.DR)
if !CRFlag {
fmt.Printf("error: commitment(r) not pass at user: %v\n", round.save.CurDNodeID)
return errors.New("smpc back-end internal error:commitment verification fail in ed sign")
}
msg2, ok := round.temp.signRound2Messages[k].(*SignRound2Message)
if !ok {
return errors.New("get zkr fail")
}
var temR [32]byte
copy(temR[:], msg3.DR[32:])
zkRFlag := ed_ristretto.VerifyZk2(msg2.ZkR, temR)
if !zkRFlag {
fmt.Printf("Error: ZeroKnowledge Proof (R) Not Pass at User: %v\n", round.save.CurDNodeID)
return errors.New("smpc back-end internal error:zeroknowledge verification fail in ed sign")
}
var temRBytes [32]byte
copy(temRBytes[:], msg3.DR[32:])
temR2 := new(r255.Element)
temR2.Decode(temRBytes[:])
if k == 0 {
FinalR = temR2
} else {
FinalR = new(r255.Element).Add(FinalR, temR2)
}
}
FinalR.Encode(FinalRBytes[:0])
}else {
var FinalR, temR2 ed.ExtendedGroupElement
for k := range round.idsign {
msg1, ok := round.temp.signRound1Messages[k].(*SignRound1Message)
if !ok {
return errors.New("get cr fail")
}
msg3, ok := round.temp.signRound3Messages[k].(*SignRound3Message)
if !ok {
return errors.New("get dr fail")
}
CRFlag := ed.Verify(msg1.CR, msg3.DR)
if !CRFlag {
fmt.Printf("error: commitment(r) not pass at user: %v\n", round.save.CurDNodeID)
return errors.New("smpc back-end internal error:commitment verification fail in ed sign")
}
msg2, ok := round.temp.signRound2Messages[k].(*SignRound2Message)
if !ok {
return errors.New("get zkr fail")
}
var temR [32]byte
copy(temR[:], msg3.DR[32:])
zkRFlag := ed.VerifyZk2(msg2.ZkR, temR)
if !zkRFlag {
fmt.Printf("Error: ZeroKnowledge Proof (R) Not Pass at User: %v\n", round.save.CurDNodeID)
return errors.New("smpc back-end internal error:zeroknowledge verification fail in ed sign")
}
var temRBytes [32]byte
copy(temRBytes[:], msg3.DR[32:])
temR2.FromBytes(&temRBytes)
if k == 0 {
FinalR = temR2
} else {
ed.GeAdd(&FinalR, &FinalR, &temR2)
}
}
FinalR.ToBytes(&FinalRBytes)
}
round.temp.FinalRBytes = FinalRBytes
k, err := CalKValue(round.temp.keyType, round.temp.message, round.temp.pkfinal[:], FinalRBytes[:])
if err != nil {
fmt.Printf("error in Round 4 CalKValue: %v\n", round.save.CurDNodeID)
return err
}
// 2.7 calculate lambda1
var lambda [32]byte
lambda[0] = 1
order := ed.GetBytesOrder()
var curByte [32]byte
copy(curByte[:], round.save.CurDNodeID.Bytes())
for kk, vv := range round.idsign {
if kk == curIndex {
continue
}
var indexByte [32]byte
copy(indexByte[:], vv.Bytes())
var time [32]byte
t := indexByte //round.temp.uids[oldindex]
tt := curByte //round.temp.uids[cur_oldindex]
if round.temp.keyType == smpc.SR25519 {
ed_ristretto.ScSub(&time, &t, &tt)
time = ed_ristretto.ScModInverse(time)
}else {
ed.ScSub(&time, &t, &tt)
time = ed.ScModInverse(time, order)
}
count := 0
for index:=0;index<32;index++ {
if time[index] == byte('0') {
count++
}
}
if count == 32 {
return errors.New("calc time mod inverse fail")
}
if round.temp.keyType == smpc.SR25519 {
ed_ristretto.ScMul(&time, &time, &t)
ed_ristretto.ScMul(&lambda, &lambda, &time)
}else {
ed.ScMul(&time, &time, &t)
ed.ScMul(&lambda, &lambda, &time)
}
}
var s [32]byte
var sBBytes [32]byte
if round.temp.keyType == smpc.SR25519 {
ed_ristretto.ScMul(&s, &lambda, &round.temp.tsk)
//stmp := hex.EncodeToString(s[:])
ed_ristretto.ScMul(&s, &s, &k)
ed_ristretto.ScAdd(&s, &s, &round.temp.r)
// 2.9 calculate sBBytes
var sScalar = new(r255.Scalar)
sScalar.Decode(s[:])
sB := new(r255.Element).ScalarBaseMult(sScalar)
sB.Encode(sBBytes[:0])
}else {
ed.ScMul(&s, &lambda, &round.temp.tsk)
//stmp := hex.EncodeToString(s[:])
ed.ScMul(&s, &s, &k)
ed.ScAdd(&s, &s, &round.temp.r)
// 2.9 calculate sBBytes
var sB ed.ExtendedGroupElement
ed.GeScalarMultBase(&sB, &s)
sB.ToBytes(&sBBytes)
}
// 2.10 commit(sBBytes)
CSB, DSB,err := ed.Commit(sBBytes)
if err != nil {
return err
}
round.temp.DSB = DSB
round.temp.sBBytes = sBBytes
round.temp.s = s
srm := &SignRound4Message{
SignRoundMessage: new(SignRoundMessage),
CSB: CSB,
}
srm.SetFromID(round.kgid)
srm.SetFromIndex(curIndex)
round.temp.signRound4Messages[curIndex] = srm
round.out <- srm
return nil
}
// CanAccept is it legal to receive this message
func (round *round4) CanAccept(msg smpc.Message) bool {
if _, ok := msg.(*SignRound4Message); ok {
return msg.IsBroadcast()
}
return false
}
// Update is the message received and ready for the next round?
func (round *round4) Update() (bool, error) {
for j, msg := range round.temp.signRound4Messages {
if round.ok[j] {
continue
}
if msg == nil || !round.CanAccept(msg) {
return false, nil
}
round.ok[j] = true
}
return true, nil
}
// NextRound enter next round
func (round *round4) NextRound() smpc.Round {
round.started = false
return &round5{round}
}