arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/common/utils.go
common/utils.goRaw
 1package common
 2
 3import (
 4	"math/big"
 5
 6	ethCommon "github.com/ethereum/go-ethereum/common"
 7	"github.com/iden3/go-iden3-crypto/babyjub"
 8)
 9
10// SwapEndianness swaps the order of the bytes in the slice.
11func SwapEndianness(b []byte) []byte {
12	o := make([]byte, len(b))
13	for i := range b {
14		o[len(b)-1-i] = b[i]
15	}
16	return o
17}
18
19// EthAddrToBigInt returns a *big.Int from a given ethereum common.Address.
20func EthAddrToBigInt(a ethCommon.Address) *big.Int {
21	return new(big.Int).SetBytes(a.Bytes())
22}
23
24// BJJCompressedTo256BigInts returns a [256]*big.Int array with the bit
25// representation of the babyjub.PublicKeyComp
26func BJJCompressedTo256BigInts(pkComp babyjub.PublicKeyComp) [256]*big.Int {
27	var r [256]*big.Int
28	b := pkComp[:]
29
30	for i := 0; i < 256; i++ {
31		if b[i/8]&(1<<(i%8)) == 0 {
32			r[i] = big.NewInt(0)
33		} else {
34			r[i] = big.NewInt(1)
35		}
36	}
37
38	return r
39}