/
puzzlepromise.go
298 lines (257 loc) · 8.67 KB
/
puzzlepromise.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
package tumbler
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/decred/tumblebit/contract"
"github.com/decred/tumblebit/puzzle"
)
// EscrowRequest asks tumbler to escrow the specified amount redeemable by
// the owner of the public key in case it obtains a correct puzzle solution.
type EscrowRequest struct {
Address string
PublicKey string
Amount int64
}
// EscrowOffer presents the client with a signed but not published escrow
// transaction set up for a particular epoch and with a specified locktime.
type EscrowOffer struct {
Epoch int32
LockTime int32
Address string
PublicKey string
EscrowScript []byte
EscrowTx []byte
}
// SetupEscrow creates and signs a transaction that escrows tumbler's funds
// for an EpochDuration. The transaction is a P2SH that requires signatures
// from both client and tumbler to transfer escrowed funds to the client.
func (s *Session) SetupEscrow(ctx context.Context, er *EscrowRequest) (*EscrowOffer, error) {
if ok, err := s.ready(StateEscrowComplete); !ok {
return nil, err
}
epoch, err := s.tb.getCurrentEpoch()
if err != nil {
return nil, err
}
s.contract, err = contract.New(s.tb.ChainParams(), er.Amount,
epoch+s.tb.epochDuration)
if err != nil {
return nil, err
}
if err = s.contract.SetAddress(contract.ReceiverAddress, er.Address,
er.PublicKey); err != nil {
return nil, err
}
if err = s.tb.wallet.CreateEscrow(ctx, s.contract); err != nil {
return nil, err
}
s.epoch = epoch
s.state = StateEscrowComplete
log.Debugf("Escrow setup for %s", s.String())
return &EscrowOffer{
Epoch: epoch,
LockTime: epoch + s.tb.epochDuration,
Address: s.contract.SenderAddrStr,
PublicKey: s.contract.SenderAddr.EncodeAddress(),
EscrowScript: s.contract.EscrowScript,
EscrowTx: s.contract.EscrowBytes,
}, nil
}
// SignChallengeHashes is a helper function that asks wallet to sign
// challenge hash values. It's not part of GetPuzzlePromises to make
// testing feasible.
func (s *Session) SignChallengeHashes(ctx context.Context, hashes [][]byte) ([][]byte, []byte, error) {
signatures, pubKey, err := s.tb.wallet.SignHashes(ctx, s.contract, hashes)
if err != nil {
return nil, nil, err
}
return signatures, pubKey, nil
}
// SignatureChallenges requests signature promises for specified transaction
// hashes, some of which are dummy as indicated by the FakeSetHash as
// opposed to legitimate ones indicated by the RealSetHash. Hash values
// act as a proof that client has included both in the mix.
type SignatureChallenges struct {
FakeSetHash []byte
RealSetHash []byte
TransactionHashes [][]byte
Signatures [][]byte
PublicKey []byte
}
// SignaturePromises contains signature promises for transactions requested
// in SignatureChallenges as well as computational puzzles that unlock
// appropriate promises once solved.
type SignaturePromises struct {
PublicKey []byte
PuzzleKey []byte
Puzzles [][]byte
Promises [][]byte
}
// GetPuzzlePromises obtains cryptographically concealed signature promises.
//
// This marks the starting point for the Puzzle-Promise fairness test where
// TumbleBit server attempts to convince the client that it will correctly
// sign the Cash-out transaction when presented by the client without
// revealing any secret information about the process.
func (s *Session) GetPuzzlePromises(ctx context.Context, cp *SignatureChallenges) (*SignaturePromises, error) {
if ok, err := s.ready(StatePuzzlesPromised); !ok {
return nil, err
}
pk, err := s.tb.getPuzzleKey(s.epoch)
if err != nil {
return nil, err
}
key, err := puzzle.MarshalPubKey(&pk)
if err != nil {
return nil, err
}
puzzles := make([][]byte, len(cp.Signatures))
promises := make([][]byte, len(cp.Signatures))
secrets := make([][]byte, len(cp.Signatures))
for i := range cp.Signatures {
puzzles[i], promises[i], secrets[i], err =
puzzle.NewPuzzlePromise(&pk, cp.Signatures[i])
if err != nil {
return nil, err
}
}
s.secrets = secrets
s.realSetHash = cp.RealSetHash
s.fakeSetHash = cp.FakeSetHash
s.txHashes = cp.TransactionHashes
s.state = StatePuzzlesPromised
log.Debugf("Puzzle promises offered to %s", s.String())
return &SignaturePromises{
PublicKey: cp.PublicKey,
PuzzleKey: key,
Puzzles: puzzles,
Promises: promises,
}, nil
}
// TransactionDisclosure reveals secret data used to build dummy transactions
// along with indexes of legitimate and dummy transactions specified in the
// TransactionHashes vector in the proposal.
type TransactionDisclosure struct {
FakeTxList []byte
RealTxList []byte
RandomPads [][]byte
Salt []byte
}
// TransactionSecrets provides the required proof that tumbler has signed all
// provided transactions indiscriminately by revealing secret values used
// to construct promises for dummy transactions.
type TransactionSecrets struct {
Secrets [][]byte
Quotients [][]byte
}
// ValidatePuzzles obtains the proof that server is fair and indiscriminate.
//
// A client reveals dummy transactions that were mixed in into the pool of
// potential Cash-out transactions signed by the tumbler. Iff they verify
// as dummy transactions, the tumbler discloses secret values used to
// create associated promises showing its fairness.
//
// Tumbler also creates a proof that it possesses secrets needed to unlock
// remaining puzzles by returning quotients of their secrets that can be
// verified by the client with puzzle.VerifyQuotients.
func (s *Session) ValidatePuzzles(ctx context.Context, cd *TransactionDisclosure) (*TransactionSecrets, error) {
if ok, err := s.ready(StatePuzzlesValidated); !ok {
return nil, err
}
fakeTxList, err := puzzle.DecodeIndexList(cd.FakeTxList)
if err != nil {
return nil, fmt.Errorf("failed to decode fake tx index list: %v",
err)
}
realTxList, err := puzzle.DecodeIndexList(cd.RealTxList)
if err != nil {
return nil, fmt.Errorf("failed to decode real tx index list: %v",
err)
}
if (len(fakeTxList) > len(s.txHashes)) ||
(len(realTxList) > len(s.txHashes)) ||
(len(cd.RandomPads) > len(s.txHashes)) ||
(len(fakeTxList) > len(cd.RandomPads)) ||
(len(cd.Salt) != 32) {
return nil, errors.New("bad input values")
}
pk, err := s.tb.getPuzzleKey(s.epoch)
if err != nil {
return nil, fmt.Errorf("failed to obtain a puzzle key for "+
"epoch %d: %v", s.epoch, err)
}
// Verify hash of the fake set
fakeSetHash, err := puzzle.HashIndexList(cd.Salt, fakeTxList)
if err != nil {
return nil, fmt.Errorf("failed to hash the fake tx list: %v", err)
}
if !bytes.Equal(fakeSetHash, s.fakeSetHash) {
return nil, errors.New("fake set didn't verify")
}
// Verify structure of fake transactions
for i, idx := range fakeTxList {
if idx > len(s.txHashes) {
return nil, errors.New("bad tx reference")
}
if len(cd.RandomPads[i]) != 32 {
return nil, errors.New("bad input values")
}
fkh := puzzle.FakeTxFormat(cd.RandomPads[i])
if !bytes.Equal(fkh, s.txHashes[idx]) {
return nil, errors.New("fake tx didn't verify")
}
}
// Verify hash of the real set
realSetHash, err := puzzle.HashIndexList(cd.Salt, realTxList)
if err != nil {
return nil, fmt.Errorf("failed to hash the real tx list: %v", err)
}
if !bytes.Equal(realSetHash, s.realSetHash) {
return nil, errors.New("real set didn't verify")
}
// Reveal secrets for the fake set
fakeSecrets := make([][]byte, len(fakeTxList))
for i, idx := range fakeTxList {
fakeSecrets[i] = s.secrets[idx]
}
// Prepare quotients to verify puzzles for the real set
realSecrets := make([][]byte, len(realTxList))
for i, idx := range realTxList {
if idx > len(s.secrets) {
return nil, errors.New("bad tx reference")
}
realSecrets[i] = s.secrets[idx]
}
quotients, err := puzzle.Quotients(pk.PublicKey(), realSecrets)
if err != nil {
return nil, fmt.Errorf("failed to generate quotients: %v", err)
}
// Garbage-collect cached puzzles, tx hashes, ets.
s.puzzles = nil
s.txHashes = nil
s.realSetHash = nil
s.fakeSetHash = nil
s.state = StatePuzzlesValidated
log.Debugf("Promise proof offered to %s", s.String())
return &TransactionSecrets{
Secrets: fakeSecrets,
Quotients: quotients,
}, nil
}
// FinalizeEscrow publishes the escrow transaction onto the blockchain.
func (s *Session) FinalizeEscrow(ctx context.Context) ([]byte, error) {
if ok, err := s.ready(StateEscrowPublished); !ok {
return nil, err
}
if err := s.tb.wallet.PublishEscrow(ctx, s.contract); err != nil {
return nil, fmt.Errorf("failed to publish escrow tx :%v", err)
}
s.state = StateEscrowPublished
log.Debugf("Escrow published for %s", s.String())
log.Tracef("Escrow %s", s.contract.String())
// Defer to safely return the escrow tx hash
defer s.FinalizeExchange(ctx, ReasonSuccess, nil)
return s.contract.EscrowHash, nil
}