hermez-node/common/leaf.go
common/leaf.goRaw
1package common
2
3import (
4 "bytes"
5 "encoding/binary"
6 "fmt"
7 "math/big"
8
9 eth "github.com/ethereum/go-ethereum/common"
10 "github.com/iden3/go-iden3-crypto/poseidon"
11 cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
12)
13
14const NLEAFELEMS = 4
15
16// Leaf is the data structure stored in the Leaf of the MerkleTree
17type Leaf struct {
18 TokenID TokenID
19 Nonce uint64 // max of 40 bits used
20 Balance *big.Int // max of 192 bits used
21 Sign bool
22 Ay *big.Int
23 EthAddr eth.Address
24}
25
26// Bytes returns the bytes representing the Leaf, in a way that each BigInt is represented by 32 bytes, in spite of the BigInt could be represented in less bytes (due a small big.Int), so in this way each BigInt is always 32 bytes and can be automatically parsed from a byte array.
27func (l *Leaf) Bytes() ([32 * NLEAFELEMS]byte, error) {
28 var b [32 * NLEAFELEMS]byte
29
30 if l.Nonce > 0xffffffffff {
31 return b, fmt.Errorf("%s Nonce", ErrNumOverflow)
32 }
33 if len(l.Balance.Bytes()) > 24 {
34 return b, fmt.Errorf("%s Balance", ErrNumOverflow)
35 }
36
37 var tokenIDBytes [4]byte
38 binary.LittleEndian.PutUint32(tokenIDBytes[:], uint32(l.TokenID))
39 var nonceBytes [8]byte
40 binary.LittleEndian.PutUint64(nonceBytes[:], l.Nonce)
41
42 copy(b[0:4], tokenIDBytes[:])
43 copy(b[4:9], nonceBytes[:])
44 if l.Sign {
45 b[10] = 1
46 }
47 copy(b[32:64], SwapEndianness(l.Balance.Bytes())) // SwapEndianness, as big.Int uses BigEndian
48 copy(b[64:96], SwapEndianness(l.Ay.Bytes()))
49 copy(b[96:116], l.EthAddr.Bytes())
50
51 return b, nil
52}
53
54// BigInts returns the [5]*big.Int, where each *big.Int is inside the Finite Field
55func (l *Leaf) BigInts() ([NLEAFELEMS]*big.Int, error) {
56 e := [NLEAFELEMS]*big.Int{}
57
58 b, err := l.Bytes()
59 if err != nil {
60 return e, err
61 }
62
63 e[0] = new(big.Int).SetBytes(SwapEndianness(b[0:32]))
64 e[1] = new(big.Int).SetBytes(SwapEndianness(b[32:64]))
65 e[2] = new(big.Int).SetBytes(SwapEndianness(b[64:96]))
66 e[3] = new(big.Int).SetBytes(SwapEndianness(b[96:128]))
67
68 return e, nil
69}
70
71// HashValue returns the value of the Leaf, which is the Poseidon hash of its *big.Int representation
72func (l *Leaf) HashValue() (*big.Int, error) {
73 toHash := [poseidon.T]*big.Int{}
74 lBI, err := l.BigInts()
75 if err != nil {
76 return nil, err
77 }
78 copy(toHash[:], lBI[:])
79
80 v, err := poseidon.Hash(toHash)
81 return v, err
82}
83
84// LeafFromBigInts returns a Leaf from a [5]*big.Int
85func LeafFromBigInts(e [NLEAFELEMS]*big.Int) (*Leaf, error) {
86 if !cryptoUtils.CheckBigIntArrayInField(e[:]) {
87 return nil, ErrNotInFF
88 }
89 var b [32 * NLEAFELEMS]byte
90 copy(b[0:32], SwapEndianness(e[0].Bytes())) // SwapEndianness, as big.Int uses BigEndian
91 copy(b[32:64], SwapEndianness(e[1].Bytes()))
92 copy(b[64:96], SwapEndianness(e[2].Bytes()))
93 copy(b[96:128], SwapEndianness(e[3].Bytes()))
94
95 return LeafFromBytes(b)
96}
97
98// LeafFromBytes returns a Leaf from a byte array
99func LeafFromBytes(b [32 * NLEAFELEMS]byte) (*Leaf, error) {
100 tokenID := binary.LittleEndian.Uint32(b[0:4])
101 var nonceBytes [8]byte
102 copy(nonceBytes[:], b[4:9])
103 nonce := binary.LittleEndian.Uint64(nonceBytes[:])
104 sign := b[10] == 1
105 balance := new(big.Int).SetBytes(SwapEndianness(b[32:56])) // b[32:56], as Balance is 192 bits (24 bytes)
106 if !bytes.Equal(b[56:64], []byte{0, 0, 0, 0, 0, 0, 0, 0}) {
107 return nil, fmt.Errorf("%s Balance", ErrNumOverflow)
108 }
109 ay := new(big.Int).SetBytes(SwapEndianness(b[64:96]))
110 ethAddr := eth.BytesToAddress(b[96:116])
111
112 if !cryptoUtils.CheckBigIntInField(balance) {
113 return nil, ErrNotInFF
114 }
115 if !cryptoUtils.CheckBigIntInField(ay) {
116 return nil, ErrNotInFF
117 }
118
119 l := Leaf{
120 TokenID: TokenID(tokenID),
121 Nonce: nonce,
122 Balance: balance,
123 Sign: sign,
124 Ay: ay,
125 EthAddr: ethAddr,
126 }
127 return &l, nil
128}