arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/txprocessor/utils.go
txprocessor/utils.goRaw
 1package txprocessor
 2
 3import (
 4	"math/big"
 5
 6	"github.com/hermeznetwork/hermez-node/common"
 7	"github.com/iden3/go-iden3-crypto/babyjub"
 8	"github.com/iden3/go-merkletree"
 9)
10
11func siblingsToZKInputFormat(s []*merkletree.Hash) []*big.Int {
12	b := make([]*big.Int, len(s))
13	for i := 0; i < len(s); i++ {
14		b[i] = s[i].BigInt()
15	}
16	return b
17}
18
19// BJJCompressedTo256BigInts returns a [256]*big.Int array with the bit
20// representation of the babyjub.PublicKeyComp
21func BJJCompressedTo256BigInts(pkComp babyjub.PublicKeyComp) [256]*big.Int {
22	var r [256]*big.Int
23	b := pkComp[:]
24
25	for i := 0; i < 256; i++ {
26		if b[i/8]&(1<<(i%8)) == 0 { //nolint:gomnd
27			r[i] = big.NewInt(0)
28		} else {
29			r[i] = big.NewInt(1)
30		}
31	}
32
33	return r
34}
35
36// formatAccumulatedFees returns an array of [nFeeAccounts]*big.Int containing
37// the balance of each FeeAccount, taken from the 'collectedFees' map, in the
38// order of the 'orderTokenIDs'
39func formatAccumulatedFees(collectedFees map[common.TokenID]*big.Int, orderTokenIDs []*big.Int,
40	coordIdxs []common.Idx) []*big.Int {
41	accFeeOut := make([]*big.Int, len(orderTokenIDs))
42	for i := 0; i < len(accFeeOut); i++ {
43		accFeeOut[i] = big.NewInt(0)
44	}
45	for i := 0; i < len(coordIdxs); i++ {
46		tokenID := common.TokenIDFromBigInt(orderTokenIDs[i])
47		if _, ok := collectedFees[tokenID]; ok {
48			accFeeOut[i] = new(big.Int).Set(collectedFees[tokenID])
49		}
50	}
51	return accFeeOut
52}