Files
arbo/utils.go
arnaucube 30d8b42fd3 Add checks that len(key)<=maxKeyLen
Add checks that the key is not bigger than maximum key length for the tree
maxLevels size, where maximum key len = ceil(maxLevels/8).

This is because if the key bits length is bigger than the maxLevels of the
tree, two different keys that their difference is at the end, will collision in
the same leaf of the tree (at the max depth).
2021-10-04 14:33:49 +02:00

29 lines
678 B
Go

package arbo
import (
"math/big"
)
// SwapEndianness swaps the order of the bytes in the byte slice.
func SwapEndianness(b []byte) []byte {
o := make([]byte, len(b))
for i := range b {
o[len(b)-1-i] = b[i]
}
return o
}
// BigIntToBytes converts a *big.Int into a byte array in Little-Endian
func BigIntToBytes(blen int, bi *big.Int) []byte {
// TODO make the length depending on the tree.hashFunction.Len()
b := make([]byte, blen)
copy(b[:], SwapEndianness(bi.Bytes()))
return b[:]
}
// BytesToBigInt converts a byte array in Little-Endian representation into
// *big.Int
func BytesToBigInt(b []byte) *big.Int {
return new(big.Int).SetBytes(SwapEndianness(b))
}