forked from taurushq-io/multi-party-sig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
presign3.go
218 lines (187 loc) · 6.33 KB
/
presign3.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
package presign
import (
"errors"
"fmt"
"github.com/capsule-org/multi-party-sig/internal/elgamal"
"github.com/capsule-org/multi-party-sig/internal/round"
"github.com/capsule-org/multi-party-sig/pkg/hash"
"github.com/capsule-org/multi-party-sig/pkg/math/curve"
"github.com/capsule-org/multi-party-sig/pkg/paillier"
"github.com/capsule-org/multi-party-sig/pkg/party"
zkaffg "github.com/capsule-org/multi-party-sig/pkg/zk/affg"
zkaffp "github.com/capsule-org/multi-party-sig/pkg/zk/affp"
"github.com/cronokirby/safenum"
)
var _ round.Round = (*presign3)(nil)
type presign3 struct {
*presign2
// DeltaShareBeta[j] = βᵢⱼ
DeltaShareBeta map[party.ID]*safenum.Int
// ChiShareBeta[j] = β̂ᵢⱼ
ChiShareBeta map[party.ID]*safenum.Int
// DeltaCiphertext[j][k] = Dₖⱼ
DeltaCiphertext map[party.ID]map[party.ID]*paillier.Ciphertext
// ChiCiphertext[j][k] = D̂ₖⱼ
ChiCiphertext map[party.ID]map[party.ID]*paillier.Ciphertext
}
type broadcast3 struct {
round.NormalBroadcastContent
// DeltaCiphertext[k] = Dₖⱼ
DeltaCiphertext map[party.ID]*paillier.Ciphertext
// ChiCiphertext[k] = D̂ₖⱼ
ChiCiphertext map[party.ID]*paillier.Ciphertext
}
type message3 struct {
DeltaF *paillier.Ciphertext // DeltaF = Fᵢⱼ
DeltaProof *zkaffp.Proof
ChiF *paillier.Ciphertext // ChiF = F̂ᵢⱼ
ChiProof *zkaffg.Proof
}
// StoreBroadcastMessage implements round.BroadcastRound.
func (r *presign3) StoreBroadcastMessage(msg round.Message) error {
from := msg.From
body, ok := msg.Content.(*broadcast3)
if !ok || body == nil {
return round.ErrInvalidContent
}
if body.DeltaCiphertext == nil || body.ChiCiphertext == nil {
return round.ErrNilFields
}
for _, id := range r.PartyIDs() {
if id == from {
continue
}
DeltaCiphertext, ChiCiphertext := body.DeltaCiphertext[id], body.ChiCiphertext[id]
if !r.Paillier[id].ValidateCiphertexts(DeltaCiphertext, ChiCiphertext) {
return errors.New("received invalid ciphertext")
}
}
r.DeltaCiphertext[from] = body.DeltaCiphertext
r.ChiCiphertext[from] = body.ChiCiphertext
return nil
}
// VerifyMessage implements round.Round.
//
// - verify zkaffg, zkaffp.
func (r *presign3) VerifyMessage(msg round.Message) error {
from, to := msg.From, msg.To
body, ok := msg.Content.(*message3)
if !ok || body == nil {
return round.ErrInvalidContent
}
if !body.DeltaProof.Verify(r.Group(), r.HashForID(from), zkaffp.Public{
Kv: r.K[to],
Dv: r.DeltaCiphertext[from][to],
Fp: body.DeltaF,
Xp: r.G[from],
Prover: r.Paillier[from],
Verifier: r.Paillier[to],
Aux: r.Pedersen[to],
}) {
return errors.New("failed to validate affp proof for Delta MtA")
}
if !body.ChiProof.Verify(r.HashForID(from), zkaffg.Public{
Kv: r.K[to],
Dv: r.ChiCiphertext[from][to],
Fp: body.ChiF,
Xp: r.ECDSA[from],
Prover: r.Paillier[from],
Verifier: r.Paillier[to],
Aux: r.Pedersen[to],
}) {
return errors.New("failed to validate affg proof for Chi MtA")
}
return nil
}
// StoreMessage implements round.Round.
func (presign3) StoreMessage(round.Message) error { return nil }
// Finalize implements round.Round
//
// - Decrypt MtA shares,
// - save αᵢⱼ, α̂ᵢⱼ.
// - Γ = ∑ⱼ Γⱼ
// - Δᵢ = [kᵢ]Γ
// - δᵢ = γᵢ kᵢ + ∑ⱼ αᵢⱼ + βᵢⱼ
// - χᵢ = xᵢ kᵢ + ∑ⱼ α̂ᵢⱼ + β̂ᵢⱼ
// - Ẑⱼ, b̂ⱼ
func (r *presign3) Finalize(out chan<- *round.Message) (round.Session, error) {
// δᵢ = γᵢ kᵢ
KShareInt := curve.MakeInt(r.KShare)
DeltaShare := new(safenum.Int).Mul(r.GammaShare, KShareInt, -1)
DeltaSharesAlpha := make(map[party.ID]*safenum.Int, r.N())
ChiSharesAlpha := make(map[party.ID]*safenum.Int, r.N())
// χᵢ = xᵢ kᵢ
ChiShare := new(safenum.Int).Mul(curve.MakeInt(r.SecretECDSA), KShareInt, -1)
var (
culprits []party.ID
err error
)
for _, j := range r.OtherPartyIDs() {
// αᵢⱼ
DeltaSharesAlpha[j], err = r.SecretPaillier.Dec(r.DeltaCiphertext[j][r.SelfID()])
if err != nil {
culprits = append(culprits, j)
continue
}
// α̂ᵢⱼ
ChiSharesAlpha[j], err = r.SecretPaillier.Dec(r.ChiCiphertext[j][r.SelfID()])
if err != nil {
culprits = append(culprits, j)
continue
}
//δᵢ += αᵢⱼ + βᵢⱼ
DeltaShare.Add(DeltaShare, DeltaSharesAlpha[j], -1)
DeltaShare.Add(DeltaShare, r.DeltaShareBeta[j], -1)
// χᵢ += α̂ᵢⱼ + β̂ᵢⱼ
ChiShare.Add(ChiShare, ChiSharesAlpha[j], -1)
ChiShare.Add(ChiShare, r.ChiShareBeta[j], -1)
}
if culprits != nil {
return r.AbortRound(fmt.Errorf("failed to decrypt alpha shares for mta"), culprits...), nil
}
// ElGamalChi = Ẑⱼ = (b̂ⱼ⋅G, χᵢ+b̂ⱼ⋅Yᵢ)
// ElGamalChiNonce = b̂ⱼ
ElGamalChi, ElGamalChiNonce := elgamal.Encrypt(r.ElGamal[r.SelfID()], r.Group().NewScalar().SetNat(ChiShare.Mod(r.Group().Order())))
DeltaShareScalar := r.Group().NewScalar().SetNat(DeltaShare.Mod(r.Group().Order()))
msg := &broadcast4{
DeltaShare: DeltaShareScalar,
ElGamalChi: ElGamalChi,
}
if err = r.BroadcastMessage(out, msg); err != nil {
return r, err
}
return &presign4{
presign3: r,
DeltaShareAlpha: DeltaSharesAlpha,
ChiShareAlpha: ChiSharesAlpha,
ElGamalChiNonce: ElGamalChiNonce,
ElGamalChi: map[party.ID]*elgamal.Ciphertext{r.SelfID(): ElGamalChi},
DeltaShares: map[party.ID]curve.Scalar{r.SelfID(): DeltaShareScalar},
ChiShare: r.Group().NewScalar().SetNat(ChiShare.Mod(r.Group().Order())),
}, nil
}
// RoundNumber implements round.Content.
func (message3) RoundNumber() round.Number { return 3 }
// MessageContent implements round.Round.
func (r *presign3) MessageContent() round.Content {
return &message3{ChiProof: zkaffg.Empty(r.Group())}
}
// RoundNumber implements round.Content.
func (broadcast3) RoundNumber() round.Number { return 3 }
// BroadcastContent implements round.BroadcastRound.
func (presign3) BroadcastContent() round.BroadcastContent { return &broadcast3{} }
// Number implements round.Round.
func (presign3) Number() round.Number { return 3 }
// BroadcastData implements broadcast.Broadcaster.
func (m broadcast3) BroadcastData() []byte {
h := hash.New()
ids := make([]party.ID, 0, len(m.DeltaCiphertext))
for id := range m.DeltaCiphertext {
ids = append(ids, id)
}
sortedIDs := party.NewIDSlice(ids)
for _, id := range sortedIDs {
_ = h.WriteAny(id, m.DeltaCiphertext[id], m.ChiCiphertext[id])
}
return h.Sum()
}