arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/common/account.go
common/account.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/babyjub"
 11	"github.com/iden3/go-iden3-crypto/poseidon"
 12	cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
 13)
 14
 15const NLEAFELEMS = 4
 16
 17// Account is a struct that gives information of the holdings of an address and a specific token. Is the data structure that generates the Value stored in the leaf of the MerkleTree
 18type Account struct {
 19	TokenID   TokenID
 20	Nonce     uint64   // max of 40 bits used
 21	Balance   *big.Int // max of 192 bits used
 22	PublicKey *babyjub.PublicKey
 23	EthAddr   eth.Address
 24}
 25
 26// Bytes returns the bytes representing the Account, 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 *Account) 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 babyjub.PointCoordSign(l.PublicKey.X) {
 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.PublicKey.Y.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 *Account) 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 Account, which is the Poseidon hash of its *big.Int representation
 72func (l *Account) HashValue() (*big.Int, error) {
 73	b0 := big.NewInt(0)
 74	toHash := [poseidon.T]*big.Int{b0, b0, b0, b0, b0, b0}
 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// AccountFromBigInts returns a Account from a [5]*big.Int
 86func AccountFromBigInts(e [NLEAFELEMS]*big.Int) (*Account, 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 AccountFromBytes(b)
 97}
 98
 99// AccountFromBytes returns a Account from a byte array
100func AccountFromBytes(b [32 * NLEAFELEMS]byte) (*Account, 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	sign := b[10] == 1
106	balance := new(big.Int).SetBytes(SwapEndianness(b[32:56])) // b[32:56], as Balance is 192 bits (24 bytes)
107	if !bytes.Equal(b[56:64], []byte{0, 0, 0, 0, 0, 0, 0, 0}) {
108		return nil, fmt.Errorf("%s Balance", ErrNumOverflow)
109	}
110	ay := new(big.Int).SetBytes(SwapEndianness(b[64:96]))
111	pkPoint, err := babyjub.PointFromSignAndY(sign, ay)
112	if err != nil {
113		return nil, err
114	}
115	publicKey := babyjub.PublicKey(*pkPoint)
116	ethAddr := eth.BytesToAddress(b[96:116])
117
118	if !cryptoUtils.CheckBigIntInField(balance) {
119		return nil, ErrNotInFF
120	}
121	if !cryptoUtils.CheckBigIntInField(ay) {
122		return nil, ErrNotInFF
123	}
124
125	l := Account{
126		TokenID:   TokenID(tokenID),
127		Nonce:     nonce,
128		Balance:   balance,
129		PublicKey: &publicKey,
130		EthAddr:   ethAddr,
131	}
132	return &l, nil
133}