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(blen int, bi *big.Int) []byte {
16	// var b [blen]byte // TODO make the length depending on the tree.hashFunction.Len()
17	b := make([]byte, blen)
18	copy(b[:], SwapEndianness(bi.Bytes()))
19	return b[:]
20}
21
22// BytesToBigInt converts a byte array in Little-Endian representation into
23// *big.Int
24func BytesToBigInt(b []byte) *big.Int {
25	return new(big.Int).SetBytes(SwapEndianness(b))
26}