use super::error::RangeProofError;
pub fn compute_num_left_siblings(node_idx: usize) -> usize {
let mut num_left_siblings = 0;
let mut start_idx = node_idx;
while start_idx != 0 {
if start_idx & 1 != 0 {
num_left_siblings += 1;
}
start_idx >>= 1;
}
num_left_siblings
}
pub fn compute_tree_size(
num_right_siblings: usize,
index_of_last_included_leaf: usize,
) -> Result<usize, RangeProofError> {
let mut index_of_final_node = index_of_last_included_leaf;
let mut mask = 1;
let mut remaining_right_siblings = num_right_siblings;
while remaining_right_siblings > 0 {
if index_of_final_node & mask == 0 {
index_of_final_node |= mask;
remaining_right_siblings -= 1;
}
mask <<= 1;
if index_of_final_node == u32::MAX as usize {
return Err(RangeProofError::TreeTooLarge);
}
}
Ok(index_of_final_node + 1)
}