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