-
Notifications
You must be signed in to change notification settings - Fork 3
/
tx_prefix.go
322 lines (258 loc) · 8.71 KB
/
tx_prefix.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
package api
import (
"encoding/binary"
"encoding/hex"
"strconv"
"github.com/bwesterb/go-ristretto"
"github.com/gtank/merlin"
)
const (
PRIMITIVE = "prim"
SEQUENCE = "seq"
AGGREGATE = "agg"
AGGREGATE_END = "agg-end"
VARIANT = "var"
NONE = ""
)
// Convert tx_prefix to merlin transcript
func HashOfTxPrefix(tx *TxPrefix) []byte {
t := merlin.NewTranscript("digestible")
appendTxPrefix(tx, t)
return t.ExtractBytes([]byte("digest32"), 32)
}
// TxIn: append transaction inputs to transcript
// TxOutMembershipProof: append membership proof to transcript
func appendIndex(index string, t *merlin.Transcript) {
i, err := strconv.ParseUint(index, 10, 64)
if err != nil {
panic(err)
}
appendBytes([]byte("index"), []byte(PRIMITIVE), t)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, i)
appendBytes([]byte("uint"), bytes, t)
}
func appendHighestIndex(index string, t *merlin.Transcript) {
i, err := strconv.ParseUint(index, 10, 64)
if err != nil {
panic(err)
}
appendBytes([]byte("highest_index"), []byte(PRIMITIVE), t)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, i)
appendBytes([]byte("uint"), bytes, t)
}
func appendRange(r *Range, t *merlin.Transcript) {
appendBytes([]byte("range"), []byte(AGGREGATE), t)
appendBytes([]byte("name"), []byte("Range"), t)
appendBytes([]byte("from"), []byte("prim"), t)
i, err := strconv.ParseUint(r.From, 10, 64)
if err != nil {
panic(err)
}
bufi := make([]byte, 8)
binary.LittleEndian.PutUint64(bufi, i)
appendBytes([]byte("uint"), bufi, t)
appendBytes([]byte("to"), []byte("prim"), t)
j, err := strconv.ParseUint(r.To, 10, 64)
if err != nil {
panic(err)
}
bufj := make([]byte, 8)
binary.LittleEndian.PutUint64(bufj, j)
appendBytes([]byte("uint"), bufj, t)
appendBytes([]byte("range"), []byte(AGGREGATE_END), t)
appendBytes([]byte("name"), []byte("Range"), t)
}
func appendHash(hash string, t *merlin.Transcript) {
appendBytes([]byte("hash"), []byte("prim"), t)
buf, err := hex.DecodeString(hash)
if err != nil {
panic(err)
}
appendBytes([]byte("bytes"), buf, t)
}
func appendElement(element *TxOutMembershipElement, t *merlin.Transcript) {
appendBytes([]byte(""), []byte(AGGREGATE), t)
appendBytes([]byte("name"), []byte("TxOutMembershipElement"), t)
appendRange(element.Range, t)
appendHash(element.Hash, t)
appendBytes([]byte(""), []byte(AGGREGATE_END), t)
appendBytes([]byte("name"), []byte("TxOutMembershipElement"), t)
}
func appendElements(elements []*TxOutMembershipElement, t *merlin.Transcript) {
appendBytes([]byte("elements"), []byte(SEQUENCE), t)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, uint64(len(elements)))
appendBytes([]byte("len"), bytes, t)
for _, element := range elements {
appendElement(element, t)
}
}
// "Ring" of inputs, one of which is actually being spent.
func appendRing(outputs []*TxOut, t *merlin.Transcript) {
appendBytes([]byte("ring"), []byte(SEQUENCE), t)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, uint64(len(outputs)))
appendBytes([]byte("len"), bytes, t)
for _, output := range outputs {
appendTxOut(output, t)
}
}
// Proof that each TxOut in `ring` is in the ledger.
func appendTxOutMembershipProof(proof *TxOutMembershipProof, t *merlin.Transcript) {
appendBytes([]byte(""), []byte(AGGREGATE), t)
appendBytes([]byte("name"), []byte("TxOutMembershipProof"), t)
appendIndex(proof.Index, t)
appendHighestIndex(proof.HighestIndex, t)
appendElements(proof.Elements, t)
appendBytes([]byte(""), []byte(AGGREGATE_END), t)
appendBytes([]byte("name"), []byte("TxOutMembershipProof"), t)
}
func appendTxOutMembershipProofs(proofs []*TxOutMembershipProof, t *merlin.Transcript) {
appendBytes([]byte("proofs"), []byte(SEQUENCE), t)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, uint64(len(proofs)))
appendBytes([]byte("len"), bytes, t)
for _, proof := range proofs {
appendTxOutMembershipProof(proof, t)
}
}
func appendTxIn(in *TxIn, t *merlin.Transcript) {
appendBytes([]byte(""), []byte(AGGREGATE), t)
appendBytes([]byte("name"), []byte("TxIn"), t)
appendRing(in.Ring, t)
appendTxOutMembershipProofs(in.Proofs, t)
appendBytes([]byte(""), []byte(AGGREGATE_END), t)
appendBytes([]byte("name"), []byte("TxIn"), t)
}
func appendInputs(inputs []*TxIn, t *merlin.Transcript) {
appendBytes([]byte("inputs"), []byte(SEQUENCE), t)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, uint64(len(inputs)))
appendBytes([]byte("len"), bytes, t)
for _, input := range inputs {
appendTxIn(input, t)
}
}
// TxOut: append tx out to transcript
// Append TxOut Amount
func appendCommitment(commitment string, t *merlin.Transcript) {
buf, err := hex.DecodeString(commitment)
if err != nil {
panic(err)
}
appendBytes([]byte("commitment"), []byte(PRIMITIVE), t)
appendBytes([]byte("ristretto"), buf, t)
}
func appendMaskedValue(value MaskedValue, t *merlin.Transcript) {
appendBytes([]byte("masked_value"), []byte(PRIMITIVE), t)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, uint64(value))
appendBytes([]byte("uint"), bytes, t)
}
func appendAmount(amount *Amount, t *merlin.Transcript) {
appendBytes([]byte("amount"), []byte(AGGREGATE), t)
appendBytes([]byte("name"), []byte("Amount"), t)
appendCommitment(amount.Commitment, t)
appendMaskedValue(amount.MaskedValue, t)
appendBytes([]byte("amount"), []byte(AGGREGATE_END), t)
appendBytes([]byte("name"), []byte("Amount"), t)
}
// Append TxOut TargetKey
func appendTargetKey(key string, t *merlin.Transcript) {
buf, err := hex.DecodeString(key)
if err != nil {
panic(err)
}
appendBytes([]byte("target_key"), []byte(PRIMITIVE), t)
appendBytes([]byte("ristretto"), buf, t)
}
// Append TxOut PublicKey
func appendPublicKey(key string, t *merlin.Transcript) {
buf, err := hex.DecodeString(key)
if err != nil {
panic(err)
}
appendBytes([]byte("public_key"), []byte(PRIMITIVE), t)
appendBytes([]byte("ristretto"), buf, t)
}
// Append TxOut EFogHint
func appendEFogHint(hint string, t *merlin.Transcript) {
buf, err := hex.DecodeString(hint)
if err != nil {
panic(err)
}
appendBytes([]byte("e_fog_hint"), []byte(PRIMITIVE), t)
appendBytes([]byte("bytes"), buf, t)
}
func appendTxOut(txOut *TxOut, t *merlin.Transcript) {
appendBytes([]byte(""), []byte(AGGREGATE), t)
appendBytes([]byte("name"), []byte("TxOut"), t)
appendAmount(txOut.Amount, t)
appendTargetKey(txOut.TargetKey, t)
appendPublicKey(txOut.PublicKey, t)
appendEFogHint(txOut.EFogHint, t)
appendBytes([]byte(""), []byte(AGGREGATE_END), t)
appendBytes([]byte("name"), []byte("TxOut"), t)
}
func appendOutputs(outputs []*TxOut, t *merlin.Transcript) {
appendBytes([]byte("outputs"), []byte(SEQUENCE), t)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, uint64(len(outputs)))
appendBytes([]byte("len"), bytes, t)
for _, output := range outputs {
appendTxOut(output, t)
}
}
// Fee: append fee to transcript
func appendFee(fee uint64, t *merlin.Transcript) {
appendBytes([]byte("fee"), []byte("prim"), t)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, fee)
appendBytes([]byte("uint"), bytes, t)
}
// Tombstone: append tombstone block to transcript
func appendTombstoneBlock(tombstone uint64, t *merlin.Transcript) {
appendBytes([]byte("tombstone_block"), []byte("prim"), t)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, tombstone)
appendBytes([]byte("uint"), bytes, t)
}
func appendTxPrefix(tx *TxPrefix, t *merlin.Transcript) {
appendBytes([]byte("mobilecoin-tx-prefix"), []byte(AGGREGATE), t)
appendBytes([]byte("name"), []byte("TxPrefix"), t)
appendInputs(tx.Inputs, t)
appendOutputs(tx.Outputs, t)
appendFee(uint64(tx.Fee), t)
appendTombstoneBlock(uint64(tx.TombstoneBlock), t)
appendBytes([]byte("mobilecoin-tx-prefix"), []byte(AGGREGATE_END), t)
appendBytes([]byte("name"), []byte("TxPrefix"), t)
}
func appendInt64(label string, i uint64, t *merlin.Transcript) {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, i)
appendBytes([]byte(label), buf, t)
}
func InnerproductDomainSep(n uint64, t *merlin.Transcript) {
appendBytes([]byte("dom-sep"), []byte("ipp v1"), t)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, n)
appendBytes([]byte("n"), bytes, t)
}
func appendBytes(field, data []byte, t *merlin.Transcript) {
t.AppendMessage(field, data)
}
func ChallengeScalar(label string, t *merlin.Transcript) *ristretto.Scalar {
data := t.ExtractBytes([]byte(label), 64)
var dataBytes [64]byte
copy(dataBytes[:], data[:])
var s ristretto.Scalar
return s.SetReduced(&dataBytes)
}
func AppendScalar(label string, s *ristretto.Scalar, t *merlin.Transcript) {
appendBytes([]byte(label), s.Bytes(), t)
}
func AppendPoint(label string, p *ristretto.Point, t *merlin.Transcript) {
appendBytes([]byte(label), p.Bytes(), t)
}