/
round_1.go
139 lines (118 loc) · 3.17 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
/*
* 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 keygen
import (
cryptorand "crypto/rand"
"errors"
"fmt"
"io"
"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"
r255 "github.com/gtank/ristretto255"
)
// Start get sk pk
func (round *round1) Start() error {
if round.started {
fmt.Printf("============ round1 start error,already started============\n")
return errors.New("round already started")
}
round.number = 1
round.started = true
round.ResetOK()
//1.1-1.2 generate 32-bits privatekey', then bit calculation to privatekey
rand := cryptorand.Reader
var sk [32]byte
var pk [32]byte
var zkPk [64]byte
var err error
if round.temp.sigtype == smpc.SR25519 {
skScalar, err := ed_ristretto.NewRandomScalar()
if err != nil {
return err
}
PK := new(r255.Element).ScalarBaseMult(skScalar)
skScalar.Encode(sk[:0])
PK.Encode(pk[:0])
zkPk, err = ed_ristretto.Prove2(sk, pk)
if err != nil {
return err
}
}else{
var skTem [64]byte
if _, err = io.ReadFull(rand, skTem[:]); err != nil {
fmt.Println("Error: io.ReadFull(rand, sk)")
}
ed.ScReduce(&sk, &skTem)
var A ed.ExtendedGroupElement
ed.GeScalarMultBase(&A, &sk)
A.ToBytes(&pk)
zkPk, err = ed.Prove2(sk,pk)
if err != nil {
return err
}
}
CPk, DPk, err := ed.Commit(pk)
if err != nil {
return err
}
round.temp.sk = sk
round.temp.pk = pk
round.temp.DPk = DPk
round.temp.zkPk = zkPk
index, err := round.GetDNodeIDIndex(round.dnodeid)
if err != nil {
fmt.Printf("============round1 start,get dnode id index fail,uid = %v,err = %v ===========\n", round.dnodeid,err)
return err
}
kg := &KGRound1Message{
KGRoundMessage: new(KGRoundMessage),
CPk: CPk,
}
kg.SetFromID(round.dnodeid)
kg.SetFromIndex(index)
round.Save.Sk = sk
round.Save.Pk = pk
round.temp.kgRound1Messages[index] = kg
round.out <- kg
//fmt.Printf("============ round1 start success,index = %v ============\n",index)
return nil
}
// CanAccept is it legal to receive this message
func (round *round1) CanAccept(msg smpc.Message) bool {
if _, ok := msg.(*KGRound1Message); ok {
return msg.IsBroadcast()
}
return false
}
// Update is the message received and ready for the next round?
func (round *round1) Update() (bool, error) {
for j, msg := range round.temp.kgRound1Messages {
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 *round1) NextRound() smpc.Round {
round.started = false
return &round2{round}
}