2023-05-23 18:58:34 +00:00
|
|
|
package merkle_tree
|
|
|
|
|
|
|
|
func NextPowerOfTwo(n uint64) uint64 {
|
|
|
|
if n == 0 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
|
|
|
|
n--
|
|
|
|
n |= n >> 1
|
|
|
|
n |= n >> 2
|
|
|
|
n |= n >> 4
|
|
|
|
n |= n >> 8
|
|
|
|
n |= n >> 16
|
|
|
|
n |= n >> 32
|
|
|
|
n++
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2023-06-01 23:21:08 +00:00
|
|
|
// GetDepth returns the depth of a merkle tree with a given number of nodes.
|
2023-05-23 18:58:34 +00:00
|
|
|
// The depth is defined as the number of levels in the tree, with the root
|
|
|
|
// node at level 0 and each child node at a level one greater than its parent.
|
|
|
|
// If the number of nodes is less than or equal to 1, the depth is 0.
|
2023-06-01 23:21:08 +00:00
|
|
|
func GetDepth(v uint64) uint8 {
|
2023-05-23 18:58:34 +00:00
|
|
|
// If there are 0 or 1 nodes, the depth is 0.
|
|
|
|
if v <= 1 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the depth to 0.
|
|
|
|
depth := uint8(0)
|
|
|
|
|
|
|
|
// Divide the number of nodes by 2 until it is less than or equal to 1.
|
|
|
|
// The number of iterations is the depth of the tree.
|
|
|
|
for v > 1 {
|
|
|
|
v >>= 1
|
|
|
|
depth++
|
|
|
|
}
|
|
|
|
|
|
|
|
return depth
|
|
|
|
}
|