mirror of
https://github.com/vocdoni/arbo.git
synced 2026-01-08 21:37:57 -05:00
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).
29 lines
678 B
Go
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))
|
|
}
|