arnaucube
arnaucube/arboPublic Self-hosted repository
arbo/utils.go
utils.goRaw
 1package arbo
 2
 3import (
 4	"math/big"
 5)
 6
 7// SwapEndianness swaps the order of the bytes in the byte slice.
 8func SwapEndianness(b []byte) []byte {
 9	o := make([]byte, len(b))
10	for i := range b {
11		o[len(b)-1-i] = b[i]
12	}
13	return o
14}
15
16// BigIntToBytes converts a *big.Int into a byte array in Little-Endian
17func BigIntToBytes(blen int, bi *big.Int) []byte {
18	// TODO make the length depending on the tree.hashFunction.Len()
19	b := make([]byte, blen)
20	copy(b[:], SwapEndianness(bi.Bytes()))
21	return b[:]
22}
23
24// BytesToBigInt converts a byte array in Little-Endian representation into
25// *big.Int
26func BytesToBigInt(b []byte) *big.Int {
27	return new(big.Int).SetBytes(SwapEndianness(b))
28}