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