/
coin.go
627 lines (541 loc) · 16.3 KB
/
coin.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
package privacy
import (
"encoding/json"
"errors"
"math/big"
"strconv"
"github.com/incognitochain/go-incognito-sdk/common"
"github.com/incognitochain/go-incognito-sdk/common/base58"
)
// Coin represents a coin
type Coin struct {
publicKey *Point
coinCommitment *Point
snDerivator *Scalar
serialNumber *Point
randomness *Scalar
value uint64
info []byte //256 bytes
}
// Start GET/SET
func (coin Coin) GetPublicKey() *Point {
return coin.publicKey
}
func (coin *Coin) SetPublicKey(v *Point) {
coin.publicKey = v
}
func (coin Coin) GetCoinCommitment() *Point {
return coin.coinCommitment
}
func (coin *Coin) SetCoinCommitment(v *Point) {
coin.coinCommitment = v
}
func (coin Coin) GetSNDerivator() *Scalar {
return coin.snDerivator
}
func (coin *Coin) SetSNDerivator(v *Scalar) {
coin.snDerivator = v
}
func (coin Coin) GetSerialNumber() *Point {
return coin.serialNumber
}
func (coin *Coin) SetSerialNumber(v *Point) {
coin.serialNumber = v
}
func (coin Coin) GetRandomness() *Scalar {
return coin.randomness
}
func (coin *Coin) SetRandomness(v *Scalar) {
coin.randomness = v
}
func (coin Coin) GetValue() uint64 {
return coin.value
}
func (coin *Coin) SetValue(v uint64) {
coin.value = v
}
func (coin Coin) GetInfo() []byte {
return coin.info
}
func (coin *Coin) SetInfo(v []byte) {
coin.info = make([]byte, len(v))
copy(coin.info, v)
}
// Init (Coin) initializes a coin
func (coin *Coin) Init() *Coin {
coin.publicKey = new(Point).Identity()
coin.coinCommitment = new(Point).Identity()
coin.snDerivator = new(Scalar).FromUint64(0)
coin.serialNumber = new(Point).Identity()
coin.randomness = new(Scalar)
coin.value = 0
return coin
}
// GetPubKeyLastByte returns the last byte of public key
func (coin *Coin) GetPubKeyLastByte() byte {
pubKeyBytes := coin.publicKey.ToBytes()
return pubKeyBytes[Ed25519KeySize-1]
}
// MarshalJSON (Coin) converts coin to bytes array,
// base58 check encode that bytes array into string
// json.Marshal the string
func (coin Coin) MarshalJSON() ([]byte, error) {
data := coin.Bytes()
temp := base58.Base58Check{}.Encode(data, common.ZeroByte)
return json.Marshal(temp)
}
// UnmarshalJSON (Coin) receives bytes array of coin (it was be MarshalJSON before),
// json.Unmarshal the bytes array to string
// base58 check decode that string to bytes array
// and set bytes array to coin
func (coin *Coin) UnmarshalJSON(data []byte) error {
dataStr := ""
_ = json.Unmarshal(data, &dataStr)
temp, _, err := base58.Base58Check{}.Decode(dataStr)
if err != nil {
return err
}
coin.SetBytes(temp)
return nil
}
// HashH returns the SHA3-256 hashing of coin bytes array
func (coin *Coin) HashH() *common.Hash {
hash := common.HashH(coin.Bytes())
return &hash
}
//CommitAll commits a coin with 5 attributes include:
// public key, value, serial number derivator, shardID form last byte public key, randomness
func (coin *Coin) CommitAll() error {
shardID := common.GetShardIDFromLastByte(coin.GetPubKeyLastByte())
values := []*Scalar{new(Scalar).FromUint64(0), new(Scalar).FromUint64(coin.value), coin.snDerivator, new(Scalar).FromUint64(uint64(shardID)), coin.randomness}
commitment, err := PedCom.commitAll(values)
if err != nil {
return err
}
coin.coinCommitment = commitment
coin.coinCommitment.Add(coin.coinCommitment, coin.publicKey)
return nil
}
// Bytes converts a coin's details to a bytes array
// Each fields in coin is saved in len - body format
func (coin *Coin) Bytes() []byte {
var coinBytes []byte
if coin.publicKey != nil {
publicKey := coin.publicKey.ToBytesS()
coinBytes = append(coinBytes, byte(Ed25519KeySize))
coinBytes = append(coinBytes, publicKey...)
} else {
coinBytes = append(coinBytes, byte(0))
}
if coin.coinCommitment != nil {
coinCommitment := coin.coinCommitment.ToBytesS()
coinBytes = append(coinBytes, byte(Ed25519KeySize))
coinBytes = append(coinBytes, coinCommitment...)
} else {
coinBytes = append(coinBytes, byte(0))
}
if coin.snDerivator != nil {
coinBytes = append(coinBytes, byte(Ed25519KeySize))
coinBytes = append(coinBytes, coin.snDerivator.ToBytesS()...)
} else {
coinBytes = append(coinBytes, byte(0))
}
if coin.serialNumber != nil {
serialNumber := coin.serialNumber.ToBytesS()
coinBytes = append(coinBytes, byte(Ed25519KeySize))
coinBytes = append(coinBytes, serialNumber...)
} else {
coinBytes = append(coinBytes, byte(0))
}
if coin.randomness != nil {
coinBytes = append(coinBytes, byte(Ed25519KeySize))
coinBytes = append(coinBytes, coin.randomness.ToBytesS()...)
} else {
coinBytes = append(coinBytes, byte(0))
}
if coin.value > 0 {
value := new(big.Int).SetUint64(coin.value).Bytes()
coinBytes = append(coinBytes, byte(len(value)))
coinBytes = append(coinBytes, value...)
} else {
coinBytes = append(coinBytes, byte(0))
}
if len(coin.info) > 0 {
byteLengthInfo := byte(0)
if len(coin.info) > MaxSizeInfoCoin {
// only get 255 byte of info
byteLengthInfo = byte(MaxSizeInfoCoin)
} else {
lengthInfo := len(coin.info)
byteLengthInfo = byte(lengthInfo)
}
coinBytes = append(coinBytes, byteLengthInfo)
infoBytes := coin.info[0:byteLengthInfo]
coinBytes = append(coinBytes, infoBytes...)
} else {
coinBytes = append(coinBytes, byte(0))
}
return coinBytes
}
// SetBytes receives a coinBytes (in bytes array), and
// reverts coinBytes to a Coin object
func (coin *Coin) SetBytes(coinBytes []byte) error {
if len(coinBytes) == 0 {
return errors.New("coinBytes is empty")
}
offset := 0
var err error
// Parse PublicKey
lenField := coinBytes[offset]
offset++
if lenField != 0 {
if offset+int(lenField) > len(coinBytes) {
// out of range
return errors.New("out of range Parse PublicKey")
}
data := coinBytes[offset : offset+int(lenField)]
coin.publicKey, err = new(Point).FromBytesS(data)
if err != nil {
return err
}
offset += int(lenField)
}
// Parse CoinCommitment
if offset > len(coinBytes) {
// out of range
return errors.New("out of range Parse CoinCommitment")
}
lenField = coinBytes[offset]
offset++
if lenField != 0 {
if offset+int(lenField) > len(coinBytes) {
// out of range
return errors.New("out of range Parse CoinCommitment")
}
data := coinBytes[offset : offset+int(lenField)]
coin.coinCommitment, err = new(Point).FromBytesS(data)
if err != nil {
return err
}
offset += int(lenField)
}
// Parse SNDerivator
if offset > len(coinBytes) {
// out of range
return errors.New("out of range Parse SNDerivator")
}
lenField = coinBytes[offset]
offset++
if lenField != 0 {
if offset+int(lenField) > len(coinBytes) {
// out of range
return errors.New("out of range Parse SNDerivator")
}
data := coinBytes[offset : offset+int(lenField)]
coin.snDerivator = new(Scalar).FromBytesS(data)
offset += int(lenField)
}
//Parse sn
if offset > len(coinBytes) {
// out of range
return errors.New("out of range Parse sn")
}
lenField = coinBytes[offset]
offset++
if lenField != 0 {
if offset+int(lenField) > len(coinBytes) {
// out of range
return errors.New("out of range Parse sn")
}
data := coinBytes[offset : offset+int(lenField)]
coin.serialNumber, err = new(Point).FromBytesS(data)
if err != nil {
return err
}
offset += int(lenField)
}
// Parse Randomness
if offset > len(coinBytes) {
// out of range
return errors.New("out of range Parse Randomness")
}
lenField = coinBytes[offset]
offset++
if lenField != 0 {
if offset+int(lenField) > len(coinBytes) {
// out of range
return errors.New("out of range Parse Randomness")
}
data := coinBytes[offset : offset+int(lenField)]
coin.randomness = new(Scalar).FromBytesS(data)
offset += int(lenField)
}
// Parse Value
if offset > len(coinBytes) {
// out of range
return errors.New("out of range Parse PublicKey")
}
lenField = coinBytes[offset]
offset++
if lenField != 0 {
if offset+int(lenField) > len(coinBytes) {
// out of range
return errors.New("out of range Parse PublicKey")
}
coin.value = new(big.Int).SetBytes(coinBytes[offset : offset+int(lenField)]).Uint64()
offset += int(lenField)
}
// Parse Info
if offset > len(coinBytes) {
// out of range
return errors.New("out of range Parse Info")
}
lenField = coinBytes[offset]
offset++
if lenField != 0 {
if offset+int(lenField) > len(coinBytes) {
// out of range
return errors.New("out of range Parse Info")
}
coin.info = make([]byte, lenField)
copy(coin.info, coinBytes[offset:offset+int(lenField)])
}
return nil
}
// InputCoin represents a input coin of transaction
type InputCoin struct {
CoinDetails *Coin
}
// Init (InputCoin) initializes a input coin
func (inputCoin *InputCoin) Init() *InputCoin {
if inputCoin.CoinDetails == nil {
inputCoin.CoinDetails = new(Coin).Init()
}
return inputCoin
}
// Bytes (InputCoin) converts a input coin's details to a bytes array
// Each fields in coin is saved in len - body format
func (inputCoin *InputCoin) Bytes() []byte {
return inputCoin.CoinDetails.Bytes()
}
// SetBytes (InputCoin) receives a coinBytes (in bytes array), and
// reverts coinBytes to a InputCoin object
func (inputCoin *InputCoin) SetBytes(bytes []byte) error {
inputCoin.CoinDetails = new(Coin)
return inputCoin.CoinDetails.SetBytes(bytes)
}
type CoinObject struct {
PublicKey string `json:"PublicKey"`
CoinCommitment string `json:"CoinCommitment"`
SNDerivator string `json:"SNDerivator"`
SerialNumber string `json:"SerialNumber"`
Randomness string `json:"Randomness"`
Value string `json:"Value"`
Info string `json:"Info"`
}
// SetBytes (InputCoin) receives a coinBytes (in bytes array), and
// reverts coinBytes to a InputCoin object
func (inputCoin *InputCoin) ParseCoinObjectToInputCoin(coinObj CoinObject) error {
inputCoin.CoinDetails = new(Coin).Init()
if coinObj.PublicKey != "" {
publicKey, _, err := base58.Base58Check{}.Decode(coinObj.PublicKey)
if err != nil {
return err
}
publicKeyPoint, err := new(Point).FromBytesS(publicKey)
if err != nil {
return err
}
inputCoin.CoinDetails.SetPublicKey(publicKeyPoint)
}
if coinObj.CoinCommitment != "" {
coinCommitment, _, err := base58.Base58Check{}.Decode(coinObj.CoinCommitment)
if err != nil {
return err
}
coinCommitmentPoint, err := new(Point).FromBytesS(coinCommitment)
if err != nil {
return err
}
inputCoin.CoinDetails.SetCoinCommitment(coinCommitmentPoint)
}
if coinObj.SNDerivator != "" {
snderivator, _, err := base58.Base58Check{}.Decode(coinObj.SNDerivator)
if err != nil {
return err
}
snderivatorScalar := new(Scalar).FromBytesS(snderivator)
if err != nil {
return err
}
inputCoin.CoinDetails.SetSNDerivator(snderivatorScalar)
}
if coinObj.SerialNumber != "" {
serialNumber, _, err := base58.Base58Check{}.Decode(coinObj.SerialNumber)
if err != nil {
return err
}
serialNumberPoint, err := new(Point).FromBytesS(serialNumber)
if err != nil {
return err
}
inputCoin.CoinDetails.SetSerialNumber(serialNumberPoint)
}
if coinObj.Randomness != "" {
randomness, _, err := base58.Base58Check{}.Decode(coinObj.Randomness)
if err != nil {
return err
}
randomnessScalar := new(Scalar).FromBytesS(randomness)
if err != nil {
return err
}
inputCoin.CoinDetails.SetRandomness(randomnessScalar)
}
if coinObj.Value != "" {
value, err := strconv.ParseUint(coinObj.Value, 10, 64)
if err != nil {
return err
}
inputCoin.CoinDetails.SetValue(value)
}
if coinObj.Info != "" {
infoBytes, _, err := base58.Base58Check{}.Decode(coinObj.Info)
if err != nil {
return err
}
inputCoin.CoinDetails.SetInfo(infoBytes)
}
return nil
}
// OutputCoin represents a output coin of transaction
// It contains CoinDetails and CoinDetailsEncrypted (encrypted value and randomness)
// CoinDetailsEncrypted is nil when you send tx without privacy
type OutputCoin struct {
CoinDetails *Coin
CoinDetailsEncrypted *HybridCipherText
}
// Init (OutputCoin) initializes a output coin
func (outputCoin *OutputCoin) Init() *OutputCoin {
outputCoin.CoinDetails = new(Coin).Init()
outputCoin.CoinDetailsEncrypted = new(HybridCipherText)
return outputCoin
}
// Bytes (OutputCoin) converts a output coin's details to a bytes array
// Each fields in coin is saved in len - body format
func (outputCoin *OutputCoin) Bytes() []byte {
var outCoinBytes []byte
if outputCoin.CoinDetailsEncrypted != nil {
coinDetailsEncryptedBytes := outputCoin.CoinDetailsEncrypted.Bytes()
outCoinBytes = append(outCoinBytes, byte(len(coinDetailsEncryptedBytes)))
outCoinBytes = append(outCoinBytes, coinDetailsEncryptedBytes...)
} else {
outCoinBytes = append(outCoinBytes, byte(0))
}
coinDetailBytes := outputCoin.CoinDetails.Bytes()
lenCoinDetailBytes := []byte{}
if len(coinDetailBytes) <= 255 {
lenCoinDetailBytes = []byte{byte(len(coinDetailBytes))}
} else {
lenCoinDetailBytes = common.IntToBytes(len(coinDetailBytes))
}
outCoinBytes = append(outCoinBytes, lenCoinDetailBytes...)
outCoinBytes = append(outCoinBytes, coinDetailBytes...)
return outCoinBytes
}
// SetBytes (OutputCoin) receives a coinBytes (in bytes array), and
// reverts coinBytes to a OutputCoin object
func (outputCoin *OutputCoin) SetBytes(bytes []byte) error {
if len(bytes) == 0 {
return errors.New("coinBytes is empty")
}
offset := 0
lenCoinDetailEncrypted := int(bytes[0])
offset += 1
if lenCoinDetailEncrypted > 0 {
if offset+lenCoinDetailEncrypted > len(bytes) {
// out of range
return errors.New("out of range Parse CoinDetailsEncrypted")
}
outputCoin.CoinDetailsEncrypted = new(HybridCipherText)
err := outputCoin.CoinDetailsEncrypted.SetBytes(bytes[offset : offset+lenCoinDetailEncrypted])
if err != nil {
return err
}
offset += lenCoinDetailEncrypted
}
// try get 1-byte for len
if offset > len(bytes) {
// out of range
return errors.New("out of range Parse CoinDetails")
}
lenOutputCoin := int(bytes[offset])
outputCoin.CoinDetails = new(Coin)
if lenOutputCoin != 0 {
offset += 1
if offset+lenOutputCoin > len(bytes) {
// out of range
return errors.New("out of range Parse output coin details")
}
err := outputCoin.CoinDetails.SetBytes(bytes[offset : offset+lenOutputCoin])
if err != nil {
// 1-byte is wrong
// try get 2-byte for len
if offset+1 > len(bytes) {
// out of range
return errors.New("out of range Parse output coin details")
}
lenOutputCoin = common.BytesToInt(bytes[offset-1 : offset+1])
offset += 1
if offset+lenOutputCoin > len(bytes) {
// out of range
return errors.New("out of range Parse output coin details")
}
err1 := outputCoin.CoinDetails.SetBytes(bytes[offset : offset+lenOutputCoin])
return err1
}
} else {
// 1-byte is wrong
// try get 2-byte for len
if offset+2 > len(bytes) {
// out of range
return errors.New("out of range Parse output coin details")
}
lenOutputCoin = common.BytesToInt(bytes[offset : offset+2])
offset += 2
if offset+lenOutputCoin > len(bytes) {
// out of range
return errors.New("out of range Parse output coin details")
}
err1 := outputCoin.CoinDetails.SetBytes(bytes[offset : offset+lenOutputCoin])
return err1
}
return nil
}
// Encrypt returns a ciphertext encrypting for a coin using a hybrid cryptosystem,
// in which AES encryption scheme is used as a data encapsulation scheme,
// and ElGamal cryptosystem is used as a key encapsulation scheme.
func (outputCoin *OutputCoin) Encrypt(recipientTK TransmissionKey) *PrivacyError {
// 32-byte first: Randomness, the rest of msg is value of coin
msg := append(outputCoin.CoinDetails.randomness.ToBytesS(), new(big.Int).SetUint64(outputCoin.CoinDetails.value).Bytes()...)
pubKeyPoint, err := new(Point).FromBytesS(recipientTK)
if err != nil {
return NewPrivacyErr(EncryptOutputCoinErr, err)
}
outputCoin.CoinDetailsEncrypted, err = HybridEncrypt(msg, pubKeyPoint)
if err != nil {
return NewPrivacyErr(EncryptOutputCoinErr, err)
}
return nil
}
// Decrypt decrypts a ciphertext encrypting for coin with recipient's receiving key
func (outputCoin *OutputCoin) Decrypt(viewingKey ViewingKey) *PrivacyError {
msg, err := HybridDecrypt(outputCoin.CoinDetailsEncrypted, new(Scalar).FromBytesS(viewingKey.Rk))
if err != nil {
return NewPrivacyErr(DecryptOutputCoinErr, err)
}
// Assign randomness and value to outputCoin details
outputCoin.CoinDetails.randomness = new(Scalar).FromBytesS(msg[0:Ed25519KeySize])
outputCoin.CoinDetails.value = new(big.Int).SetBytes(msg[Ed25519KeySize:]).Uint64()
return nil
}