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